mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
Merge pull request #1140 from richardshiue/improv-select-options
grid: fix pressing enter on an option that already exists recreates it
This commit is contained in:
commit
ae3a68efb6
@ -54,6 +54,9 @@ class SelectOptionCellEditorBloc
|
|||||||
selectOption: (_SelectOption value) {
|
selectOption: (_SelectOption value) {
|
||||||
_onSelectOption(value.optionId);
|
_onSelectOption(value.optionId);
|
||||||
},
|
},
|
||||||
|
trySelectOption: (_TrySelectOption value) {
|
||||||
|
_trySelectOption(value.optionName, emit);
|
||||||
|
},
|
||||||
filterOption: (_SelectOptionFilter value) {
|
filterOption: (_SelectOptionFilter value) {
|
||||||
_filterOption(value.optionName, emit);
|
_filterOption(value.optionName, emit);
|
||||||
},
|
},
|
||||||
@ -100,6 +103,36 @@ class SelectOptionCellEditorBloc
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void _trySelectOption(
|
||||||
|
String optionName, Emitter<SelectOptionEditorState> 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<SelectOptionEditorState> emit) {
|
void _filterOption(String optionName, Emitter<SelectOptionEditorState> emit) {
|
||||||
final _MakeOptionResult result =
|
final _MakeOptionResult result =
|
||||||
_makeOptions(Some(optionName), state.allOptions);
|
_makeOptions(Some(optionName), state.allOptions);
|
||||||
@ -187,6 +220,8 @@ class SelectOptionEditorEvent with _$SelectOptionEditorEvent {
|
|||||||
_DeleteOption;
|
_DeleteOption;
|
||||||
const factory SelectOptionEditorEvent.filterOption(String optionName) =
|
const factory SelectOptionEditorEvent.filterOption(String optionName) =
|
||||||
_SelectOptionFilter;
|
_SelectOptionFilter;
|
||||||
|
const factory SelectOptionEditorEvent.trySelectOption(String optionName) =
|
||||||
|
_TrySelectOption;
|
||||||
}
|
}
|
||||||
|
|
||||||
@freezed
|
@freezed
|
||||||
|
@ -154,10 +154,10 @@ class _TextField extends StatelessWidget {
|
|||||||
.read<SelectOptionCellEditorBloc>()
|
.read<SelectOptionCellEditorBloc>()
|
||||||
.add(SelectOptionEditorEvent.filterOption(text));
|
.add(SelectOptionEditorEvent.filterOption(text));
|
||||||
},
|
},
|
||||||
onNewTag: (tagName) {
|
onSubmitted: (tagName) {
|
||||||
context
|
context
|
||||||
.read<SelectOptionCellEditorBloc>()
|
.read<SelectOptionCellEditorBloc>()
|
||||||
.add(SelectOptionEditorEvent.newOption(tagName));
|
.add(SelectOptionEditorEvent.trySelectOption(tagName));
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
@ -17,7 +17,7 @@ class SelectOptionTextField extends StatefulWidget {
|
|||||||
final LinkedHashMap<String, SelectOptionPB> selectedOptionMap;
|
final LinkedHashMap<String, SelectOptionPB> selectedOptionMap;
|
||||||
final double distanceToText;
|
final double distanceToText;
|
||||||
|
|
||||||
final Function(String) onNewTag;
|
final Function(String) onSubmitted;
|
||||||
final Function(String) newText;
|
final Function(String) newText;
|
||||||
final VoidCallback? onClick;
|
final VoidCallback? onClick;
|
||||||
|
|
||||||
@ -26,7 +26,7 @@ class SelectOptionTextField extends StatefulWidget {
|
|||||||
required this.selectedOptionMap,
|
required this.selectedOptionMap,
|
||||||
required this.distanceToText,
|
required this.distanceToText,
|
||||||
required this.tagController,
|
required this.tagController,
|
||||||
required this.onNewTag,
|
required this.onSubmitted,
|
||||||
required this.newText,
|
required this.newText,
|
||||||
this.onClick,
|
this.onClick,
|
||||||
TextEditingController? textController,
|
TextEditingController? textController,
|
||||||
@ -88,7 +88,7 @@ class _SelectOptionTextFieldState extends State<SelectOptionTextField> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (text.isNotEmpty) {
|
if (text.isNotEmpty) {
|
||||||
widget.onNewTag(text);
|
widget.onSubmitted(text);
|
||||||
focusNode.requestFocus();
|
focusNode.requestFocus();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user