Skip to content

Commit 39246df

Browse files
committed
[FORM] Form handlers can define an initial value for inputs
1 parent 5d8925f commit 39246df

File tree

4 files changed

+31
-6
lines changed

4 files changed

+31
-6
lines changed

package/pheno_ui/lib/tools/figma_form_handler.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'figma_user_data.dart';
66
abstract class FigmaFormHandler {
77
const FigmaFormHandler();
88
bool shouldDisplayInput(String id) => true;
9+
Future<T?> initialValueForInputID<T>(BuildContext context, Map<String, FigmaFormInput> inputs, String id) async => null;
910
void onInputRegistered<T>(BuildContext context, Map<String, FigmaFormInput> inputs, FigmaFormInput<T> input) { /* nothing */ }
1011
void onInputValueChanged<T>(BuildContext context, Map<String, FigmaFormInput> inputs, FigmaFormInput<T> input) { /* nothing */ }
1112
void onInputEditingComplete<T>(BuildContext context, Map<String, FigmaFormInput> inputs, FigmaFormInput<T> input) { /* nothing */ }

package/pheno_ui/lib/widgets/figma_checkbox.dart

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class FigmaCheckboxState extends FigmaComponentState {
2121
FigmaFormState? form;
2222
FocusNode? focusNode;
2323
String _state = 'unchecked';
24+
bool _hasInitialValue = false;
2425
late final String _id = userData.get('id', context: context, listen: false);
2526

2627
bool get checked => _state == 'checked';
@@ -37,7 +38,11 @@ class FigmaCheckboxState extends FigmaComponentState {
3738
super.initState();
3839
form = FigmaForm.maybeOf(context);
3940
if (form != null) {
40-
focusNode = form!.registerInput(_id, checked);
41+
form!.registerInput(_id, checked).then((value) {
42+
focusNode = value.$1;
43+
checked = value.$2;
44+
_hasInitialValue = true;
45+
});
4146
}
4247
}
4348

@@ -48,6 +53,10 @@ class FigmaCheckboxState extends FigmaComponentState {
4853

4954
@override
5055
Widget buildFigmaNode(BuildContext context) {
56+
if (!_hasInitialValue) {
57+
return const SizedBox();
58+
}
59+
5160
return GestureDetector(
5261
onTap: () {
5362
checked = !checked;

package/pheno_ui/lib/widgets/figma_form.dart

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,17 @@ class FigmaFormState extends StatefulFigmaNodeState<FigmaForm> {
7979
return handler.shouldDisplayInput(id) ?? true;
8080
}
8181

82-
FocusNode registerInput<T>(String id, T initialValue) {
82+
Future<(FocusNode, T)> registerInput<T>(String id, T initialValue) async {
8383
if (inputs.containsKey(id)) {
8484
logger.w('Input with id $id already exists.');
8585
}
8686
FocusNode node = FocusNode();
87-
inputs[id] = FigmaFormInput<T>(node, id, initialValue);
88-
handler.onInputRegistered(context, inputs, inputs[id]!);
89-
return node;
87+
T value = await handler.initialValueForInputID<T>(context, inputs, id) ?? initialValue;
88+
inputs[id] = FigmaFormInput<T>(node, id, value);
89+
if (context.mounted && mounted) {
90+
handler.onInputRegistered(context, inputs, inputs[id]!);
91+
}
92+
return (node, value);
9093
}
9194

9295
void inputValueChanged<T>(String id, T value) {

package/pheno_ui/lib/widgets/figma_text_field.dart

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'package:flutter/cupertino.dart';
12
import 'package:flutter/material.dart';
23

34
import '../models/figma_text_model.dart';
@@ -21,19 +22,29 @@ class FigmaTextField extends StatefulFigmaNode<FigmaTextModel> with FigmaFormWid
2122
class FigmaTextFieldState extends StatefulFigmaNodeState<FigmaTextField> {
2223
FigmaFormState? form;
2324
FocusNode? focusNode;
25+
bool _hasInitialValue = false;
26+
final TextEditingController _controller = TextEditingController();
2427
late final String _id = widget.model.userData.get('id', context: context, listen: false);
2528

2629
@override
2730
void initState() {
2831
super.initState();
2932
form = FigmaForm.maybeOf(context);
3033
if (form != null) {
31-
focusNode = form!.registerInput(_id, '');
34+
form!.registerInput(_id, '').then((value) {
35+
focusNode = value.$1;
36+
_controller.text = value.$2;
37+
_hasInitialValue = true;
38+
});
3239
}
3340
}
3441

3542
@override
3643
Widget buildFigmaNode(BuildContext context) {
44+
if (!_hasInitialValue) {
45+
return const SizedBox();
46+
}
47+
3748
var modelSegments = FigmaText.textSegmentsFromModel(context, widget.model);
3849
List<TextSpan> segments = modelSegments.map((m) => m.span).toList();
3950

@@ -54,6 +65,7 @@ class FigmaTextFieldState extends StatefulFigmaNodeState<FigmaTextField> {
5465
return Align(
5566
alignment: alignment,
5667
child: TextField(
68+
controller: _controller,
5769
focusNode: focusNode,
5870
onTapOutside: (_) => focusNode?.unfocus(),
5971
style: segments[0].style,

0 commit comments

Comments
 (0)