Skip to content

Commit fdcf26d

Browse files
committed
feat: add defaultValue property in input component
1 parent 6cbe5c6 commit fdcf26d

File tree

5 files changed

+18
-7
lines changed

5 files changed

+18
-7
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 1.5.0
2+
3+
- Add `defaultValue` property on `input` component
4+
15
## 1.4.1
26

37
- Add `max` property on `checkbox` component

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ user input.
1010
To use Commander in your Dart project, add this to your `pubspec.yaml` file :
1111
```yaml
1212
dependencies:
13-
commander_ui: ^1.3.0
13+
commander_ui: ^1.5.0
1414
```
1515
1616
Then run `pub get` to install the dependencies.
@@ -23,7 +23,7 @@ A simple example of using Commander to create an input component :
2323

2424
- ✅ Placeholder
2525
- ✅ Validator with error message as callback
26-
- Default value
26+
- Default value
2727

2828
```dart
2929
Future<void> main() async {

example/input.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Future<void> main() async {
44
final input = Input(
55
answer: 'What is your name?',
66
placeholder: 'Name',
7+
defaultValue: 'John Doe',
78
validate: (value) {
89
if (value.isEmpty) {
910
return Err('Name cannot be empty');

lib/src/components/input.dart

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class Input with Tools implements Component<String> {
1818
final bool hidden;
1919
late final List<Sequence> exitMessage;
2020
String value = '';
21+
String defaultValue;
2122
String? errorMessage;
2223
late Result Function(String value) validate;
2324

@@ -33,6 +34,7 @@ class Input with Tools implements Component<String> {
3334
this.placeholder,
3435
this.secure = false,
3536
this.hidden = false,
37+
this.defaultValue = '',
3638
Result Function(String value)? validate,
3739
List<Sequence>? exitMessage,
3840
}) {
@@ -66,7 +68,7 @@ class Input with Tools implements Component<String> {
6668
}
6769

6870
void onSubmit(String key, void Function() dispose) {
69-
final result = validate(value);
71+
final result = validate(value.isEmpty ? defaultValue : value);
7072
if (result case Err(:final String error)) {
7173
errorMessage = error;
7274
render();
@@ -88,14 +90,16 @@ class Input with Tools implements Component<String> {
8890
SetStyles.reset,
8991
Print(' $answer '),
9092
SetStyles(Style.foreground(Color.brightBlack)),
91-
Print(generateValue()),
93+
Print(defaultValue.isNotEmpty
94+
? defaultValue
95+
: placeholder ?? generateValue()),
9296
SetStyles.reset,
9397
]);
9498

9599
stdout.writeln();
96100

97101
saveCursorPosition();
98-
_completer.complete(value);
102+
_completer.complete(value.isEmpty ? defaultValue : value);
99103
}
100104

101105
void onExit(void Function() dispose) {
@@ -137,7 +141,9 @@ class Input with Tools implements Component<String> {
137141
Print(' $answer '),
138142
SetStyles(Style.foreground(Color.brightBlack)),
139143
Print(value.isEmpty && errorMessage == null
140-
? placeholder ?? generateValue()
144+
? defaultValue.isNotEmpty
145+
? defaultValue
146+
: placeholder ?? generateValue()
141147
: generateValue()),
142148
SetStyles.reset,
143149
]);

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: commander_ui
22
description: Commander is a Dart library for creating user interfaces within the terminal.
3-
version: 1.4.1
3+
version: 1.5.0
44
repository: https://github.com/LeadcodeDev/commander
55

66
topics:

0 commit comments

Comments
 (0)