Skip to content

Commit ae3a68e

Browse files
authored
Merge pull request #1140 from richardshiue/improv-select-options
grid: fix pressing enter on an option that already exists recreates it
2 parents cd183b5 + edd9128 commit ae3a68e

File tree

3 files changed

+40
-5
lines changed

3 files changed

+40
-5
lines changed

frontend/app_flowy/lib/plugins/grid/application/cell/select_option_editor_bloc.dart

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ class SelectOptionCellEditorBloc
5454
selectOption: (_SelectOption value) {
5555
_onSelectOption(value.optionId);
5656
},
57+
trySelectOption: (_TrySelectOption value) {
58+
_trySelectOption(value.optionName, emit);
59+
},
5760
filterOption: (_SelectOptionFilter value) {
5861
_filterOption(value.optionName, emit);
5962
},
@@ -100,6 +103,36 @@ class SelectOptionCellEditorBloc
100103
}
101104
}
102105

106+
void _trySelectOption(
107+
String optionName, Emitter<SelectOptionEditorState> emit) async {
108+
SelectOptionPB? matchingOption;
109+
bool optionExistsButSelected = false;
110+
111+
for (final option in state.options) {
112+
if (option.name.toLowerCase() == optionName.toLowerCase()) {
113+
if (!state.selectedOptions.contains(option)) {
114+
matchingOption = option;
115+
break;
116+
} else {
117+
optionExistsButSelected = true;
118+
}
119+
}
120+
}
121+
122+
// if there isn't a matching option at all, then create it
123+
if (matchingOption == null && !optionExistsButSelected) {
124+
_createOption(optionName);
125+
}
126+
127+
// if there is an unselected matching option, select it
128+
if (matchingOption != null) {
129+
_selectOptionService.select(optionId: matchingOption.id);
130+
}
131+
132+
// clear the filter
133+
emit(state.copyWith(filter: none()));
134+
}
135+
103136
void _filterOption(String optionName, Emitter<SelectOptionEditorState> emit) {
104137
final _MakeOptionResult result =
105138
_makeOptions(Some(optionName), state.allOptions);
@@ -187,6 +220,8 @@ class SelectOptionEditorEvent with _$SelectOptionEditorEvent {
187220
_DeleteOption;
188221
const factory SelectOptionEditorEvent.filterOption(String optionName) =
189222
_SelectOptionFilter;
223+
const factory SelectOptionEditorEvent.trySelectOption(String optionName) =
224+
_TrySelectOption;
190225
}
191226

192227
@freezed

frontend/app_flowy/lib/plugins/grid/presentation/widgets/cell/select_option_cell/select_option_editor.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,10 @@ class _TextField extends StatelessWidget {
154154
.read<SelectOptionCellEditorBloc>()
155155
.add(SelectOptionEditorEvent.filterOption(text));
156156
},
157-
onNewTag: (tagName) {
157+
onSubmitted: (tagName) {
158158
context
159159
.read<SelectOptionCellEditorBloc>()
160-
.add(SelectOptionEditorEvent.newOption(tagName));
160+
.add(SelectOptionEditorEvent.trySelectOption(tagName));
161161
},
162162
),
163163
);

frontend/app_flowy/lib/plugins/grid/presentation/widgets/cell/select_option_cell/text_field.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class SelectOptionTextField extends StatefulWidget {
1717
final LinkedHashMap<String, SelectOptionPB> selectedOptionMap;
1818
final double distanceToText;
1919

20-
final Function(String) onNewTag;
20+
final Function(String) onSubmitted;
2121
final Function(String) newText;
2222
final VoidCallback? onClick;
2323

@@ -26,7 +26,7 @@ class SelectOptionTextField extends StatefulWidget {
2626
required this.selectedOptionMap,
2727
required this.distanceToText,
2828
required this.tagController,
29-
required this.onNewTag,
29+
required this.onSubmitted,
3030
required this.newText,
3131
this.onClick,
3232
TextEditingController? textController,
@@ -88,7 +88,7 @@ class _SelectOptionTextFieldState extends State<SelectOptionTextField> {
8888
}
8989

9090
if (text.isNotEmpty) {
91-
widget.onNewTag(text);
91+
widget.onSubmitted(text);
9292
focusNode.requestFocus();
9393
}
9494
},

0 commit comments

Comments
 (0)