From edd9128046a4ec8f4756cfb05c113cc44045b0b3 Mon Sep 17 00:00:00 2001 From: Richard Shiue <71320345+richardshiue@users.noreply.github.com> Date: Sun, 25 Sep 2022 12:32:40 +0800 Subject: [PATCH] fix: press enter to create or select option in text field --- .../cell/select_option_editor_bloc.dart | 35 +++++++++++++++++++ .../select_option_editor.dart | 4 +-- .../cell/select_option_cell/text_field.dart | 6 ++-- 3 files changed, 40 insertions(+), 5 deletions(-) diff --git a/frontend/app_flowy/lib/plugins/grid/application/cell/select_option_editor_bloc.dart b/frontend/app_flowy/lib/plugins/grid/application/cell/select_option_editor_bloc.dart index 93343d8c4c..b9285890f1 100644 --- a/frontend/app_flowy/lib/plugins/grid/application/cell/select_option_editor_bloc.dart +++ b/frontend/app_flowy/lib/plugins/grid/application/cell/select_option_editor_bloc.dart @@ -54,6 +54,9 @@ class SelectOptionCellEditorBloc selectOption: (_SelectOption value) { _onSelectOption(value.optionId); }, + trySelectOption: (_TrySelectOption value) { + _trySelectOption(value.optionName, emit); + }, filterOption: (_SelectOptionFilter value) { _filterOption(value.optionName, emit); }, @@ -100,6 +103,36 @@ class SelectOptionCellEditorBloc } } + void _trySelectOption( + String optionName, Emitter emit) async { + SelectOptionPB? matchingOption; + bool optionExistsButSelected = false; + + for (final option in state.options) { + if (option.name.toLowerCase() == optionName.toLowerCase()) { + if (!state.selectedOptions.contains(option)) { + matchingOption = option; + break; + } else { + optionExistsButSelected = true; + } + } + } + + // if there isn't a matching option at all, then create it + if (matchingOption == null && !optionExistsButSelected) { + _createOption(optionName); + } + + // if there is an unselected matching option, select it + if (matchingOption != null) { + _selectOptionService.select(optionId: matchingOption.id); + } + + // clear the filter + emit(state.copyWith(filter: none())); + } + void _filterOption(String optionName, Emitter emit) { final _MakeOptionResult result = _makeOptions(Some(optionName), state.allOptions); @@ -187,6 +220,8 @@ class SelectOptionEditorEvent with _$SelectOptionEditorEvent { _DeleteOption; const factory SelectOptionEditorEvent.filterOption(String optionName) = _SelectOptionFilter; + const factory SelectOptionEditorEvent.trySelectOption(String optionName) = + _TrySelectOption; } @freezed diff --git a/frontend/app_flowy/lib/plugins/grid/presentation/widgets/cell/select_option_cell/select_option_editor.dart b/frontend/app_flowy/lib/plugins/grid/presentation/widgets/cell/select_option_cell/select_option_editor.dart index 9a0252e009..bc80798fea 100644 --- a/frontend/app_flowy/lib/plugins/grid/presentation/widgets/cell/select_option_cell/select_option_editor.dart +++ b/frontend/app_flowy/lib/plugins/grid/presentation/widgets/cell/select_option_cell/select_option_editor.dart @@ -154,10 +154,10 @@ class _TextField extends StatelessWidget { .read() .add(SelectOptionEditorEvent.filterOption(text)); }, - onNewTag: (tagName) { + onSubmitted: (tagName) { context .read() - .add(SelectOptionEditorEvent.newOption(tagName)); + .add(SelectOptionEditorEvent.trySelectOption(tagName)); }, ), ); diff --git a/frontend/app_flowy/lib/plugins/grid/presentation/widgets/cell/select_option_cell/text_field.dart b/frontend/app_flowy/lib/plugins/grid/presentation/widgets/cell/select_option_cell/text_field.dart index f77d56d635..2bb249dbd5 100644 --- a/frontend/app_flowy/lib/plugins/grid/presentation/widgets/cell/select_option_cell/text_field.dart +++ b/frontend/app_flowy/lib/plugins/grid/presentation/widgets/cell/select_option_cell/text_field.dart @@ -17,7 +17,7 @@ class SelectOptionTextField extends StatefulWidget { final LinkedHashMap selectedOptionMap; final double distanceToText; - final Function(String) onNewTag; + final Function(String) onSubmitted; final Function(String) newText; final VoidCallback? onClick; @@ -26,7 +26,7 @@ class SelectOptionTextField extends StatefulWidget { required this.selectedOptionMap, required this.distanceToText, required this.tagController, - required this.onNewTag, + required this.onSubmitted, required this.newText, this.onClick, TextEditingController? textController, @@ -88,7 +88,7 @@ class _SelectOptionTextFieldState extends State { } if (text.isNotEmpty) { - widget.onNewTag(text); + widget.onSubmitted(text); focusNode.requestFocus(); } },