Skip to content

Commit ba3f2f3

Browse files
committed
fix: focus when SelectOptionCellEditor show
1 parent 5191b6a commit ba3f2f3

File tree

1 file changed

+33
-19
lines changed
  • frontend/app_flowy/lib/plugins/grid/presentation/widgets/cell/select_option_cell

1 file changed

+33
-19
lines changed

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

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,17 @@ import 'package:textfield_tags/textfield_tags.dart';
1111

1212
import 'extension.dart';
1313

14-
class SelectOptionTextField extends StatelessWidget {
15-
final FocusNode _focusNode;
16-
final TextEditingController _controller;
14+
class SelectOptionTextField extends StatefulWidget {
1715
final TextfieldTagsController tagController;
1816
final List<SelectOptionPB> options;
1917
final LinkedHashMap<String, SelectOptionPB> selectedOptionMap;
20-
2118
final double distanceToText;
2219

2320
final Function(String) onNewTag;
2421
final Function(String) newText;
2522
final VoidCallback? onClick;
2623

27-
SelectOptionTextField({
24+
const SelectOptionTextField({
2825
required this.options,
2926
required this.selectedOptionMap,
3027
required this.distanceToText,
@@ -35,19 +32,36 @@ class SelectOptionTextField extends StatelessWidget {
3532
TextEditingController? textController,
3633
FocusNode? focusNode,
3734
Key? key,
38-
}) : _controller = textController ?? TextEditingController(),
39-
_focusNode = focusNode ?? FocusNode(),
40-
super(key: key);
35+
}) : super(key: key);
36+
37+
@override
38+
State<SelectOptionTextField> createState() => _SelectOptionTextFieldState();
39+
}
40+
41+
class _SelectOptionTextFieldState extends State<SelectOptionTextField> {
42+
late FocusNode focusNode;
43+
late TextEditingController controller;
44+
45+
@override
46+
void initState() {
47+
focusNode = FocusNode();
48+
controller = TextEditingController();
49+
50+
WidgetsBinding.instance.addPostFrameCallback((_) {
51+
focusNode.requestFocus();
52+
});
53+
super.initState();
54+
}
4155

4256
@override
4357
Widget build(BuildContext context) {
4458
final theme = context.watch<AppTheme>();
4559

4660
return TextFieldTags(
47-
textEditingController: _controller,
48-
textfieldTagsController: tagController,
49-
initialTags: selectedOptionMap.keys.toList(),
50-
focusNode: _focusNode,
61+
textEditingController: controller,
62+
textfieldTagsController: widget.tagController,
63+
initialTags: widget.selectedOptionMap.keys.toList(),
64+
focusNode: focusNode,
5165
textSeparators: const [','],
5266
inputfieldBuilder: (
5367
BuildContext context,
@@ -59,23 +73,22 @@ class SelectOptionTextField extends StatelessWidget {
5973
) {
6074
return ((context, sc, tags, onTagDelegate) {
6175
return TextField(
62-
autofocus: true,
6376
controller: editController,
6477
focusNode: focusNode,
65-
onTap: onClick,
78+
onTap: widget.onClick,
6679
onChanged: (text) {
6780
if (onChanged != null) {
6881
onChanged(text);
6982
}
70-
newText(text);
83+
widget.newText(text);
7184
},
7285
onSubmitted: (text) {
7386
if (onSubmitted != null) {
7487
onSubmitted(text);
7588
}
7689

7790
if (text.isNotEmpty) {
78-
onNewTag(text);
91+
widget.onNewTag(text);
7992
focusNode.requestFocus();
8093
}
8194
},
@@ -89,7 +102,8 @@ class SelectOptionTextField extends StatelessWidget {
89102
isDense: true,
90103
prefixIcon: _renderTags(context, sc),
91104
hintText: LocaleKeys.grid_selectOption_searchOption.tr(),
92-
prefixIconConstraints: BoxConstraints(maxWidth: distanceToText),
105+
prefixIconConstraints:
106+
BoxConstraints(maxWidth: widget.distanceToText),
93107
focusedBorder: OutlineInputBorder(
94108
borderSide: BorderSide(color: theme.main1, width: 1.0),
95109
borderRadius: Corners.s10Border,
@@ -102,11 +116,11 @@ class SelectOptionTextField extends StatelessWidget {
102116
}
103117

104118
Widget? _renderTags(BuildContext context, ScrollController sc) {
105-
if (selectedOptionMap.isEmpty) {
119+
if (widget.selectedOptionMap.isEmpty) {
106120
return null;
107121
}
108122

109-
final children = selectedOptionMap.values
123+
final children = widget.selectedOptionMap.values
110124
.map((option) =>
111125
SelectOptionTag.fromOption(context: context, option: option))
112126
.toList();

0 commit comments

Comments
 (0)