Skip to content

Commit

Permalink
Implement FormModelBuilder (#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
BenjiFarquhar committed May 11, 2024
1 parent c66f055 commit 667e208
Show file tree
Hide file tree
Showing 41 changed files with 3,508 additions and 2 deletions.
80 changes: 80 additions & 0 deletions packages/generator_tests/test/doc/annotateless_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,86 @@ class _AnnotatelessFormBuilderState extends State<AnnotatelessFormBuilder> {
}
}
/// Similar to the AnnotatelessFormBuilder but opts out of automatic form lifecycle
/// management.
///
/// See `AnnotatelessFormBuilder.initState` and `AnnotatelessFormBuilder.dispose` for examples
/// of initializing/disposing the formModel.
class AnnotatelessFormModelBuilder extends StatefulWidget {
const AnnotatelessFormModelBuilder({
Key? key,
required this.formModel,
this.child,
this.canPop,
this.onPopInvoked,
required this.builder,
this.initState,
}) : super(key: key);
final AnnotatelessForm formModel;
final Widget? child;
final bool Function(FormGroup formGroup)? canPop;
final void Function(FormGroup formGroup, bool didPop)? onPopInvoked;
final Widget Function(
BuildContext context, AnnotatelessForm formModel, Widget? child) builder;
final void Function(BuildContext context, AnnotatelessForm formModel)?
initState;
@override
_AnnotatelessFormModelBuilderState createState() =>
_AnnotatelessFormModelBuilderState();
}
class _AnnotatelessFormModelBuilderState
extends State<AnnotatelessFormModelBuilder> {
late AnnotatelessForm _formModel;
@override
void initState() {
super.initState();
_formModel = widget.formModel;
if (_formModel.form.disabled) {
_formModel.form.markAsDisabled();
}
widget.initState?.call(context, _formModel);
}
@override
void didUpdateWidget(covariant AnnotatelessFormModelBuilder oldWidget) {
super.didUpdateWidget(oldWidget);
if (widget.formModel != oldWidget.formModel) {
_formModel = widget.formModel;
}
}
@override
Widget build(BuildContext context) {
return ReactiveAnnotatelessForm(
key: ObjectKey(_formModel),
form: _formModel,
canPop: widget.canPop,
onPopInvoked: widget.onPopInvoked,
child: ReactiveFormBuilder(
form: () => _formModel.form,
canPop: widget.canPop,
onPopInvoked: widget.onPopInvoked,
builder: (context, formGroup, child) =>
widget.builder(context, _formModel, widget.child),
child: widget.child,
),
);
}
}
class AnnotatelessForm implements FormModel<Annotateless> {
AnnotatelessForm(
this.form,
Expand Down
80 changes: 80 additions & 0 deletions packages/generator_tests/test/doc/array_nullable_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,86 @@ class _ArrayNullableFormBuilderState extends State<ArrayNullableFormBuilder> {
}
}
/// Similar to the ArrayNullableFormBuilder but opts out of automatic form lifecycle
/// management.
///
/// See `ArrayNullableFormBuilder.initState` and `ArrayNullableFormBuilder.dispose` for examples
/// of initializing/disposing the formModel.
class ArrayNullableFormModelBuilder extends StatefulWidget {
const ArrayNullableFormModelBuilder({
Key? key,
required this.formModel,
this.child,
this.canPop,
this.onPopInvoked,
required this.builder,
this.initState,
}) : super(key: key);
final ArrayNullableForm formModel;
final Widget? child;
final bool Function(FormGroup formGroup)? canPop;
final void Function(FormGroup formGroup, bool didPop)? onPopInvoked;
final Widget Function(
BuildContext context, ArrayNullableForm formModel, Widget? child) builder;
final void Function(BuildContext context, ArrayNullableForm formModel)?
initState;
@override
_ArrayNullableFormModelBuilderState createState() =>
_ArrayNullableFormModelBuilderState();
}
class _ArrayNullableFormModelBuilderState
extends State<ArrayNullableFormModelBuilder> {
late ArrayNullableForm _formModel;
@override
void initState() {
super.initState();
_formModel = widget.formModel;
if (_formModel.form.disabled) {
_formModel.form.markAsDisabled();
}
widget.initState?.call(context, _formModel);
}
@override
void didUpdateWidget(covariant ArrayNullableFormModelBuilder oldWidget) {
super.didUpdateWidget(oldWidget);
if (widget.formModel != oldWidget.formModel) {
_formModel = widget.formModel;
}
}
@override
Widget build(BuildContext context) {
return ReactiveArrayNullableForm(
key: ObjectKey(_formModel),
form: _formModel,
canPop: widget.canPop,
onPopInvoked: widget.onPopInvoked,
child: ReactiveFormBuilder(
form: () => _formModel.form,
canPop: widget.canPop,
onPopInvoked: widget.onPopInvoked,
builder: (context, formGroup, child) =>
widget.builder(context, _formModel, widget.child),
child: widget.child,
),
);
}
}
class ArrayNullableForm implements FormModel<ArrayNullable> {
ArrayNullableForm(
this.form,
Expand Down
161 changes: 161 additions & 0 deletions packages/generator_tests/test/doc/delivery_list_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,86 @@ class _DeliveryListFormBuilderState extends State<DeliveryListFormBuilder> {
}
}
/// Similar to the DeliveryListFormBuilder but opts out of automatic form lifecycle
/// management.
///
/// See `DeliveryListFormBuilder.initState` and `DeliveryListFormBuilder.dispose` for examples
/// of initializing/disposing the formModel.
class DeliveryListFormModelBuilder extends StatefulWidget {
const DeliveryListFormModelBuilder({
Key? key,
required this.formModel,
this.child,
this.canPop,
this.onPopInvoked,
required this.builder,
this.initState,
}) : super(key: key);
final DeliveryListForm formModel;
final Widget? child;
final bool Function(FormGroup formGroup)? canPop;
final void Function(FormGroup formGroup, bool didPop)? onPopInvoked;
final Widget Function(
BuildContext context, DeliveryListForm formModel, Widget? child) builder;
final void Function(BuildContext context, DeliveryListForm formModel)?
initState;
@override
_DeliveryListFormModelBuilderState createState() =>
_DeliveryListFormModelBuilderState();
}
class _DeliveryListFormModelBuilderState
extends State<DeliveryListFormModelBuilder> {
late DeliveryListForm _formModel;
@override
void initState() {
super.initState();
_formModel = widget.formModel;
if (_formModel.form.disabled) {
_formModel.form.markAsDisabled();
}
widget.initState?.call(context, _formModel);
}
@override
void didUpdateWidget(covariant DeliveryListFormModelBuilder oldWidget) {
super.didUpdateWidget(oldWidget);
if (widget.formModel != oldWidget.formModel) {
_formModel = widget.formModel;
}
}
@override
Widget build(BuildContext context) {
return ReactiveDeliveryListForm(
key: ObjectKey(_formModel),
form: _formModel,
canPop: widget.canPop,
onPopInvoked: widget.onPopInvoked,
child: ReactiveFormBuilder(
form: () => _formModel.form,
canPop: widget.canPop,
onPopInvoked: widget.onPopInvoked,
builder: (context, formGroup, child) =>
widget.builder(context, _formModel, widget.child),
child: widget.child,
),
);
}
}
class DeliveryListForm implements FormModel<DeliveryList> {
DeliveryListForm(
this.form,
Expand Down Expand Up @@ -2072,6 +2152,87 @@ class _StandaloneDeliveryPointFormBuilderState
}
}
/// Similar to the StandaloneDeliveryPointFormBuilder but opts out of automatic form lifecycle
/// management.
///
/// See `StandaloneDeliveryPointFormBuilder.initState` and `StandaloneDeliveryPointFormBuilder.dispose` for examples
/// of initializing/disposing the formModel.
class StandaloneDeliveryPointFormModelBuilder extends StatefulWidget {
const StandaloneDeliveryPointFormModelBuilder({
Key? key,
required this.formModel,
this.child,
this.canPop,
this.onPopInvoked,
required this.builder,
this.initState,
}) : super(key: key);
final StandaloneDeliveryPointForm formModel;
final Widget? child;
final bool Function(FormGroup formGroup)? canPop;
final void Function(FormGroup formGroup, bool didPop)? onPopInvoked;
final Widget Function(BuildContext context,
StandaloneDeliveryPointForm formModel, Widget? child) builder;
final void Function(
BuildContext context, StandaloneDeliveryPointForm formModel)? initState;
@override
_StandaloneDeliveryPointFormModelBuilderState createState() =>
_StandaloneDeliveryPointFormModelBuilderState();
}
class _StandaloneDeliveryPointFormModelBuilderState
extends State<StandaloneDeliveryPointFormModelBuilder> {
late StandaloneDeliveryPointForm _formModel;
@override
void initState() {
super.initState();
_formModel = widget.formModel;
if (_formModel.form.disabled) {
_formModel.form.markAsDisabled();
}
widget.initState?.call(context, _formModel);
}
@override
void didUpdateWidget(
covariant StandaloneDeliveryPointFormModelBuilder oldWidget) {
super.didUpdateWidget(oldWidget);
if (widget.formModel != oldWidget.formModel) {
_formModel = widget.formModel;
}
}
@override
Widget build(BuildContext context) {
return ReactiveStandaloneDeliveryPointForm(
key: ObjectKey(_formModel),
form: _formModel,
canPop: widget.canPop,
onPopInvoked: widget.onPopInvoked,
child: ReactiveFormBuilder(
form: () => _formModel.form,
canPop: widget.canPop,
onPopInvoked: widget.onPopInvoked,
builder: (context, formGroup, child) =>
widget.builder(context, _formModel, widget.child),
child: widget.child,
),
);
}
}
class StandaloneDeliveryPointForm implements FormModel<DeliveryPoint> {
StandaloneDeliveryPointForm(
this.form,
Expand Down
Loading

0 comments on commit 667e208

Please sign in to comment.