diff --git a/packages/generator_tests/test/doc/animated_url_list_output_test.dart b/packages/generator_tests/test/doc/animated_url_list_output_test.dart index 75e56f85..a0ff27db 100644 --- a/packages/generator_tests/test/doc/animated_url_list_output_test.dart +++ b/packages/generator_tests/test/doc/animated_url_list_output_test.dart @@ -69,8 +69,11 @@ class ReactiveAnimatedUrlLisOFormConsumer extends StatelessWidget { final Widget? child; final Widget Function( - BuildContext context, AnimatedUrlLisOForm formModel, Widget? child) - builder; + BuildContext context, + AnimatedUrlLisOForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -89,11 +92,7 @@ class AnimatedUrlLisOFormInheritedStreamer extends InheritedStreamer { required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final AnimatedUrlLisOForm form; } @@ -115,19 +114,19 @@ class ReactiveAnimatedUrlLisOForm extends StatelessWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - static AnimatedUrlLisOForm? of( - BuildContext context, { - bool listen = true, - }) { + static AnimatedUrlLisOForm? of(BuildContext context, {bool listen = true}) { if (listen) { return context .dependOnInheritedWidgetOfExactType< - AnimatedUrlLisOFormInheritedStreamer>() + AnimatedUrlLisOFormInheritedStreamer + >() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - AnimatedUrlLisOFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + AnimatedUrlLisOFormInheritedStreamer + >(); return element == null ? null : (element.widget as AnimatedUrlLisOFormInheritedStreamer).form; @@ -175,11 +174,14 @@ class AnimatedUrlLisOFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, AnimatedUrlLisOForm formModel, Widget? child) - builder; + BuildContext context, + AnimatedUrlLisOForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, AnimatedUrlLisOForm formModel)? - initState; + initState; @override _AnimatedUrlLisOFormBuilderState createState() => @@ -195,7 +197,10 @@ class _AnimatedUrlLisOFormBuilderState @override void initState() { _formModel = AnimatedUrlLisOForm( - AnimatedUrlLisOForm.formElements(widget.model), null); + AnimatedUrlLisOForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -273,10 +278,8 @@ final _logAnimatedUrlLisOForm = Logger.detached('AnimatedUrlLisOForm'); class AnimatedUrlLisOForm implements FormModel { - AnimatedUrlLisOForm( - this.form, - this.path, - ); + AnimatedUrlLisOForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String urlListControlName = "urlList"; @@ -284,8 +287,14 @@ class AnimatedUrlLisOForm final String? path; + // ignore: unused_field + final FormModel? _formModel; + final Map _disabled = {}; + @override + final Map initial; + String urlListControlPath() => pathBuilder(urlListControlName); List get _urlListValue => @@ -294,8 +303,6 @@ class AnimatedUrlLisOForm List get _urlListRawValue => urlListUrlEntityOForm.map((e) => e.rawModel).toList(); - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsUrlList { try { form.control(urlListControlPath()); @@ -337,15 +344,19 @@ class AnimatedUrlLisOForm if (toUpdate.isNotEmpty) { urlListControl.updateValue( - toUpdate.map((e) => UrlEntityOForm.formElements(e).rawValue).toList(), - updateParent: updateParent, - emitEvent: emitEvent); + toUpdate.map((e) => UrlEntityOForm.formElements(e).rawValue).toList(), + updateParent: updateParent, + emitEvent: emitEvent, + ); } if (toAdd.isNotEmpty) { toAdd.forEach((e) { - urlListControl.add(UrlEntityOForm.formElements(e), - updateParent: updateParent, emitEvent: emitEvent); + urlListControl.add( + UrlEntityOForm.formElements(e), + updateParent: updateParent, + emitEvent: emitEvent, + ); }); } } @@ -370,10 +381,7 @@ class AnimatedUrlLisOForm ); } - void urlListClear({ - bool updateParent = true, - bool emitEvent = true, - }) { + void urlListClear({bool updateParent = true, bool emitEvent = true}) { urlListUrlEntityOForm.clear(); urlListControl.clear(updateParent: updateParent, emitEvent: emitEvent); } @@ -386,18 +394,17 @@ class AnimatedUrlLisOForm final keys = urlListUrlEntityOForm.asMap().keys; final toPatch = []; - (value).asMap().forEach( - (k, v) { - if (keys.contains(k)) { - toPatch.add(v); - } - }, - ); + (value).asMap().forEach((k, v) { + if (keys.contains(k)) { + toPatch.add(v); + } + }); urlListControl.patchValue( - toPatch.map((e) => UrlEntityOForm.formElements(e).rawValue).toList(), - updateParent: updateParent, - emitEvent: emitEvent); + toPatch.map((e) => UrlEntityOForm.formElements(e).rawValue).toList(), + updateParent: updateParent, + emitEvent: emitEvent, + ); } void urlListValueReset( @@ -406,13 +413,11 @@ class AnimatedUrlLisOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - urlListControl.reset( - value: value - .map((e) => UrlEntityOForm.formElements(e).rawValue) - .toList(), - updateParent: updateParent, - emitEvent: emitEvent); + }) => urlListControl.reset( + value: value.map((e) => UrlEntityOForm.formElements(e).rawValue).toList(), + updateParent: updateParent, + emitEvent: emitEvent, + ); FormArray> get urlListControl => form.control(urlListControlPath()) as FormArray>; @@ -422,8 +427,12 @@ class AnimatedUrlLisOForm return values .asMap() - .map((k, v) => - MapEntry(k, UrlEntityOForm(form, pathBuilder("urlList.$k")))) + .map( + (k, v) => MapEntry( + k, + UrlEntityOForm(form, pathBuilder("urlList.$k"), _formModel ?? this), + ), + ) .values .toList(); } @@ -447,11 +456,11 @@ class AnimatedUrlLisOForm } ExtendedControl?>, List> - get urlListExtendedControl => - ExtendedControl?>, List>( - form.control(urlListControlPath()) - as FormArray>, - () => urlListUrlEntityOForm); + get urlListExtendedControl => + ExtendedControl?>, List>( + form.control(urlListControlPath()) as FormArray>, + () => urlListUrlEntityOForm, + ); void addUrlListItem(UrlEntityO value) { urlListControl.add(UrlEntityOForm.formElements(value)); @@ -488,31 +497,24 @@ class AnimatedUrlLisOForm } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { - final currentFormInstance = currentForm; - - if (currentFormInstance is! FormGroup) { - return; - } - + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); urlListUrlEntityOForm.forEach((e) => e.toggleDisabled()); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { urlListUrlEntityOForm.forEach((e) => e.toggleDisabled()); - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { - currentFormInstance.controls[key]?.markAsEnabled( + currentForm.controls[key]?.markAsEnabled( updateParent: updateParent, emitEvent: emitEvent, ); @@ -528,9 +530,7 @@ class AnimatedUrlLisOForm final currentForm = this.currentForm; return const DeepCollectionEquality().equals( - currentForm is FormControlCollection - ? currentForm.rawValue - : currentForm.value, + currentForm.rawValue, AnimatedUrlLisOForm.formElements(other).rawValue, ); } @@ -551,8 +551,16 @@ class AnimatedUrlLisOForm } @override - AbstractControl get currentForm { - return path == null ? form : form.control(path!); + bool get hasChanged { + return !const DeepCollectionEquality().equals( + currentForm.rawValue, + initial, + ); + } + + @override + FormGroup get currentForm { + return path == null ? form : form.control(path!) as FormGroup; } @override @@ -560,47 +568,110 @@ class AnimatedUrlLisOForm AnimatedUrlLisO? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(AnimatedUrlLisOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + AnimatedUrlLisOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void upsertValue( + AnimatedUrlLisO? value, { + bool updateParent = true, + bool emitEvent = true, + }) { + final formElements = AnimatedUrlLisOForm.formElements(value); + + currentForm.addAll(formElements.controls); + } @override void reset({ AnimatedUrlLisO? value, bool updateParent = true, bool emitEvent = true, - }) => - form.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void updateInitial(Map? value, String? path) { + if (_formModel != null) { + _formModel?.updateInitial(currentForm.rawValue, path); + return; + } + + if (value == null) return; + + if (path == null || path.isEmpty) { + initial.addAll(value); + return; + } + + final keys = path.split('.'); + Object? current = initial; + for (var i = 0; i < keys.length - 1; i++) { + final key = keys[i]; + + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current = current[index]; + continue; + } + } + + if (current is Map) { + if (!current.containsKey(key)) { + current[key] = {}; + } + current = current[key]; + continue; + } + + return; + } + + final key = keys.last; + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current[index] = value; + } + } else if (current is Map) { + current[key] = value; + } + } String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(AnimatedUrlLisO? animatedUrlLisO) => FormGroup({ - urlListControlName: FormArray( - (animatedUrlLisO?.urlList ?? []) - .map((e) => UrlEntityOForm.formElements(e)) - .toList(), - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(AnimatedUrlLisO? animatedUrlLisO) => FormGroup( + { + urlListControlName: FormArray( + (animatedUrlLisO?.urlList ?? []) + .map((e) => UrlEntityOForm.formElements(e)) + .toList(), + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } final _logUrlEntityOForm = Logger.detached('UrlEntityOForm'); class UrlEntityOForm implements FormModel { - UrlEntityOForm( - this.form, - this.path, - ); + UrlEntityOForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String labelControlName = "label"; @@ -610,8 +681,14 @@ class UrlEntityOForm implements FormModel { final String? path; + // ignore: unused_field + final FormModel? _formModel; + final Map _disabled = {}; + @override + final Map initial; + String labelControlPath() => pathBuilder(labelControlName); String urlControlPath() => pathBuilder(urlControlName); @@ -620,12 +697,10 @@ class UrlEntityOForm implements FormModel { String get _urlValue => urlControl.value as String; - String? get _labelRawValue => labelControl.value; + String? get _labelRawValue => containsLabel ? labelControl.value : null; - String? get _urlRawValue => urlControl.value; + String? get _urlRawValue => containsUrl ? urlControl.value : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsLabel { try { form.control(labelControlPath()); @@ -635,8 +710,6 @@ class UrlEntityOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsUrl { try { form.control(urlControlPath()); @@ -654,12 +727,7 @@ class UrlEntityOForm implements FormModel { void get urlFocus => form.focus(urlControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void labelRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void labelRemove({bool updateParent = true, bool emitEvent = true}) { if (containsLabel) { final controlPath = path; if (controlPath == null) { @@ -682,12 +750,7 @@ class UrlEntityOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void urlRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void urlRemove({bool updateParent = true, bool emitEvent = true}) { if (containsUrl) { final controlPath = path; if (controlPath == null) { @@ -715,8 +778,11 @@ class UrlEntityOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - labelControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + labelControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void urlValueUpdate( @@ -724,8 +790,11 @@ class UrlEntityOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - urlControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + urlControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void labelValuePatch( @@ -733,8 +802,11 @@ class UrlEntityOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - labelControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + labelControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void urlValuePatch( @@ -742,8 +814,11 @@ class UrlEntityOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - urlControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + urlControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void labelValueReset( @@ -752,14 +827,13 @@ class UrlEntityOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - labelControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => labelControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void urlValueReset( String? value, { @@ -767,14 +841,13 @@ class UrlEntityOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - urlControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => urlControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get labelControl => form.control(labelControlPath()) as FormControl; @@ -839,27 +912,20 @@ class UrlEntityOForm implements FormModel { } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { - final currentFormInstance = currentForm; - - if (currentFormInstance is! FormGroup) { - return; - } - + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { - currentFormInstance.controls[key]?.markAsEnabled( + currentForm.controls[key]?.markAsEnabled( updateParent: updateParent, emitEvent: emitEvent, ); @@ -875,9 +941,7 @@ class UrlEntityOForm implements FormModel { final currentForm = this.currentForm; return const DeepCollectionEquality().equals( - currentForm is FormControlCollection - ? currentForm.rawValue - : currentForm.value, + currentForm.rawValue, UrlEntityOForm.formElements(other).rawValue, ); } @@ -898,8 +962,16 @@ class UrlEntityOForm implements FormModel { } @override - AbstractControl get currentForm { - return path == null ? form : form.control(path!); + bool get hasChanged { + return !const DeepCollectionEquality().equals( + currentForm.rawValue, + initial, + ); + } + + @override + FormGroup get currentForm { + return path == null ? form : form.control(path!) as FormGroup; } @override @@ -907,44 +979,110 @@ class UrlEntityOForm implements FormModel { UrlEntityO? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(UrlEntityOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + UrlEntityOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void upsertValue( + UrlEntityO? value, { + bool updateParent = true, + bool emitEvent = true, + }) { + final formElements = UrlEntityOForm.formElements(value); + + currentForm.addAll(formElements.controls); + } @override void reset({ UrlEntityO? value, bool updateParent = true, bool emitEvent = true, - }) => - form.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void updateInitial(Map? value, String? path) { + if (_formModel != null) { + _formModel?.updateInitial(currentForm.rawValue, path); + return; + } + + if (value == null) return; + + if (path == null || path.isEmpty) { + initial.addAll(value); + return; + } + + final keys = path.split('.'); + Object? current = initial; + for (var i = 0; i < keys.length - 1; i++) { + final key = keys[i]; + + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current = current[index]; + continue; + } + } + + if (current is Map) { + if (!current.containsKey(key)) { + current[key] = {}; + } + current = current[key]; + continue; + } + + return; + } + + final key = keys.last; + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current[index] = value; + } + } else if (current is Map) { + current[key] = value; + } + } String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(UrlEntityO? urlEntityO) => FormGroup({ - labelControlName: FormControl( - value: urlEntityO?.label, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - urlControlName: FormControl( - value: urlEntityO?.url, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(UrlEntityO? urlEntityO) => FormGroup( + { + labelControlName: FormControl( + value: urlEntityO?.label, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + urlControlName: FormControl( + value: urlEntityO?.url, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } @Rf(output: true) @@ -957,13 +1095,16 @@ class AnimatedUrlLisOOutput { class UrlEntityOOutput { final String label; final String url; - UrlEntityOOutput( - {@RfControl(validators: [RequiredValidator()]) required this.label, - @RfControl(validators: [RequiredValidator()]) required this.url}); + UrlEntityOOutput({ + @RfControl(validators: [RequiredValidator()]) required this.label, + @RfControl(validators: [RequiredValidator()]) required this.url, + }); } class ReactiveAnimatedUrlLisOFormArrayBuilder< - ReactiveAnimatedUrlLisOFormArrayBuilderT> extends StatelessWidget { + ReactiveAnimatedUrlLisOFormArrayBuilderT +> + extends StatelessWidget { const ReactiveAnimatedUrlLisOFormArrayBuilder({ Key? key, this.control, @@ -972,30 +1113,41 @@ class ReactiveAnimatedUrlLisOFormArrayBuilder< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - AnimatedUrlLisOForm formModel)? control; + AnimatedUrlLisOForm formModel, + )? + control; - final Widget Function(BuildContext context, List itemList, - AnimatedUrlLisOForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + AnimatedUrlLisOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveAnimatedUrlLisOFormArrayBuilderT? item, - AnimatedUrlLisOForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveAnimatedUrlLisOFormArrayBuilderT? item, + AnimatedUrlLisOForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -1009,21 +1161,13 @@ class ReactiveAnimatedUrlLisOFormArrayBuilder< final itemBuilder = this.itemBuilder; return ReactiveFormArrayItemBuilder< - ReactiveAnimatedUrlLisOFormArrayBuilderT>( + ReactiveAnimatedUrlLisOFormArrayBuilderT + >( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1032,7 +1176,9 @@ class ReactiveAnimatedUrlLisOFormArrayBuilder< } class ReactiveAnimatedUrlLisOFormArrayBuilder2< - ReactiveAnimatedUrlLisOFormArrayBuilderT> extends StatelessWidget { + ReactiveAnimatedUrlLisOFormArrayBuilderT +> + extends StatelessWidget { const ReactiveAnimatedUrlLisOFormArrayBuilder2({ Key? key, this.control, @@ -1041,36 +1187,47 @@ class ReactiveAnimatedUrlLisOFormArrayBuilder2< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - AnimatedUrlLisOForm formModel)? control; + AnimatedUrlLisOForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - AnimatedUrlLisOForm formModel - }) params)? builder; + ({ + BuildContext context, + List itemList, + AnimatedUrlLisOForm formModel, + }) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveAnimatedUrlLisOFormArrayBuilderT? item, - AnimatedUrlLisOForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveAnimatedUrlLisOFormArrayBuilderT? item, + AnimatedUrlLisOForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -1084,27 +1241,22 @@ class ReactiveAnimatedUrlLisOFormArrayBuilder2< final itemBuilder = this.itemBuilder; return ReactiveFormArrayItemBuilder< - ReactiveAnimatedUrlLisOFormArrayBuilderT>( + ReactiveAnimatedUrlLisOFormArrayBuilderT + >( formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1113,32 +1265,48 @@ class ReactiveAnimatedUrlLisOFormArrayBuilder2< } class ReactiveAnimatedUrlLisOFormFormGroupArrayBuilder< - ReactiveAnimatedUrlLisOFormFormGroupArrayBuilderT> extends StatelessWidget { + ReactiveAnimatedUrlLisOFormFormGroupArrayBuilderT +> + extends StatelessWidget { const ReactiveAnimatedUrlLisOFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? extended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(AnimatedUrlLisOForm formModel)? + getExtended; - final ExtendedControl?>, - List> - Function(AnimatedUrlLisOForm formModel)? getExtended; - - final Widget Function(BuildContext context, List itemList, - AnimatedUrlLisOForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + AnimatedUrlLisOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveAnimatedUrlLisOFormFormGroupArrayBuilderT? item, - AnimatedUrlLisOForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveAnimatedUrlLisOFormFormGroupArrayBuilderT? item, + AnimatedUrlLisOForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -1153,26 +1321,18 @@ class ReactiveAnimatedUrlLisOFormFormGroupArrayBuilder< return StreamBuilder?>?>( stream: value.control.valueChanges, builder: (context, snapshot) { - final itemList = (value.value() ?? - []) - .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) - .values - .toList(); - - return builder?.call( - context, - itemList, - formModel, - ) ?? + final itemList = + (value.value() ?? + []) + .asMap() + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) + .values + .toList(); + + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/generator_tests/test/doc/animated_url_list_test.dart b/packages/generator_tests/test/doc/animated_url_list_test.dart index 5dda9e39..b5b06e07 100644 --- a/packages/generator_tests/test/doc/animated_url_list_test.dart +++ b/packages/generator_tests/test/doc/animated_url_list_test.dart @@ -69,8 +69,11 @@ class ReactiveAnimatedUrlListFormConsumer extends StatelessWidget { final Widget? child; final Widget Function( - BuildContext context, AnimatedUrlListForm formModel, Widget? child) - builder; + BuildContext context, + AnimatedUrlListForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -89,11 +92,7 @@ class AnimatedUrlListFormInheritedStreamer extends InheritedStreamer { required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final AnimatedUrlListForm form; } @@ -115,19 +114,19 @@ class ReactiveAnimatedUrlListForm extends StatelessWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - static AnimatedUrlListForm? of( - BuildContext context, { - bool listen = true, - }) { + static AnimatedUrlListForm? of(BuildContext context, {bool listen = true}) { if (listen) { return context .dependOnInheritedWidgetOfExactType< - AnimatedUrlListFormInheritedStreamer>() + AnimatedUrlListFormInheritedStreamer + >() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - AnimatedUrlListFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + AnimatedUrlListFormInheritedStreamer + >(); return element == null ? null : (element.widget as AnimatedUrlListFormInheritedStreamer).form; @@ -175,11 +174,14 @@ class AnimatedUrlListFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, AnimatedUrlListForm formModel, Widget? child) - builder; + BuildContext context, + AnimatedUrlListForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, AnimatedUrlListForm formModel)? - initState; + initState; @override _AnimatedUrlListFormBuilderState createState() => @@ -195,7 +197,10 @@ class _AnimatedUrlListFormBuilderState @override void initState() { _formModel = AnimatedUrlListForm( - AnimatedUrlListForm.formElements(widget.model), null); + AnimatedUrlListForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -273,10 +278,8 @@ final _logAnimatedUrlListForm = Logger.detached('AnimatedUrlListForm'); class AnimatedUrlListForm implements FormModel { - AnimatedUrlListForm( - this.form, - this.path, - ); + AnimatedUrlListForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String urlListControlName = "urlList"; @@ -284,8 +287,14 @@ class AnimatedUrlListForm final String? path; + // ignore: unused_field + final FormModel? _formModel; + final Map _disabled = {}; + @override + final Map initial; + String urlListControlPath() => pathBuilder(urlListControlName); List get _urlListValue => @@ -294,8 +303,6 @@ class AnimatedUrlListForm List get _urlListRawValue => urlListUrlEntityForm.map((e) => e.rawModel).toList(); - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsUrlList { try { form.control(urlListControlPath()); @@ -337,15 +344,19 @@ class AnimatedUrlListForm if (toUpdate.isNotEmpty) { urlListControl.updateValue( - toUpdate.map((e) => UrlEntityForm.formElements(e).rawValue).toList(), - updateParent: updateParent, - emitEvent: emitEvent); + toUpdate.map((e) => UrlEntityForm.formElements(e).rawValue).toList(), + updateParent: updateParent, + emitEvent: emitEvent, + ); } if (toAdd.isNotEmpty) { toAdd.forEach((e) { - urlListControl.add(UrlEntityForm.formElements(e), - updateParent: updateParent, emitEvent: emitEvent); + urlListControl.add( + UrlEntityForm.formElements(e), + updateParent: updateParent, + emitEvent: emitEvent, + ); }); } } @@ -370,10 +381,7 @@ class AnimatedUrlListForm ); } - void urlListClear({ - bool updateParent = true, - bool emitEvent = true, - }) { + void urlListClear({bool updateParent = true, bool emitEvent = true}) { urlListUrlEntityForm.clear(); urlListControl.clear(updateParent: updateParent, emitEvent: emitEvent); } @@ -386,18 +394,17 @@ class AnimatedUrlListForm final keys = urlListUrlEntityForm.asMap().keys; final toPatch = []; - (value).asMap().forEach( - (k, v) { - if (keys.contains(k)) { - toPatch.add(v); - } - }, - ); + (value).asMap().forEach((k, v) { + if (keys.contains(k)) { + toPatch.add(v); + } + }); urlListControl.patchValue( - toPatch.map((e) => UrlEntityForm.formElements(e).rawValue).toList(), - updateParent: updateParent, - emitEvent: emitEvent); + toPatch.map((e) => UrlEntityForm.formElements(e).rawValue).toList(), + updateParent: updateParent, + emitEvent: emitEvent, + ); } void urlListValueReset( @@ -406,12 +413,11 @@ class AnimatedUrlListForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - urlListControl.reset( - value: - value.map((e) => UrlEntityForm.formElements(e).rawValue).toList(), - updateParent: updateParent, - emitEvent: emitEvent); + }) => urlListControl.reset( + value: value.map((e) => UrlEntityForm.formElements(e).rawValue).toList(), + updateParent: updateParent, + emitEvent: emitEvent, + ); FormArray> get urlListControl => form.control(urlListControlPath()) as FormArray>; @@ -421,8 +427,12 @@ class AnimatedUrlListForm return values .asMap() - .map((k, v) => - MapEntry(k, UrlEntityForm(form, pathBuilder("urlList.$k")))) + .map( + (k, v) => MapEntry( + k, + UrlEntityForm(form, pathBuilder("urlList.$k"), _formModel ?? this), + ), + ) .values .toList(); } @@ -446,11 +456,11 @@ class AnimatedUrlListForm } ExtendedControl?>, List> - get urlListExtendedControl => - ExtendedControl?>, List>( - form.control(urlListControlPath()) - as FormArray>, - () => urlListUrlEntityForm); + get urlListExtendedControl => + ExtendedControl?>, List>( + form.control(urlListControlPath()) as FormArray>, + () => urlListUrlEntityForm, + ); void addUrlListItem(UrlEntity value) { urlListControl.add(UrlEntityForm.formElements(value)); @@ -486,31 +496,24 @@ class AnimatedUrlListForm } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { - final currentFormInstance = currentForm; - - if (currentFormInstance is! FormGroup) { - return; - } - + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); urlListUrlEntityForm.forEach((e) => e.toggleDisabled()); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { urlListUrlEntityForm.forEach((e) => e.toggleDisabled()); - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { - currentFormInstance.controls[key]?.markAsEnabled( + currentForm.controls[key]?.markAsEnabled( updateParent: updateParent, emitEvent: emitEvent, ); @@ -526,9 +529,7 @@ class AnimatedUrlListForm final currentForm = this.currentForm; return const DeepCollectionEquality().equals( - currentForm is FormControlCollection - ? currentForm.rawValue - : currentForm.value, + currentForm.rawValue, AnimatedUrlListForm.formElements(other).rawValue, ); } @@ -549,8 +550,16 @@ class AnimatedUrlListForm } @override - AbstractControl get currentForm { - return path == null ? form : form.control(path!); + bool get hasChanged { + return !const DeepCollectionEquality().equals( + currentForm.rawValue, + initial, + ); + } + + @override + FormGroup get currentForm { + return path == null ? form : form.control(path!) as FormGroup; } @override @@ -558,47 +567,110 @@ class AnimatedUrlListForm AnimatedUrlList? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(AnimatedUrlListForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + AnimatedUrlListForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void upsertValue( + AnimatedUrlList? value, { + bool updateParent = true, + bool emitEvent = true, + }) { + final formElements = AnimatedUrlListForm.formElements(value); + + currentForm.addAll(formElements.controls); + } @override void reset({ AnimatedUrlList? value, bool updateParent = true, bool emitEvent = true, - }) => - form.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void updateInitial(Map? value, String? path) { + if (_formModel != null) { + _formModel?.updateInitial(currentForm.rawValue, path); + return; + } + + if (value == null) return; + + if (path == null || path.isEmpty) { + initial.addAll(value); + return; + } + + final keys = path.split('.'); + Object? current = initial; + for (var i = 0; i < keys.length - 1; i++) { + final key = keys[i]; + + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current = current[index]; + continue; + } + } + + if (current is Map) { + if (!current.containsKey(key)) { + current[key] = {}; + } + current = current[key]; + continue; + } + + return; + } + + final key = keys.last; + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current[index] = value; + } + } else if (current is Map) { + current[key] = value; + } + } String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(AnimatedUrlList? animatedUrlList) => FormGroup({ - urlListControlName: FormArray( - (animatedUrlList?.urlList ?? []) - .map((e) => UrlEntityForm.formElements(e)) - .toList(), - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(AnimatedUrlList? animatedUrlList) => FormGroup( + { + urlListControlName: FormArray( + (animatedUrlList?.urlList ?? []) + .map((e) => UrlEntityForm.formElements(e)) + .toList(), + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } final _logUrlEntityForm = Logger.detached('UrlEntityForm'); class UrlEntityForm implements FormModel { - UrlEntityForm( - this.form, - this.path, - ); + UrlEntityForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String labelControlName = "label"; @@ -608,8 +680,14 @@ class UrlEntityForm implements FormModel { final String? path; + // ignore: unused_field + final FormModel? _formModel; + final Map _disabled = {}; + @override + final Map initial; + String labelControlPath() => pathBuilder(labelControlName); String urlControlPath() => pathBuilder(urlControlName); @@ -622,8 +700,6 @@ class UrlEntityForm implements FormModel { String get _urlRawValue => urlControl.value ?? ""; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsLabel { try { form.control(labelControlPath()); @@ -633,8 +709,6 @@ class UrlEntityForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsUrl { try { form.control(urlControlPath()); @@ -657,8 +731,11 @@ class UrlEntityForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - labelControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + labelControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void urlValueUpdate( @@ -666,8 +743,11 @@ class UrlEntityForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - urlControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + urlControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void labelValuePatch( @@ -675,8 +755,11 @@ class UrlEntityForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - labelControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + labelControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void urlValuePatch( @@ -684,8 +767,11 @@ class UrlEntityForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - urlControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + urlControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void labelValueReset( @@ -694,14 +780,13 @@ class UrlEntityForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - labelControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => labelControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void urlValueReset( String value, { @@ -709,14 +794,13 @@ class UrlEntityForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - urlControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => urlControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get labelControl => form.control(labelControlPath()) as FormControl; @@ -780,27 +864,20 @@ class UrlEntityForm implements FormModel { } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { - final currentFormInstance = currentForm; - - if (currentFormInstance is! FormGroup) { - return; - } - + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { - currentFormInstance.controls[key]?.markAsEnabled( + currentForm.controls[key]?.markAsEnabled( updateParent: updateParent, emitEvent: emitEvent, ); @@ -816,9 +893,7 @@ class UrlEntityForm implements FormModel { final currentForm = this.currentForm; return const DeepCollectionEquality().equals( - currentForm is FormControlCollection - ? currentForm.rawValue - : currentForm.value, + currentForm.rawValue, UrlEntityForm.formElements(other).rawValue, ); } @@ -839,8 +914,16 @@ class UrlEntityForm implements FormModel { } @override - AbstractControl get currentForm { - return path == null ? form : form.control(path!); + bool get hasChanged { + return !const DeepCollectionEquality().equals( + currentForm.rawValue, + initial, + ); + } + + @override + FormGroup get currentForm { + return path == null ? form : form.control(path!) as FormGroup; } @override @@ -848,48 +931,116 @@ class UrlEntityForm implements FormModel { UrlEntity? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(UrlEntityForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + UrlEntityForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void upsertValue( + UrlEntity? value, { + bool updateParent = true, + bool emitEvent = true, + }) { + final formElements = UrlEntityForm.formElements(value); + + currentForm.addAll(formElements.controls); + } @override void reset({ UrlEntity? value, bool updateParent = true, bool emitEvent = true, - }) => - form.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void updateInitial(Map? value, String? path) { + if (_formModel != null) { + _formModel?.updateInitial(currentForm.rawValue, path); + return; + } + + if (value == null) return; + + if (path == null || path.isEmpty) { + initial.addAll(value); + return; + } + + final keys = path.split('.'); + Object? current = initial; + for (var i = 0; i < keys.length - 1; i++) { + final key = keys[i]; + + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current = current[index]; + continue; + } + } + + if (current is Map) { + if (!current.containsKey(key)) { + current[key] = {}; + } + current = current[key]; + continue; + } + + return; + } + + final key = keys.last; + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current[index] = value; + } + } else if (current is Map) { + current[key] = value; + } + } String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(UrlEntity? urlEntity) => FormGroup({ - labelControlName: FormControl( - value: urlEntity?.label, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - urlControlName: FormControl( - value: urlEntity?.url, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(UrlEntity? urlEntity) => FormGroup( + { + labelControlName: FormControl( + value: urlEntity?.label, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + urlControlName: FormControl( + value: urlEntity?.url, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } class ReactiveAnimatedUrlListFormArrayBuilder< - ReactiveAnimatedUrlListFormArrayBuilderT> extends StatelessWidget { + ReactiveAnimatedUrlListFormArrayBuilderT +> + extends StatelessWidget { const ReactiveAnimatedUrlListFormArrayBuilder({ Key? key, this.control, @@ -898,30 +1049,41 @@ class ReactiveAnimatedUrlListFormArrayBuilder< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - AnimatedUrlListForm formModel)? control; + AnimatedUrlListForm formModel, + )? + control; - final Widget Function(BuildContext context, List itemList, - AnimatedUrlListForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + AnimatedUrlListForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveAnimatedUrlListFormArrayBuilderT? item, - AnimatedUrlListForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveAnimatedUrlListFormArrayBuilderT? item, + AnimatedUrlListForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -935,21 +1097,13 @@ class ReactiveAnimatedUrlListFormArrayBuilder< final itemBuilder = this.itemBuilder; return ReactiveFormArrayItemBuilder< - ReactiveAnimatedUrlListFormArrayBuilderT>( + ReactiveAnimatedUrlListFormArrayBuilderT + >( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -958,7 +1112,9 @@ class ReactiveAnimatedUrlListFormArrayBuilder< } class ReactiveAnimatedUrlListFormArrayBuilder2< - ReactiveAnimatedUrlListFormArrayBuilderT> extends StatelessWidget { + ReactiveAnimatedUrlListFormArrayBuilderT +> + extends StatelessWidget { const ReactiveAnimatedUrlListFormArrayBuilder2({ Key? key, this.control, @@ -967,36 +1123,47 @@ class ReactiveAnimatedUrlListFormArrayBuilder2< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - AnimatedUrlListForm formModel)? control; + AnimatedUrlListForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - AnimatedUrlListForm formModel - }) params)? builder; + ({ + BuildContext context, + List itemList, + AnimatedUrlListForm formModel, + }) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveAnimatedUrlListFormArrayBuilderT? item, - AnimatedUrlListForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveAnimatedUrlListFormArrayBuilderT? item, + AnimatedUrlListForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -1010,27 +1177,22 @@ class ReactiveAnimatedUrlListFormArrayBuilder2< final itemBuilder = this.itemBuilder; return ReactiveFormArrayItemBuilder< - ReactiveAnimatedUrlListFormArrayBuilderT>( + ReactiveAnimatedUrlListFormArrayBuilderT + >( formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1039,32 +1201,48 @@ class ReactiveAnimatedUrlListFormArrayBuilder2< } class ReactiveAnimatedUrlListFormFormGroupArrayBuilder< - ReactiveAnimatedUrlListFormFormGroupArrayBuilderT> extends StatelessWidget { + ReactiveAnimatedUrlListFormFormGroupArrayBuilderT +> + extends StatelessWidget { const ReactiveAnimatedUrlListFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? extended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(AnimatedUrlListForm formModel)? + getExtended; - final ExtendedControl?>, - List> - Function(AnimatedUrlListForm formModel)? getExtended; - - final Widget Function(BuildContext context, List itemList, - AnimatedUrlListForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + AnimatedUrlListForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveAnimatedUrlListFormFormGroupArrayBuilderT? item, - AnimatedUrlListForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveAnimatedUrlListFormFormGroupArrayBuilderT? item, + AnimatedUrlListForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -1079,26 +1257,18 @@ class ReactiveAnimatedUrlListFormFormGroupArrayBuilder< return StreamBuilder?>?>( stream: value.control.valueChanges, builder: (context, snapshot) { - final itemList = (value.value() ?? - []) - .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) - .values - .toList(); - - return builder?.call( - context, - itemList, - formModel, - ) ?? + final itemList = + (value.value() ?? + []) + .asMap() + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) + .values + .toList(); + + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/generator_tests/test/doc/annotateless_output_test.dart b/packages/generator_tests/test/doc/annotateless_output_test.dart index cca10604..e693dbbd 100644 --- a/packages/generator_tests/test/doc/annotateless_output_test.dart +++ b/packages/generator_tests/test/doc/annotateless_output_test.dart @@ -60,7 +60,11 @@ class ReactiveAnnotatelessOFormConsumer extends StatelessWidget { final Widget? child; final Widget Function( - BuildContext context, AnnotatelessOForm formModel, Widget? child) builder; + BuildContext context, + AnnotatelessOForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -79,11 +83,7 @@ class AnnotatelessOFormInheritedStreamer extends InheritedStreamer { required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final AnnotatelessOForm form; } @@ -105,19 +105,19 @@ class ReactiveAnnotatelessOForm extends StatelessWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - static AnnotatelessOForm? of( - BuildContext context, { - bool listen = true, - }) { + static AnnotatelessOForm? of(BuildContext context, {bool listen = true}) { if (listen) { return context .dependOnInheritedWidgetOfExactType< - AnnotatelessOFormInheritedStreamer>() + AnnotatelessOFormInheritedStreamer + >() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - AnnotatelessOFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + AnnotatelessOFormInheritedStreamer + >(); return element == null ? null : (element.widget as AnnotatelessOFormInheritedStreamer).form; @@ -165,10 +165,14 @@ class AnnotatelessOFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, AnnotatelessOForm formModel, Widget? child) builder; + BuildContext context, + AnnotatelessOForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, AnnotatelessOForm formModel)? - initState; + initState; @override _AnnotatelessOFormBuilderState createState() => @@ -182,8 +186,11 @@ class _AnnotatelessOFormBuilderState extends State { @override void initState() { - _formModel = - AnnotatelessOForm(AnnotatelessOForm.formElements(widget.model), null); + _formModel = AnnotatelessOForm( + AnnotatelessOForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -261,10 +268,8 @@ final _logAnnotatelessOForm = Logger.detached('AnnotatelessOForm'); class AnnotatelessOForm implements FormModel { - AnnotatelessOForm( - this.form, - this.path, - ); + AnnotatelessOForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String emailControlName = "email"; @@ -274,8 +279,14 @@ class AnnotatelessOForm final String? path; + // ignore: unused_field + final FormModel? _formModel; + final Map _disabled = {}; + @override + final Map initial; + String emailControlPath() => pathBuilder(emailControlName); String passwordControlPath() => pathBuilder(passwordControlName); @@ -288,8 +299,6 @@ class AnnotatelessOForm String get _passwordRawValue => passwordControl.value ?? ""; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsEmail { try { form.control(emailControlPath()); @@ -299,8 +308,6 @@ class AnnotatelessOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsPassword { try { form.control(passwordControlPath()); @@ -323,8 +330,11 @@ class AnnotatelessOForm bool updateParent = true, bool emitEvent = true, }) { - emailControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void passwordValueUpdate( @@ -332,8 +342,11 @@ class AnnotatelessOForm bool updateParent = true, bool emitEvent = true, }) { - passwordControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + passwordControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailValuePatch( @@ -341,8 +354,11 @@ class AnnotatelessOForm bool updateParent = true, bool emitEvent = true, }) { - emailControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void passwordValuePatch( @@ -350,8 +366,11 @@ class AnnotatelessOForm bool updateParent = true, bool emitEvent = true, }) { - passwordControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + passwordControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailValueReset( @@ -360,14 +379,13 @@ class AnnotatelessOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - emailControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => emailControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void passwordValueReset( String value, { @@ -375,14 +393,13 @@ class AnnotatelessOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - passwordControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => passwordControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get emailControl => form.control(emailControlPath()) as FormControl; @@ -447,27 +464,20 @@ class AnnotatelessOForm } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { - final currentFormInstance = currentForm; - - if (currentFormInstance is! FormGroup) { - return; - } - + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { - currentFormInstance.controls[key]?.markAsEnabled( + currentForm.controls[key]?.markAsEnabled( updateParent: updateParent, emitEvent: emitEvent, ); @@ -483,9 +493,7 @@ class AnnotatelessOForm final currentForm = this.currentForm; return const DeepCollectionEquality().equals( - currentForm is FormControlCollection - ? currentForm.rawValue - : currentForm.value, + currentForm.rawValue, AnnotatelessOForm.formElements(other).rawValue, ); } @@ -506,8 +514,16 @@ class AnnotatelessOForm } @override - AbstractControl get currentForm { - return path == null ? form : form.control(path!); + bool get hasChanged { + return !const DeepCollectionEquality().equals( + currentForm.rawValue, + initial, + ); + } + + @override + FormGroup get currentForm { + return path == null ? form : form.control(path!) as FormGroup; } @override @@ -515,44 +531,110 @@ class AnnotatelessOForm AnnotatelessO? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(AnnotatelessOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + AnnotatelessOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void upsertValue( + AnnotatelessO? value, { + bool updateParent = true, + bool emitEvent = true, + }) { + final formElements = AnnotatelessOForm.formElements(value); + + currentForm.addAll(formElements.controls); + } @override void reset({ AnnotatelessO? value, bool updateParent = true, bool emitEvent = true, - }) => - form.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void updateInitial(Map? value, String? path) { + if (_formModel != null) { + _formModel?.updateInitial(currentForm.rawValue, path); + return; + } + + if (value == null) return; + + if (path == null || path.isEmpty) { + initial.addAll(value); + return; + } + + final keys = path.split('.'); + Object? current = initial; + for (var i = 0; i < keys.length - 1; i++) { + final key = keys[i]; + + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current = current[index]; + continue; + } + } + + if (current is Map) { + if (!current.containsKey(key)) { + current[key] = {}; + } + current = current[key]; + continue; + } + + return; + } + + final key = keys.last; + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current[index] = value; + } + } else if (current is Map) { + current[key] = value; + } + } String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(AnnotatelessO? annotatelessO) => FormGroup({ - emailControlName: FormControl( - value: annotatelessO?.email, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - passwordControlName: FormControl( - value: annotatelessO?.password, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(AnnotatelessO? annotatelessO) => FormGroup( + { + emailControlName: FormControl( + value: annotatelessO?.email, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + passwordControlName: FormControl( + value: annotatelessO?.password, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } @Rf(output: true) @@ -565,7 +647,9 @@ class AnnotatelessOOutput extends Equatable { } class ReactiveAnnotatelessOFormArrayBuilder< - ReactiveAnnotatelessOFormArrayBuilderT> extends StatelessWidget { + ReactiveAnnotatelessOFormArrayBuilderT +> + extends StatelessWidget { const ReactiveAnnotatelessOFormArrayBuilder({ Key? key, this.control, @@ -574,30 +658,41 @@ class ReactiveAnnotatelessOFormArrayBuilder< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - AnnotatelessOForm formModel)? control; + AnnotatelessOForm formModel, + )? + control; - final Widget Function(BuildContext context, List itemList, - AnnotatelessOForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + AnnotatelessOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveAnnotatelessOFormArrayBuilderT? item, - AnnotatelessOForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveAnnotatelessOFormArrayBuilderT? item, + AnnotatelessOForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -613,18 +708,9 @@ class ReactiveAnnotatelessOFormArrayBuilder< return ReactiveFormArrayItemBuilder( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -633,7 +719,9 @@ class ReactiveAnnotatelessOFormArrayBuilder< } class ReactiveAnnotatelessOFormArrayBuilder2< - ReactiveAnnotatelessOFormArrayBuilderT> extends StatelessWidget { + ReactiveAnnotatelessOFormArrayBuilderT +> + extends StatelessWidget { const ReactiveAnnotatelessOFormArrayBuilder2({ Key? key, this.control, @@ -642,36 +730,43 @@ class ReactiveAnnotatelessOFormArrayBuilder2< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - AnnotatelessOForm formModel)? control; + AnnotatelessOForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - AnnotatelessOForm formModel - }) params)? builder; + ({BuildContext context, List itemList, AnnotatelessOForm formModel}) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveAnnotatelessOFormArrayBuilderT? item, - AnnotatelessOForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveAnnotatelessOFormArrayBuilderT? item, + AnnotatelessOForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -688,23 +783,17 @@ class ReactiveAnnotatelessOFormArrayBuilder2< formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -713,32 +802,48 @@ class ReactiveAnnotatelessOFormArrayBuilder2< } class ReactiveAnnotatelessOFormFormGroupArrayBuilder< - ReactiveAnnotatelessOFormFormGroupArrayBuilderT> extends StatelessWidget { + ReactiveAnnotatelessOFormFormGroupArrayBuilderT +> + extends StatelessWidget { const ReactiveAnnotatelessOFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(AnnotatelessOForm formModel)? + getExtended; - final ExtendedControl?>, - List>? extended; - - final ExtendedControl?>, - List> - Function(AnnotatelessOForm formModel)? getExtended; - - final Widget Function(BuildContext context, List itemList, - AnnotatelessOForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + AnnotatelessOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveAnnotatelessOFormFormGroupArrayBuilderT? item, - AnnotatelessOForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveAnnotatelessOFormFormGroupArrayBuilderT? item, + AnnotatelessOForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -753,26 +858,18 @@ class ReactiveAnnotatelessOFormFormGroupArrayBuilder< return StreamBuilder?>?>( stream: value.control.valueChanges, builder: (context, snapshot) { - final itemList = (value.value() ?? - []) - .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) - .values - .toList(); - - return builder?.call( - context, - itemList, - formModel, - ) ?? + final itemList = + (value.value() ?? + []) + .asMap() + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) + .values + .toList(); + + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/generator_tests/test/doc/annotateless_test.dart b/packages/generator_tests/test/doc/annotateless_test.dart index 72c95f7f..cb1e9426 100644 --- a/packages/generator_tests/test/doc/annotateless_test.dart +++ b/packages/generator_tests/test/doc/annotateless_test.dart @@ -57,7 +57,11 @@ class ReactiveAnnotatelessFormConsumer extends StatelessWidget { final Widget? child; final Widget Function( - BuildContext context, AnnotatelessForm formModel, Widget? child) builder; + BuildContext context, + AnnotatelessForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -76,11 +80,7 @@ class AnnotatelessFormInheritedStreamer extends InheritedStreamer { required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final AnnotatelessForm form; } @@ -102,19 +102,19 @@ class ReactiveAnnotatelessForm extends StatelessWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - static AnnotatelessForm? of( - BuildContext context, { - bool listen = true, - }) { + static AnnotatelessForm? of(BuildContext context, {bool listen = true}) { if (listen) { return context .dependOnInheritedWidgetOfExactType< - AnnotatelessFormInheritedStreamer>() + AnnotatelessFormInheritedStreamer + >() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - AnnotatelessFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + AnnotatelessFormInheritedStreamer + >(); return element == null ? null : (element.widget as AnnotatelessFormInheritedStreamer).form; @@ -162,10 +162,14 @@ class AnnotatelessFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, AnnotatelessForm formModel, Widget? child) builder; + BuildContext context, + AnnotatelessForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, AnnotatelessForm formModel)? - initState; + initState; @override _AnnotatelessFormBuilderState createState() => @@ -179,8 +183,11 @@ class _AnnotatelessFormBuilderState extends State { @override void initState() { - _formModel = - AnnotatelessForm(AnnotatelessForm.formElements(widget.model), null); + _formModel = AnnotatelessForm( + AnnotatelessForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -257,10 +264,8 @@ class _AnnotatelessFormBuilderState extends State { final _logAnnotatelessForm = Logger.detached('AnnotatelessForm'); class AnnotatelessForm implements FormModel { - AnnotatelessForm( - this.form, - this.path, - ); + AnnotatelessForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String emailControlName = "email"; @@ -270,8 +275,14 @@ class AnnotatelessForm implements FormModel { final String? path; + // ignore: unused_field + final FormModel? _formModel; + final Map _disabled = {}; + @override + final Map initial; + String emailControlPath() => pathBuilder(emailControlName); String passwordControlPath() => pathBuilder(passwordControlName); @@ -284,8 +295,6 @@ class AnnotatelessForm implements FormModel { String get _passwordRawValue => passwordControl.value ?? ""; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsEmail { try { form.control(emailControlPath()); @@ -295,8 +304,6 @@ class AnnotatelessForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsPassword { try { form.control(passwordControlPath()); @@ -319,8 +326,11 @@ class AnnotatelessForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - emailControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void passwordValueUpdate( @@ -328,8 +338,11 @@ class AnnotatelessForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - passwordControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + passwordControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailValuePatch( @@ -337,8 +350,11 @@ class AnnotatelessForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - emailControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void passwordValuePatch( @@ -346,8 +362,11 @@ class AnnotatelessForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - passwordControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + passwordControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailValueReset( @@ -356,14 +375,13 @@ class AnnotatelessForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - emailControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => emailControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void passwordValueReset( String value, { @@ -371,14 +389,13 @@ class AnnotatelessForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - passwordControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => passwordControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get emailControl => form.control(emailControlPath()) as FormControl; @@ -442,27 +459,20 @@ class AnnotatelessForm implements FormModel { } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { - final currentFormInstance = currentForm; - - if (currentFormInstance is! FormGroup) { - return; - } - + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { - currentFormInstance.controls[key]?.markAsEnabled( + currentForm.controls[key]?.markAsEnabled( updateParent: updateParent, emitEvent: emitEvent, ); @@ -478,9 +488,7 @@ class AnnotatelessForm implements FormModel { final currentForm = this.currentForm; return const DeepCollectionEquality().equals( - currentForm is FormControlCollection - ? currentForm.rawValue - : currentForm.value, + currentForm.rawValue, AnnotatelessForm.formElements(other).rawValue, ); } @@ -501,8 +509,16 @@ class AnnotatelessForm implements FormModel { } @override - AbstractControl get currentForm { - return path == null ? form : form.control(path!); + bool get hasChanged { + return !const DeepCollectionEquality().equals( + currentForm.rawValue, + initial, + ); + } + + @override + FormGroup get currentForm { + return path == null ? form : form.control(path!) as FormGroup; } @override @@ -510,48 +526,116 @@ class AnnotatelessForm implements FormModel { Annotateless? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(AnnotatelessForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + AnnotatelessForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void upsertValue( + Annotateless? value, { + bool updateParent = true, + bool emitEvent = true, + }) { + final formElements = AnnotatelessForm.formElements(value); + + currentForm.addAll(formElements.controls); + } @override void reset({ Annotateless? value, bool updateParent = true, bool emitEvent = true, - }) => - form.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void updateInitial(Map? value, String? path) { + if (_formModel != null) { + _formModel?.updateInitial(currentForm.rawValue, path); + return; + } + + if (value == null) return; + + if (path == null || path.isEmpty) { + initial.addAll(value); + return; + } + + final keys = path.split('.'); + Object? current = initial; + for (var i = 0; i < keys.length - 1; i++) { + final key = keys[i]; + + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current = current[index]; + continue; + } + } + + if (current is Map) { + if (!current.containsKey(key)) { + current[key] = {}; + } + current = current[key]; + continue; + } + + return; + } + + final key = keys.last; + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current[index] = value; + } + } else if (current is Map) { + current[key] = value; + } + } String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(Annotateless? annotateless) => FormGroup({ - emailControlName: FormControl( - value: annotateless?.email, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - passwordControlName: FormControl( - value: annotateless?.password, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(Annotateless? annotateless) => FormGroup( + { + emailControlName: FormControl( + value: annotateless?.email, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + passwordControlName: FormControl( + value: annotateless?.password, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } class ReactiveAnnotatelessFormArrayBuilder< - ReactiveAnnotatelessFormArrayBuilderT> extends StatelessWidget { + ReactiveAnnotatelessFormArrayBuilderT +> + extends StatelessWidget { const ReactiveAnnotatelessFormArrayBuilder({ Key? key, this.control, @@ -560,30 +644,41 @@ class ReactiveAnnotatelessFormArrayBuilder< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - AnnotatelessForm formModel)? control; + AnnotatelessForm formModel, + )? + control; - final Widget Function(BuildContext context, List itemList, - AnnotatelessForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + AnnotatelessForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveAnnotatelessFormArrayBuilderT? item, - AnnotatelessForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveAnnotatelessFormArrayBuilderT? item, + AnnotatelessForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -599,18 +694,9 @@ class ReactiveAnnotatelessFormArrayBuilder< return ReactiveFormArrayItemBuilder( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -619,7 +705,9 @@ class ReactiveAnnotatelessFormArrayBuilder< } class ReactiveAnnotatelessFormArrayBuilder2< - ReactiveAnnotatelessFormArrayBuilderT> extends StatelessWidget { + ReactiveAnnotatelessFormArrayBuilderT +> + extends StatelessWidget { const ReactiveAnnotatelessFormArrayBuilder2({ Key? key, this.control, @@ -628,36 +716,43 @@ class ReactiveAnnotatelessFormArrayBuilder2< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - AnnotatelessForm formModel)? control; + AnnotatelessForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - AnnotatelessForm formModel - }) params)? builder; + ({BuildContext context, List itemList, AnnotatelessForm formModel}) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveAnnotatelessFormArrayBuilderT? item, - AnnotatelessForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveAnnotatelessFormArrayBuilderT? item, + AnnotatelessForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -674,23 +769,17 @@ class ReactiveAnnotatelessFormArrayBuilder2< formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -699,32 +788,48 @@ class ReactiveAnnotatelessFormArrayBuilder2< } class ReactiveAnnotatelessFormFormGroupArrayBuilder< - ReactiveAnnotatelessFormFormGroupArrayBuilderT> extends StatelessWidget { + ReactiveAnnotatelessFormFormGroupArrayBuilderT +> + extends StatelessWidget { const ReactiveAnnotatelessFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(AnnotatelessForm formModel)? + getExtended; - final ExtendedControl?>, - List>? extended; - - final ExtendedControl?>, - List> - Function(AnnotatelessForm formModel)? getExtended; - - final Widget Function(BuildContext context, List itemList, - AnnotatelessForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + AnnotatelessForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveAnnotatelessFormFormGroupArrayBuilderT? item, - AnnotatelessForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveAnnotatelessFormFormGroupArrayBuilderT? item, + AnnotatelessForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -739,26 +844,18 @@ class ReactiveAnnotatelessFormFormGroupArrayBuilder< return StreamBuilder?>?>( stream: value.control.valueChanges, builder: (context, snapshot) { - final itemList = (value.value() ?? - []) - .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) - .values - .toList(); - - return builder?.call( - context, - itemList, - formModel, - ) ?? + final itemList = + (value.value() ?? + []) + .asMap() + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) + .values + .toList(); + + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/generator_tests/test/doc/array_nullable_output_test.dart b/packages/generator_tests/test/doc/array_nullable_output_test.dart index 78566201..2fc6ca51 100644 --- a/packages/generator_tests/test/doc/array_nullable_output_test.dart +++ b/packages/generator_tests/test/doc/array_nullable_output_test.dart @@ -72,8 +72,11 @@ class ReactiveArrayNullableOFormConsumer extends StatelessWidget { final Widget? child; final Widget Function( - BuildContext context, ArrayNullableOForm formModel, Widget? child) - builder; + BuildContext context, + ArrayNullableOForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -92,11 +95,7 @@ class ArrayNullableOFormInheritedStreamer extends InheritedStreamer { required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final ArrayNullableOForm form; } @@ -118,19 +117,19 @@ class ReactiveArrayNullableOForm extends StatelessWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - static ArrayNullableOForm? of( - BuildContext context, { - bool listen = true, - }) { + static ArrayNullableOForm? of(BuildContext context, {bool listen = true}) { if (listen) { return context .dependOnInheritedWidgetOfExactType< - ArrayNullableOFormInheritedStreamer>() + ArrayNullableOFormInheritedStreamer + >() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - ArrayNullableOFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + ArrayNullableOFormInheritedStreamer + >(); return element == null ? null : (element.widget as ArrayNullableOFormInheritedStreamer).form; @@ -178,11 +177,14 @@ class ArrayNullableOFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, ArrayNullableOForm formModel, Widget? child) - builder; + BuildContext context, + ArrayNullableOForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, ArrayNullableOForm formModel)? - initState; + initState; @override _ArrayNullableOFormBuilderState createState() => @@ -196,8 +198,11 @@ class _ArrayNullableOFormBuilderState extends State { @override void initState() { - _formModel = - ArrayNullableOForm(ArrayNullableOForm.formElements(widget.model), null); + _formModel = ArrayNullableOForm( + ArrayNullableOForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -275,10 +280,8 @@ final _logArrayNullableOForm = Logger.detached('ArrayNullableOForm'); class ArrayNullableOForm implements FormModel { - ArrayNullableOForm( - this.form, - this.path, - ); + ArrayNullableOForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String emailListControlName = "emailList"; @@ -294,8 +297,14 @@ class ArrayNullableOForm final String? path; + // ignore: unused_field + final FormModel? _formModel; + final Map _disabled = {}; + @override + final Map initial; + String someListControlPath() => pathBuilder(someListControlName); String emailListControlPath() => pathBuilder(emailListControlName); @@ -306,7 +315,8 @@ class ArrayNullableOForm String modeListControlPath() => pathBuilder(modeListControlName); - List? get _someListValue => someListControl.value; + List? get _someListValue => + containsSomeList ? someListControl.value : null; List get _emailListValue => emailListControl.rawValue.whereType().toList(); @@ -314,13 +324,16 @@ class ArrayNullableOForm List get _fruitListValue => fruitListControl.rawValue.whereType().toList(); - List? get _vegetablesListValue => - vegetablesListControl.rawValue.whereType().toList(); + List? get _vegetablesListValue => containsVegetablesList + ? vegetablesListControl.rawValue.whereType().toList() + : null; - List? get _modeListValue => - modeListControl.rawValue.whereType().toList(); + List? get _modeListValue => containsModeList + ? modeListControl.rawValue.whereType().toList() + : null; - List? get _someListRawValue => someListControl.value; + List? get _someListRawValue => + containsSomeList ? someListControl.value : null; List get _emailListRawValue => emailListControl.rawValue.whereType().toList(); @@ -328,14 +341,14 @@ class ArrayNullableOForm List get _fruitListRawValue => fruitListControl.rawValue.whereType().toList(); - List? get _vegetablesListRawValue => - vegetablesListControl.rawValue.whereType().toList(); + List? get _vegetablesListRawValue => containsVegetablesList + ? vegetablesListControl.rawValue.whereType().toList() + : null; - List? get _modeListRawValue => - modeListControl.rawValue.whereType().toList(); + List? get _modeListRawValue => containsModeList + ? modeListControl.rawValue.whereType().toList() + : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsSomeList { try { form.control(someListControlPath()); @@ -345,8 +358,6 @@ class ArrayNullableOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsEmailList { try { form.control(emailListControlPath()); @@ -356,8 +367,6 @@ class ArrayNullableOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsFruitList { try { form.control(fruitListControlPath()); @@ -367,8 +376,6 @@ class ArrayNullableOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsVegetablesList { try { form.control(vegetablesListControlPath()); @@ -378,8 +385,6 @@ class ArrayNullableOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsModeList { try { form.control(modeListControlPath()); @@ -409,12 +414,7 @@ class ArrayNullableOForm void get modeListFocus => form.focus(modeListControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void someListRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void someListRemove({bool updateParent = true, bool emitEvent = true}) { if (containsSomeList) { final controlPath = path; if (controlPath == null) { @@ -437,12 +437,7 @@ class ArrayNullableOForm } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void vegetablesListRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void vegetablesListRemove({bool updateParent = true, bool emitEvent = true}) { if (containsVegetablesList) { final controlPath = path; if (controlPath == null) { @@ -465,12 +460,7 @@ class ArrayNullableOForm } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void modeListRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void modeListRemove({bool updateParent = true, bool emitEvent = true}) { if (containsModeList) { final controlPath = path; if (controlPath == null) { @@ -498,8 +488,11 @@ class ArrayNullableOForm bool updateParent = true, bool emitEvent = true, }) { - someListControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + someListControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailListValueUpdate( @@ -507,8 +500,11 @@ class ArrayNullableOForm bool updateParent = true, bool emitEvent = true, }) { - emailListControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailListControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void fruitListValueUpdate( @@ -516,8 +512,11 @@ class ArrayNullableOForm bool updateParent = true, bool emitEvent = true, }) { - fruitListControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + fruitListControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void vegetablesListValueUpdate( @@ -525,8 +524,11 @@ class ArrayNullableOForm bool updateParent = true, bool emitEvent = true, }) { - vegetablesListControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + vegetablesListControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void modeListValueUpdate( @@ -534,8 +536,11 @@ class ArrayNullableOForm bool updateParent = true, bool emitEvent = true, }) { - modeListControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + modeListControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void someListValuePatch( @@ -543,8 +548,11 @@ class ArrayNullableOForm bool updateParent = true, bool emitEvent = true, }) { - someListControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + someListControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailListValuePatch( @@ -552,8 +560,11 @@ class ArrayNullableOForm bool updateParent = true, bool emitEvent = true, }) { - emailListControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailListControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void fruitListValuePatch( @@ -561,8 +572,11 @@ class ArrayNullableOForm bool updateParent = true, bool emitEvent = true, }) { - fruitListControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + fruitListControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void vegetablesListValuePatch( @@ -570,8 +584,11 @@ class ArrayNullableOForm bool updateParent = true, bool emitEvent = true, }) { - vegetablesListControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + vegetablesListControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void modeListValuePatch( @@ -579,8 +596,11 @@ class ArrayNullableOForm bool updateParent = true, bool emitEvent = true, }) { - modeListControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + modeListControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void someListValueReset( @@ -589,14 +609,13 @@ class ArrayNullableOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - someListControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => someListControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void emailListValueReset( List value, { @@ -604,14 +623,13 @@ class ArrayNullableOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - emailListControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => emailListControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void fruitListValueReset( List value, { @@ -619,14 +637,13 @@ class ArrayNullableOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - fruitListControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => fruitListControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void vegetablesListValueReset( List? value, { @@ -634,14 +651,13 @@ class ArrayNullableOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - vegetablesListControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => vegetablesListControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void modeListValueReset( List? value, { @@ -649,14 +665,13 @@ class ArrayNullableOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - modeListControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => modeListControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl> get someListControl => form.control(someListControlPath()) as FormControl>; @@ -806,13 +821,15 @@ class ArrayNullableOForm break; } - emailListControl.add(FormControl( - value: value, - validators: resultingValidators, - asyncValidators: resultingAsyncValidators, - asyncValidatorsDebounceTime: asyncValidatorsDebounceTime ?? 250, - disabled: disabled ?? false, - )); + emailListControl.add( + FormControl( + value: value, + validators: resultingValidators, + asyncValidators: resultingAsyncValidators, + asyncValidatorsDebounceTime: asyncValidatorsDebounceTime ?? 250, + disabled: disabled ?? false, + ), + ); } void addFruitListItem( @@ -846,13 +863,15 @@ class ArrayNullableOForm break; } - fruitListControl.add(FormControl( - value: value, - validators: resultingValidators, - asyncValidators: resultingAsyncValidators, - asyncValidatorsDebounceTime: asyncValidatorsDebounceTime ?? 250, - disabled: disabled ?? false, - )); + fruitListControl.add( + FormControl( + value: value, + validators: resultingValidators, + asyncValidators: resultingAsyncValidators, + asyncValidatorsDebounceTime: asyncValidatorsDebounceTime ?? 250, + disabled: disabled ?? false, + ), + ); } void addVegetablesListItem( @@ -886,13 +905,15 @@ class ArrayNullableOForm break; } - vegetablesListControl.add(FormControl( - value: value, - validators: resultingValidators, - asyncValidators: resultingAsyncValidators, - asyncValidatorsDebounceTime: asyncValidatorsDebounceTime ?? 250, - disabled: disabled ?? false, - )); + vegetablesListControl.add( + FormControl( + value: value, + validators: resultingValidators, + asyncValidators: resultingAsyncValidators, + asyncValidatorsDebounceTime: asyncValidatorsDebounceTime ?? 250, + disabled: disabled ?? false, + ), + ); } void addModeListItem( @@ -926,13 +947,15 @@ class ArrayNullableOForm break; } - modeListControl.add(FormControl( - value: value, - validators: resultingValidators, - asyncValidators: resultingAsyncValidators, - asyncValidatorsDebounceTime: asyncValidatorsDebounceTime ?? 250, - disabled: disabled ?? false, - )); + modeListControl.add( + FormControl( + value: value, + validators: resultingValidators, + asyncValidators: resultingAsyncValidators, + asyncValidatorsDebounceTime: asyncValidatorsDebounceTime ?? 250, + disabled: disabled ?? false, + ), + ); } @override @@ -948,45 +971,40 @@ class ArrayNullableOForm ); } return ArrayNullableOOutput( - emailList: _emailListValue, - fruitList: _fruitListValue, - vegetablesList: _vegetablesListValue, - modeList: _modeListValue, - someList: _someListValue); + emailList: _emailListValue, + fruitList: _fruitListValue, + vegetablesList: _vegetablesListValue, + modeList: _modeListValue, + someList: _someListValue, + ); } @override ArrayNullableO get rawModel { return ArrayNullableO( - emailList: _emailListRawValue, - fruitList: _fruitListRawValue, - vegetablesList: _vegetablesListRawValue, - modeList: _modeListRawValue, - someList: _someListRawValue); + emailList: _emailListRawValue, + fruitList: _fruitListRawValue, + vegetablesList: _vegetablesListRawValue, + modeList: _modeListRawValue, + someList: _someListRawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { - final currentFormInstance = currentForm; - - if (currentFormInstance is! FormGroup) { - return; - } - + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { - currentFormInstance.controls[key]?.markAsEnabled( + currentForm.controls[key]?.markAsEnabled( updateParent: updateParent, emitEvent: emitEvent, ); @@ -1002,9 +1020,7 @@ class ArrayNullableOForm final currentForm = this.currentForm; return const DeepCollectionEquality().equals( - currentForm is FormControlCollection - ? currentForm.rawValue - : currentForm.value, + currentForm.rawValue, ArrayNullableOForm.formElements(other).rawValue, ); } @@ -1025,8 +1041,16 @@ class ArrayNullableOForm } @override - AbstractControl get currentForm { - return path == null ? form : form.control(path!); + bool get hasChanged { + return !const DeepCollectionEquality().equals( + currentForm.rawValue, + initial, + ); + } + + @override + FormGroup get currentForm { + return path == null ? form : form.control(path!) as FormGroup; } @override @@ -1034,93 +1058,170 @@ class ArrayNullableOForm ArrayNullableO? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(ArrayNullableOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + ArrayNullableOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void upsertValue( + ArrayNullableO? value, { + bool updateParent = true, + bool emitEvent = true, + }) { + final formElements = ArrayNullableOForm.formElements(value); + + currentForm.addAll(formElements.controls); + } @override void reset({ ArrayNullableO? value, bool updateParent = true, bool emitEvent = true, - }) => - form.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void updateInitial(Map? value, String? path) { + if (_formModel != null) { + _formModel?.updateInitial(currentForm.rawValue, path); + return; + } + + if (value == null) return; + + if (path == null || path.isEmpty) { + initial.addAll(value); + return; + } + + final keys = path.split('.'); + Object? current = initial; + for (var i = 0; i < keys.length - 1; i++) { + final key = keys[i]; + + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current = current[index]; + continue; + } + } + + if (current is Map) { + if (!current.containsKey(key)) { + current[key] = {}; + } + current = current[key]; + continue; + } + + return; + } + + final key = keys.last; + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current[index] = value; + } + } else if (current is Map) { + current[key] = value; + } + } String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(ArrayNullableO? arrayNullableO) => FormGroup({ - emailListControlName: FormArray( - (arrayNullableO?.emailList ?? []) - .map((e) => FormControl( - value: e, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - )) - .toList(), - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false), - fruitListControlName: FormArray( - (arrayNullableO?.fruitList ?? []) - .map((e) => FormControl( - value: e, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - )) - .toList(), - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false), - vegetablesListControlName: FormArray( - (arrayNullableO?.vegetablesList ?? []) - .map((e) => FormControl( - value: e, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - )) - .toList(), - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false), - modeListControlName: FormArray( - (arrayNullableO?.modeList ?? []) - .map((e) => FormControl( - value: e, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - )) - .toList(), - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false), - someListControlName: FormControl>( - value: arrayNullableO?.someList, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(ArrayNullableO? arrayNullableO) => FormGroup( + { + emailListControlName: FormArray( + (arrayNullableO?.emailList ?? []) + .map( + (e) => FormControl( + value: e, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ), + ) + .toList(), + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ), + fruitListControlName: FormArray( + (arrayNullableO?.fruitList ?? []) + .map( + (e) => FormControl( + value: e, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ), + ) + .toList(), + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ), + vegetablesListControlName: FormArray( + (arrayNullableO?.vegetablesList ?? []) + .map( + (e) => FormControl( + value: e, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ), + ) + .toList(), + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ), + modeListControlName: FormArray( + (arrayNullableO?.modeList ?? []) + .map( + (e) => FormControl( + value: e, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ), + ) + .toList(), + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ), + someListControlName: FormControl>( + value: arrayNullableO?.someList, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } @Rf(output: true) @@ -1130,16 +1231,19 @@ class ArrayNullableOOutput { final List? vegetablesList; final List? someList; final List? modeList; - ArrayNullableOOutput( - {@RfArray(validators: [RequiredValidator()]) required this.emailList, - @RfArray() this.fruitList = const [], - @RfArray() this.vegetablesList, - @RfArray() this.modeList, - @RfControl>() this.someList}); + ArrayNullableOOutput({ + @RfArray(validators: [RequiredValidator()]) required this.emailList, + @RfArray() this.fruitList = const [], + @RfArray() this.vegetablesList, + @RfArray() this.modeList, + @RfControl>() this.someList, + }); } class ReactiveArrayNullableOFormArrayBuilder< - ReactiveArrayNullableOFormArrayBuilderT> extends StatelessWidget { + ReactiveArrayNullableOFormArrayBuilderT +> + extends StatelessWidget { const ReactiveArrayNullableOFormArrayBuilder({ Key? key, this.control, @@ -1148,30 +1252,41 @@ class ReactiveArrayNullableOFormArrayBuilder< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - ArrayNullableOForm formModel)? control; + ArrayNullableOForm formModel, + )? + control; - final Widget Function(BuildContext context, List itemList, - ArrayNullableOForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + ArrayNullableOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveArrayNullableOFormArrayBuilderT? item, - ArrayNullableOForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveArrayNullableOFormArrayBuilderT? item, + ArrayNullableOForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -1185,21 +1300,13 @@ class ReactiveArrayNullableOFormArrayBuilder< final itemBuilder = this.itemBuilder; return ReactiveFormArrayItemBuilder< - ReactiveArrayNullableOFormArrayBuilderT>( + ReactiveArrayNullableOFormArrayBuilderT + >( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1208,7 +1315,9 @@ class ReactiveArrayNullableOFormArrayBuilder< } class ReactiveArrayNullableOFormArrayBuilder2< - ReactiveArrayNullableOFormArrayBuilderT> extends StatelessWidget { + ReactiveArrayNullableOFormArrayBuilderT +> + extends StatelessWidget { const ReactiveArrayNullableOFormArrayBuilder2({ Key? key, this.control, @@ -1217,36 +1326,47 @@ class ReactiveArrayNullableOFormArrayBuilder2< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - ArrayNullableOForm formModel)? control; + ArrayNullableOForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - ArrayNullableOForm formModel - }) params)? builder; + ({ + BuildContext context, + List itemList, + ArrayNullableOForm formModel, + }) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveArrayNullableOFormArrayBuilderT? item, - ArrayNullableOForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveArrayNullableOFormArrayBuilderT? item, + ArrayNullableOForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -1260,27 +1380,22 @@ class ReactiveArrayNullableOFormArrayBuilder2< final itemBuilder = this.itemBuilder; return ReactiveFormArrayItemBuilder< - ReactiveArrayNullableOFormArrayBuilderT>( + ReactiveArrayNullableOFormArrayBuilderT + >( formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1289,32 +1404,48 @@ class ReactiveArrayNullableOFormArrayBuilder2< } class ReactiveArrayNullableOFormFormGroupArrayBuilder< - ReactiveArrayNullableOFormFormGroupArrayBuilderT> extends StatelessWidget { + ReactiveArrayNullableOFormFormGroupArrayBuilderT +> + extends StatelessWidget { const ReactiveArrayNullableOFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? extended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(ArrayNullableOForm formModel)? + getExtended; - final ExtendedControl?>, - List> - Function(ArrayNullableOForm formModel)? getExtended; - - final Widget Function(BuildContext context, List itemList, - ArrayNullableOForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + ArrayNullableOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveArrayNullableOFormFormGroupArrayBuilderT? item, - ArrayNullableOForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveArrayNullableOFormFormGroupArrayBuilderT? item, + ArrayNullableOForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -1329,26 +1460,18 @@ class ReactiveArrayNullableOFormFormGroupArrayBuilder< return StreamBuilder?>?>( stream: value.control.valueChanges, builder: (context, snapshot) { - final itemList = (value.value() ?? - []) - .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) - .values - .toList(); - - return builder?.call( - context, - itemList, - formModel, - ) ?? + final itemList = + (value.value() ?? + []) + .asMap() + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) + .values + .toList(); + + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/generator_tests/test/doc/array_nullable_test.dart b/packages/generator_tests/test/doc/array_nullable_test.dart index 2327ef98..b902db2b 100644 --- a/packages/generator_tests/test/doc/array_nullable_test.dart +++ b/packages/generator_tests/test/doc/array_nullable_test.dart @@ -75,7 +75,11 @@ class ReactiveArrayNullableFormConsumer extends StatelessWidget { final Widget? child; final Widget Function( - BuildContext context, ArrayNullableForm formModel, Widget? child) builder; + BuildContext context, + ArrayNullableForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -94,11 +98,7 @@ class ArrayNullableFormInheritedStreamer extends InheritedStreamer { required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final ArrayNullableForm form; } @@ -120,19 +120,19 @@ class ReactiveArrayNullableForm extends StatelessWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - static ArrayNullableForm? of( - BuildContext context, { - bool listen = true, - }) { + static ArrayNullableForm? of(BuildContext context, {bool listen = true}) { if (listen) { return context .dependOnInheritedWidgetOfExactType< - ArrayNullableFormInheritedStreamer>() + ArrayNullableFormInheritedStreamer + >() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - ArrayNullableFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + ArrayNullableFormInheritedStreamer + >(); return element == null ? null : (element.widget as ArrayNullableFormInheritedStreamer).form; @@ -180,10 +180,14 @@ class ArrayNullableFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, ArrayNullableForm formModel, Widget? child) builder; + BuildContext context, + ArrayNullableForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, ArrayNullableForm formModel)? - initState; + initState; @override _ArrayNullableFormBuilderState createState() => @@ -197,8 +201,11 @@ class _ArrayNullableFormBuilderState extends State { @override void initState() { - _formModel = - ArrayNullableForm(ArrayNullableForm.formElements(widget.model), null); + _formModel = ArrayNullableForm( + ArrayNullableForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -275,10 +282,8 @@ class _ArrayNullableFormBuilderState extends State { final _logArrayNullableForm = Logger.detached('ArrayNullableForm'); class ArrayNullableForm implements FormModel { - ArrayNullableForm( - this.form, - this.path, - ); + ArrayNullableForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String emailListControlName = "emailList"; @@ -296,8 +301,14 @@ class ArrayNullableForm implements FormModel { final String? path; + // ignore: unused_field + final FormModel? _formModel; + final Map _disabled = {}; + @override + final Map initial; + String someListControlPath() => pathBuilder(someListControlName); String someListRequiredControlPath() => @@ -311,7 +322,8 @@ class ArrayNullableForm implements FormModel { String modeListControlPath() => pathBuilder(modeListControlName); - List? get _someListValue => someListControl.value; + List? get _someListValue => + containsSomeList ? someListControl.value : null; List get _someListRequiredValue => someListRequiredControl.value ?? const []; @@ -322,13 +334,16 @@ class ArrayNullableForm implements FormModel { List get _fruitListValue => fruitListControl.rawValue.whereType().toList(); - List? get _vegetablesListValue => - vegetablesListControl.rawValue.whereType().toList(); + List? get _vegetablesListValue => containsVegetablesList + ? vegetablesListControl.rawValue.whereType().toList() + : null; - List? get _modeListValue => - modeListControl.rawValue.whereType().toList(); + List? get _modeListValue => containsModeList + ? modeListControl.rawValue.whereType().toList() + : null; - List? get _someListRawValue => someListControl.value; + List? get _someListRawValue => + containsSomeList ? someListControl.value : null; List get _someListRequiredRawValue => someListRequiredControl.value ?? const []; @@ -339,14 +354,14 @@ class ArrayNullableForm implements FormModel { List get _fruitListRawValue => fruitListControl.rawValue.whereType().toList(); - List? get _vegetablesListRawValue => - vegetablesListControl.rawValue.whereType().toList(); + List? get _vegetablesListRawValue => containsVegetablesList + ? vegetablesListControl.rawValue.whereType().toList() + : null; - List? get _modeListRawValue => - modeListControl.rawValue.whereType().toList(); + List? get _modeListRawValue => containsModeList + ? modeListControl.rawValue.whereType().toList() + : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsSomeList { try { form.control(someListControlPath()); @@ -356,8 +371,6 @@ class ArrayNullableForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsSomeListRequired { try { form.control(someListRequiredControlPath()); @@ -367,8 +380,6 @@ class ArrayNullableForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsEmailList { try { form.control(emailListControlPath()); @@ -378,8 +389,6 @@ class ArrayNullableForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsFruitList { try { form.control(fruitListControlPath()); @@ -389,8 +398,6 @@ class ArrayNullableForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsVegetablesList { try { form.control(vegetablesListControlPath()); @@ -400,8 +407,6 @@ class ArrayNullableForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsModeList { try { form.control(modeListControlPath()); @@ -436,12 +441,7 @@ class ArrayNullableForm implements FormModel { void get modeListFocus => form.focus(modeListControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void someListRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void someListRemove({bool updateParent = true, bool emitEvent = true}) { if (containsSomeList) { final controlPath = path; if (controlPath == null) { @@ -464,12 +464,7 @@ class ArrayNullableForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void vegetablesListRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void vegetablesListRemove({bool updateParent = true, bool emitEvent = true}) { if (containsVegetablesList) { final controlPath = path; if (controlPath == null) { @@ -492,12 +487,7 @@ class ArrayNullableForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void modeListRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void modeListRemove({bool updateParent = true, bool emitEvent = true}) { if (containsModeList) { final controlPath = path; if (controlPath == null) { @@ -525,8 +515,11 @@ class ArrayNullableForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - someListControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + someListControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void someListRequiredValueUpdate( @@ -534,8 +527,11 @@ class ArrayNullableForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - someListRequiredControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + someListRequiredControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailListValueUpdate( @@ -543,8 +539,11 @@ class ArrayNullableForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - emailListControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailListControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void fruitListValueUpdate( @@ -552,8 +551,11 @@ class ArrayNullableForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - fruitListControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + fruitListControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void vegetablesListValueUpdate( @@ -561,8 +563,11 @@ class ArrayNullableForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - vegetablesListControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + vegetablesListControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void modeListValueUpdate( @@ -570,8 +575,11 @@ class ArrayNullableForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - modeListControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + modeListControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void someListValuePatch( @@ -579,8 +587,11 @@ class ArrayNullableForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - someListControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + someListControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void someListRequiredValuePatch( @@ -588,8 +599,11 @@ class ArrayNullableForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - someListRequiredControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + someListRequiredControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailListValuePatch( @@ -597,8 +611,11 @@ class ArrayNullableForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - emailListControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailListControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void fruitListValuePatch( @@ -606,8 +623,11 @@ class ArrayNullableForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - fruitListControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + fruitListControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void vegetablesListValuePatch( @@ -615,8 +635,11 @@ class ArrayNullableForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - vegetablesListControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + vegetablesListControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void modeListValuePatch( @@ -624,8 +647,11 @@ class ArrayNullableForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - modeListControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + modeListControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void someListValueReset( @@ -634,14 +660,13 @@ class ArrayNullableForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - someListControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => someListControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void someListRequiredValueReset( List value, { @@ -649,14 +674,13 @@ class ArrayNullableForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - someListRequiredControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => someListRequiredControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void emailListValueReset( List value, { @@ -664,14 +688,13 @@ class ArrayNullableForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - emailListControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => emailListControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void fruitListValueReset( List value, { @@ -679,14 +702,13 @@ class ArrayNullableForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - fruitListControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => fruitListControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void vegetablesListValueReset( List? value, { @@ -694,14 +716,13 @@ class ArrayNullableForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - vegetablesListControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => vegetablesListControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void modeListValueReset( List? value, { @@ -709,14 +730,13 @@ class ArrayNullableForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - modeListControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => modeListControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl> get someListControl => form.control(someListControlPath()) as FormControl>; @@ -887,13 +907,15 @@ class ArrayNullableForm implements FormModel { break; } - emailListControl.add(FormControl( - value: value, - validators: resultingValidators, - asyncValidators: resultingAsyncValidators, - asyncValidatorsDebounceTime: asyncValidatorsDebounceTime ?? 250, - disabled: disabled ?? false, - )); + emailListControl.add( + FormControl( + value: value, + validators: resultingValidators, + asyncValidators: resultingAsyncValidators, + asyncValidatorsDebounceTime: asyncValidatorsDebounceTime ?? 250, + disabled: disabled ?? false, + ), + ); } void addFruitListItem( @@ -927,13 +949,15 @@ class ArrayNullableForm implements FormModel { break; } - fruitListControl.add(FormControl( - value: value, - validators: resultingValidators, - asyncValidators: resultingAsyncValidators, - asyncValidatorsDebounceTime: asyncValidatorsDebounceTime ?? 250, - disabled: disabled ?? false, - )); + fruitListControl.add( + FormControl( + value: value, + validators: resultingValidators, + asyncValidators: resultingAsyncValidators, + asyncValidatorsDebounceTime: asyncValidatorsDebounceTime ?? 250, + disabled: disabled ?? false, + ), + ); } void addVegetablesListItem( @@ -967,13 +991,15 @@ class ArrayNullableForm implements FormModel { break; } - vegetablesListControl.add(FormControl( - value: value, - validators: resultingValidators, - asyncValidators: resultingAsyncValidators, - asyncValidatorsDebounceTime: asyncValidatorsDebounceTime ?? 250, - disabled: disabled ?? false, - )); + vegetablesListControl.add( + FormControl( + value: value, + validators: resultingValidators, + asyncValidators: resultingAsyncValidators, + asyncValidatorsDebounceTime: asyncValidatorsDebounceTime ?? 250, + disabled: disabled ?? false, + ), + ); } void addModeListItem( @@ -1007,13 +1033,15 @@ class ArrayNullableForm implements FormModel { break; } - modeListControl.add(FormControl( - value: value, - validators: resultingValidators, - asyncValidators: resultingAsyncValidators, - asyncValidatorsDebounceTime: asyncValidatorsDebounceTime ?? 250, - disabled: disabled ?? false, - )); + modeListControl.add( + FormControl( + value: value, + validators: resultingValidators, + asyncValidators: resultingAsyncValidators, + asyncValidatorsDebounceTime: asyncValidatorsDebounceTime ?? 250, + disabled: disabled ?? false, + ), + ); } @override @@ -1028,47 +1056,42 @@ class ArrayNullableForm implements FormModel { ); } return ArrayNullable( - emailList: _emailListValue, - fruitList: _fruitListValue, - vegetablesList: _vegetablesListValue, - modeList: _modeListValue, - someList: _someListValue, - someListRequired: _someListRequiredValue); + emailList: _emailListValue, + fruitList: _fruitListValue, + vegetablesList: _vegetablesListValue, + modeList: _modeListValue, + someList: _someListValue, + someListRequired: _someListRequiredValue, + ); } @override ArrayNullable get rawModel { return ArrayNullable( - emailList: _emailListRawValue, - fruitList: _fruitListRawValue, - vegetablesList: _vegetablesListRawValue, - modeList: _modeListRawValue, - someList: _someListRawValue, - someListRequired: _someListRequiredRawValue); + emailList: _emailListRawValue, + fruitList: _fruitListRawValue, + vegetablesList: _vegetablesListRawValue, + modeList: _modeListRawValue, + someList: _someListRawValue, + someListRequired: _someListRequiredRawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { - final currentFormInstance = currentForm; - - if (currentFormInstance is! FormGroup) { - return; - } - + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { - currentFormInstance.controls[key]?.markAsEnabled( + currentForm.controls[key]?.markAsEnabled( updateParent: updateParent, emitEvent: emitEvent, ); @@ -1084,9 +1107,7 @@ class ArrayNullableForm implements FormModel { final currentForm = this.currentForm; return const DeepCollectionEquality().equals( - currentForm is FormControlCollection - ? currentForm.rawValue - : currentForm.value, + currentForm.rawValue, ArrayNullableForm.formElements(other).rawValue, ); } @@ -1107,8 +1128,16 @@ class ArrayNullableForm implements FormModel { } @override - AbstractControl get currentForm { - return path == null ? form : form.control(path!); + bool get hasChanged { + return !const DeepCollectionEquality().equals( + currentForm.rawValue, + initial, + ); + } + + @override + FormGroup get currentForm { + return path == null ? form : form.control(path!) as FormGroup; } @override @@ -1116,104 +1145,184 @@ class ArrayNullableForm implements FormModel { ArrayNullable? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(ArrayNullableForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + ArrayNullableForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void upsertValue( + ArrayNullable? value, { + bool updateParent = true, + bool emitEvent = true, + }) { + final formElements = ArrayNullableForm.formElements(value); + + currentForm.addAll(formElements.controls); + } @override void reset({ ArrayNullable? value, bool updateParent = true, bool emitEvent = true, - }) => - form.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void updateInitial(Map? value, String? path) { + if (_formModel != null) { + _formModel?.updateInitial(currentForm.rawValue, path); + return; + } + + if (value == null) return; + + if (path == null || path.isEmpty) { + initial.addAll(value); + return; + } + + final keys = path.split('.'); + Object? current = initial; + for (var i = 0; i < keys.length - 1; i++) { + final key = keys[i]; + + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current = current[index]; + continue; + } + } + + if (current is Map) { + if (!current.containsKey(key)) { + current[key] = {}; + } + current = current[key]; + continue; + } + + return; + } + + final key = keys.last; + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current[index] = value; + } + } else if (current is Map) { + current[key] = value; + } + } String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(ArrayNullable? arrayNullable) => FormGroup({ - emailListControlName: FormArray( - (arrayNullable?.emailList ?? []) - .map((e) => FormControl( - value: e, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - )) - .toList(), - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false), - fruitListControlName: FormArray( - (arrayNullable?.fruitList ?? []) - .map((e) => FormControl( - value: e, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - )) - .toList(), - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false), - vegetablesListControlName: FormArray( - (arrayNullable?.vegetablesList ?? []) - .map((e) => FormControl( - value: e, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - )) - .toList(), - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false), - modeListControlName: FormArray( - (arrayNullable?.modeList ?? []) - .map((e) => FormControl( - value: e, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - )) - .toList(), - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false), - someListControlName: FormControl>( - value: arrayNullable?.someList, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - someListRequiredControlName: FormControl>( - value: arrayNullable?.someListRequired, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(ArrayNullable? arrayNullable) => FormGroup( + { + emailListControlName: FormArray( + (arrayNullable?.emailList ?? []) + .map( + (e) => FormControl( + value: e, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ), + ) + .toList(), + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ), + fruitListControlName: FormArray( + (arrayNullable?.fruitList ?? []) + .map( + (e) => FormControl( + value: e, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ), + ) + .toList(), + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ), + vegetablesListControlName: FormArray( + (arrayNullable?.vegetablesList ?? []) + .map( + (e) => FormControl( + value: e, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ), + ) + .toList(), + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ), + modeListControlName: FormArray( + (arrayNullable?.modeList ?? []) + .map( + (e) => FormControl( + value: e, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ), + ) + .toList(), + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ), + someListControlName: FormControl>( + value: arrayNullable?.someList, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + someListRequiredControlName: FormControl>( + value: arrayNullable?.someListRequired, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } class ReactiveArrayNullableFormArrayBuilder< - ReactiveArrayNullableFormArrayBuilderT> extends StatelessWidget { + ReactiveArrayNullableFormArrayBuilderT +> + extends StatelessWidget { const ReactiveArrayNullableFormArrayBuilder({ Key? key, this.control, @@ -1222,30 +1331,41 @@ class ReactiveArrayNullableFormArrayBuilder< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - ArrayNullableForm formModel)? control; + ArrayNullableForm formModel, + )? + control; - final Widget Function(BuildContext context, List itemList, - ArrayNullableForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + ArrayNullableForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveArrayNullableFormArrayBuilderT? item, - ArrayNullableForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveArrayNullableFormArrayBuilderT? item, + ArrayNullableForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -1261,18 +1381,9 @@ class ReactiveArrayNullableFormArrayBuilder< return ReactiveFormArrayItemBuilder( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1281,7 +1392,9 @@ class ReactiveArrayNullableFormArrayBuilder< } class ReactiveArrayNullableFormArrayBuilder2< - ReactiveArrayNullableFormArrayBuilderT> extends StatelessWidget { + ReactiveArrayNullableFormArrayBuilderT +> + extends StatelessWidget { const ReactiveArrayNullableFormArrayBuilder2({ Key? key, this.control, @@ -1290,36 +1403,43 @@ class ReactiveArrayNullableFormArrayBuilder2< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - ArrayNullableForm formModel)? control; + ArrayNullableForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - ArrayNullableForm formModel - }) params)? builder; + ({BuildContext context, List itemList, ArrayNullableForm formModel}) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveArrayNullableFormArrayBuilderT? item, - ArrayNullableForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveArrayNullableFormArrayBuilderT? item, + ArrayNullableForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -1336,23 +1456,17 @@ class ReactiveArrayNullableFormArrayBuilder2< formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1361,32 +1475,48 @@ class ReactiveArrayNullableFormArrayBuilder2< } class ReactiveArrayNullableFormFormGroupArrayBuilder< - ReactiveArrayNullableFormFormGroupArrayBuilderT> extends StatelessWidget { + ReactiveArrayNullableFormFormGroupArrayBuilderT +> + extends StatelessWidget { const ReactiveArrayNullableFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? extended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(ArrayNullableForm formModel)? + getExtended; - final ExtendedControl?>, - List> - Function(ArrayNullableForm formModel)? getExtended; - - final Widget Function(BuildContext context, List itemList, - ArrayNullableForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + ArrayNullableForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveArrayNullableFormFormGroupArrayBuilderT? item, - ArrayNullableForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveArrayNullableFormFormGroupArrayBuilderT? item, + ArrayNullableForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -1401,26 +1531,18 @@ class ReactiveArrayNullableFormFormGroupArrayBuilder< return StreamBuilder?>?>( stream: value.control.valueChanges, builder: (context, snapshot) { - final itemList = (value.value() ?? - []) - .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) - .values - .toList(); - - return builder?.call( - context, - itemList, - formModel, - ) ?? + final itemList = + (value.value() ?? + []) + .asMap() + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) + .values + .toList(); + + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/generator_tests/test/doc/create_output_test.dart b/packages/generator_tests/test/doc/create_output_test.dart index 65373c8e..fc2ff63e 100644 --- a/packages/generator_tests/test/doc/create_output_test.dart +++ b/packages/generator_tests/test/doc/create_output_test.dart @@ -121,7 +121,11 @@ class ReactiveMSICreateFormConsumer extends StatelessWidget { final Widget? child; final Widget Function( - BuildContext context, MSICreateForm formModel, Widget? child) builder; + BuildContext context, + MSICreateForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -140,11 +144,7 @@ class MSICreateFormInheritedStreamer extends InheritedStreamer { required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final MSICreateForm form; } @@ -166,18 +166,17 @@ class ReactiveMSICreateForm extends StatelessWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - static MSICreateForm? of( - BuildContext context, { - bool listen = true, - }) { + static MSICreateForm? of(BuildContext context, {bool listen = true}) { if (listen) { return context .dependOnInheritedWidgetOfExactType() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - MSICreateFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + MSICreateFormInheritedStreamer + >(); return element == null ? null : (element.widget as MSICreateFormInheritedStreamer).form; @@ -224,7 +223,11 @@ class MSICreateFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, MSICreateForm formModel, Widget? child) builder; + BuildContext context, + MSICreateForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, MSICreateForm formModel)? initState; @@ -239,7 +242,11 @@ class _MSICreateFormBuilderState extends State { @override void initState() { - _formModel = MSICreateForm(MSICreateForm.formElements(widget.model), null); + _formModel = MSICreateForm( + MSICreateForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -316,10 +323,8 @@ class _MSICreateFormBuilderState extends State { final _logMSICreateForm = Logger.detached('MSICreateForm'); class MSICreateForm implements FormModel { - MSICreateForm( - this.form, - this.path, - ); + MSICreateForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String idControlName = "id"; @@ -346,8 +351,14 @@ class MSICreateForm implements FormModel { final String? path; + // ignore: unused_field + final FormModel? _formModel; + final Map _disabled = {}; + @override + final Map initial; + String idControlPath() => pathBuilder(idControlName); String businessNumberControlPath() => pathBuilder(businessNumberControlName); @@ -369,11 +380,13 @@ class MSICreateForm implements FormModel { String adminsControlPath() => pathBuilder(adminsControlName); - String? get _idValue => idControl.value; + String? get _idValue => containsId ? idControl.value : null; - String? get _businessNumberValue => businessNumberControl.value; + String? get _businessNumberValue => + containsBusinessNumber ? businessNumberControl.value : null; - List? get _fileIdsValue => fileIdsControl.value; + List? get _fileIdsValue => + containsFileIds ? fileIdsControl.value : null; String get _nameValue => nameControl.value as String; @@ -391,15 +404,17 @@ class MSICreateForm implements FormModel { List get _adminsValue => adminsAdminContactInformationForm.map((e) => e.model).toList(); - String? get _idRawValue => idControl.value; + String? get _idRawValue => containsId ? idControl.value : null; - String? get _businessNumberRawValue => businessNumberControl.value; + String? get _businessNumberRawValue => + containsBusinessNumber ? businessNumberControl.value : null; - List? get _fileIdsRawValue => fileIdsControl.value; + List? get _fileIdsRawValue => + containsFileIds ? fileIdsControl.value : null; - String? get _nameRawValue => nameControl.value; + String? get _nameRawValue => containsName ? nameControl.value : null; - String? get _emailRawValue => emailControl.value; + String? get _emailRawValue => containsEmail ? emailControl.value : null; bool get _sameMailingAddressAsCompanyRawValue => sameMailingAddressAsCompanyControl.value ?? false; @@ -413,8 +428,6 @@ class MSICreateForm implements FormModel { List get _adminsRawValue => adminsAdminContactInformationForm.map((e) => e.rawModel).toList(); - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsId { try { form.control(idControlPath()); @@ -424,8 +437,6 @@ class MSICreateForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsBusinessNumber { try { form.control(businessNumberControlPath()); @@ -435,8 +446,6 @@ class MSICreateForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsFileIds { try { form.control(fileIdsControlPath()); @@ -446,8 +455,6 @@ class MSICreateForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsName { try { form.control(nameControlPath()); @@ -457,8 +464,6 @@ class MSICreateForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsEmail { try { form.control(emailControlPath()); @@ -468,8 +473,6 @@ class MSICreateForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsSameMailingAddressAsCompany { try { form.control(sameMailingAddressAsCompanyControlPath()); @@ -479,8 +482,6 @@ class MSICreateForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsCompanyAddress { try { form.control(companyAddressControlPath()); @@ -490,8 +491,6 @@ class MSICreateForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsPrimaryContact { try { form.control(primaryContactControlPath()); @@ -501,8 +500,6 @@ class MSICreateForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsMailingAddress { try { form.control(mailingAddressControlPath()); @@ -512,8 +509,6 @@ class MSICreateForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsAdmins { try { form.control(adminsControlPath()); @@ -565,12 +560,7 @@ class MSICreateForm implements FormModel { void get adminsFocus => form.focus(adminsControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void idRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void idRemove({bool updateParent = true, bool emitEvent = true}) { if (containsId) { final controlPath = path; if (controlPath == null) { @@ -593,12 +583,7 @@ class MSICreateForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void businessNumberRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void businessNumberRemove({bool updateParent = true, bool emitEvent = true}) { if (containsBusinessNumber) { final controlPath = path; if (controlPath == null) { @@ -621,12 +606,7 @@ class MSICreateForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void fileIdsRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void fileIdsRemove({bool updateParent = true, bool emitEvent = true}) { if (containsFileIds) { final controlPath = path; if (controlPath == null) { @@ -649,12 +629,7 @@ class MSICreateForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void nameRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void nameRemove({bool updateParent = true, bool emitEvent = true}) { if (containsName) { final controlPath = path; if (controlPath == null) { @@ -677,12 +652,7 @@ class MSICreateForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void emailRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void emailRemove({bool updateParent = true, bool emitEvent = true}) { if (containsEmail) { final controlPath = path; if (controlPath == null) { @@ -710,8 +680,11 @@ class MSICreateForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - idControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + idControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void businessNumberValueUpdate( @@ -719,8 +692,11 @@ class MSICreateForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - businessNumberControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + businessNumberControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void fileIdsValueUpdate( @@ -728,8 +704,11 @@ class MSICreateForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - fileIdsControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + fileIdsControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void nameValueUpdate( @@ -737,8 +716,11 @@ class MSICreateForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - nameControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + nameControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailValueUpdate( @@ -746,8 +728,11 @@ class MSICreateForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - emailControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void sameMailingAddressAsCompanyValueUpdate( @@ -755,8 +740,11 @@ class MSICreateForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - sameMailingAddressAsCompanyControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + sameMailingAddressAsCompanyControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void companyAddressValueUpdate( @@ -764,8 +752,11 @@ class MSICreateForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - companyAddressControl.updateValue(AddressForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + companyAddressControl.updateValue( + AddressForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void primaryContactValueUpdate( @@ -774,9 +765,10 @@ class MSICreateForm implements FormModel { bool emitEvent = true, }) { primaryContactControl.updateValue( - PrimaryContactForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + PrimaryContactForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void mailingAddressValueUpdate( @@ -784,8 +776,11 @@ class MSICreateForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - mailingAddressControl.updateValue(AddressForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + mailingAddressControl.updateValue( + AddressForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void adminsValueUpdate( @@ -816,17 +811,21 @@ class MSICreateForm implements FormModel { if (toUpdate.isNotEmpty) { adminsControl.updateValue( - toUpdate - .map((e) => AdminContactInformationForm.formElements(e).rawValue) - .toList(), - updateParent: updateParent, - emitEvent: emitEvent); + toUpdate + .map((e) => AdminContactInformationForm.formElements(e).rawValue) + .toList(), + updateParent: updateParent, + emitEvent: emitEvent, + ); } if (toAdd.isNotEmpty) { toAdd.forEach((e) { - adminsControl.add(AdminContactInformationForm.formElements(e), - updateParent: updateParent, emitEvent: emitEvent); + adminsControl.add( + AdminContactInformationForm.formElements(e), + updateParent: updateParent, + emitEvent: emitEvent, + ); }); } } @@ -851,10 +850,7 @@ class MSICreateForm implements FormModel { ); } - void adminsClear({ - bool updateParent = true, - bool emitEvent = true, - }) { + void adminsClear({bool updateParent = true, bool emitEvent = true}) { adminsAdminContactInformationForm.clear(); adminsControl.clear(updateParent: updateParent, emitEvent: emitEvent); } @@ -864,8 +860,11 @@ class MSICreateForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - idControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + idControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void businessNumberValuePatch( @@ -873,8 +872,11 @@ class MSICreateForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - businessNumberControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + businessNumberControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void fileIdsValuePatch( @@ -882,8 +884,11 @@ class MSICreateForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - fileIdsControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + fileIdsControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void nameValuePatch( @@ -891,8 +896,11 @@ class MSICreateForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - nameControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + nameControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailValuePatch( @@ -900,8 +908,11 @@ class MSICreateForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - emailControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void sameMailingAddressAsCompanyValuePatch( @@ -909,8 +920,11 @@ class MSICreateForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - sameMailingAddressAsCompanyControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + sameMailingAddressAsCompanyControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void companyAddressValuePatch( @@ -918,8 +932,11 @@ class MSICreateForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - companyAddressControl.updateValue(AddressForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + companyAddressControl.updateValue( + AddressForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void primaryContactValuePatch( @@ -928,9 +945,10 @@ class MSICreateForm implements FormModel { bool emitEvent = true, }) { primaryContactControl.updateValue( - PrimaryContactForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + PrimaryContactForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void mailingAddressValuePatch( @@ -938,8 +956,11 @@ class MSICreateForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - mailingAddressControl.updateValue(AddressForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + mailingAddressControl.updateValue( + AddressForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void adminsValuePatch( @@ -950,20 +971,19 @@ class MSICreateForm implements FormModel { final keys = adminsAdminContactInformationForm.asMap().keys; final toPatch = []; - (value).asMap().forEach( - (k, v) { - if (keys.contains(k)) { - toPatch.add(v); - } - }, - ); + (value).asMap().forEach((k, v) { + if (keys.contains(k)) { + toPatch.add(v); + } + }); adminsControl.patchValue( - toPatch - .map((e) => AdminContactInformationForm.formElements(e).rawValue) - .toList(), - updateParent: updateParent, - emitEvent: emitEvent); + toPatch + .map((e) => AdminContactInformationForm.formElements(e).rawValue) + .toList(), + updateParent: updateParent, + emitEvent: emitEvent, + ); } void idValueReset( @@ -972,14 +992,13 @@ class MSICreateForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - idControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => idControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void businessNumberValueReset( String? value, { @@ -987,14 +1006,13 @@ class MSICreateForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - businessNumberControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => businessNumberControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void fileIdsValueReset( List? value, { @@ -1002,14 +1020,13 @@ class MSICreateForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - fileIdsControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => fileIdsControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void nameValueReset( String? value, { @@ -1017,14 +1034,13 @@ class MSICreateForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - nameControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => nameControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void emailValueReset( String? value, { @@ -1032,14 +1048,13 @@ class MSICreateForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - emailControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => emailControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void sameMailingAddressAsCompanyValueReset( bool value, { @@ -1047,14 +1062,13 @@ class MSICreateForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - sameMailingAddressAsCompanyControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => sameMailingAddressAsCompanyControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void companyAddressValueReset( Address value, { @@ -1062,11 +1076,11 @@ class MSICreateForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - companyAddressControl.reset( - value: AddressForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => companyAddressControl.reset( + value: AddressForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); void primaryContactValueReset( PrimaryContact value, { @@ -1074,11 +1088,11 @@ class MSICreateForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - primaryContactControl.reset( - value: PrimaryContactForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => primaryContactControl.reset( + value: PrimaryContactForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); void mailingAddressValueReset( Address value, { @@ -1086,11 +1100,11 @@ class MSICreateForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - mailingAddressControl.reset( - value: AddressForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => mailingAddressControl.reset( + value: AddressForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); void adminsValueReset( List value, { @@ -1098,13 +1112,13 @@ class MSICreateForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - adminsControl.reset( - value: value - .map((e) => AdminContactInformationForm.formElements(e).rawValue) - .toList(), - updateParent: updateParent, - emitEvent: emitEvent); + }) => adminsControl.reset( + value: value + .map((e) => AdminContactInformationForm.formElements(e).rawValue) + .toList(), + updateParent: updateParent, + emitEvent: emitEvent, + ); FormControl get idControl => form.control(idControlPath()) as FormControl; @@ -1138,21 +1152,32 @@ class MSICreateForm implements FormModel { form.control(adminsControlPath()) as FormArray>; AddressForm get companyAddressForm => - AddressForm(form, pathBuilder('companyAddress')); + AddressForm(form, pathBuilder('companyAddress'), _formModel ?? this); - PrimaryContactForm get primaryContactForm => - PrimaryContactForm(form, pathBuilder('primaryContact')); + PrimaryContactForm get primaryContactForm => PrimaryContactForm( + form, + pathBuilder('primaryContact'), + _formModel ?? this, + ); AddressForm get mailingAddressForm => - AddressForm(form, pathBuilder('mailingAddress')); + AddressForm(form, pathBuilder('mailingAddress'), _formModel ?? this); List get adminsAdminContactInformationForm { final values = adminsControl.controls.map((e) => e.value).toList(); return values .asMap() - .map((k, v) => MapEntry( - k, AdminContactInformationForm(form, pathBuilder("admins.$k")))) + .map( + (k, v) => MapEntry( + k, + AdminContactInformationForm( + form, + pathBuilder("admins.$k"), + _formModel ?? this, + ), + ), + ) .values .toList(); } @@ -1168,10 +1193,7 @@ class MSICreateForm implements FormModel { emitEvent: emitEvent, ); } else { - idControl.markAsEnabled( - updateParent: updateParent, - emitEvent: emitEvent, - ); + idControl.markAsEnabled(updateParent: updateParent, emitEvent: emitEvent); } } @@ -1337,12 +1359,18 @@ class MSICreateForm implements FormModel { } } - ExtendedControl?>, - List> - get adminsExtendedControl => ExtendedControl?>, - List>( - form.control(adminsControlPath()) as FormArray>, - () => adminsAdminContactInformationForm); + ExtendedControl< + List?>, + List + > + get adminsExtendedControl => + ExtendedControl< + List?>, + List + >( + form.control(adminsControlPath()) as FormArray>, + () => adminsAdminContactInformationForm, + ); void addAdminsItem(AdminContactInformation value) { adminsControl.add(AdminContactInformationForm.formElements(value)); @@ -1371,46 +1399,39 @@ class MSICreateForm implements FormModel { ); } return MSICreateOutput( - id: _idValue, - businessNumber: _businessNumberValue, - fileIds: _fileIdsValue, - name: _nameValue, - companyAddress: _companyAddressValue, - email: _emailValue, - primaryContact: _primaryContactValue, - sameMailingAddressAsCompany: _sameMailingAddressAsCompanyValue, - mailingAddress: _mailingAddressValue, - admins: _adminsValue); + id: _idValue, + businessNumber: _businessNumberValue, + fileIds: _fileIdsValue, + name: _nameValue, + companyAddress: _companyAddressValue, + email: _emailValue, + primaryContact: _primaryContactValue, + sameMailingAddressAsCompany: _sameMailingAddressAsCompanyValue, + mailingAddress: _mailingAddressValue, + admins: _adminsValue, + ); } @override MSICreate get rawModel { return MSICreate( - id: _idRawValue, - businessNumber: _businessNumberRawValue, - fileIds: _fileIdsRawValue, - name: _nameRawValue, - companyAddress: _companyAddressRawValue, - email: _emailRawValue, - primaryContact: _primaryContactRawValue, - sameMailingAddressAsCompany: _sameMailingAddressAsCompanyRawValue, - mailingAddress: _mailingAddressRawValue, - admins: _adminsRawValue); + id: _idRawValue, + businessNumber: _businessNumberRawValue, + fileIds: _fileIdsRawValue, + name: _nameRawValue, + companyAddress: _companyAddressRawValue, + email: _emailRawValue, + primaryContact: _primaryContactRawValue, + sameMailingAddressAsCompany: _sameMailingAddressAsCompanyRawValue, + mailingAddress: _mailingAddressRawValue, + admins: _adminsRawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { - final currentFormInstance = currentForm; - - if (currentFormInstance is! FormGroup) { - return; - } - + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); @@ -1419,15 +1440,17 @@ class MSICreateForm implements FormModel { primaryContactForm.toggleDisabled(); mailingAddressForm.toggleDisabled(); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { adminsAdminContactInformationForm.forEach((e) => e.toggleDisabled()); companyAddressForm.toggleDisabled(); primaryContactForm.toggleDisabled(); mailingAddressForm.toggleDisabled(); - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { - currentFormInstance.controls[key]?.markAsEnabled( + currentForm.controls[key]?.markAsEnabled( updateParent: updateParent, emitEvent: emitEvent, ); @@ -1443,9 +1466,7 @@ class MSICreateForm implements FormModel { final currentForm = this.currentForm; return const DeepCollectionEquality().equals( - currentForm is FormControlCollection - ? currentForm.rawValue - : currentForm.value, + currentForm.rawValue, MSICreateForm.formElements(other).rawValue, ); } @@ -1466,8 +1487,16 @@ class MSICreateForm implements FormModel { } @override - AbstractControl get currentForm { - return path == null ? form : form.control(path!); + bool get hasChanged { + return !const DeepCollectionEquality().equals( + currentForm.rawValue, + initial, + ); + } + + @override + FormGroup get currentForm { + return path == null ? form : form.control(path!) as FormGroup; } @override @@ -1475,95 +1504,166 @@ class MSICreateForm implements FormModel { MSICreate? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(MSICreateForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + MSICreateForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void upsertValue( + MSICreate? value, { + bool updateParent = true, + bool emitEvent = true, + }) { + final formElements = MSICreateForm.formElements(value); + + currentForm.addAll(formElements.controls); + } @override void reset({ MSICreate? value, bool updateParent = true, bool emitEvent = true, - }) => - form.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void updateInitial(Map? value, String? path) { + if (_formModel != null) { + _formModel?.updateInitial(currentForm.rawValue, path); + return; + } + + if (value == null) return; + + if (path == null || path.isEmpty) { + initial.addAll(value); + return; + } + + final keys = path.split('.'); + Object? current = initial; + for (var i = 0; i < keys.length - 1; i++) { + final key = keys[i]; + + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current = current[index]; + continue; + } + } + + if (current is Map) { + if (!current.containsKey(key)) { + current[key] = {}; + } + current = current[key]; + continue; + } + + return; + } + + final key = keys.last; + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current[index] = value; + } + } else if (current is Map) { + current[key] = value; + } + } String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(MSICreate? mSICreate) => FormGroup({ - idControlName: FormControl( - value: mSICreate?.id, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - businessNumberControlName: FormControl( - value: mSICreate?.businessNumber, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - fileIdsControlName: FormControl>( - value: mSICreate?.fileIds, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - nameControlName: FormControl( - value: mSICreate?.name, - validators: [RequiredValidator(), MaxLengthValidator(120)], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - emailControlName: FormControl( - value: mSICreate?.email, - validators: [RequiredValidator(), EmailValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - sameMailingAddressAsCompanyControlName: FormControl( - value: mSICreate?.sameMailingAddressAsCompany, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - adminsControlName: FormArray( - (mSICreate?.admins ?? []) - .map((e) => AdminContactInformationForm.formElements(e)) - .toList(), - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false), - companyAddressControlName: - AddressForm.formElements(mSICreate?.companyAddress), - primaryContactControlName: - PrimaryContactForm.formElements(mSICreate?.primaryContact), - mailingAddressControlName: - AddressForm.formElements(mSICreate?.mailingAddress) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(MSICreate? mSICreate) => FormGroup( + { + idControlName: FormControl( + value: mSICreate?.id, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + businessNumberControlName: FormControl( + value: mSICreate?.businessNumber, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + fileIdsControlName: FormControl>( + value: mSICreate?.fileIds, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + nameControlName: FormControl( + value: mSICreate?.name, + validators: [RequiredValidator(), MaxLengthValidator(120)], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + emailControlName: FormControl( + value: mSICreate?.email, + validators: [RequiredValidator(), EmailValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + sameMailingAddressAsCompanyControlName: FormControl( + value: mSICreate?.sameMailingAddressAsCompany, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + adminsControlName: FormArray( + (mSICreate?.admins ?? []) + .map((e) => AdminContactInformationForm.formElements(e)) + .toList(), + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ), + companyAddressControlName: AddressForm.formElements( + mSICreate?.companyAddress, + ), + primaryContactControlName: PrimaryContactForm.formElements( + mSICreate?.primaryContact, + ), + mailingAddressControlName: AddressForm.formElements( + mSICreate?.mailingAddress, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } final _logAddressForm = Logger.detached('AddressForm'); class AddressForm implements FormModel { - AddressForm( - this.form, - this.path, - ); + AddressForm(this.form, this.path, this._formModel) : initial = form.rawValue; static const String streetControlName = "street"; @@ -1577,8 +1677,14 @@ class AddressForm implements FormModel { final String? path; + // ignore: unused_field + final FormModel? _formModel; + final Map _disabled = {}; + @override + final Map initial; + String streetControlPath() => pathBuilder(streetControlName); String cityControlPath() => pathBuilder(cityControlName); @@ -1596,16 +1702,15 @@ class AddressForm implements FormModel { String get _zipCodeValue => zipCodeControl.value as String; - String? get _streetRawValue => streetControl.value; + String? get _streetRawValue => containsStreet ? streetControl.value : null; - String? get _cityRawValue => cityControl.value; + String? get _cityRawValue => containsCity ? cityControl.value : null; - String? get _stateOrProvinceRawValue => stateOrProvinceControl.value; + String? get _stateOrProvinceRawValue => + containsStateOrProvince ? stateOrProvinceControl.value : null; - String? get _zipCodeRawValue => zipCodeControl.value; + String? get _zipCodeRawValue => containsZipCode ? zipCodeControl.value : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsStreet { try { form.control(streetControlPath()); @@ -1615,8 +1720,6 @@ class AddressForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsCity { try { form.control(cityControlPath()); @@ -1626,8 +1729,6 @@ class AddressForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsStateOrProvince { try { form.control(stateOrProvinceControlPath()); @@ -1637,8 +1738,6 @@ class AddressForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsZipCode { try { form.control(zipCodeControlPath()); @@ -1665,12 +1764,7 @@ class AddressForm implements FormModel { void get zipCodeFocus => form.focus(zipCodeControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void streetRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void streetRemove({bool updateParent = true, bool emitEvent = true}) { if (containsStreet) { final controlPath = path; if (controlPath == null) { @@ -1693,12 +1787,7 @@ class AddressForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void cityRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void cityRemove({bool updateParent = true, bool emitEvent = true}) { if (containsCity) { final controlPath = path; if (controlPath == null) { @@ -1721,8 +1810,6 @@ class AddressForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') void stateOrProvinceRemove({ bool updateParent = true, bool emitEvent = true, @@ -1749,12 +1836,7 @@ class AddressForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void zipCodeRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void zipCodeRemove({bool updateParent = true, bool emitEvent = true}) { if (containsZipCode) { final controlPath = path; if (controlPath == null) { @@ -1782,8 +1864,11 @@ class AddressForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - streetControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + streetControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void cityValueUpdate( @@ -1791,8 +1876,11 @@ class AddressForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - cityControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + cityControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void stateOrProvinceValueUpdate( @@ -1800,8 +1888,11 @@ class AddressForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - stateOrProvinceControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + stateOrProvinceControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void zipCodeValueUpdate( @@ -1809,8 +1900,11 @@ class AddressForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - zipCodeControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + zipCodeControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void streetValuePatch( @@ -1818,8 +1912,11 @@ class AddressForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - streetControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + streetControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void cityValuePatch( @@ -1827,8 +1924,11 @@ class AddressForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - cityControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + cityControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void stateOrProvinceValuePatch( @@ -1836,8 +1936,11 @@ class AddressForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - stateOrProvinceControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + stateOrProvinceControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void zipCodeValuePatch( @@ -1845,8 +1948,11 @@ class AddressForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - zipCodeControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + zipCodeControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void streetValueReset( @@ -1855,14 +1961,13 @@ class AddressForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - streetControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => streetControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void cityValueReset( String? value, { @@ -1870,14 +1975,13 @@ class AddressForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - cityControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => cityControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void stateOrProvinceValueReset( String? value, { @@ -1885,14 +1989,13 @@ class AddressForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - stateOrProvinceControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => stateOrProvinceControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void zipCodeValueReset( String? value, { @@ -1900,14 +2003,13 @@ class AddressForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - zipCodeControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => zipCodeControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get streetControl => form.control(streetControlPath()) as FormControl; @@ -2006,43 +2108,38 @@ class AddressForm implements FormModel { ); } return AddressOutput( - street: _streetValue, - city: _cityValue, - stateOrProvince: _stateOrProvinceValue, - zipCode: _zipCodeValue); + street: _streetValue, + city: _cityValue, + stateOrProvince: _stateOrProvinceValue, + zipCode: _zipCodeValue, + ); } @override Address get rawModel { return Address( - street: _streetRawValue, - city: _cityRawValue, - stateOrProvince: _stateOrProvinceRawValue, - zipCode: _zipCodeRawValue); + street: _streetRawValue, + city: _cityRawValue, + stateOrProvince: _stateOrProvinceRawValue, + zipCode: _zipCodeRawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { - final currentFormInstance = currentForm; - - if (currentFormInstance is! FormGroup) { - return; - } - + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { - currentFormInstance.controls[key]?.markAsEnabled( + currentForm.controls[key]?.markAsEnabled( updateParent: updateParent, emitEvent: emitEvent, ); @@ -2058,9 +2155,7 @@ class AddressForm implements FormModel { final currentForm = this.currentForm; return const DeepCollectionEquality().equals( - currentForm is FormControlCollection - ? currentForm.rawValue - : currentForm.value, + currentForm.rawValue, AddressForm.formElements(other).rawValue, ); } @@ -2081,8 +2176,16 @@ class AddressForm implements FormModel { } @override - AbstractControl get currentForm { - return path == null ? form : form.control(path!); + bool get hasChanged { + return !const DeepCollectionEquality().equals( + currentForm.rawValue, + initial, + ); + } + + @override + FormGroup get currentForm { + return path == null ? form : form.control(path!) as FormGroup; } @override @@ -2090,68 +2193,134 @@ class AddressForm implements FormModel { Address? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(AddressForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + AddressForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void upsertValue( + Address? value, { + bool updateParent = true, + bool emitEvent = true, + }) { + final formElements = AddressForm.formElements(value); + + currentForm.addAll(formElements.controls); + } @override void reset({ Address? value, bool updateParent = true, bool emitEvent = true, - }) => - form.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void updateInitial(Map? value, String? path) { + if (_formModel != null) { + _formModel?.updateInitial(currentForm.rawValue, path); + return; + } + + if (value == null) return; + + if (path == null || path.isEmpty) { + initial.addAll(value); + return; + } + + final keys = path.split('.'); + Object? current = initial; + for (var i = 0; i < keys.length - 1; i++) { + final key = keys[i]; + + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current = current[index]; + continue; + } + } + + if (current is Map) { + if (!current.containsKey(key)) { + current[key] = {}; + } + current = current[key]; + continue; + } + + return; + } + + final key = keys.last; + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current[index] = value; + } + } else if (current is Map) { + current[key] = value; + } + } String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(Address? address) => FormGroup({ - streetControlName: FormControl( - value: address?.street, - validators: [RequiredValidator(), MaxLengthValidator(120)], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - cityControlName: FormControl( - value: address?.city, - validators: [RequiredValidator(), MaxLengthValidator(120)], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - stateOrProvinceControlName: FormControl( - value: address?.stateOrProvince, - validators: [RequiredValidator(), MaxLengthValidator(120)], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - zipCodeControlName: FormControl( - value: address?.zipCode, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(Address? address) => FormGroup( + { + streetControlName: FormControl( + value: address?.street, + validators: [RequiredValidator(), MaxLengthValidator(120)], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + cityControlName: FormControl( + value: address?.city, + validators: [RequiredValidator(), MaxLengthValidator(120)], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + stateOrProvinceControlName: FormControl( + value: address?.stateOrProvince, + validators: [RequiredValidator(), MaxLengthValidator(120)], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + zipCodeControlName: FormControl( + value: address?.zipCode, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } final _logPrimaryContactForm = Logger.detached('PrimaryContactForm'); class PrimaryContactForm implements FormModel { - PrimaryContactForm( - this.form, - this.path, - ); + PrimaryContactForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String fullNameControlName = "fullName"; @@ -2163,8 +2332,14 @@ class PrimaryContactForm final String? path; + // ignore: unused_field + final FormModel? _formModel; + final Map _disabled = {}; + @override + final Map initial; + String fullNameControlPath() => pathBuilder(fullNameControlName); String jobTitleControlPath() => pathBuilder(jobTitleControlName); @@ -2173,18 +2348,18 @@ class PrimaryContactForm String get _fullNameValue => fullNameControl.value as String; - String? get _jobTitleValue => jobTitleControl.value; + String? get _jobTitleValue => containsJobTitle ? jobTitleControl.value : null; String get _emailValue => emailControl.value as String; - String? get _fullNameRawValue => fullNameControl.value; + String? get _fullNameRawValue => + containsFullName ? fullNameControl.value : null; - String? get _jobTitleRawValue => jobTitleControl.value; + String? get _jobTitleRawValue => + containsJobTitle ? jobTitleControl.value : null; - String? get _emailRawValue => emailControl.value; + String? get _emailRawValue => containsEmail ? emailControl.value : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsFullName { try { form.control(fullNameControlPath()); @@ -2194,8 +2369,6 @@ class PrimaryContactForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsJobTitle { try { form.control(jobTitleControlPath()); @@ -2205,8 +2378,6 @@ class PrimaryContactForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsEmail { try { form.control(emailControlPath()); @@ -2228,12 +2399,7 @@ class PrimaryContactForm void get emailFocus => form.focus(emailControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void fullNameRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void fullNameRemove({bool updateParent = true, bool emitEvent = true}) { if (containsFullName) { final controlPath = path; if (controlPath == null) { @@ -2256,12 +2422,7 @@ class PrimaryContactForm } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void jobTitleRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void jobTitleRemove({bool updateParent = true, bool emitEvent = true}) { if (containsJobTitle) { final controlPath = path; if (controlPath == null) { @@ -2284,12 +2445,7 @@ class PrimaryContactForm } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void emailRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void emailRemove({bool updateParent = true, bool emitEvent = true}) { if (containsEmail) { final controlPath = path; if (controlPath == null) { @@ -2317,8 +2473,11 @@ class PrimaryContactForm bool updateParent = true, bool emitEvent = true, }) { - fullNameControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + fullNameControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void jobTitleValueUpdate( @@ -2326,8 +2485,11 @@ class PrimaryContactForm bool updateParent = true, bool emitEvent = true, }) { - jobTitleControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + jobTitleControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailValueUpdate( @@ -2335,8 +2497,11 @@ class PrimaryContactForm bool updateParent = true, bool emitEvent = true, }) { - emailControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void fullNameValuePatch( @@ -2344,8 +2509,11 @@ class PrimaryContactForm bool updateParent = true, bool emitEvent = true, }) { - fullNameControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + fullNameControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void jobTitleValuePatch( @@ -2353,8 +2521,11 @@ class PrimaryContactForm bool updateParent = true, bool emitEvent = true, }) { - jobTitleControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + jobTitleControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailValuePatch( @@ -2362,8 +2533,11 @@ class PrimaryContactForm bool updateParent = true, bool emitEvent = true, }) { - emailControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void fullNameValueReset( @@ -2372,14 +2546,13 @@ class PrimaryContactForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - fullNameControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => fullNameControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void jobTitleValueReset( String? value, { @@ -2387,14 +2560,13 @@ class PrimaryContactForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - jobTitleControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => jobTitleControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void emailValueReset( String? value, { @@ -2402,14 +2574,13 @@ class PrimaryContactForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - emailControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => emailControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get fullNameControl => form.control(fullNameControlPath()) as FormControl; @@ -2487,39 +2658,36 @@ class PrimaryContactForm ); } return PrimaryContactOutput( - fullName: _fullNameValue, jobTitle: _jobTitleValue, email: _emailValue); + fullName: _fullNameValue, + jobTitle: _jobTitleValue, + email: _emailValue, + ); } @override PrimaryContact get rawModel { return PrimaryContact( - fullName: _fullNameRawValue, - jobTitle: _jobTitleRawValue, - email: _emailRawValue); + fullName: _fullNameRawValue, + jobTitle: _jobTitleRawValue, + email: _emailRawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { - final currentFormInstance = currentForm; - - if (currentFormInstance is! FormGroup) { - return; - } - + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { - currentFormInstance.controls[key]?.markAsEnabled( + currentForm.controls[key]?.markAsEnabled( updateParent: updateParent, emitEvent: emitEvent, ); @@ -2535,9 +2703,7 @@ class PrimaryContactForm final currentForm = this.currentForm; return const DeepCollectionEquality().equals( - currentForm is FormControlCollection - ? currentForm.rawValue - : currentForm.value, + currentForm.rawValue, PrimaryContactForm.formElements(other).rawValue, ); } @@ -2558,8 +2724,16 @@ class PrimaryContactForm } @override - AbstractControl get currentForm { - return path == null ? form : form.control(path!); + bool get hasChanged { + return !const DeepCollectionEquality().equals( + currentForm.rawValue, + initial, + ); + } + + @override + FormGroup get currentForm { + return path == null ? form : form.control(path!) as FormGroup; } @override @@ -2567,63 +2741,129 @@ class PrimaryContactForm PrimaryContact? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(PrimaryContactForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + PrimaryContactForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void upsertValue( + PrimaryContact? value, { + bool updateParent = true, + bool emitEvent = true, + }) { + final formElements = PrimaryContactForm.formElements(value); + + currentForm.addAll(formElements.controls); + } @override void reset({ PrimaryContact? value, bool updateParent = true, bool emitEvent = true, - }) => - form.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void updateInitial(Map? value, String? path) { + if (_formModel != null) { + _formModel?.updateInitial(currentForm.rawValue, path); + return; + } + + if (value == null) return; + + if (path == null || path.isEmpty) { + initial.addAll(value); + return; + } + + final keys = path.split('.'); + Object? current = initial; + for (var i = 0; i < keys.length - 1; i++) { + final key = keys[i]; + + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current = current[index]; + continue; + } + } + + if (current is Map) { + if (!current.containsKey(key)) { + current[key] = {}; + } + current = current[key]; + continue; + } + + return; + } + + final key = keys.last; + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current[index] = value; + } + } else if (current is Map) { + current[key] = value; + } + } String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(PrimaryContact? primaryContact) => FormGroup({ - fullNameControlName: FormControl( - value: primaryContact?.fullName, - validators: [RequiredValidator(), MaxLengthValidator(120)], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - jobTitleControlName: FormControl( - value: primaryContact?.jobTitle, - validators: [MaxLengthValidator(120)], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - emailControlName: FormControl( - value: primaryContact?.email, - validators: [RequiredValidator(), EmailValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(PrimaryContact? primaryContact) => FormGroup( + { + fullNameControlName: FormControl( + value: primaryContact?.fullName, + validators: [RequiredValidator(), MaxLengthValidator(120)], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + jobTitleControlName: FormControl( + value: primaryContact?.jobTitle, + validators: [MaxLengthValidator(120)], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + emailControlName: FormControl( + value: primaryContact?.email, + validators: [RequiredValidator(), EmailValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } -final _logAdminContactInformationForm = - Logger.detached('AdminContactInformationForm'); +final _logAdminContactInformationForm = Logger.detached( + 'AdminContactInformationForm', +); class AdminContactInformationForm implements FormModel { - AdminContactInformationForm( - this.form, - this.path, - ); + AdminContactInformationForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String firstNameControlName = "firstName"; @@ -2635,8 +2875,14 @@ class AdminContactInformationForm final String? path; + // ignore: unused_field + final FormModel? _formModel; + final Map _disabled = {}; + @override + final Map initial; + String firstNameControlPath() => pathBuilder(firstNameControlName); String lastNameControlPath() => pathBuilder(lastNameControlName); @@ -2649,14 +2895,14 @@ class AdminContactInformationForm String get _emailValue => emailControl.value as String; - String? get _firstNameRawValue => firstNameControl.value; + String? get _firstNameRawValue => + containsFirstName ? firstNameControl.value : null; - String? get _lastNameRawValue => lastNameControl.value; + String? get _lastNameRawValue => + containsLastName ? lastNameControl.value : null; - String? get _emailRawValue => emailControl.value; + String? get _emailRawValue => containsEmail ? emailControl.value : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsFirstName { try { form.control(firstNameControlPath()); @@ -2666,8 +2912,6 @@ class AdminContactInformationForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsLastName { try { form.control(lastNameControlPath()); @@ -2677,8 +2921,6 @@ class AdminContactInformationForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsEmail { try { form.control(emailControlPath()); @@ -2700,12 +2942,7 @@ class AdminContactInformationForm void get emailFocus => form.focus(emailControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void firstNameRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void firstNameRemove({bool updateParent = true, bool emitEvent = true}) { if (containsFirstName) { final controlPath = path; if (controlPath == null) { @@ -2728,12 +2965,7 @@ class AdminContactInformationForm } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void lastNameRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void lastNameRemove({bool updateParent = true, bool emitEvent = true}) { if (containsLastName) { final controlPath = path; if (controlPath == null) { @@ -2756,12 +2988,7 @@ class AdminContactInformationForm } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void emailRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void emailRemove({bool updateParent = true, bool emitEvent = true}) { if (containsEmail) { final controlPath = path; if (controlPath == null) { @@ -2789,8 +3016,11 @@ class AdminContactInformationForm bool updateParent = true, bool emitEvent = true, }) { - firstNameControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + firstNameControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void lastNameValueUpdate( @@ -2798,8 +3028,11 @@ class AdminContactInformationForm bool updateParent = true, bool emitEvent = true, }) { - lastNameControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + lastNameControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailValueUpdate( @@ -2807,8 +3040,11 @@ class AdminContactInformationForm bool updateParent = true, bool emitEvent = true, }) { - emailControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void firstNameValuePatch( @@ -2816,8 +3052,11 @@ class AdminContactInformationForm bool updateParent = true, bool emitEvent = true, }) { - firstNameControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + firstNameControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void lastNameValuePatch( @@ -2825,8 +3064,11 @@ class AdminContactInformationForm bool updateParent = true, bool emitEvent = true, }) { - lastNameControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + lastNameControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailValuePatch( @@ -2834,8 +3076,11 @@ class AdminContactInformationForm bool updateParent = true, bool emitEvent = true, }) { - emailControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void firstNameValueReset( @@ -2844,14 +3089,13 @@ class AdminContactInformationForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - firstNameControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => firstNameControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void lastNameValueReset( String? value, { @@ -2859,14 +3103,13 @@ class AdminContactInformationForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - lastNameControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => lastNameControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void emailValueReset( String? value, { @@ -2874,14 +3117,13 @@ class AdminContactInformationForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - emailControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => emailControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get firstNameControl => form.control(firstNameControlPath()) as FormControl; @@ -2959,41 +3201,36 @@ class AdminContactInformationForm ); } return AdminContactInformationOutput( - firstName: _firstNameValue, - lastName: _lastNameValue, - email: _emailValue); + firstName: _firstNameValue, + lastName: _lastNameValue, + email: _emailValue, + ); } @override AdminContactInformation get rawModel { return AdminContactInformation( - firstName: _firstNameRawValue, - lastName: _lastNameRawValue, - email: _emailRawValue); + firstName: _firstNameRawValue, + lastName: _lastNameRawValue, + email: _emailRawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { - final currentFormInstance = currentForm; - - if (currentFormInstance is! FormGroup) { - return; - } - + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { - currentFormInstance.controls[key]?.markAsEnabled( + currentForm.controls[key]?.markAsEnabled( updateParent: updateParent, emitEvent: emitEvent, ); @@ -3009,9 +3246,7 @@ class AdminContactInformationForm final currentForm = this.currentForm; return const DeepCollectionEquality().equals( - currentForm is FormControlCollection - ? currentForm.rawValue - : currentForm.value, + currentForm.rawValue, AdminContactInformationForm.formElements(other).rawValue, ); } @@ -3032,8 +3267,16 @@ class AdminContactInformationForm } @override - AbstractControl get currentForm { - return path == null ? form : form.control(path!); + bool get hasChanged { + return !const DeepCollectionEquality().equals( + currentForm.rawValue, + initial, + ); + } + + @override + FormGroup get currentForm { + return path == null ? form : form.control(path!) as FormGroup; } @override @@ -3041,112 +3284,180 @@ class AdminContactInformationForm AdminContactInformation? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue( - AdminContactInformationForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.updateValue( + AdminContactInformationForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void upsertValue( + AdminContactInformation? value, { + bool updateParent = true, + bool emitEvent = true, + }) { + final formElements = AdminContactInformationForm.formElements(value); + + currentForm.addAll(formElements.controls); + } @override void reset({ AdminContactInformation? value, bool updateParent = true, bool emitEvent = true, - }) => - form.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void updateInitial(Map? value, String? path) { + if (_formModel != null) { + _formModel?.updateInitial(currentForm.rawValue, path); + return; + } + + if (value == null) return; + + if (path == null || path.isEmpty) { + initial.addAll(value); + return; + } + + final keys = path.split('.'); + Object? current = initial; + for (var i = 0; i < keys.length - 1; i++) { + final key = keys[i]; + + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current = current[index]; + continue; + } + } + + if (current is Map) { + if (!current.containsKey(key)) { + current[key] = {}; + } + current = current[key]; + continue; + } + + return; + } + + final key = keys.last; + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current[index] = value; + } + } else if (current is Map) { + current[key] = value; + } + } String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); static FormGroup formElements( - AdminContactInformation? adminContactInformation) => - FormGroup({ - firstNameControlName: FormControl( - value: adminContactInformation?.firstName, - validators: [RequiredValidator(), MaxLengthValidator(120)], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - lastNameControlName: FormControl( - value: adminContactInformation?.lastName, - validators: [RequiredValidator(), MaxLengthValidator(120)], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - emailControlName: FormControl( - value: adminContactInformation?.email, - validators: [RequiredValidator(), EmailValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + AdminContactInformation? adminContactInformation, + ) => FormGroup( + { + firstNameControlName: FormControl( + value: adminContactInformation?.firstName, + validators: [RequiredValidator(), MaxLengthValidator(120)], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + lastNameControlName: FormControl( + value: adminContactInformation?.lastName, + validators: [RequiredValidator(), MaxLengthValidator(120)], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + emailControlName: FormControl( + value: adminContactInformation?.email, + validators: [RequiredValidator(), EmailValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } @Rf(output: true) @freezed abstract class MSICreateOutput with _$MSICreateOutput { - const factory MSICreateOutput( - {String? id, - String? businessNumber, - List? fileIds, - @RfControl(validators: [RequiredValidator(), MaxLengthValidator(120)]) - required String name, - required AddressOutput companyAddress, - @RfControl(validators: [RequiredValidator(), EmailValidator()]) - required String email, - required PrimaryContactOutput primaryContact, - @RfControl() @Default(false) bool sameMailingAddressAsCompany, - required AddressOutput mailingAddress, - @RfArray() - required List admins}) = _MSICreateOutput; + const factory MSICreateOutput({ + String? id, + String? businessNumber, + List? fileIds, + @RfControl(validators: [RequiredValidator(), MaxLengthValidator(120)]) + required String name, + required AddressOutput companyAddress, + @RfControl(validators: [RequiredValidator(), EmailValidator()]) + required String email, + required PrimaryContactOutput primaryContact, + @RfControl() @Default(false) bool sameMailingAddressAsCompany, + required AddressOutput mailingAddress, + @RfArray() + required List admins, + }) = _MSICreateOutput; } @RfGroup() @freezed abstract class AddressOutput with _$AddressOutput { - const factory AddressOutput( - {@RfControl(validators: [RequiredValidator(), MaxLengthValidator(120)]) - required String street, - @RfControl(validators: [RequiredValidator(), MaxLengthValidator(120)]) - required String city, - @RfControl(validators: [RequiredValidator(), MaxLengthValidator(120)]) - required String stateOrProvince, - @RfControl(validators: [RequiredValidator()]) - required String zipCode}) = _AddressOutput; + const factory AddressOutput({ + @RfControl(validators: [RequiredValidator(), MaxLengthValidator(120)]) + required String street, + @RfControl(validators: [RequiredValidator(), MaxLengthValidator(120)]) + required String city, + @RfControl(validators: [RequiredValidator(), MaxLengthValidator(120)]) + required String stateOrProvince, + @RfControl(validators: [RequiredValidator()]) required String zipCode, + }) = _AddressOutput; } @RfGroup() @freezed abstract class PrimaryContactOutput with _$PrimaryContactOutput { - const factory PrimaryContactOutput( - {@RfControl(validators: [RequiredValidator(), MaxLengthValidator(120)]) - required String fullName, - @RfControl(validators: [MaxLengthValidator(120)]) String? jobTitle, - @RfControl(validators: [RequiredValidator(), EmailValidator()]) - required String email}) = _PrimaryContactOutput; + const factory PrimaryContactOutput({ + @RfControl(validators: [RequiredValidator(), MaxLengthValidator(120)]) + required String fullName, + @RfControl(validators: [MaxLengthValidator(120)]) String? jobTitle, + @RfControl(validators: [RequiredValidator(), EmailValidator()]) + required String email, + }) = _PrimaryContactOutput; } @RfGroup() @freezed abstract class AdminContactInformationOutput with _$AdminContactInformationOutput { - const factory AdminContactInformationOutput( - {@RfControl(validators: [RequiredValidator(), MaxLengthValidator(120)]) - required String firstName, - @RfControl(validators: [RequiredValidator(), MaxLengthValidator(120)]) - required String lastName, - @RfControl(validators: [RequiredValidator(), EmailValidator()]) - required String email}) = _AdminContactInformationOutput; + const factory AdminContactInformationOutput({ + @RfControl(validators: [RequiredValidator(), MaxLengthValidator(120)]) + required String firstName, + @RfControl(validators: [RequiredValidator(), MaxLengthValidator(120)]) + required String lastName, + @RfControl(validators: [RequiredValidator(), EmailValidator()]) + required String email, + }) = _AdminContactInformationOutput; } class ReactiveMSICreateFormArrayBuilder @@ -3159,30 +3470,39 @@ class ReactiveMSICreateFormArrayBuilder required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - MSICreateForm formModel)? control; + MSICreateForm formModel, + )? + control; final Widget Function( - BuildContext context, List itemList, MSICreateForm formModel)? - builder; + BuildContext context, + List itemList, + MSICreateForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveMSICreateFormArrayBuilderT? item, - MSICreateForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveMSICreateFormArrayBuilderT? item, + MSICreateForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function(FormControl control)? - controlFilter; + controlFilter; @override Widget build(BuildContext context) { @@ -3198,18 +3518,9 @@ class ReactiveMSICreateFormArrayBuilder return ReactiveFormArrayItemBuilder( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -3227,35 +3538,41 @@ class ReactiveMSICreateFormArrayBuilder2 required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - MSICreateForm formModel)? control; + MSICreateForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - MSICreateForm formModel - }) params)? builder; + ({BuildContext context, List itemList, MSICreateForm formModel}) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveMSICreateFormArrayBuilderT? item, - MSICreateForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveMSICreateFormArrayBuilderT? item, + MSICreateForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function(FormControl control)? - controlFilter; + controlFilter; @override Widget build(BuildContext context) { @@ -3272,23 +3589,17 @@ class ReactiveMSICreateFormArrayBuilder2 formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -3297,33 +3608,48 @@ class ReactiveMSICreateFormArrayBuilder2 } class ReactiveMSICreateFormFormGroupArrayBuilder< - ReactiveMSICreateFormFormGroupArrayBuilderT> extends StatelessWidget { + ReactiveMSICreateFormFormGroupArrayBuilderT +> + extends StatelessWidget { const ReactiveMSICreateFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? extended; - - final ExtendedControl?>, - List> - Function(MSICreateForm formModel)? getExtended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(MSICreateForm formModel)? + getExtended; final Widget Function( - BuildContext context, List itemList, MSICreateForm formModel)? - builder; + BuildContext context, + List itemList, + MSICreateForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveMSICreateFormFormGroupArrayBuilderT? item, - MSICreateForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveMSICreateFormFormGroupArrayBuilderT? item, + MSICreateForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -3341,23 +3667,14 @@ class ReactiveMSICreateFormFormGroupArrayBuilder< final itemList = (value.value() ?? []) .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) .values .toList(); - return builder?.call( - context, - itemList, - formModel, - ) ?? + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/generator_tests/test/doc/delivery_list_output_test.dart b/packages/generator_tests/test/doc/delivery_list_output_test.dart index c0632136..dafd8816 100644 --- a/packages/generator_tests/test/doc/delivery_list_output_test.dart +++ b/packages/generator_tests/test/doc/delivery_list_output_test.dart @@ -113,7 +113,11 @@ class ReactiveDeliveryListOFormConsumer extends StatelessWidget { final Widget? child; final Widget Function( - BuildContext context, DeliveryListOForm formModel, Widget? child) builder; + BuildContext context, + DeliveryListOForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -132,11 +136,7 @@ class DeliveryListOFormInheritedStreamer extends InheritedStreamer { required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final DeliveryListOForm form; } @@ -158,19 +158,19 @@ class ReactiveDeliveryListOForm extends StatelessWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - static DeliveryListOForm? of( - BuildContext context, { - bool listen = true, - }) { + static DeliveryListOForm? of(BuildContext context, {bool listen = true}) { if (listen) { return context .dependOnInheritedWidgetOfExactType< - DeliveryListOFormInheritedStreamer>() + DeliveryListOFormInheritedStreamer + >() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - DeliveryListOFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + DeliveryListOFormInheritedStreamer + >(); return element == null ? null : (element.widget as DeliveryListOFormInheritedStreamer).form; @@ -218,10 +218,14 @@ class DeliveryListOFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, DeliveryListOForm formModel, Widget? child) builder; + BuildContext context, + DeliveryListOForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, DeliveryListOForm formModel)? - initState; + initState; @override _DeliveryListOFormBuilderState createState() => @@ -235,8 +239,11 @@ class _DeliveryListOFormBuilderState extends State { @override void initState() { - _formModel = - DeliveryListOForm(DeliveryListOForm.formElements(widget.model), null); + _formModel = DeliveryListOForm( + DeliveryListOForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -314,10 +321,8 @@ final _logDeliveryListOForm = Logger.detached('DeliveryListOForm'); class DeliveryListOForm implements FormModel { - DeliveryListOForm( - this.form, - this.path, - ); + DeliveryListOForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String deliveryListControlName = "deliveryList"; @@ -327,8 +332,14 @@ class DeliveryListOForm final String? path; + // ignore: unused_field + final FormModel? _formModel; + final Map _disabled = {}; + @override + final Map initial; + String deliveryListControlPath() => pathBuilder(deliveryListControlName); String clientListControlPath() => pathBuilder(clientListControlName); @@ -336,17 +347,17 @@ class DeliveryListOForm List get _deliveryListValue => deliveryListDeliveryPointOForm.map((e) => e.model).toList(); - List? get _clientListValue => - clientListClientOForm.map((e) => e.model).toList(); + List? get _clientListValue => containsClientList + ? clientListClientOForm.map((e) => e.model).toList() + : null; List get _deliveryListRawValue => deliveryListDeliveryPointOForm.map((e) => e.rawModel).toList(); - List? get _clientListRawValue => - clientListClientOForm.map((e) => e.rawModel).toList(); + List? get _clientListRawValue => containsClientList + ? clientListClientOForm.map((e) => e.rawModel).toList() + : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsDeliveryList { try { form.control(deliveryListControlPath()); @@ -356,8 +367,6 @@ class DeliveryListOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsClientList { try { form.control(clientListControlPath()); @@ -375,12 +384,7 @@ class DeliveryListOForm void get clientListFocus => form.focus(clientListControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void clientListRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void clientListRemove({bool updateParent = true, bool emitEvent = true}) { if (containsClientList) { final controlPath = path; if (controlPath == null) { @@ -431,17 +435,21 @@ class DeliveryListOForm if (toUpdate.isNotEmpty) { deliveryListControl.updateValue( - toUpdate - .map((e) => DeliveryPointOForm.formElements(e).rawValue) - .toList(), - updateParent: updateParent, - emitEvent: emitEvent); + toUpdate + .map((e) => DeliveryPointOForm.formElements(e).rawValue) + .toList(), + updateParent: updateParent, + emitEvent: emitEvent, + ); } if (toAdd.isNotEmpty) { toAdd.forEach((e) { - deliveryListControl.add(DeliveryPointOForm.formElements(e), - updateParent: updateParent, emitEvent: emitEvent); + deliveryListControl.add( + DeliveryPointOForm.formElements(e), + updateParent: updateParent, + emitEvent: emitEvent, + ); }); } } @@ -474,15 +482,19 @@ class DeliveryListOForm if (toUpdate.isNotEmpty) { clientListControl.updateValue( - toUpdate.map((e) => ClientOForm.formElements(e).rawValue).toList(), - updateParent: updateParent, - emitEvent: emitEvent); + toUpdate.map((e) => ClientOForm.formElements(e).rawValue).toList(), + updateParent: updateParent, + emitEvent: emitEvent, + ); } if (toAdd.isNotEmpty) { toAdd.forEach((e) { - clientListControl.add(ClientOForm.formElements(e), - updateParent: updateParent, emitEvent: emitEvent); + clientListControl.add( + ClientOForm.formElements(e), + updateParent: updateParent, + emitEvent: emitEvent, + ); }); } } @@ -527,18 +539,12 @@ class DeliveryListOForm ); } - void deliveryListClear({ - bool updateParent = true, - bool emitEvent = true, - }) { + void deliveryListClear({bool updateParent = true, bool emitEvent = true}) { deliveryListDeliveryPointOForm.clear(); deliveryListControl.clear(updateParent: updateParent, emitEvent: emitEvent); } - void clientListClear({ - bool updateParent = true, - bool emitEvent = true, - }) { + void clientListClear({bool updateParent = true, bool emitEvent = true}) { clientListClientOForm.clear(); clientListControl.clear(updateParent: updateParent, emitEvent: emitEvent); } @@ -551,20 +557,17 @@ class DeliveryListOForm final keys = deliveryListDeliveryPointOForm.asMap().keys; final toPatch = []; - (value).asMap().forEach( - (k, v) { - if (keys.contains(k)) { - toPatch.add(v); - } - }, - ); + (value).asMap().forEach((k, v) { + if (keys.contains(k)) { + toPatch.add(v); + } + }); deliveryListControl.patchValue( - toPatch - .map((e) => DeliveryPointOForm.formElements(e).rawValue) - .toList(), - updateParent: updateParent, - emitEvent: emitEvent); + toPatch.map((e) => DeliveryPointOForm.formElements(e).rawValue).toList(), + updateParent: updateParent, + emitEvent: emitEvent, + ); } void clientListValuePatch( @@ -575,18 +578,17 @@ class DeliveryListOForm final keys = clientListClientOForm.asMap().keys; final toPatch = []; - (value ?? []).asMap().forEach( - (k, v) { - if (keys.contains(k)) { - toPatch.add(v); - } - }, - ); + (value ?? []).asMap().forEach((k, v) { + if (keys.contains(k)) { + toPatch.add(v); + } + }); clientListControl.patchValue( - toPatch.map((e) => ClientOForm.formElements(e).rawValue).toList(), - updateParent: updateParent, - emitEvent: emitEvent); + toPatch.map((e) => ClientOForm.formElements(e).rawValue).toList(), + updateParent: updateParent, + emitEvent: emitEvent, + ); } void deliveryListValueReset( @@ -595,13 +597,13 @@ class DeliveryListOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - deliveryListControl.reset( - value: value - .map((e) => DeliveryPointOForm.formElements(e).rawValue) - .toList(), - updateParent: updateParent, - emitEvent: emitEvent); + }) => deliveryListControl.reset( + value: value + .map((e) => DeliveryPointOForm.formElements(e).rawValue) + .toList(), + updateParent: updateParent, + emitEvent: emitEvent, + ); void clientListValueReset( List? value, { @@ -609,12 +611,11 @@ class DeliveryListOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - clientListControl.reset( - value: - value?.map((e) => ClientOForm.formElements(e).rawValue).toList(), - updateParent: updateParent, - emitEvent: emitEvent); + }) => clientListControl.reset( + value: value?.map((e) => ClientOForm.formElements(e).rawValue).toList(), + updateParent: updateParent, + emitEvent: emitEvent, + ); FormArray> get deliveryListControl => form.control(deliveryListControlPath()) @@ -628,8 +629,16 @@ class DeliveryListOForm return values .asMap() - .map((k, v) => MapEntry( - k, DeliveryPointOForm(form, pathBuilder("deliveryList.$k")))) + .map( + (k, v) => MapEntry( + k, + DeliveryPointOForm( + form, + pathBuilder("deliveryList.$k"), + _formModel ?? this, + ), + ), + ) .values .toList(); } @@ -639,8 +648,12 @@ class DeliveryListOForm return values .asMap() - .map((k, v) => - MapEntry(k, ClientOForm(form, pathBuilder("clientList.$k")))) + .map( + (k, v) => MapEntry( + k, + ClientOForm(form, pathBuilder("clientList.$k"), _formModel ?? this), + ), + ) .values .toList(); } @@ -682,18 +695,20 @@ class DeliveryListOForm } ExtendedControl?>, List> - get deliveryListExtendedControl => ExtendedControl< - List?>, List>( - form.control(deliveryListControlPath()) - as FormArray>, - () => deliveryListDeliveryPointOForm); + get deliveryListExtendedControl => + ExtendedControl?>, List>( + form.control(deliveryListControlPath()) + as FormArray>, + () => deliveryListDeliveryPointOForm, + ); ExtendedControl?>, List> - get clientListExtendedControl => - ExtendedControl?>, List>( - form.control(clientListControlPath()) - as FormArray>, - () => clientListClientOForm); + get clientListExtendedControl => + ExtendedControl?>, List>( + form.control(clientListControlPath()) + as FormArray>, + () => clientListClientOForm, + ); void addDeliveryListItem(DeliveryPointO value) { deliveryListControl.add(DeliveryPointOForm.formElements(value)); @@ -736,28 +751,23 @@ class DeliveryListOForm ); } return DeliveryListOOutput( - deliveryList: _deliveryListValue, clientList: _clientListValue); + deliveryList: _deliveryListValue, + clientList: _clientListValue, + ); } @override DeliveryListO get rawModel { return DeliveryListO( - deliveryList: _deliveryListRawValue, clientList: _clientListRawValue); + deliveryList: _deliveryListRawValue, + clientList: _clientListRawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { - final currentFormInstance = currentForm; - - if (currentFormInstance is! FormGroup) { - return; - } - + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); @@ -765,14 +775,16 @@ class DeliveryListOForm clientListClientOForm.forEach((e) => e.toggleDisabled()); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { deliveryListDeliveryPointOForm.forEach((e) => e.toggleDisabled()); clientListClientOForm.forEach((e) => e.toggleDisabled()); - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { - currentFormInstance.controls[key]?.markAsEnabled( + currentForm.controls[key]?.markAsEnabled( updateParent: updateParent, emitEvent: emitEvent, ); @@ -788,9 +800,7 @@ class DeliveryListOForm final currentForm = this.currentForm; return const DeepCollectionEquality().equals( - currentForm is FormControlCollection - ? currentForm.rawValue - : currentForm.value, + currentForm.rawValue, DeliveryListOForm.formElements(other).rawValue, ); } @@ -811,8 +821,16 @@ class DeliveryListOForm } @override - AbstractControl get currentForm { - return path == null ? form : form.control(path!); + bool get hasChanged { + return !const DeepCollectionEquality().equals( + currentForm.rawValue, + initial, + ); + } + + @override + FormGroup get currentForm { + return path == null ? form : form.control(path!) as FormGroup; } @override @@ -820,56 +838,120 @@ class DeliveryListOForm DeliveryListO? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(DeliveryListOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + DeliveryListOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void upsertValue( + DeliveryListO? value, { + bool updateParent = true, + bool emitEvent = true, + }) { + final formElements = DeliveryListOForm.formElements(value); + + currentForm.addAll(formElements.controls); + } @override void reset({ DeliveryListO? value, bool updateParent = true, bool emitEvent = true, - }) => - form.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void updateInitial(Map? value, String? path) { + if (_formModel != null) { + _formModel?.updateInitial(currentForm.rawValue, path); + return; + } + + if (value == null) return; + + if (path == null || path.isEmpty) { + initial.addAll(value); + return; + } + + final keys = path.split('.'); + Object? current = initial; + for (var i = 0; i < keys.length - 1; i++) { + final key = keys[i]; + + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current = current[index]; + continue; + } + } + + if (current is Map) { + if (!current.containsKey(key)) { + current[key] = {}; + } + current = current[key]; + continue; + } + + return; + } + + final key = keys.last; + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current[index] = value; + } + } else if (current is Map) { + current[key] = value; + } + } String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(DeliveryListO? deliveryListO) => FormGroup({ - deliveryListControlName: FormArray( - (deliveryListO?.deliveryList ?? []) - .map((e) => DeliveryPointOForm.formElements(e)) - .toList(), - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false), - clientListControlName: FormArray( - (deliveryListO?.clientList ?? []) - .map((e) => ClientOForm.formElements(e)) - .toList(), - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(DeliveryListO? deliveryListO) => FormGroup( + { + deliveryListControlName: FormArray( + (deliveryListO?.deliveryList ?? []) + .map((e) => DeliveryPointOForm.formElements(e)) + .toList(), + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ), + clientListControlName: FormArray( + (deliveryListO?.clientList ?? []) + .map((e) => ClientOForm.formElements(e)) + .toList(), + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } final _logDeliveryPointOForm = Logger.detached('DeliveryPointOForm'); class DeliveryPointOForm implements FormModel { - DeliveryPointOForm( - this.form, - this.path, - ); + DeliveryPointOForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String nameControlName = "name"; @@ -879,22 +961,28 @@ class DeliveryPointOForm final String? path; + // ignore: unused_field + final FormModel? _formModel; + final Map _disabled = {}; + @override + final Map initial; + String nameControlPath() => pathBuilder(nameControlName); String addressControlPath() => pathBuilder(addressControlName); String get _nameValue => nameControl.value ?? ""; - AddressOOutput? get _addressValue => addressForm.model; + AddressOOutput? get _addressValue => + containsAddress ? addressForm.model : null; String get _nameRawValue => nameControl.value ?? ""; - AddressO? get _addressRawValue => addressForm.rawModel; + AddressO? get _addressRawValue => + containsAddress ? addressForm.rawModel : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsName { try { form.control(nameControlPath()); @@ -904,8 +992,6 @@ class DeliveryPointOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsAddress { try { form.control(addressControlPath()); @@ -923,12 +1009,7 @@ class DeliveryPointOForm void get addressFocus => form.focus(addressControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void addressRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void addressRemove({bool updateParent = true, bool emitEvent = true}) { if (containsAddress) { final controlPath = path; if (controlPath == null) { @@ -956,8 +1037,11 @@ class DeliveryPointOForm bool updateParent = true, bool emitEvent = true, }) { - nameControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + nameControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void addressValueUpdate( @@ -965,8 +1049,11 @@ class DeliveryPointOForm bool updateParent = true, bool emitEvent = true, }) { - addressControl.updateValue(AddressOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + addressControl.updateValue( + AddressOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void nameValuePatch( @@ -974,8 +1061,11 @@ class DeliveryPointOForm bool updateParent = true, bool emitEvent = true, }) { - nameControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + nameControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void addressValuePatch( @@ -983,8 +1073,11 @@ class DeliveryPointOForm bool updateParent = true, bool emitEvent = true, }) { - addressControl.updateValue(AddressOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + addressControl.updateValue( + AddressOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void nameValueReset( @@ -993,14 +1086,13 @@ class DeliveryPointOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - nameControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => nameControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void addressValueReset( AddressO? value, { @@ -1008,11 +1100,11 @@ class DeliveryPointOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - addressControl.reset( - value: AddressOForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => addressControl.reset( + value: AddressOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); FormControl get nameControl => form.control(nameControlPath()) as FormControl; @@ -1020,7 +1112,8 @@ class DeliveryPointOForm FormGroup get addressControl => form.control(addressControlPath()) as FormGroup; - AddressOForm get addressForm => AddressOForm(form, pathBuilder('address')); + AddressOForm get addressForm => + AddressOForm(form, pathBuilder('address'), _formModel ?? this); void nameSetDisabled( bool disabled, { @@ -1079,29 +1172,22 @@ class DeliveryPointOForm } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { - final currentFormInstance = currentForm; - - if (currentFormInstance is! FormGroup) { - return; - } - + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); addressForm.toggleDisabled(); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { addressForm.toggleDisabled(); - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { - currentFormInstance.controls[key]?.markAsEnabled( + currentForm.controls[key]?.markAsEnabled( updateParent: updateParent, emitEvent: emitEvent, ); @@ -1117,9 +1203,7 @@ class DeliveryPointOForm final currentForm = this.currentForm; return const DeepCollectionEquality().equals( - currentForm is FormControlCollection - ? currentForm.rawValue - : currentForm.value, + currentForm.rawValue, DeliveryPointOForm.formElements(other).rawValue, ); } @@ -1140,8 +1224,16 @@ class DeliveryPointOForm } @override - AbstractControl get currentForm { - return path == null ? form : form.control(path!); + bool get hasChanged { + return !const DeepCollectionEquality().equals( + currentForm.rawValue, + initial, + ); + } + + @override + FormGroup get currentForm { + return path == null ? form : form.control(path!) as FormGroup; } @override @@ -1149,47 +1241,109 @@ class DeliveryPointOForm DeliveryPointO? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(DeliveryPointOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + DeliveryPointOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void upsertValue( + DeliveryPointO? value, { + bool updateParent = true, + bool emitEvent = true, + }) { + final formElements = DeliveryPointOForm.formElements(value); + + currentForm.addAll(formElements.controls); + } @override void reset({ DeliveryPointO? value, bool updateParent = true, bool emitEvent = true, - }) => - form.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void updateInitial(Map? value, String? path) { + if (_formModel != null) { + _formModel?.updateInitial(currentForm.rawValue, path); + return; + } + + if (value == null) return; + + if (path == null || path.isEmpty) { + initial.addAll(value); + return; + } + + final keys = path.split('.'); + Object? current = initial; + for (var i = 0; i < keys.length - 1; i++) { + final key = keys[i]; + + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current = current[index]; + continue; + } + } + + if (current is Map) { + if (!current.containsKey(key)) { + current[key] = {}; + } + current = current[key]; + continue; + } + + return; + } + + final key = keys.last; + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current[index] = value; + } + } else if (current is Map) { + current[key] = value; + } + } String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(DeliveryPointO? deliveryPointO) => FormGroup({ - nameControlName: FormControl( - value: deliveryPointO?.name, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - addressControlName: AddressOForm.formElements(deliveryPointO?.address) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(DeliveryPointO? deliveryPointO) => FormGroup( + { + nameControlName: FormControl( + value: deliveryPointO?.name, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + addressControlName: AddressOForm.formElements(deliveryPointO?.address), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } final _logAddressOForm = Logger.detached('AddressOForm'); class AddressOForm implements FormModel { - AddressOForm( - this.form, - this.path, - ); + AddressOForm(this.form, this.path, this._formModel) : initial = form.rawValue; static const String streetControlName = "street"; @@ -1199,22 +1353,26 @@ class AddressOForm implements FormModel { final String? path; + // ignore: unused_field + final FormModel? _formModel; + final Map _disabled = {}; + @override + final Map initial; + String streetControlPath() => pathBuilder(streetControlName); String cityControlPath() => pathBuilder(cityControlName); String get _streetValue => streetControl.value as String; - String? get _cityValue => cityControl.value; + String? get _cityValue => containsCity ? cityControl.value : null; - String? get _streetRawValue => streetControl.value; + String? get _streetRawValue => containsStreet ? streetControl.value : null; - String? get _cityRawValue => cityControl.value; + String? get _cityRawValue => containsCity ? cityControl.value : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsStreet { try { form.control(streetControlPath()); @@ -1224,8 +1382,6 @@ class AddressOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsCity { try { form.control(cityControlPath()); @@ -1243,12 +1399,7 @@ class AddressOForm implements FormModel { void get cityFocus => form.focus(cityControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void streetRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void streetRemove({bool updateParent = true, bool emitEvent = true}) { if (containsStreet) { final controlPath = path; if (controlPath == null) { @@ -1271,12 +1422,7 @@ class AddressOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void cityRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void cityRemove({bool updateParent = true, bool emitEvent = true}) { if (containsCity) { final controlPath = path; if (controlPath == null) { @@ -1304,8 +1450,11 @@ class AddressOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - streetControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + streetControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void cityValueUpdate( @@ -1313,8 +1462,11 @@ class AddressOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - cityControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + cityControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void streetValuePatch( @@ -1322,8 +1474,11 @@ class AddressOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - streetControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + streetControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void cityValuePatch( @@ -1331,8 +1486,11 @@ class AddressOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - cityControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + cityControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void streetValueReset( @@ -1341,14 +1499,13 @@ class AddressOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - streetControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => streetControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void cityValueReset( String? value, { @@ -1356,14 +1513,13 @@ class AddressOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - cityControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => cityControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get streetControl => form.control(streetControlPath()) as FormControl; @@ -1428,27 +1584,20 @@ class AddressOForm implements FormModel { } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { - final currentFormInstance = currentForm; - - if (currentFormInstance is! FormGroup) { - return; - } - + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { - currentFormInstance.controls[key]?.markAsEnabled( + currentForm.controls[key]?.markAsEnabled( updateParent: updateParent, emitEvent: emitEvent, ); @@ -1464,9 +1613,7 @@ class AddressOForm implements FormModel { final currentForm = this.currentForm; return const DeepCollectionEquality().equals( - currentForm is FormControlCollection - ? currentForm.rawValue - : currentForm.value, + currentForm.rawValue, AddressOForm.formElements(other).rawValue, ); } @@ -1487,8 +1634,16 @@ class AddressOForm implements FormModel { } @override - AbstractControl get currentForm { - return path == null ? form : form.control(path!); + bool get hasChanged { + return !const DeepCollectionEquality().equals( + currentForm.rawValue, + initial, + ); + } + + @override + FormGroup get currentForm { + return path == null ? form : form.control(path!) as FormGroup; } @override @@ -1496,53 +1651,116 @@ class AddressOForm implements FormModel { AddressO? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(AddressOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + AddressOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void upsertValue( + AddressO? value, { + bool updateParent = true, + bool emitEvent = true, + }) { + final formElements = AddressOForm.formElements(value); + + currentForm.addAll(formElements.controls); + } @override void reset({ AddressO? value, bool updateParent = true, bool emitEvent = true, - }) => - form.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void updateInitial(Map? value, String? path) { + if (_formModel != null) { + _formModel?.updateInitial(currentForm.rawValue, path); + return; + } + + if (value == null) return; + + if (path == null || path.isEmpty) { + initial.addAll(value); + return; + } + + final keys = path.split('.'); + Object? current = initial; + for (var i = 0; i < keys.length - 1; i++) { + final key = keys[i]; + + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current = current[index]; + continue; + } + } + + if (current is Map) { + if (!current.containsKey(key)) { + current[key] = {}; + } + current = current[key]; + continue; + } + + return; + } + + final key = keys.last; + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current[index] = value; + } + } else if (current is Map) { + current[key] = value; + } + } String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(AddressO? addressO) => FormGroup({ - streetControlName: FormControl( - value: addressO?.street, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - cityControlName: FormControl( - value: addressO?.city, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(AddressO? addressO) => FormGroup( + { + streetControlName: FormControl( + value: addressO?.street, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + cityControlName: FormControl( + value: addressO?.city, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } final _logClientOForm = Logger.detached('ClientOForm'); class ClientOForm implements FormModel { - ClientOForm( - this.form, - this.path, - ); + ClientOForm(this.form, this.path, this._formModel) : initial = form.rawValue; static const String clientTypeControlName = "clientType"; @@ -1554,8 +1772,14 @@ class ClientOForm implements FormModel { final String? path; + // ignore: unused_field + final FormModel? _formModel; + final Map _disabled = {}; + @override + final Map initial; + String clientTypeControlPath() => pathBuilder(clientTypeControlName); String nameControlPath() => pathBuilder(nameControlName); @@ -1564,18 +1788,16 @@ class ClientOForm implements FormModel { ClientType get _clientTypeValue => clientTypeControl.value as ClientType; - String? get _nameValue => nameControl.value; + String? get _nameValue => containsName ? nameControl.value : null; - String? get _notesValue => notesControl.value; + String? get _notesValue => containsNotes ? notesControl.value : null; ClientType get _clientTypeRawValue => clientTypeControl.value as ClientType; - String? get _nameRawValue => nameControl.value; + String? get _nameRawValue => containsName ? nameControl.value : null; - String? get _notesRawValue => notesControl.value; + String? get _notesRawValue => containsNotes ? notesControl.value : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsClientType { try { form.control(clientTypeControlPath()); @@ -1585,8 +1807,6 @@ class ClientOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsName { try { form.control(nameControlPath()); @@ -1596,8 +1816,6 @@ class ClientOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsNotes { try { form.control(notesControlPath()); @@ -1619,12 +1837,7 @@ class ClientOForm implements FormModel { void get notesFocus => form.focus(notesControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void nameRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void nameRemove({bool updateParent = true, bool emitEvent = true}) { if (containsName) { final controlPath = path; if (controlPath == null) { @@ -1647,12 +1860,7 @@ class ClientOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void notesRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void notesRemove({bool updateParent = true, bool emitEvent = true}) { if (containsNotes) { final controlPath = path; if (controlPath == null) { @@ -1680,8 +1888,11 @@ class ClientOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - clientTypeControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + clientTypeControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void nameValueUpdate( @@ -1689,8 +1900,11 @@ class ClientOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - nameControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + nameControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void notesValueUpdate( @@ -1698,8 +1912,11 @@ class ClientOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - notesControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + notesControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void clientTypeValuePatch( @@ -1707,8 +1924,11 @@ class ClientOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - clientTypeControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + clientTypeControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void nameValuePatch( @@ -1716,8 +1936,11 @@ class ClientOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - nameControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + nameControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void notesValuePatch( @@ -1725,8 +1948,11 @@ class ClientOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - notesControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + notesControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void clientTypeValueReset( @@ -1735,14 +1961,13 @@ class ClientOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - clientTypeControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => clientTypeControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void nameValueReset( String? value, { @@ -1750,14 +1975,13 @@ class ClientOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - nameControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => nameControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void notesValueReset( String? value, { @@ -1765,14 +1989,13 @@ class ClientOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - notesControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => notesControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get clientTypeControl => form.control(clientTypeControlPath()) as FormControl; @@ -1850,39 +2073,36 @@ class ClientOForm implements FormModel { ); } return ClientOOutput( - clientType: _clientTypeValue, name: _nameValue, notes: _notesValue); + clientType: _clientTypeValue, + name: _nameValue, + notes: _notesValue, + ); } @override ClientO get rawModel { return ClientO( - clientType: _clientTypeRawValue, - name: _nameRawValue, - notes: _notesRawValue); + clientType: _clientTypeRawValue, + name: _nameRawValue, + notes: _notesRawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { - final currentFormInstance = currentForm; - - if (currentFormInstance is! FormGroup) { - return; - } - + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { - currentFormInstance.controls[key]?.markAsEnabled( + currentForm.controls[key]?.markAsEnabled( updateParent: updateParent, emitEvent: emitEvent, ); @@ -1898,9 +2118,7 @@ class ClientOForm implements FormModel { final currentForm = this.currentForm; return const DeepCollectionEquality().equals( - currentForm is FormControlCollection - ? currentForm.rawValue - : currentForm.value, + currentForm.rawValue, ClientOForm.formElements(other).rawValue, ); } @@ -1921,8 +2139,16 @@ class ClientOForm implements FormModel { } @override - AbstractControl get currentForm { - return path == null ? form : form.control(path!); + bool get hasChanged { + return !const DeepCollectionEquality().equals( + currentForm.rawValue, + initial, + ); + } + + @override + FormGroup get currentForm { + return path == null ? form : form.control(path!) as FormGroup; } @override @@ -1930,59 +2156,128 @@ class ClientOForm implements FormModel { ClientO? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(ClientOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + ClientOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void upsertValue( + ClientO? value, { + bool updateParent = true, + bool emitEvent = true, + }) { + final formElements = ClientOForm.formElements(value); + + currentForm.addAll(formElements.controls); + } @override void reset({ ClientO? value, bool updateParent = true, bool emitEvent = true, - }) => - form.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void updateInitial(Map? value, String? path) { + if (_formModel != null) { + _formModel?.updateInitial(currentForm.rawValue, path); + return; + } + + if (value == null) return; + + if (path == null || path.isEmpty) { + initial.addAll(value); + return; + } + + final keys = path.split('.'); + Object? current = initial; + for (var i = 0; i < keys.length - 1; i++) { + final key = keys[i]; + + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current = current[index]; + continue; + } + } + + if (current is Map) { + if (!current.containsKey(key)) { + current[key] = {}; + } + current = current[key]; + continue; + } + + return; + } + + final key = keys.last; + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current[index] = value; + } + } else if (current is Map) { + current[key] = value; + } + } String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(ClientO? clientO) => FormGroup({ - clientTypeControlName: FormControl( - value: clientO?.clientType, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - nameControlName: FormControl( - value: clientO?.name, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - notesControlName: FormControl( - value: clientO?.notes, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(ClientO? clientO) => FormGroup( + { + clientTypeControlName: FormControl( + value: clientO?.clientType, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + nameControlName: FormControl( + value: clientO?.name, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + notesControlName: FormControl( + value: clientO?.notes, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } @Rf(output: true) class DeliveryListOOutput extends Equatable { final List deliveryList; final List? clientList; - const DeliveryListOOutput( - {@RfArray() required this.deliveryList, @RfArray() this.clientList}); + const DeliveryListOOutput({ + @RfArray() required this.deliveryList, + @RfArray() this.clientList, + }); @override List get props => [deliveryList, clientList]; } @@ -1992,9 +2287,10 @@ class DeliveryListOOutput extends Equatable { class DeliveryPointOOutput extends Equatable { final String name; final AddressOOutput? address; - const DeliveryPointOOutput( - {@RfControl(validators: [RequiredValidator()]) required this.name, - this.address}); + const DeliveryPointOOutput({ + @RfControl(validators: [RequiredValidator()]) required this.name, + this.address, + }); @override List get props => [name, address]; } @@ -2004,10 +2300,11 @@ class ClientOOutput extends Equatable { final ClientType clientType; final String? name; final String? notes; - const ClientOOutput( - {@RfControl() required this.clientType, - @RfControl() this.name, - @RfControl() this.notes}); + const ClientOOutput({ + @RfControl() required this.clientType, + @RfControl() this.name, + @RfControl() this.notes, + }); @override List get props => [name, notes]; } @@ -2016,15 +2313,18 @@ class ClientOOutput extends Equatable { class AddressOOutput extends Equatable { final String street; final String? city; - const AddressOOutput( - {@RfControl(validators: [RequiredValidator()]) required this.street, - @RfControl() this.city}); + const AddressOOutput({ + @RfControl(validators: [RequiredValidator()]) required this.street, + @RfControl() this.city, + }); @override List get props => [street, city]; } class ReactiveDeliveryListOFormArrayBuilder< - ReactiveDeliveryListOFormArrayBuilderT> extends StatelessWidget { + ReactiveDeliveryListOFormArrayBuilderT +> + extends StatelessWidget { const ReactiveDeliveryListOFormArrayBuilder({ Key? key, this.control, @@ -2033,30 +2333,41 @@ class ReactiveDeliveryListOFormArrayBuilder< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - DeliveryListOForm formModel)? control; + DeliveryListOForm formModel, + )? + control; - final Widget Function(BuildContext context, List itemList, - DeliveryListOForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + DeliveryListOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveDeliveryListOFormArrayBuilderT? item, - DeliveryListOForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveDeliveryListOFormArrayBuilderT? item, + DeliveryListOForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -2072,18 +2383,9 @@ class ReactiveDeliveryListOFormArrayBuilder< return ReactiveFormArrayItemBuilder( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -2092,7 +2394,9 @@ class ReactiveDeliveryListOFormArrayBuilder< } class ReactiveDeliveryListOFormArrayBuilder2< - ReactiveDeliveryListOFormArrayBuilderT> extends StatelessWidget { + ReactiveDeliveryListOFormArrayBuilderT +> + extends StatelessWidget { const ReactiveDeliveryListOFormArrayBuilder2({ Key? key, this.control, @@ -2101,36 +2405,43 @@ class ReactiveDeliveryListOFormArrayBuilder2< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - DeliveryListOForm formModel)? control; + DeliveryListOForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - DeliveryListOForm formModel - }) params)? builder; + ({BuildContext context, List itemList, DeliveryListOForm formModel}) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveDeliveryListOFormArrayBuilderT? item, - DeliveryListOForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveDeliveryListOFormArrayBuilderT? item, + DeliveryListOForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -2147,23 +2458,17 @@ class ReactiveDeliveryListOFormArrayBuilder2< formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -2172,32 +2477,48 @@ class ReactiveDeliveryListOFormArrayBuilder2< } class ReactiveDeliveryListOFormFormGroupArrayBuilder< - ReactiveDeliveryListOFormFormGroupArrayBuilderT> extends StatelessWidget { + ReactiveDeliveryListOFormFormGroupArrayBuilderT +> + extends StatelessWidget { const ReactiveDeliveryListOFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? extended; - - final ExtendedControl?>, - List> - Function(DeliveryListOForm formModel)? getExtended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(DeliveryListOForm formModel)? + getExtended; - final Widget Function(BuildContext context, List itemList, - DeliveryListOForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + DeliveryListOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveDeliveryListOFormFormGroupArrayBuilderT? item, - DeliveryListOForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveDeliveryListOFormFormGroupArrayBuilderT? item, + DeliveryListOForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -2212,26 +2533,18 @@ class ReactiveDeliveryListOFormFormGroupArrayBuilder< return StreamBuilder?>?>( stream: value.control.valueChanges, builder: (context, snapshot) { - final itemList = (value.value() ?? - []) - .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) - .values - .toList(); - - return builder?.call( - context, - itemList, - formModel, - ) ?? + final itemList = + (value.value() ?? + []) + .asMap() + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) + .values + .toList(); + + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); @@ -2247,8 +2560,12 @@ class ReactiveStandaloneDeliveryPointFormConsumer extends StatelessWidget { final Widget? child; - final Widget Function(BuildContext context, - StandaloneDeliveryPointForm formModel, Widget? child) builder; + final Widget Function( + BuildContext context, + StandaloneDeliveryPointForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -2268,11 +2585,7 @@ class StandaloneDeliveryPointFormInheritedStreamer required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final StandaloneDeliveryPointForm form; } @@ -2301,12 +2614,15 @@ class ReactiveStandaloneDeliveryPointForm extends StatelessWidget { if (listen) { return context .dependOnInheritedWidgetOfExactType< - StandaloneDeliveryPointFormInheritedStreamer>() + StandaloneDeliveryPointFormInheritedStreamer + >() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - StandaloneDeliveryPointFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + StandaloneDeliveryPointFormInheritedStreamer + >(); return element == null ? null : (element.widget as StandaloneDeliveryPointFormInheritedStreamer).form; @@ -2353,11 +2669,18 @@ class StandaloneDeliveryPointFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - final Widget Function(BuildContext context, - StandaloneDeliveryPointForm formModel, Widget? child) builder; + final Widget Function( + BuildContext context, + StandaloneDeliveryPointForm formModel, + Widget? child, + ) + builder; final void Function( - BuildContext context, StandaloneDeliveryPointForm formModel)? initState; + BuildContext context, + StandaloneDeliveryPointForm formModel, + )? + initState; @override _StandaloneDeliveryPointFormBuilderState createState() => @@ -2373,7 +2696,10 @@ class _StandaloneDeliveryPointFormBuilderState @override void initState() { _formModel = StandaloneDeliveryPointForm( - StandaloneDeliveryPointForm.formElements(widget.model), null); + StandaloneDeliveryPointForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -2381,8 +2707,9 @@ class _StandaloneDeliveryPointFormBuilderState widget.initState?.call(context, _formModel); - _logSubscription = - _logStandaloneDeliveryPointForm.onRecord.listen((LogRecord e) { + _logSubscription = _logStandaloneDeliveryPointForm.onRecord.listen(( + LogRecord e, + ) { // use `dumpErrorToConsole` for severe messages to ensure that severe // exceptions are formatted consistently with other Flutter examples and // avoids printing duplicate exceptions @@ -2448,15 +2775,14 @@ class _StandaloneDeliveryPointFormBuilderState } } -final _logStandaloneDeliveryPointForm = - Logger.detached('StandaloneDeliveryPointForm'); +final _logStandaloneDeliveryPointForm = Logger.detached( + 'StandaloneDeliveryPointForm', +); class StandaloneDeliveryPointForm implements FormModel { - StandaloneDeliveryPointForm( - this.form, - this.path, - ); + StandaloneDeliveryPointForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String nameControlName = "name"; @@ -2466,22 +2792,28 @@ class StandaloneDeliveryPointForm final String? path; + // ignore: unused_field + final FormModel? _formModel; + final Map _disabled = {}; + @override + final Map initial; + String nameControlPath() => pathBuilder(nameControlName); String addressControlPath() => pathBuilder(addressControlName); String get _nameValue => nameControl.value ?? ""; - AddressOOutput? get _addressValue => addressForm.model; + AddressOOutput? get _addressValue => + containsAddress ? addressForm.model : null; String get _nameRawValue => nameControl.value ?? ""; - AddressO? get _addressRawValue => addressForm.rawModel; + AddressO? get _addressRawValue => + containsAddress ? addressForm.rawModel : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsName { try { form.control(nameControlPath()); @@ -2491,8 +2823,6 @@ class StandaloneDeliveryPointForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsAddress { try { form.control(addressControlPath()); @@ -2510,12 +2840,7 @@ class StandaloneDeliveryPointForm void get addressFocus => form.focus(addressControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void addressRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void addressRemove({bool updateParent = true, bool emitEvent = true}) { if (containsAddress) { final controlPath = path; if (controlPath == null) { @@ -2543,8 +2868,11 @@ class StandaloneDeliveryPointForm bool updateParent = true, bool emitEvent = true, }) { - nameControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + nameControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void addressValueUpdate( @@ -2552,8 +2880,11 @@ class StandaloneDeliveryPointForm bool updateParent = true, bool emitEvent = true, }) { - addressControl.updateValue(AddressOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + addressControl.updateValue( + AddressOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void nameValuePatch( @@ -2561,8 +2892,11 @@ class StandaloneDeliveryPointForm bool updateParent = true, bool emitEvent = true, }) { - nameControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + nameControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void addressValuePatch( @@ -2570,8 +2904,11 @@ class StandaloneDeliveryPointForm bool updateParent = true, bool emitEvent = true, }) { - addressControl.updateValue(AddressOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + addressControl.updateValue( + AddressOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void nameValueReset( @@ -2580,14 +2917,13 @@ class StandaloneDeliveryPointForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - nameControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => nameControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void addressValueReset( AddressO? value, { @@ -2595,11 +2931,11 @@ class StandaloneDeliveryPointForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - addressControl.reset( - value: AddressOForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => addressControl.reset( + value: AddressOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); FormControl get nameControl => form.control(nameControlPath()) as FormControl; @@ -2607,7 +2943,8 @@ class StandaloneDeliveryPointForm FormGroup get addressControl => form.control(addressControlPath()) as FormGroup; - AddressOForm get addressForm => AddressOForm(form, pathBuilder('address')); + AddressOForm get addressForm => + AddressOForm(form, pathBuilder('address'), _formModel ?? this); void nameSetDisabled( bool disabled, { @@ -2666,29 +3003,22 @@ class StandaloneDeliveryPointForm } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { - final currentFormInstance = currentForm; - - if (currentFormInstance is! FormGroup) { - return; - } - + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); addressForm.toggleDisabled(); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { addressForm.toggleDisabled(); - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { - currentFormInstance.controls[key]?.markAsEnabled( + currentForm.controls[key]?.markAsEnabled( updateParent: updateParent, emitEvent: emitEvent, ); @@ -2704,9 +3034,7 @@ class StandaloneDeliveryPointForm final currentForm = this.currentForm; return const DeepCollectionEquality().equals( - currentForm is FormControlCollection - ? currentForm.rawValue - : currentForm.value, + currentForm.rawValue, StandaloneDeliveryPointForm.formElements(other).rawValue, ); } @@ -2727,8 +3055,16 @@ class StandaloneDeliveryPointForm } @override - AbstractControl get currentForm { - return path == null ? form : form.control(path!); + bool get hasChanged { + return !const DeepCollectionEquality().equals( + currentForm.rawValue, + initial, + ); + } + + @override + FormGroup get currentForm { + return path == null ? form : form.control(path!) as FormGroup; } @override @@ -2736,44 +3072,109 @@ class StandaloneDeliveryPointForm DeliveryPointO? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue( - StandaloneDeliveryPointForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.updateValue( + StandaloneDeliveryPointForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void upsertValue( + DeliveryPointO? value, { + bool updateParent = true, + bool emitEvent = true, + }) { + final formElements = StandaloneDeliveryPointForm.formElements(value); + + currentForm.addAll(formElements.controls); + } @override void reset({ DeliveryPointO? value, bool updateParent = true, bool emitEvent = true, - }) => - form.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void updateInitial(Map? value, String? path) { + if (_formModel != null) { + _formModel?.updateInitial(currentForm.rawValue, path); + return; + } + + if (value == null) return; + + if (path == null || path.isEmpty) { + initial.addAll(value); + return; + } + + final keys = path.split('.'); + Object? current = initial; + for (var i = 0; i < keys.length - 1; i++) { + final key = keys[i]; + + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current = current[index]; + continue; + } + } + + if (current is Map) { + if (!current.containsKey(key)) { + current[key] = {}; + } + current = current[key]; + continue; + } + + return; + } + + final key = keys.last; + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current[index] = value; + } + } else if (current is Map) { + current[key] = value; + } + } String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(DeliveryPointO? deliveryPointO) => FormGroup({ - nameControlName: FormControl( - value: deliveryPointO?.name, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - addressControlName: AddressOForm.formElements(deliveryPointO?.address) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(DeliveryPointO? deliveryPointO) => FormGroup( + { + nameControlName: FormControl( + value: deliveryPointO?.name, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + addressControlName: AddressOForm.formElements(deliveryPointO?.address), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } class ReactiveStandaloneDeliveryPointFormArrayBuilder< - ReactiveStandaloneDeliveryPointFormArrayBuilderT> extends StatelessWidget { + ReactiveStandaloneDeliveryPointFormArrayBuilderT +> + extends StatelessWidget { const ReactiveStandaloneDeliveryPointFormArrayBuilder({ Key? key, this.control, @@ -2782,31 +3183,42 @@ class ReactiveStandaloneDeliveryPointFormArrayBuilder< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? - formControl; + formControl; final FormArray? Function( - StandaloneDeliveryPointForm formModel)? control; + StandaloneDeliveryPointForm formModel, + )? + control; - final Widget Function(BuildContext context, List itemList, - StandaloneDeliveryPointForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + StandaloneDeliveryPointForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveStandaloneDeliveryPointFormArrayBuilderT? item, - StandaloneDeliveryPointForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveStandaloneDeliveryPointFormArrayBuilderT? item, + StandaloneDeliveryPointForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl - control)? controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -2820,21 +3232,13 @@ class ReactiveStandaloneDeliveryPointFormArrayBuilder< final itemBuilder = this.itemBuilder; return ReactiveFormArrayItemBuilder< - ReactiveStandaloneDeliveryPointFormArrayBuilderT>( + ReactiveStandaloneDeliveryPointFormArrayBuilderT + >( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -2843,7 +3247,9 @@ class ReactiveStandaloneDeliveryPointFormArrayBuilder< } class ReactiveStandaloneDeliveryPointFormArrayBuilder2< - ReactiveStandaloneDeliveryPointFormArrayBuilderT> extends StatelessWidget { + ReactiveStandaloneDeliveryPointFormArrayBuilderT +> + extends StatelessWidget { const ReactiveStandaloneDeliveryPointFormArrayBuilder2({ Key? key, this.control, @@ -2852,37 +3258,48 @@ class ReactiveStandaloneDeliveryPointFormArrayBuilder2< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? - formControl; + formControl; final FormArray? Function( - StandaloneDeliveryPointForm formModel)? control; + StandaloneDeliveryPointForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - StandaloneDeliveryPointForm formModel - }) params)? builder; + ({ + BuildContext context, + List itemList, + StandaloneDeliveryPointForm formModel, + }) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveStandaloneDeliveryPointFormArrayBuilderT? item, - StandaloneDeliveryPointForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveStandaloneDeliveryPointFormArrayBuilderT? item, + StandaloneDeliveryPointForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl - control)? controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -2896,27 +3313,22 @@ class ReactiveStandaloneDeliveryPointFormArrayBuilder2< final itemBuilder = this.itemBuilder; return ReactiveFormArrayItemBuilder< - ReactiveStandaloneDeliveryPointFormArrayBuilderT>( + ReactiveStandaloneDeliveryPointFormArrayBuilderT + >( formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -2925,7 +3337,8 @@ class ReactiveStandaloneDeliveryPointFormArrayBuilder2< } class ReactiveStandaloneDeliveryPointFormFormGroupArrayBuilder< - ReactiveStandaloneDeliveryPointFormFormGroupArrayBuilderT> + ReactiveStandaloneDeliveryPointFormFormGroupArrayBuilderT +> extends StatelessWidget { const ReactiveStandaloneDeliveryPointFormFormGroupArrayBuilder({ Key? key, @@ -2933,26 +3346,39 @@ class ReactiveStandaloneDeliveryPointFormFormGroupArrayBuilder< this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? - extended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(StandaloneDeliveryPointForm formModel)? + getExtended; - final ExtendedControl?>, - List> - Function(StandaloneDeliveryPointForm formModel)? getExtended; - - final Widget Function(BuildContext context, List itemList, - StandaloneDeliveryPointForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + StandaloneDeliveryPointForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveStandaloneDeliveryPointFormFormGroupArrayBuilderT? item, - StandaloneDeliveryPointForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveStandaloneDeliveryPointFormFormGroupArrayBuilderT? item, + StandaloneDeliveryPointForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -2967,26 +3393,20 @@ class ReactiveStandaloneDeliveryPointFormFormGroupArrayBuilder< return StreamBuilder?>?>( stream: value.control.valueChanges, builder: (context, snapshot) { - final itemList = (value.value() ?? - []) - .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) - .values - .toList(); - - return builder?.call( - context, - itemList, - formModel, - ) ?? + final itemList = + (value.value() ?? + < + ReactiveStandaloneDeliveryPointFormFormGroupArrayBuilderT + >[]) + .asMap() + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) + .values + .toList(); + + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/generator_tests/test/doc/delivery_list_test.dart b/packages/generator_tests/test/doc/delivery_list_test.dart index 38cb5659..592d3ae0 100644 --- a/packages/generator_tests/test/doc/delivery_list_test.dart +++ b/packages/generator_tests/test/doc/delivery_list_test.dart @@ -105,7 +105,11 @@ class ReactiveDeliveryListFormConsumer extends StatelessWidget { final Widget? child; final Widget Function( - BuildContext context, DeliveryListForm formModel, Widget? child) builder; + BuildContext context, + DeliveryListForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -124,11 +128,7 @@ class DeliveryListFormInheritedStreamer extends InheritedStreamer { required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final DeliveryListForm form; } @@ -150,19 +150,19 @@ class ReactiveDeliveryListForm extends StatelessWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - static DeliveryListForm? of( - BuildContext context, { - bool listen = true, - }) { + static DeliveryListForm? of(BuildContext context, {bool listen = true}) { if (listen) { return context .dependOnInheritedWidgetOfExactType< - DeliveryListFormInheritedStreamer>() + DeliveryListFormInheritedStreamer + >() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - DeliveryListFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + DeliveryListFormInheritedStreamer + >(); return element == null ? null : (element.widget as DeliveryListFormInheritedStreamer).form; @@ -210,10 +210,14 @@ class DeliveryListFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, DeliveryListForm formModel, Widget? child) builder; + BuildContext context, + DeliveryListForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, DeliveryListForm formModel)? - initState; + initState; @override _DeliveryListFormBuilderState createState() => @@ -227,8 +231,11 @@ class _DeliveryListFormBuilderState extends State { @override void initState() { - _formModel = - DeliveryListForm(DeliveryListForm.formElements(widget.model), null); + _formModel = DeliveryListForm( + DeliveryListForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -305,10 +312,8 @@ class _DeliveryListFormBuilderState extends State { final _logDeliveryListForm = Logger.detached('DeliveryListForm'); class DeliveryListForm implements FormModel { - DeliveryListForm( - this.form, - this.path, - ); + DeliveryListForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String deliveryListControlName = "deliveryList"; @@ -318,8 +323,14 @@ class DeliveryListForm implements FormModel { final String? path; + // ignore: unused_field + final FormModel? _formModel; + final Map _disabled = {}; + @override + final Map initial; + String deliveryListControlPath() => pathBuilder(deliveryListControlName); String clientListControlPath() => pathBuilder(clientListControlName); @@ -327,17 +338,17 @@ class DeliveryListForm implements FormModel { List get _deliveryListValue => deliveryListDeliveryPointForm.map((e) => e.model).toList(); - List? get _clientListValue => - clientListClientForm.map((e) => e.model).toList(); + List? get _clientListValue => containsClientList + ? clientListClientForm.map((e) => e.model).toList() + : null; List get _deliveryListRawValue => deliveryListDeliveryPointForm.map((e) => e.rawModel).toList(); - List? get _clientListRawValue => - clientListClientForm.map((e) => e.rawModel).toList(); + List? get _clientListRawValue => containsClientList + ? clientListClientForm.map((e) => e.rawModel).toList() + : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsDeliveryList { try { form.control(deliveryListControlPath()); @@ -347,8 +358,6 @@ class DeliveryListForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsClientList { try { form.control(clientListControlPath()); @@ -366,12 +375,7 @@ class DeliveryListForm implements FormModel { void get clientListFocus => form.focus(clientListControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void clientListRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void clientListRemove({bool updateParent = true, bool emitEvent = true}) { if (containsClientList) { final controlPath = path; if (controlPath == null) { @@ -422,17 +426,21 @@ class DeliveryListForm implements FormModel { if (toUpdate.isNotEmpty) { deliveryListControl.updateValue( - toUpdate - .map((e) => DeliveryPointForm.formElements(e).rawValue) - .toList(), - updateParent: updateParent, - emitEvent: emitEvent); + toUpdate + .map((e) => DeliveryPointForm.formElements(e).rawValue) + .toList(), + updateParent: updateParent, + emitEvent: emitEvent, + ); } if (toAdd.isNotEmpty) { toAdd.forEach((e) { - deliveryListControl.add(DeliveryPointForm.formElements(e), - updateParent: updateParent, emitEvent: emitEvent); + deliveryListControl.add( + DeliveryPointForm.formElements(e), + updateParent: updateParent, + emitEvent: emitEvent, + ); }); } } @@ -465,15 +473,19 @@ class DeliveryListForm implements FormModel { if (toUpdate.isNotEmpty) { clientListControl.updateValue( - toUpdate.map((e) => ClientForm.formElements(e).rawValue).toList(), - updateParent: updateParent, - emitEvent: emitEvent); + toUpdate.map((e) => ClientForm.formElements(e).rawValue).toList(), + updateParent: updateParent, + emitEvent: emitEvent, + ); } if (toAdd.isNotEmpty) { toAdd.forEach((e) { - clientListControl.add(ClientForm.formElements(e), - updateParent: updateParent, emitEvent: emitEvent); + clientListControl.add( + ClientForm.formElements(e), + updateParent: updateParent, + emitEvent: emitEvent, + ); }); } } @@ -518,18 +530,12 @@ class DeliveryListForm implements FormModel { ); } - void deliveryListClear({ - bool updateParent = true, - bool emitEvent = true, - }) { + void deliveryListClear({bool updateParent = true, bool emitEvent = true}) { deliveryListDeliveryPointForm.clear(); deliveryListControl.clear(updateParent: updateParent, emitEvent: emitEvent); } - void clientListClear({ - bool updateParent = true, - bool emitEvent = true, - }) { + void clientListClear({bool updateParent = true, bool emitEvent = true}) { clientListClientForm.clear(); clientListControl.clear(updateParent: updateParent, emitEvent: emitEvent); } @@ -542,18 +548,17 @@ class DeliveryListForm implements FormModel { final keys = deliveryListDeliveryPointForm.asMap().keys; final toPatch = []; - (value).asMap().forEach( - (k, v) { - if (keys.contains(k)) { - toPatch.add(v); - } - }, - ); + (value).asMap().forEach((k, v) { + if (keys.contains(k)) { + toPatch.add(v); + } + }); deliveryListControl.patchValue( - toPatch.map((e) => DeliveryPointForm.formElements(e).rawValue).toList(), - updateParent: updateParent, - emitEvent: emitEvent); + toPatch.map((e) => DeliveryPointForm.formElements(e).rawValue).toList(), + updateParent: updateParent, + emitEvent: emitEvent, + ); } void clientListValuePatch( @@ -564,18 +569,17 @@ class DeliveryListForm implements FormModel { final keys = clientListClientForm.asMap().keys; final toPatch = []; - (value ?? []).asMap().forEach( - (k, v) { - if (keys.contains(k)) { - toPatch.add(v); - } - }, - ); + (value ?? []).asMap().forEach((k, v) { + if (keys.contains(k)) { + toPatch.add(v); + } + }); clientListControl.patchValue( - toPatch.map((e) => ClientForm.formElements(e).rawValue).toList(), - updateParent: updateParent, - emitEvent: emitEvent); + toPatch.map((e) => ClientForm.formElements(e).rawValue).toList(), + updateParent: updateParent, + emitEvent: emitEvent, + ); } void deliveryListValueReset( @@ -584,13 +588,13 @@ class DeliveryListForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - deliveryListControl.reset( - value: value - .map((e) => DeliveryPointForm.formElements(e).rawValue) - .toList(), - updateParent: updateParent, - emitEvent: emitEvent); + }) => deliveryListControl.reset( + value: value + .map((e) => DeliveryPointForm.formElements(e).rawValue) + .toList(), + updateParent: updateParent, + emitEvent: emitEvent, + ); void clientListValueReset( List? value, { @@ -598,12 +602,11 @@ class DeliveryListForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - clientListControl.reset( - value: - value?.map((e) => ClientForm.formElements(e).rawValue).toList(), - updateParent: updateParent, - emitEvent: emitEvent); + }) => clientListControl.reset( + value: value?.map((e) => ClientForm.formElements(e).rawValue).toList(), + updateParent: updateParent, + emitEvent: emitEvent, + ); FormArray> get deliveryListControl => form.control(deliveryListControlPath()) @@ -617,8 +620,16 @@ class DeliveryListForm implements FormModel { return values .asMap() - .map((k, v) => MapEntry( - k, DeliveryPointForm(form, pathBuilder("deliveryList.$k")))) + .map( + (k, v) => MapEntry( + k, + DeliveryPointForm( + form, + pathBuilder("deliveryList.$k"), + _formModel ?? this, + ), + ), + ) .values .toList(); } @@ -628,8 +639,12 @@ class DeliveryListForm implements FormModel { return values .asMap() - .map((k, v) => - MapEntry(k, ClientForm(form, pathBuilder("clientList.$k")))) + .map( + (k, v) => MapEntry( + k, + ClientForm(form, pathBuilder("clientList.$k"), _formModel ?? this), + ), + ) .values .toList(); } @@ -671,18 +686,20 @@ class DeliveryListForm implements FormModel { } ExtendedControl?>, List> - get deliveryListExtendedControl => - ExtendedControl?>, List>( - form.control(deliveryListControlPath()) - as FormArray>, - () => deliveryListDeliveryPointForm); + get deliveryListExtendedControl => + ExtendedControl?>, List>( + form.control(deliveryListControlPath()) + as FormArray>, + () => deliveryListDeliveryPointForm, + ); ExtendedControl?>, List> - get clientListExtendedControl => - ExtendedControl?>, List>( - form.control(clientListControlPath()) - as FormArray>, - () => clientListClientForm); + get clientListExtendedControl => + ExtendedControl?>, List>( + form.control(clientListControlPath()) + as FormArray>, + () => clientListClientForm, + ); void addDeliveryListItem(DeliveryPoint value) { deliveryListControl.add(DeliveryPointForm.formElements(value)); @@ -724,28 +741,23 @@ class DeliveryListForm implements FormModel { ); } return DeliveryList( - deliveryList: _deliveryListValue, clientList: _clientListValue); + deliveryList: _deliveryListValue, + clientList: _clientListValue, + ); } @override DeliveryList get rawModel { return DeliveryList( - deliveryList: _deliveryListRawValue, clientList: _clientListRawValue); + deliveryList: _deliveryListRawValue, + clientList: _clientListRawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { - final currentFormInstance = currentForm; - - if (currentFormInstance is! FormGroup) { - return; - } - + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); @@ -753,14 +765,16 @@ class DeliveryListForm implements FormModel { clientListClientForm.forEach((e) => e.toggleDisabled()); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { deliveryListDeliveryPointForm.forEach((e) => e.toggleDisabled()); clientListClientForm.forEach((e) => e.toggleDisabled()); - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { - currentFormInstance.controls[key]?.markAsEnabled( + currentForm.controls[key]?.markAsEnabled( updateParent: updateParent, emitEvent: emitEvent, ); @@ -776,9 +790,7 @@ class DeliveryListForm implements FormModel { final currentForm = this.currentForm; return const DeepCollectionEquality().equals( - currentForm is FormControlCollection - ? currentForm.rawValue - : currentForm.value, + currentForm.rawValue, DeliveryListForm.formElements(other).rawValue, ); } @@ -799,8 +811,16 @@ class DeliveryListForm implements FormModel { } @override - AbstractControl get currentForm { - return path == null ? form : form.control(path!); + bool get hasChanged { + return !const DeepCollectionEquality().equals( + currentForm.rawValue, + initial, + ); + } + + @override + FormGroup get currentForm { + return path == null ? form : form.control(path!) as FormGroup; } @override @@ -808,55 +828,119 @@ class DeliveryListForm implements FormModel { DeliveryList? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(DeliveryListForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + DeliveryListForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void upsertValue( + DeliveryList? value, { + bool updateParent = true, + bool emitEvent = true, + }) { + final formElements = DeliveryListForm.formElements(value); + + currentForm.addAll(formElements.controls); + } @override void reset({ DeliveryList? value, bool updateParent = true, bool emitEvent = true, - }) => - form.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void updateInitial(Map? value, String? path) { + if (_formModel != null) { + _formModel?.updateInitial(currentForm.rawValue, path); + return; + } + + if (value == null) return; + + if (path == null || path.isEmpty) { + initial.addAll(value); + return; + } + + final keys = path.split('.'); + Object? current = initial; + for (var i = 0; i < keys.length - 1; i++) { + final key = keys[i]; + + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current = current[index]; + continue; + } + } + + if (current is Map) { + if (!current.containsKey(key)) { + current[key] = {}; + } + current = current[key]; + continue; + } + + return; + } + + final key = keys.last; + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current[index] = value; + } + } else if (current is Map) { + current[key] = value; + } + } String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(DeliveryList? deliveryList) => FormGroup({ - deliveryListControlName: FormArray( - (deliveryList?.deliveryList ?? []) - .map((e) => DeliveryPointForm.formElements(e)) - .toList(), - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false), - clientListControlName: FormArray( - (deliveryList?.clientList ?? []) - .map((e) => ClientForm.formElements(e)) - .toList(), - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(DeliveryList? deliveryList) => FormGroup( + { + deliveryListControlName: FormArray( + (deliveryList?.deliveryList ?? []) + .map((e) => DeliveryPointForm.formElements(e)) + .toList(), + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ), + clientListControlName: FormArray( + (deliveryList?.clientList ?? []) + .map((e) => ClientForm.formElements(e)) + .toList(), + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } final _logDeliveryPointForm = Logger.detached('DeliveryPointForm'); class DeliveryPointForm implements FormModel { - DeliveryPointForm( - this.form, - this.path, - ); + DeliveryPointForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String nameControlName = "name"; @@ -866,22 +950,27 @@ class DeliveryPointForm implements FormModel { final String? path; + // ignore: unused_field + final FormModel? _formModel; + final Map _disabled = {}; + @override + final Map initial; + String nameControlPath() => pathBuilder(nameControlName); String addressControlPath() => pathBuilder(addressControlName); String get _nameValue => nameControl.value ?? ""; - Address? get _addressValue => addressForm.model; + Address? get _addressValue => containsAddress ? addressForm.model : null; String get _nameRawValue => nameControl.value ?? ""; - Address? get _addressRawValue => addressForm.rawModel; + Address? get _addressRawValue => + containsAddress ? addressForm.rawModel : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsName { try { form.control(nameControlPath()); @@ -891,8 +980,6 @@ class DeliveryPointForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsAddress { try { form.control(addressControlPath()); @@ -910,12 +997,7 @@ class DeliveryPointForm implements FormModel { void get addressFocus => form.focus(addressControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void addressRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void addressRemove({bool updateParent = true, bool emitEvent = true}) { if (containsAddress) { final controlPath = path; if (controlPath == null) { @@ -943,8 +1025,11 @@ class DeliveryPointForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - nameControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + nameControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void addressValueUpdate( @@ -952,8 +1037,11 @@ class DeliveryPointForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - addressControl.updateValue(AddressForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + addressControl.updateValue( + AddressForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void nameValuePatch( @@ -961,8 +1049,11 @@ class DeliveryPointForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - nameControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + nameControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void addressValuePatch( @@ -970,8 +1061,11 @@ class DeliveryPointForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - addressControl.updateValue(AddressForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + addressControl.updateValue( + AddressForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void nameValueReset( @@ -980,14 +1074,13 @@ class DeliveryPointForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - nameControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => nameControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void addressValueReset( Address? value, { @@ -995,11 +1088,11 @@ class DeliveryPointForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - addressControl.reset( - value: AddressForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => addressControl.reset( + value: AddressForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); FormControl get nameControl => form.control(nameControlPath()) as FormControl; @@ -1007,7 +1100,8 @@ class DeliveryPointForm implements FormModel { FormGroup get addressControl => form.control(addressControlPath()) as FormGroup; - AddressForm get addressForm => AddressForm(form, pathBuilder('address')); + AddressForm get addressForm => + AddressForm(form, pathBuilder('address'), _formModel ?? this); void nameSetDisabled( bool disabled, { @@ -1065,29 +1159,22 @@ class DeliveryPointForm implements FormModel { } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { - final currentFormInstance = currentForm; - - if (currentFormInstance is! FormGroup) { - return; - } - + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); addressForm.toggleDisabled(); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { addressForm.toggleDisabled(); - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { - currentFormInstance.controls[key]?.markAsEnabled( + currentForm.controls[key]?.markAsEnabled( updateParent: updateParent, emitEvent: emitEvent, ); @@ -1103,9 +1190,7 @@ class DeliveryPointForm implements FormModel { final currentForm = this.currentForm; return const DeepCollectionEquality().equals( - currentForm is FormControlCollection - ? currentForm.rawValue - : currentForm.value, + currentForm.rawValue, DeliveryPointForm.formElements(other).rawValue, ); } @@ -1126,8 +1211,16 @@ class DeliveryPointForm implements FormModel { } @override - AbstractControl get currentForm { - return path == null ? form : form.control(path!); + bool get hasChanged { + return !const DeepCollectionEquality().equals( + currentForm.rawValue, + initial, + ); + } + + @override + FormGroup get currentForm { + return path == null ? form : form.control(path!) as FormGroup; } @override @@ -1135,47 +1228,109 @@ class DeliveryPointForm implements FormModel { DeliveryPoint? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(DeliveryPointForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + DeliveryPointForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void upsertValue( + DeliveryPoint? value, { + bool updateParent = true, + bool emitEvent = true, + }) { + final formElements = DeliveryPointForm.formElements(value); + + currentForm.addAll(formElements.controls); + } @override void reset({ DeliveryPoint? value, bool updateParent = true, bool emitEvent = true, - }) => - form.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void updateInitial(Map? value, String? path) { + if (_formModel != null) { + _formModel?.updateInitial(currentForm.rawValue, path); + return; + } + + if (value == null) return; + + if (path == null || path.isEmpty) { + initial.addAll(value); + return; + } + + final keys = path.split('.'); + Object? current = initial; + for (var i = 0; i < keys.length - 1; i++) { + final key = keys[i]; + + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current = current[index]; + continue; + } + } + + if (current is Map) { + if (!current.containsKey(key)) { + current[key] = {}; + } + current = current[key]; + continue; + } + + return; + } + + final key = keys.last; + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current[index] = value; + } + } else if (current is Map) { + current[key] = value; + } + } String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(DeliveryPoint? deliveryPoint) => FormGroup({ - nameControlName: FormControl( - value: deliveryPoint?.name, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - addressControlName: AddressForm.formElements(deliveryPoint?.address) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(DeliveryPoint? deliveryPoint) => FormGroup( + { + nameControlName: FormControl( + value: deliveryPoint?.name, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + addressControlName: AddressForm.formElements(deliveryPoint?.address), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } final _logAddressForm = Logger.detached('AddressForm'); class AddressForm implements FormModel { - AddressForm( - this.form, - this.path, - ); + AddressForm(this.form, this.path, this._formModel) : initial = form.rawValue; static const String streetControlName = "street"; @@ -1185,22 +1340,26 @@ class AddressForm implements FormModel { final String? path; + // ignore: unused_field + final FormModel? _formModel; + final Map _disabled = {}; + @override + final Map initial; + String streetControlPath() => pathBuilder(streetControlName); String cityControlPath() => pathBuilder(cityControlName); - String? get _streetValue => streetControl.value; + String? get _streetValue => containsStreet ? streetControl.value : null; - String? get _cityValue => cityControl.value; + String? get _cityValue => containsCity ? cityControl.value : null; - String? get _streetRawValue => streetControl.value; + String? get _streetRawValue => containsStreet ? streetControl.value : null; - String? get _cityRawValue => cityControl.value; + String? get _cityRawValue => containsCity ? cityControl.value : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsStreet { try { form.control(streetControlPath()); @@ -1210,8 +1369,6 @@ class AddressForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsCity { try { form.control(cityControlPath()); @@ -1229,12 +1386,7 @@ class AddressForm implements FormModel { void get cityFocus => form.focus(cityControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void streetRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void streetRemove({bool updateParent = true, bool emitEvent = true}) { if (containsStreet) { final controlPath = path; if (controlPath == null) { @@ -1257,12 +1409,7 @@ class AddressForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void cityRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void cityRemove({bool updateParent = true, bool emitEvent = true}) { if (containsCity) { final controlPath = path; if (controlPath == null) { @@ -1290,8 +1437,11 @@ class AddressForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - streetControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + streetControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void cityValueUpdate( @@ -1299,8 +1449,11 @@ class AddressForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - cityControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + cityControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void streetValuePatch( @@ -1308,8 +1461,11 @@ class AddressForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - streetControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + streetControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void cityValuePatch( @@ -1317,8 +1473,11 @@ class AddressForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - cityControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + cityControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void streetValueReset( @@ -1327,14 +1486,13 @@ class AddressForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - streetControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => streetControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void cityValueReset( String? value, { @@ -1342,14 +1500,13 @@ class AddressForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - cityControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => cityControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get streetControl => form.control(streetControlPath()) as FormControl; @@ -1413,27 +1570,20 @@ class AddressForm implements FormModel { } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { - final currentFormInstance = currentForm; - - if (currentFormInstance is! FormGroup) { - return; - } - + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { - currentFormInstance.controls[key]?.markAsEnabled( + currentForm.controls[key]?.markAsEnabled( updateParent: updateParent, emitEvent: emitEvent, ); @@ -1449,9 +1599,7 @@ class AddressForm implements FormModel { final currentForm = this.currentForm; return const DeepCollectionEquality().equals( - currentForm is FormControlCollection - ? currentForm.rawValue - : currentForm.value, + currentForm.rawValue, AddressForm.formElements(other).rawValue, ); } @@ -1472,8 +1620,16 @@ class AddressForm implements FormModel { } @override - AbstractControl get currentForm { - return path == null ? form : form.control(path!); + bool get hasChanged { + return !const DeepCollectionEquality().equals( + currentForm.rawValue, + initial, + ); + } + + @override + FormGroup get currentForm { + return path == null ? form : form.control(path!) as FormGroup; } @override @@ -1481,53 +1637,116 @@ class AddressForm implements FormModel { Address? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(AddressForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + AddressForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void upsertValue( + Address? value, { + bool updateParent = true, + bool emitEvent = true, + }) { + final formElements = AddressForm.formElements(value); + + currentForm.addAll(formElements.controls); + } @override void reset({ Address? value, bool updateParent = true, bool emitEvent = true, - }) => - form.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void updateInitial(Map? value, String? path) { + if (_formModel != null) { + _formModel?.updateInitial(currentForm.rawValue, path); + return; + } + + if (value == null) return; + + if (path == null || path.isEmpty) { + initial.addAll(value); + return; + } + + final keys = path.split('.'); + Object? current = initial; + for (var i = 0; i < keys.length - 1; i++) { + final key = keys[i]; + + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current = current[index]; + continue; + } + } + + if (current is Map) { + if (!current.containsKey(key)) { + current[key] = {}; + } + current = current[key]; + continue; + } + + return; + } + + final key = keys.last; + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current[index] = value; + } + } else if (current is Map) { + current[key] = value; + } + } String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(Address? address) => FormGroup({ - streetControlName: FormControl( - value: address?.street, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - cityControlName: FormControl( - value: address?.city, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(Address? address) => FormGroup( + { + streetControlName: FormControl( + value: address?.street, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + cityControlName: FormControl( + value: address?.city, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } final _logClientForm = Logger.detached('ClientForm'); class ClientForm implements FormModel { - ClientForm( - this.form, - this.path, - ); + ClientForm(this.form, this.path, this._formModel) : initial = form.rawValue; static const String clientTypeControlName = "clientType"; @@ -1539,8 +1758,14 @@ class ClientForm implements FormModel { final String? path; + // ignore: unused_field + final FormModel? _formModel; + final Map _disabled = {}; + @override + final Map initial; + String clientTypeControlPath() => pathBuilder(clientTypeControlName); String nameControlPath() => pathBuilder(nameControlName); @@ -1549,18 +1774,16 @@ class ClientForm implements FormModel { ClientType get _clientTypeValue => clientTypeControl.value as ClientType; - String? get _nameValue => nameControl.value; + String? get _nameValue => containsName ? nameControl.value : null; - String? get _notesValue => notesControl.value; + String? get _notesValue => containsNotes ? notesControl.value : null; ClientType get _clientTypeRawValue => clientTypeControl.value as ClientType; - String? get _nameRawValue => nameControl.value; + String? get _nameRawValue => containsName ? nameControl.value : null; - String? get _notesRawValue => notesControl.value; + String? get _notesRawValue => containsNotes ? notesControl.value : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsClientType { try { form.control(clientTypeControlPath()); @@ -1570,8 +1793,6 @@ class ClientForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsName { try { form.control(nameControlPath()); @@ -1581,8 +1802,6 @@ class ClientForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsNotes { try { form.control(notesControlPath()); @@ -1604,12 +1823,7 @@ class ClientForm implements FormModel { void get notesFocus => form.focus(notesControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void nameRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void nameRemove({bool updateParent = true, bool emitEvent = true}) { if (containsName) { final controlPath = path; if (controlPath == null) { @@ -1632,12 +1846,7 @@ class ClientForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void notesRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void notesRemove({bool updateParent = true, bool emitEvent = true}) { if (containsNotes) { final controlPath = path; if (controlPath == null) { @@ -1665,8 +1874,11 @@ class ClientForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - clientTypeControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + clientTypeControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void nameValueUpdate( @@ -1674,8 +1886,11 @@ class ClientForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - nameControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + nameControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void notesValueUpdate( @@ -1683,8 +1898,11 @@ class ClientForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - notesControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + notesControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void clientTypeValuePatch( @@ -1692,8 +1910,11 @@ class ClientForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - clientTypeControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + clientTypeControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void nameValuePatch( @@ -1701,8 +1922,11 @@ class ClientForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - nameControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + nameControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void notesValuePatch( @@ -1710,8 +1934,11 @@ class ClientForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - notesControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + notesControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void clientTypeValueReset( @@ -1720,14 +1947,13 @@ class ClientForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - clientTypeControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => clientTypeControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void nameValueReset( String? value, { @@ -1735,14 +1961,13 @@ class ClientForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - nameControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => nameControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void notesValueReset( String? value, { @@ -1750,14 +1975,13 @@ class ClientForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - notesControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => notesControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get clientTypeControl => form.control(clientTypeControlPath()) as FormControl; @@ -1834,39 +2058,36 @@ class ClientForm implements FormModel { ); } return Client( - clientType: _clientTypeValue, name: _nameValue, notes: _notesValue); + clientType: _clientTypeValue, + name: _nameValue, + notes: _notesValue, + ); } @override Client get rawModel { return Client( - clientType: _clientTypeRawValue, - name: _nameRawValue, - notes: _notesRawValue); + clientType: _clientTypeRawValue, + name: _nameRawValue, + notes: _notesRawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { - final currentFormInstance = currentForm; - - if (currentFormInstance is! FormGroup) { - return; - } - + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { - currentFormInstance.controls[key]?.markAsEnabled( + currentForm.controls[key]?.markAsEnabled( updateParent: updateParent, emitEvent: emitEvent, ); @@ -1882,9 +2103,7 @@ class ClientForm implements FormModel { final currentForm = this.currentForm; return const DeepCollectionEquality().equals( - currentForm is FormControlCollection - ? currentForm.rawValue - : currentForm.value, + currentForm.rawValue, ClientForm.formElements(other).rawValue, ); } @@ -1905,8 +2124,16 @@ class ClientForm implements FormModel { } @override - AbstractControl get currentForm { - return path == null ? form : form.control(path!); + bool get hasChanged { + return !const DeepCollectionEquality().equals( + currentForm.rawValue, + initial, + ); + } + + @override + FormGroup get currentForm { + return path == null ? form : form.control(path!) as FormGroup; } @override @@ -1914,55 +2141,124 @@ class ClientForm implements FormModel { Client? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(ClientForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + ClientForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void upsertValue( + Client? value, { + bool updateParent = true, + bool emitEvent = true, + }) { + final formElements = ClientForm.formElements(value); + + currentForm.addAll(formElements.controls); + } @override void reset({ Client? value, bool updateParent = true, bool emitEvent = true, - }) => - form.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void updateInitial(Map? value, String? path) { + if (_formModel != null) { + _formModel?.updateInitial(currentForm.rawValue, path); + return; + } + + if (value == null) return; + + if (path == null || path.isEmpty) { + initial.addAll(value); + return; + } + + final keys = path.split('.'); + Object? current = initial; + for (var i = 0; i < keys.length - 1; i++) { + final key = keys[i]; + + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current = current[index]; + continue; + } + } + + if (current is Map) { + if (!current.containsKey(key)) { + current[key] = {}; + } + current = current[key]; + continue; + } + + return; + } + + final key = keys.last; + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current[index] = value; + } + } else if (current is Map) { + current[key] = value; + } + } String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(Client? client) => FormGroup({ - clientTypeControlName: FormControl( - value: client?.clientType, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - nameControlName: FormControl( - value: client?.name, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - notesControlName: FormControl( - value: client?.notes, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(Client? client) => FormGroup( + { + clientTypeControlName: FormControl( + value: client?.clientType, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + nameControlName: FormControl( + value: client?.name, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + notesControlName: FormControl( + value: client?.notes, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } class ReactiveDeliveryListFormArrayBuilder< - ReactiveDeliveryListFormArrayBuilderT> extends StatelessWidget { + ReactiveDeliveryListFormArrayBuilderT +> + extends StatelessWidget { const ReactiveDeliveryListFormArrayBuilder({ Key? key, this.control, @@ -1971,30 +2267,41 @@ class ReactiveDeliveryListFormArrayBuilder< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - DeliveryListForm formModel)? control; + DeliveryListForm formModel, + )? + control; - final Widget Function(BuildContext context, List itemList, - DeliveryListForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + DeliveryListForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveDeliveryListFormArrayBuilderT? item, - DeliveryListForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveDeliveryListFormArrayBuilderT? item, + DeliveryListForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -2010,18 +2317,9 @@ class ReactiveDeliveryListFormArrayBuilder< return ReactiveFormArrayItemBuilder( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -2030,7 +2328,9 @@ class ReactiveDeliveryListFormArrayBuilder< } class ReactiveDeliveryListFormArrayBuilder2< - ReactiveDeliveryListFormArrayBuilderT> extends StatelessWidget { + ReactiveDeliveryListFormArrayBuilderT +> + extends StatelessWidget { const ReactiveDeliveryListFormArrayBuilder2({ Key? key, this.control, @@ -2039,36 +2339,43 @@ class ReactiveDeliveryListFormArrayBuilder2< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - DeliveryListForm formModel)? control; + DeliveryListForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - DeliveryListForm formModel - }) params)? builder; + ({BuildContext context, List itemList, DeliveryListForm formModel}) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveDeliveryListFormArrayBuilderT? item, - DeliveryListForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveDeliveryListFormArrayBuilderT? item, + DeliveryListForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -2085,23 +2392,17 @@ class ReactiveDeliveryListFormArrayBuilder2< formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -2110,32 +2411,48 @@ class ReactiveDeliveryListFormArrayBuilder2< } class ReactiveDeliveryListFormFormGroupArrayBuilder< - ReactiveDeliveryListFormFormGroupArrayBuilderT> extends StatelessWidget { + ReactiveDeliveryListFormFormGroupArrayBuilderT +> + extends StatelessWidget { const ReactiveDeliveryListFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? extended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(DeliveryListForm formModel)? + getExtended; - final ExtendedControl?>, - List> - Function(DeliveryListForm formModel)? getExtended; - - final Widget Function(BuildContext context, List itemList, - DeliveryListForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + DeliveryListForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveDeliveryListFormFormGroupArrayBuilderT? item, - DeliveryListForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveDeliveryListFormFormGroupArrayBuilderT? item, + DeliveryListForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -2150,26 +2467,18 @@ class ReactiveDeliveryListFormFormGroupArrayBuilder< return StreamBuilder?>?>( stream: value.control.valueChanges, builder: (context, snapshot) { - final itemList = (value.value() ?? - []) - .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) - .values - .toList(); - - return builder?.call( - context, - itemList, - formModel, - ) ?? + final itemList = + (value.value() ?? + []) + .asMap() + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) + .values + .toList(); + + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); @@ -2185,8 +2494,12 @@ class ReactiveStandaloneDeliveryPointFormConsumer extends StatelessWidget { final Widget? child; - final Widget Function(BuildContext context, - StandaloneDeliveryPointForm formModel, Widget? child) builder; + final Widget Function( + BuildContext context, + StandaloneDeliveryPointForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -2206,11 +2519,7 @@ class StandaloneDeliveryPointFormInheritedStreamer required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final StandaloneDeliveryPointForm form; } @@ -2239,12 +2548,15 @@ class ReactiveStandaloneDeliveryPointForm extends StatelessWidget { if (listen) { return context .dependOnInheritedWidgetOfExactType< - StandaloneDeliveryPointFormInheritedStreamer>() + StandaloneDeliveryPointFormInheritedStreamer + >() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - StandaloneDeliveryPointFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + StandaloneDeliveryPointFormInheritedStreamer + >(); return element == null ? null : (element.widget as StandaloneDeliveryPointFormInheritedStreamer).form; @@ -2291,11 +2603,18 @@ class StandaloneDeliveryPointFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - final Widget Function(BuildContext context, - StandaloneDeliveryPointForm formModel, Widget? child) builder; + final Widget Function( + BuildContext context, + StandaloneDeliveryPointForm formModel, + Widget? child, + ) + builder; final void Function( - BuildContext context, StandaloneDeliveryPointForm formModel)? initState; + BuildContext context, + StandaloneDeliveryPointForm formModel, + )? + initState; @override _StandaloneDeliveryPointFormBuilderState createState() => @@ -2311,7 +2630,10 @@ class _StandaloneDeliveryPointFormBuilderState @override void initState() { _formModel = StandaloneDeliveryPointForm( - StandaloneDeliveryPointForm.formElements(widget.model), null); + StandaloneDeliveryPointForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -2319,8 +2641,9 @@ class _StandaloneDeliveryPointFormBuilderState widget.initState?.call(context, _formModel); - _logSubscription = - _logStandaloneDeliveryPointForm.onRecord.listen((LogRecord e) { + _logSubscription = _logStandaloneDeliveryPointForm.onRecord.listen(( + LogRecord e, + ) { // use `dumpErrorToConsole` for severe messages to ensure that severe // exceptions are formatted consistently with other Flutter examples and // avoids printing duplicate exceptions @@ -2386,15 +2709,14 @@ class _StandaloneDeliveryPointFormBuilderState } } -final _logStandaloneDeliveryPointForm = - Logger.detached('StandaloneDeliveryPointForm'); +final _logStandaloneDeliveryPointForm = Logger.detached( + 'StandaloneDeliveryPointForm', +); class StandaloneDeliveryPointForm implements FormModel { - StandaloneDeliveryPointForm( - this.form, - this.path, - ); + StandaloneDeliveryPointForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String nameControlName = "name"; @@ -2404,22 +2726,27 @@ class StandaloneDeliveryPointForm final String? path; + // ignore: unused_field + final FormModel? _formModel; + final Map _disabled = {}; + @override + final Map initial; + String nameControlPath() => pathBuilder(nameControlName); String addressControlPath() => pathBuilder(addressControlName); String get _nameValue => nameControl.value ?? ""; - Address? get _addressValue => addressForm.model; + Address? get _addressValue => containsAddress ? addressForm.model : null; String get _nameRawValue => nameControl.value ?? ""; - Address? get _addressRawValue => addressForm.rawModel; + Address? get _addressRawValue => + containsAddress ? addressForm.rawModel : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsName { try { form.control(nameControlPath()); @@ -2429,8 +2756,6 @@ class StandaloneDeliveryPointForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsAddress { try { form.control(addressControlPath()); @@ -2448,12 +2773,7 @@ class StandaloneDeliveryPointForm void get addressFocus => form.focus(addressControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void addressRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void addressRemove({bool updateParent = true, bool emitEvent = true}) { if (containsAddress) { final controlPath = path; if (controlPath == null) { @@ -2481,8 +2801,11 @@ class StandaloneDeliveryPointForm bool updateParent = true, bool emitEvent = true, }) { - nameControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + nameControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void addressValueUpdate( @@ -2490,8 +2813,11 @@ class StandaloneDeliveryPointForm bool updateParent = true, bool emitEvent = true, }) { - addressControl.updateValue(AddressForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + addressControl.updateValue( + AddressForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void nameValuePatch( @@ -2499,8 +2825,11 @@ class StandaloneDeliveryPointForm bool updateParent = true, bool emitEvent = true, }) { - nameControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + nameControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void addressValuePatch( @@ -2508,8 +2837,11 @@ class StandaloneDeliveryPointForm bool updateParent = true, bool emitEvent = true, }) { - addressControl.updateValue(AddressForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + addressControl.updateValue( + AddressForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void nameValueReset( @@ -2518,14 +2850,13 @@ class StandaloneDeliveryPointForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - nameControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => nameControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void addressValueReset( Address? value, { @@ -2533,11 +2864,11 @@ class StandaloneDeliveryPointForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - addressControl.reset( - value: AddressForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => addressControl.reset( + value: AddressForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); FormControl get nameControl => form.control(nameControlPath()) as FormControl; @@ -2545,7 +2876,8 @@ class StandaloneDeliveryPointForm FormGroup get addressControl => form.control(addressControlPath()) as FormGroup; - AddressForm get addressForm => AddressForm(form, pathBuilder('address')); + AddressForm get addressForm => + AddressForm(form, pathBuilder('address'), _formModel ?? this); void nameSetDisabled( bool disabled, { @@ -2603,29 +2935,22 @@ class StandaloneDeliveryPointForm } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { - final currentFormInstance = currentForm; - - if (currentFormInstance is! FormGroup) { - return; - } - + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); addressForm.toggleDisabled(); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { addressForm.toggleDisabled(); - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { - currentFormInstance.controls[key]?.markAsEnabled( + currentForm.controls[key]?.markAsEnabled( updateParent: updateParent, emitEvent: emitEvent, ); @@ -2641,9 +2966,7 @@ class StandaloneDeliveryPointForm final currentForm = this.currentForm; return const DeepCollectionEquality().equals( - currentForm is FormControlCollection - ? currentForm.rawValue - : currentForm.value, + currentForm.rawValue, StandaloneDeliveryPointForm.formElements(other).rawValue, ); } @@ -2664,8 +2987,16 @@ class StandaloneDeliveryPointForm } @override - AbstractControl get currentForm { - return path == null ? form : form.control(path!); + bool get hasChanged { + return !const DeepCollectionEquality().equals( + currentForm.rawValue, + initial, + ); + } + + @override + FormGroup get currentForm { + return path == null ? form : form.control(path!) as FormGroup; } @override @@ -2673,44 +3004,109 @@ class StandaloneDeliveryPointForm DeliveryPoint? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue( - StandaloneDeliveryPointForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.updateValue( + StandaloneDeliveryPointForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void upsertValue( + DeliveryPoint? value, { + bool updateParent = true, + bool emitEvent = true, + }) { + final formElements = StandaloneDeliveryPointForm.formElements(value); + + currentForm.addAll(formElements.controls); + } @override void reset({ DeliveryPoint? value, bool updateParent = true, bool emitEvent = true, - }) => - form.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void updateInitial(Map? value, String? path) { + if (_formModel != null) { + _formModel?.updateInitial(currentForm.rawValue, path); + return; + } + + if (value == null) return; + + if (path == null || path.isEmpty) { + initial.addAll(value); + return; + } + + final keys = path.split('.'); + Object? current = initial; + for (var i = 0; i < keys.length - 1; i++) { + final key = keys[i]; + + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current = current[index]; + continue; + } + } + + if (current is Map) { + if (!current.containsKey(key)) { + current[key] = {}; + } + current = current[key]; + continue; + } + + return; + } + + final key = keys.last; + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current[index] = value; + } + } else if (current is Map) { + current[key] = value; + } + } String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(DeliveryPoint? deliveryPoint) => FormGroup({ - nameControlName: FormControl( - value: deliveryPoint?.name, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - addressControlName: AddressForm.formElements(deliveryPoint?.address) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(DeliveryPoint? deliveryPoint) => FormGroup( + { + nameControlName: FormControl( + value: deliveryPoint?.name, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + addressControlName: AddressForm.formElements(deliveryPoint?.address), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } class ReactiveStandaloneDeliveryPointFormArrayBuilder< - ReactiveStandaloneDeliveryPointFormArrayBuilderT> extends StatelessWidget { + ReactiveStandaloneDeliveryPointFormArrayBuilderT +> + extends StatelessWidget { const ReactiveStandaloneDeliveryPointFormArrayBuilder({ Key? key, this.control, @@ -2719,31 +3115,42 @@ class ReactiveStandaloneDeliveryPointFormArrayBuilder< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? - formControl; + formControl; final FormArray? Function( - StandaloneDeliveryPointForm formModel)? control; + StandaloneDeliveryPointForm formModel, + )? + control; - final Widget Function(BuildContext context, List itemList, - StandaloneDeliveryPointForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + StandaloneDeliveryPointForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveStandaloneDeliveryPointFormArrayBuilderT? item, - StandaloneDeliveryPointForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveStandaloneDeliveryPointFormArrayBuilderT? item, + StandaloneDeliveryPointForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl - control)? controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -2757,21 +3164,13 @@ class ReactiveStandaloneDeliveryPointFormArrayBuilder< final itemBuilder = this.itemBuilder; return ReactiveFormArrayItemBuilder< - ReactiveStandaloneDeliveryPointFormArrayBuilderT>( + ReactiveStandaloneDeliveryPointFormArrayBuilderT + >( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -2780,7 +3179,9 @@ class ReactiveStandaloneDeliveryPointFormArrayBuilder< } class ReactiveStandaloneDeliveryPointFormArrayBuilder2< - ReactiveStandaloneDeliveryPointFormArrayBuilderT> extends StatelessWidget { + ReactiveStandaloneDeliveryPointFormArrayBuilderT +> + extends StatelessWidget { const ReactiveStandaloneDeliveryPointFormArrayBuilder2({ Key? key, this.control, @@ -2789,37 +3190,48 @@ class ReactiveStandaloneDeliveryPointFormArrayBuilder2< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? - formControl; + formControl; final FormArray? Function( - StandaloneDeliveryPointForm formModel)? control; + StandaloneDeliveryPointForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - StandaloneDeliveryPointForm formModel - }) params)? builder; + ({ + BuildContext context, + List itemList, + StandaloneDeliveryPointForm formModel, + }) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveStandaloneDeliveryPointFormArrayBuilderT? item, - StandaloneDeliveryPointForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveStandaloneDeliveryPointFormArrayBuilderT? item, + StandaloneDeliveryPointForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl - control)? controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -2833,27 +3245,22 @@ class ReactiveStandaloneDeliveryPointFormArrayBuilder2< final itemBuilder = this.itemBuilder; return ReactiveFormArrayItemBuilder< - ReactiveStandaloneDeliveryPointFormArrayBuilderT>( + ReactiveStandaloneDeliveryPointFormArrayBuilderT + >( formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -2862,7 +3269,8 @@ class ReactiveStandaloneDeliveryPointFormArrayBuilder2< } class ReactiveStandaloneDeliveryPointFormFormGroupArrayBuilder< - ReactiveStandaloneDeliveryPointFormFormGroupArrayBuilderT> + ReactiveStandaloneDeliveryPointFormFormGroupArrayBuilderT +> extends StatelessWidget { const ReactiveStandaloneDeliveryPointFormFormGroupArrayBuilder({ Key? key, @@ -2870,26 +3278,39 @@ class ReactiveStandaloneDeliveryPointFormFormGroupArrayBuilder< this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? - extended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(StandaloneDeliveryPointForm formModel)? + getExtended; - final ExtendedControl?>, - List> - Function(StandaloneDeliveryPointForm formModel)? getExtended; - - final Widget Function(BuildContext context, List itemList, - StandaloneDeliveryPointForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + StandaloneDeliveryPointForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveStandaloneDeliveryPointFormFormGroupArrayBuilderT? item, - StandaloneDeliveryPointForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveStandaloneDeliveryPointFormFormGroupArrayBuilderT? item, + StandaloneDeliveryPointForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -2904,26 +3325,20 @@ class ReactiveStandaloneDeliveryPointFormFormGroupArrayBuilder< return StreamBuilder?>?>( stream: value.control.valueChanges, builder: (context, snapshot) { - final itemList = (value.value() ?? - []) - .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) - .values - .toList(); - - return builder?.call( - context, - itemList, - formModel, - ) ?? + final itemList = + (value.value() ?? + < + ReactiveStandaloneDeliveryPointFormFormGroupArrayBuilderT + >[]) + .asMap() + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) + .values + .toList(); + + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/generator_tests/test/doc/freezed_class_output_test.dart b/packages/generator_tests/test/doc/freezed_class_output_test.dart index 9bdfc20e..1d738ff2 100644 --- a/packages/generator_tests/test/doc/freezed_class_output_test.dart +++ b/packages/generator_tests/test/doc/freezed_class_output_test.dart @@ -73,7 +73,11 @@ class ReactiveFreezedClassOFormConsumer extends StatelessWidget { final Widget? child; final Widget Function( - BuildContext context, FreezedClassOForm formModel, Widget? child) builder; + BuildContext context, + FreezedClassOForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -92,11 +96,7 @@ class FreezedClassOFormInheritedStreamer extends InheritedStreamer { required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final FreezedClassOForm form; } @@ -118,19 +118,19 @@ class ReactiveFreezedClassOForm extends StatelessWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - static FreezedClassOForm? of( - BuildContext context, { - bool listen = true, - }) { + static FreezedClassOForm? of(BuildContext context, {bool listen = true}) { if (listen) { return context .dependOnInheritedWidgetOfExactType< - FreezedClassOFormInheritedStreamer>() + FreezedClassOFormInheritedStreamer + >() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - FreezedClassOFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + FreezedClassOFormInheritedStreamer + >(); return element == null ? null : (element.widget as FreezedClassOFormInheritedStreamer).form; @@ -178,10 +178,14 @@ class FreezedClassOFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, FreezedClassOForm formModel, Widget? child) builder; + BuildContext context, + FreezedClassOForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, FreezedClassOForm formModel)? - initState; + initState; @override _FreezedClassOFormBuilderState createState() => @@ -195,8 +199,11 @@ class _FreezedClassOFormBuilderState extends State { @override void initState() { - _formModel = - FreezedClassOForm(FreezedClassOForm.formElements(widget.model), null); + _formModel = FreezedClassOForm( + FreezedClassOForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -274,10 +281,8 @@ final _logFreezedClassOForm = Logger.detached('FreezedClassOForm'); class FreezedClassOForm implements FormModel { - FreezedClassOForm( - this.form, - this.path, - ); + FreezedClassOForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String genderControlName = "gender"; @@ -301,8 +306,14 @@ class FreezedClassOForm final String? path; + // ignore: unused_field + final FormModel? _formModel; + final Map _disabled = {}; + @override + final Map initial; + String genderControlPath() => pathBuilder(genderControlName); String genderRControlPath() => pathBuilder(genderRControlName); @@ -321,44 +332,44 @@ class FreezedClassOForm String selectedSpacesControlPath() => pathBuilder(selectedSpacesControlName); - String? get _genderValue => genderControl.value; + String? get _genderValue => containsGender ? genderControl.value : null; String get _genderRValue => genderRControl.value as String; - String? get _idValue => idControl.value; + String? get _idValue => containsId ? idControl.value : null; String get _idRValue => idRControl.value as String; String get _idR2Value => idR2Control.value ?? ''; - String? get _nameValue => nameControl.value; + String? get _nameValue => containsName ? nameControl.value : null; - String? get _logoImageValue => logoImageControl.value; + String? get _logoImageValue => + containsLogoImage ? logoImageControl.value : null; - double? get _yearValue => yearControl.value; + double? get _yearValue => containsYear ? yearControl.value : null; List get _selectedSpacesValue => selectedSpacesControl.value ?? []; - String? get _genderRawValue => genderControl.value; + String? get _genderRawValue => containsGender ? genderControl.value : null; - String? get _genderRRawValue => genderRControl.value; + String? get _genderRRawValue => containsGenderR ? genderRControl.value : null; - String? get _idRawValue => idControl.value; + String? get _idRawValue => containsId ? idControl.value : null; - String? get _idRRawValue => idRControl.value; + String? get _idRRawValue => containsIdR ? idRControl.value : null; String get _idR2RawValue => idR2Control.value ?? ''; - String? get _nameRawValue => nameControl.value; + String? get _nameRawValue => containsName ? nameControl.value : null; - String? get _logoImageRawValue => logoImageControl.value; + String? get _logoImageRawValue => + containsLogoImage ? logoImageControl.value : null; - double? get _yearRawValue => yearControl.value; + double? get _yearRawValue => containsYear ? yearControl.value : null; List get _selectedSpacesRawValue => selectedSpacesControl.value ?? []; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsGender { try { form.control(genderControlPath()); @@ -368,8 +379,6 @@ class FreezedClassOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsGenderR { try { form.control(genderRControlPath()); @@ -379,8 +388,6 @@ class FreezedClassOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsId { try { form.control(idControlPath()); @@ -390,8 +397,6 @@ class FreezedClassOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsIdR { try { form.control(idRControlPath()); @@ -401,8 +406,6 @@ class FreezedClassOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsIdR2 { try { form.control(idR2ControlPath()); @@ -412,8 +415,6 @@ class FreezedClassOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsName { try { form.control(nameControlPath()); @@ -423,8 +424,6 @@ class FreezedClassOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsLogoImage { try { form.control(logoImageControlPath()); @@ -434,8 +433,6 @@ class FreezedClassOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsYear { try { form.control(yearControlPath()); @@ -445,8 +442,6 @@ class FreezedClassOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsSelectedSpaces { try { form.control(selectedSpacesControlPath()); @@ -492,12 +487,7 @@ class FreezedClassOForm void get selectedSpacesFocus => form.focus(selectedSpacesControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void genderRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void genderRemove({bool updateParent = true, bool emitEvent = true}) { if (containsGender) { final controlPath = path; if (controlPath == null) { @@ -520,12 +510,7 @@ class FreezedClassOForm } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void genderRRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void genderRRemove({bool updateParent = true, bool emitEvent = true}) { if (containsGenderR) { final controlPath = path; if (controlPath == null) { @@ -548,12 +533,7 @@ class FreezedClassOForm } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void idRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void idRemove({bool updateParent = true, bool emitEvent = true}) { if (containsId) { final controlPath = path; if (controlPath == null) { @@ -576,12 +556,7 @@ class FreezedClassOForm } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void idRRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void idRRemove({bool updateParent = true, bool emitEvent = true}) { if (containsIdR) { final controlPath = path; if (controlPath == null) { @@ -604,12 +579,7 @@ class FreezedClassOForm } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void nameRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void nameRemove({bool updateParent = true, bool emitEvent = true}) { if (containsName) { final controlPath = path; if (controlPath == null) { @@ -632,12 +602,7 @@ class FreezedClassOForm } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void logoImageRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void logoImageRemove({bool updateParent = true, bool emitEvent = true}) { if (containsLogoImage) { final controlPath = path; if (controlPath == null) { @@ -660,12 +625,7 @@ class FreezedClassOForm } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void yearRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void yearRemove({bool updateParent = true, bool emitEvent = true}) { if (containsYear) { final controlPath = path; if (controlPath == null) { @@ -693,8 +653,11 @@ class FreezedClassOForm bool updateParent = true, bool emitEvent = true, }) { - genderControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + genderControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void genderRValueUpdate( @@ -702,8 +665,11 @@ class FreezedClassOForm bool updateParent = true, bool emitEvent = true, }) { - genderRControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + genderRControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void idValueUpdate( @@ -711,8 +677,11 @@ class FreezedClassOForm bool updateParent = true, bool emitEvent = true, }) { - idControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + idControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void idRValueUpdate( @@ -720,8 +689,11 @@ class FreezedClassOForm bool updateParent = true, bool emitEvent = true, }) { - idRControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + idRControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void idR2ValueUpdate( @@ -729,8 +701,11 @@ class FreezedClassOForm bool updateParent = true, bool emitEvent = true, }) { - idR2Control.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + idR2Control.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void nameValueUpdate( @@ -738,8 +713,11 @@ class FreezedClassOForm bool updateParent = true, bool emitEvent = true, }) { - nameControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + nameControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void logoImageValueUpdate( @@ -747,8 +725,11 @@ class FreezedClassOForm bool updateParent = true, bool emitEvent = true, }) { - logoImageControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + logoImageControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void yearValueUpdate( @@ -756,8 +737,11 @@ class FreezedClassOForm bool updateParent = true, bool emitEvent = true, }) { - yearControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + yearControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void selectedSpacesValueUpdate( @@ -765,8 +749,11 @@ class FreezedClassOForm bool updateParent = true, bool emitEvent = true, }) { - selectedSpacesControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + selectedSpacesControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void genderValuePatch( @@ -774,8 +761,11 @@ class FreezedClassOForm bool updateParent = true, bool emitEvent = true, }) { - genderControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + genderControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void genderRValuePatch( @@ -783,8 +773,11 @@ class FreezedClassOForm bool updateParent = true, bool emitEvent = true, }) { - genderRControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + genderRControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void idValuePatch( @@ -792,8 +785,11 @@ class FreezedClassOForm bool updateParent = true, bool emitEvent = true, }) { - idControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + idControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void idRValuePatch( @@ -801,8 +797,11 @@ class FreezedClassOForm bool updateParent = true, bool emitEvent = true, }) { - idRControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + idRControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void idR2ValuePatch( @@ -810,8 +809,11 @@ class FreezedClassOForm bool updateParent = true, bool emitEvent = true, }) { - idR2Control.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + idR2Control.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void nameValuePatch( @@ -819,8 +821,11 @@ class FreezedClassOForm bool updateParent = true, bool emitEvent = true, }) { - nameControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + nameControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void logoImageValuePatch( @@ -828,8 +833,11 @@ class FreezedClassOForm bool updateParent = true, bool emitEvent = true, }) { - logoImageControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + logoImageControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void yearValuePatch( @@ -837,8 +845,11 @@ class FreezedClassOForm bool updateParent = true, bool emitEvent = true, }) { - yearControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + yearControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void selectedSpacesValuePatch( @@ -846,8 +857,11 @@ class FreezedClassOForm bool updateParent = true, bool emitEvent = true, }) { - selectedSpacesControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + selectedSpacesControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void genderValueReset( @@ -856,14 +870,13 @@ class FreezedClassOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - genderControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => genderControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void genderRValueReset( String? value, { @@ -871,14 +884,13 @@ class FreezedClassOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - genderRControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => genderRControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void idValueReset( String? value, { @@ -886,14 +898,13 @@ class FreezedClassOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - idControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => idControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void idRValueReset( String? value, { @@ -901,14 +912,13 @@ class FreezedClassOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - idRControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => idRControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void idR2ValueReset( String value, { @@ -916,14 +926,13 @@ class FreezedClassOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - idR2Control.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => idR2Control.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void nameValueReset( String? value, { @@ -931,14 +940,13 @@ class FreezedClassOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - nameControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => nameControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void logoImageValueReset( String? value, { @@ -946,14 +954,13 @@ class FreezedClassOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - logoImageControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => logoImageControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void yearValueReset( double? value, { @@ -961,14 +968,13 @@ class FreezedClassOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - yearControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => yearControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void selectedSpacesValueReset( List value, { @@ -976,14 +982,13 @@ class FreezedClassOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - selectedSpacesControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => selectedSpacesControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get genderControl => form.control(genderControlPath()) as FormControl; @@ -1059,10 +1064,7 @@ class FreezedClassOForm emitEvent: emitEvent, ); } else { - idControl.markAsEnabled( - updateParent: updateParent, - emitEvent: emitEvent, - ); + idControl.markAsEnabled(updateParent: updateParent, emitEvent: emitEvent); } } @@ -1186,50 +1188,49 @@ class FreezedClassOForm StackTrace.current, ); } - return FreezedClassOOutput(_genderValue, _genderRValue, - id: _idValue, - idR: _idRValue, - idR2: _idR2Value, - name: _nameValue, - logoImage: _logoImageValue, - year: _yearValue, - selectedSpaces: _selectedSpacesValue); + return FreezedClassOOutput( + _genderValue, + _genderRValue, + id: _idValue, + idR: _idRValue, + idR2: _idR2Value, + name: _nameValue, + logoImage: _logoImageValue, + year: _yearValue, + selectedSpaces: _selectedSpacesValue, + ); } @override FreezedClassO get rawModel { - return FreezedClassO(_genderRawValue, _genderRRawValue, - id: _idRawValue, - idR: _idRRawValue, - idR2: _idR2RawValue, - name: _nameRawValue, - logoImage: _logoImageRawValue, - year: _yearRawValue, - selectedSpaces: _selectedSpacesRawValue); + return FreezedClassO( + _genderRawValue, + _genderRRawValue, + id: _idRawValue, + idR: _idRRawValue, + idR2: _idR2RawValue, + name: _nameRawValue, + logoImage: _logoImageRawValue, + year: _yearRawValue, + selectedSpaces: _selectedSpacesRawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { - final currentFormInstance = currentForm; - - if (currentFormInstance is! FormGroup) { - return; - } - + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { - currentFormInstance.controls[key]?.markAsEnabled( + currentForm.controls[key]?.markAsEnabled( updateParent: updateParent, emitEvent: emitEvent, ); @@ -1245,9 +1246,7 @@ class FreezedClassOForm final currentForm = this.currentForm; return const DeepCollectionEquality().equals( - currentForm is FormControlCollection - ? currentForm.rawValue - : currentForm.value, + currentForm.rawValue, FreezedClassOForm.formElements(other).rawValue, ); } @@ -1268,8 +1267,16 @@ class FreezedClassOForm } @override - AbstractControl get currentForm { - return path == null ? form : form.control(path!); + bool get hasChanged { + return !const DeepCollectionEquality().equals( + currentForm.rawValue, + initial, + ); + } + + @override + FormGroup get currentForm { + return path == null ? form : form.control(path!) as FormGroup; } @override @@ -1277,115 +1284,192 @@ class FreezedClassOForm FreezedClassO? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(FreezedClassOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + FreezedClassOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void upsertValue( + FreezedClassO? value, { + bool updateParent = true, + bool emitEvent = true, + }) { + final formElements = FreezedClassOForm.formElements(value); + + currentForm.addAll(formElements.controls); + } @override void reset({ FreezedClassO? value, bool updateParent = true, bool emitEvent = true, - }) => - form.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void updateInitial(Map? value, String? path) { + if (_formModel != null) { + _formModel?.updateInitial(currentForm.rawValue, path); + return; + } + + if (value == null) return; + + if (path == null || path.isEmpty) { + initial.addAll(value); + return; + } + + final keys = path.split('.'); + Object? current = initial; + for (var i = 0; i < keys.length - 1; i++) { + final key = keys[i]; + + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current = current[index]; + continue; + } + } + + if (current is Map) { + if (!current.containsKey(key)) { + current[key] = {}; + } + current = current[key]; + continue; + } + + return; + } + + final key = keys.last; + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current[index] = value; + } + } else if (current is Map) { + current[key] = value; + } + } String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(FreezedClassO? freezedClassO) => FormGroup({ - genderControlName: FormControl( - value: freezedClassO?.gender, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - genderRControlName: FormControl( - value: freezedClassO?.genderR, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - idControlName: FormControl( - value: freezedClassO?.id, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - idRControlName: FormControl( - value: freezedClassO?.idR, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - idR2ControlName: FormControl( - value: freezedClassO?.idR2, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - nameControlName: FormControl( - value: freezedClassO?.name, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - logoImageControlName: FormControl( - value: freezedClassO?.logoImage, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - yearControlName: FormControl( - value: freezedClassO?.year, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - selectedSpacesControlName: FormControl>( - value: freezedClassO?.selectedSpaces, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(FreezedClassO? freezedClassO) => FormGroup( + { + genderControlName: FormControl( + value: freezedClassO?.gender, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + genderRControlName: FormControl( + value: freezedClassO?.genderR, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + idControlName: FormControl( + value: freezedClassO?.id, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + idRControlName: FormControl( + value: freezedClassO?.idR, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + idR2ControlName: FormControl( + value: freezedClassO?.idR2, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + nameControlName: FormControl( + value: freezedClassO?.name, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + logoImageControlName: FormControl( + value: freezedClassO?.logoImage, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + yearControlName: FormControl( + value: freezedClassO?.year, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + selectedSpacesControlName: FormControl>( + value: freezedClassO?.selectedSpaces, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } @freezed @Rf(output: true) abstract class FreezedClassOOutput with _$FreezedClassOOutput { FreezedClassOOutput._(); - factory FreezedClassOOutput(@RfControl() String? gender, - @RfControl(validators: [RequiredValidator()]) String genderR, - {@RfControl() String? id, - @RfControl(validators: [RequiredValidator()]) required String idR, - @RfControl(validators: [RequiredValidator()]) required String idR2, - @RfControl() String? name, - @JsonKey(name: 'logo_image') @RfControl() String? logoImage, - @RfControl() double? year, - @Default([]) List selectedSpaces}) = _FreezedClassOOutput; + factory FreezedClassOOutput( + @RfControl() String? gender, + @RfControl(validators: [RequiredValidator()]) String genderR, { + @RfControl() String? id, + @RfControl(validators: [RequiredValidator()]) required String idR, + @RfControl(validators: [RequiredValidator()]) required String idR2, + @RfControl() String? name, + @JsonKey(name: 'logo_image') @RfControl() String? logoImage, + @RfControl() double? year, + @Default([]) List selectedSpaces, + }) = _FreezedClassOOutput; factory FreezedClassOOutput.fromJson(Map json) => _$FreezedClassOOutputFromJson(json); bool method() => false; } class ReactiveFreezedClassOFormArrayBuilder< - ReactiveFreezedClassOFormArrayBuilderT> extends StatelessWidget { + ReactiveFreezedClassOFormArrayBuilderT +> + extends StatelessWidget { const ReactiveFreezedClassOFormArrayBuilder({ Key? key, this.control, @@ -1394,30 +1478,41 @@ class ReactiveFreezedClassOFormArrayBuilder< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - FreezedClassOForm formModel)? control; + FreezedClassOForm formModel, + )? + control; - final Widget Function(BuildContext context, List itemList, - FreezedClassOForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + FreezedClassOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveFreezedClassOFormArrayBuilderT? item, - FreezedClassOForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveFreezedClassOFormArrayBuilderT? item, + FreezedClassOForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -1433,18 +1528,9 @@ class ReactiveFreezedClassOFormArrayBuilder< return ReactiveFormArrayItemBuilder( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1453,7 +1539,9 @@ class ReactiveFreezedClassOFormArrayBuilder< } class ReactiveFreezedClassOFormArrayBuilder2< - ReactiveFreezedClassOFormArrayBuilderT> extends StatelessWidget { + ReactiveFreezedClassOFormArrayBuilderT +> + extends StatelessWidget { const ReactiveFreezedClassOFormArrayBuilder2({ Key? key, this.control, @@ -1462,36 +1550,43 @@ class ReactiveFreezedClassOFormArrayBuilder2< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - FreezedClassOForm formModel)? control; + FreezedClassOForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - FreezedClassOForm formModel - }) params)? builder; + ({BuildContext context, List itemList, FreezedClassOForm formModel}) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveFreezedClassOFormArrayBuilderT? item, - FreezedClassOForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveFreezedClassOFormArrayBuilderT? item, + FreezedClassOForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -1508,23 +1603,17 @@ class ReactiveFreezedClassOFormArrayBuilder2< formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1533,32 +1622,48 @@ class ReactiveFreezedClassOFormArrayBuilder2< } class ReactiveFreezedClassOFormFormGroupArrayBuilder< - ReactiveFreezedClassOFormFormGroupArrayBuilderT> extends StatelessWidget { + ReactiveFreezedClassOFormFormGroupArrayBuilderT +> + extends StatelessWidget { const ReactiveFreezedClassOFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? extended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(FreezedClassOForm formModel)? + getExtended; - final ExtendedControl?>, - List> - Function(FreezedClassOForm formModel)? getExtended; - - final Widget Function(BuildContext context, List itemList, - FreezedClassOForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + FreezedClassOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveFreezedClassOFormFormGroupArrayBuilderT? item, - FreezedClassOForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveFreezedClassOFormFormGroupArrayBuilderT? item, + FreezedClassOForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -1573,26 +1678,18 @@ class ReactiveFreezedClassOFormFormGroupArrayBuilder< return StreamBuilder?>?>( stream: value.control.valueChanges, builder: (context, snapshot) { - final itemList = (value.value() ?? - []) - .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) - .values - .toList(); - - return builder?.call( - context, - itemList, - formModel, - ) ?? + final itemList = + (value.value() ?? + []) + .asMap() + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) + .values + .toList(); + + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/generator_tests/test/doc/freezed_class_test.dart b/packages/generator_tests/test/doc/freezed_class_test.dart index 1c753a8a..af7b64bc 100644 --- a/packages/generator_tests/test/doc/freezed_class_test.dart +++ b/packages/generator_tests/test/doc/freezed_class_test.dart @@ -72,7 +72,11 @@ class ReactiveFreezedClassFormConsumer extends StatelessWidget { final Widget? child; final Widget Function( - BuildContext context, FreezedClassForm formModel, Widget? child) builder; + BuildContext context, + FreezedClassForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -91,11 +95,7 @@ class FreezedClassFormInheritedStreamer extends InheritedStreamer { required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final FreezedClassForm form; } @@ -117,19 +117,19 @@ class ReactiveFreezedClassForm extends StatelessWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - static FreezedClassForm? of( - BuildContext context, { - bool listen = true, - }) { + static FreezedClassForm? of(BuildContext context, {bool listen = true}) { if (listen) { return context .dependOnInheritedWidgetOfExactType< - FreezedClassFormInheritedStreamer>() + FreezedClassFormInheritedStreamer + >() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - FreezedClassFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + FreezedClassFormInheritedStreamer + >(); return element == null ? null : (element.widget as FreezedClassFormInheritedStreamer).form; @@ -177,10 +177,14 @@ class FreezedClassFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, FreezedClassForm formModel, Widget? child) builder; + BuildContext context, + FreezedClassForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, FreezedClassForm formModel)? - initState; + initState; @override _FreezedClassFormBuilderState createState() => @@ -194,8 +198,11 @@ class _FreezedClassFormBuilderState extends State { @override void initState() { - _formModel = - FreezedClassForm(FreezedClassForm.formElements(widget.model), null); + _formModel = FreezedClassForm( + FreezedClassForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -272,10 +279,8 @@ class _FreezedClassFormBuilderState extends State { final _logFreezedClassForm = Logger.detached('FreezedClassForm'); class FreezedClassForm implements FormModel { - FreezedClassForm( - this.form, - this.path, - ); + FreezedClassForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String genderControlName = "gender"; @@ -293,8 +298,14 @@ class FreezedClassForm implements FormModel { final String? path; + // ignore: unused_field + final FormModel? _formModel; + final Map _disabled = {}; + @override + final Map initial; + String genderControlPath() => pathBuilder(genderControlName); String idControlPath() => pathBuilder(idControlName); @@ -307,32 +318,32 @@ class FreezedClassForm implements FormModel { String selectedSpacesControlPath() => pathBuilder(selectedSpacesControlName); - String? get _genderValue => genderControl.value; + String? get _genderValue => containsGender ? genderControl.value : null; - String? get _idValue => idControl.value; + String? get _idValue => containsId ? idControl.value : null; - String? get _nameValue => nameControl.value; + String? get _nameValue => containsName ? nameControl.value : null; - String? get _logoImageValue => logoImageControl.value; + String? get _logoImageValue => + containsLogoImage ? logoImageControl.value : null; - double? get _yearValue => yearControl.value; + double? get _yearValue => containsYear ? yearControl.value : null; List get _selectedSpacesValue => selectedSpacesControl.value ?? []; - String? get _genderRawValue => genderControl.value; + String? get _genderRawValue => containsGender ? genderControl.value : null; - String? get _idRawValue => idControl.value; + String? get _idRawValue => containsId ? idControl.value : null; - String? get _nameRawValue => nameControl.value; + String? get _nameRawValue => containsName ? nameControl.value : null; - String? get _logoImageRawValue => logoImageControl.value; + String? get _logoImageRawValue => + containsLogoImage ? logoImageControl.value : null; - double? get _yearRawValue => yearControl.value; + double? get _yearRawValue => containsYear ? yearControl.value : null; List get _selectedSpacesRawValue => selectedSpacesControl.value ?? []; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsGender { try { form.control(genderControlPath()); @@ -342,8 +353,6 @@ class FreezedClassForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsId { try { form.control(idControlPath()); @@ -353,8 +362,6 @@ class FreezedClassForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsName { try { form.control(nameControlPath()); @@ -364,8 +371,6 @@ class FreezedClassForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsLogoImage { try { form.control(logoImageControlPath()); @@ -375,8 +380,6 @@ class FreezedClassForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsYear { try { form.control(yearControlPath()); @@ -386,8 +389,6 @@ class FreezedClassForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsSelectedSpaces { try { form.control(selectedSpacesControlPath()); @@ -421,12 +422,7 @@ class FreezedClassForm implements FormModel { void get selectedSpacesFocus => form.focus(selectedSpacesControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void genderRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void genderRemove({bool updateParent = true, bool emitEvent = true}) { if (containsGender) { final controlPath = path; if (controlPath == null) { @@ -449,12 +445,7 @@ class FreezedClassForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void idRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void idRemove({bool updateParent = true, bool emitEvent = true}) { if (containsId) { final controlPath = path; if (controlPath == null) { @@ -477,12 +468,7 @@ class FreezedClassForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void nameRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void nameRemove({bool updateParent = true, bool emitEvent = true}) { if (containsName) { final controlPath = path; if (controlPath == null) { @@ -505,12 +491,7 @@ class FreezedClassForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void logoImageRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void logoImageRemove({bool updateParent = true, bool emitEvent = true}) { if (containsLogoImage) { final controlPath = path; if (controlPath == null) { @@ -533,12 +514,7 @@ class FreezedClassForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void yearRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void yearRemove({bool updateParent = true, bool emitEvent = true}) { if (containsYear) { final controlPath = path; if (controlPath == null) { @@ -566,8 +542,11 @@ class FreezedClassForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - genderControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + genderControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void idValueUpdate( @@ -575,8 +554,11 @@ class FreezedClassForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - idControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + idControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void nameValueUpdate( @@ -584,8 +566,11 @@ class FreezedClassForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - nameControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + nameControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void logoImageValueUpdate( @@ -593,8 +578,11 @@ class FreezedClassForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - logoImageControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + logoImageControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void yearValueUpdate( @@ -602,8 +590,11 @@ class FreezedClassForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - yearControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + yearControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void selectedSpacesValueUpdate( @@ -611,8 +602,11 @@ class FreezedClassForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - selectedSpacesControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + selectedSpacesControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void genderValuePatch( @@ -620,8 +614,11 @@ class FreezedClassForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - genderControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + genderControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void idValuePatch( @@ -629,8 +626,11 @@ class FreezedClassForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - idControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + idControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void nameValuePatch( @@ -638,8 +638,11 @@ class FreezedClassForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - nameControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + nameControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void logoImageValuePatch( @@ -647,8 +650,11 @@ class FreezedClassForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - logoImageControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + logoImageControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void yearValuePatch( @@ -656,8 +662,11 @@ class FreezedClassForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - yearControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + yearControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void selectedSpacesValuePatch( @@ -665,8 +674,11 @@ class FreezedClassForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - selectedSpacesControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + selectedSpacesControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void genderValueReset( @@ -675,14 +687,13 @@ class FreezedClassForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - genderControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => genderControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void idValueReset( String? value, { @@ -690,14 +701,13 @@ class FreezedClassForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - idControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => idControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void nameValueReset( String? value, { @@ -705,14 +715,13 @@ class FreezedClassForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - nameControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => nameControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void logoImageValueReset( String? value, { @@ -720,14 +729,13 @@ class FreezedClassForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - logoImageControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => logoImageControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void yearValueReset( double? value, { @@ -735,14 +743,13 @@ class FreezedClassForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - yearControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => yearControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void selectedSpacesValueReset( List value, { @@ -750,14 +757,13 @@ class FreezedClassForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - selectedSpacesControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => selectedSpacesControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get genderControl => form.control(genderControlPath()) as FormControl; @@ -806,10 +812,7 @@ class FreezedClassForm implements FormModel { emitEvent: emitEvent, ); } else { - idControl.markAsEnabled( - updateParent: updateParent, - emitEvent: emitEvent, - ); + idControl.markAsEnabled(updateParent: updateParent, emitEvent: emitEvent); } } @@ -896,46 +899,43 @@ class FreezedClassForm implements FormModel { StackTrace.current, ); } - return FreezedClass(_genderValue, - id: _idValue, - name: _nameValue, - logoImage: _logoImageValue, - year: _yearValue, - selectedSpaces: _selectedSpacesValue); + return FreezedClass( + _genderValue, + id: _idValue, + name: _nameValue, + logoImage: _logoImageValue, + year: _yearValue, + selectedSpaces: _selectedSpacesValue, + ); } @override FreezedClass get rawModel { - return FreezedClass(_genderRawValue, - id: _idRawValue, - name: _nameRawValue, - logoImage: _logoImageRawValue, - year: _yearRawValue, - selectedSpaces: _selectedSpacesRawValue); + return FreezedClass( + _genderRawValue, + id: _idRawValue, + name: _nameRawValue, + logoImage: _logoImageRawValue, + year: _yearRawValue, + selectedSpaces: _selectedSpacesRawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { - final currentFormInstance = currentForm; - - if (currentFormInstance is! FormGroup) { - return; - } - + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { - currentFormInstance.controls[key]?.markAsEnabled( + currentForm.controls[key]?.markAsEnabled( updateParent: updateParent, emitEvent: emitEvent, ); @@ -951,9 +951,7 @@ class FreezedClassForm implements FormModel { final currentForm = this.currentForm; return const DeepCollectionEquality().equals( - currentForm is FormControlCollection - ? currentForm.rawValue - : currentForm.value, + currentForm.rawValue, FreezedClassForm.formElements(other).rawValue, ); } @@ -974,8 +972,16 @@ class FreezedClassForm implements FormModel { } @override - AbstractControl get currentForm { - return path == null ? form : form.control(path!); + bool get hasChanged { + return !const DeepCollectionEquality().equals( + currentForm.rawValue, + initial, + ); + } + + @override + FormGroup get currentForm { + return path == null ? form : form.control(path!) as FormGroup; } @override @@ -983,76 +989,148 @@ class FreezedClassForm implements FormModel { FreezedClass? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(FreezedClassForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + FreezedClassForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void upsertValue( + FreezedClass? value, { + bool updateParent = true, + bool emitEvent = true, + }) { + final formElements = FreezedClassForm.formElements(value); + + currentForm.addAll(formElements.controls); + } @override void reset({ FreezedClass? value, bool updateParent = true, bool emitEvent = true, - }) => - form.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void updateInitial(Map? value, String? path) { + if (_formModel != null) { + _formModel?.updateInitial(currentForm.rawValue, path); + return; + } + + if (value == null) return; + + if (path == null || path.isEmpty) { + initial.addAll(value); + return; + } + + final keys = path.split('.'); + Object? current = initial; + for (var i = 0; i < keys.length - 1; i++) { + final key = keys[i]; + + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current = current[index]; + continue; + } + } + + if (current is Map) { + if (!current.containsKey(key)) { + current[key] = {}; + } + current = current[key]; + continue; + } + + return; + } + + final key = keys.last; + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current[index] = value; + } + } else if (current is Map) { + current[key] = value; + } + } String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(FreezedClass? freezedClass) => FormGroup({ - genderControlName: FormControl( - value: freezedClass?.gender, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - idControlName: FormControl( - value: freezedClass?.id, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - nameControlName: FormControl( - value: freezedClass?.name, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - logoImageControlName: FormControl( - value: freezedClass?.logoImage, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - yearControlName: FormControl( - value: freezedClass?.year, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - selectedSpacesControlName: FormControl>( - value: freezedClass?.selectedSpaces, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(FreezedClass? freezedClass) => FormGroup( + { + genderControlName: FormControl( + value: freezedClass?.gender, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + idControlName: FormControl( + value: freezedClass?.id, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + nameControlName: FormControl( + value: freezedClass?.name, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + logoImageControlName: FormControl( + value: freezedClass?.logoImage, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + yearControlName: FormControl( + value: freezedClass?.year, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + selectedSpacesControlName: FormControl>( + value: freezedClass?.selectedSpaces, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } class ReactiveFreezedClassFormArrayBuilder< - ReactiveFreezedClassFormArrayBuilderT> extends StatelessWidget { + ReactiveFreezedClassFormArrayBuilderT +> + extends StatelessWidget { const ReactiveFreezedClassFormArrayBuilder({ Key? key, this.control, @@ -1061,30 +1139,41 @@ class ReactiveFreezedClassFormArrayBuilder< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - FreezedClassForm formModel)? control; + FreezedClassForm formModel, + )? + control; - final Widget Function(BuildContext context, List itemList, - FreezedClassForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + FreezedClassForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveFreezedClassFormArrayBuilderT? item, - FreezedClassForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveFreezedClassFormArrayBuilderT? item, + FreezedClassForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -1100,18 +1189,9 @@ class ReactiveFreezedClassFormArrayBuilder< return ReactiveFormArrayItemBuilder( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1120,7 +1200,9 @@ class ReactiveFreezedClassFormArrayBuilder< } class ReactiveFreezedClassFormArrayBuilder2< - ReactiveFreezedClassFormArrayBuilderT> extends StatelessWidget { + ReactiveFreezedClassFormArrayBuilderT +> + extends StatelessWidget { const ReactiveFreezedClassFormArrayBuilder2({ Key? key, this.control, @@ -1129,36 +1211,43 @@ class ReactiveFreezedClassFormArrayBuilder2< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - FreezedClassForm formModel)? control; + FreezedClassForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - FreezedClassForm formModel - }) params)? builder; + ({BuildContext context, List itemList, FreezedClassForm formModel}) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveFreezedClassFormArrayBuilderT? item, - FreezedClassForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveFreezedClassFormArrayBuilderT? item, + FreezedClassForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -1175,23 +1264,17 @@ class ReactiveFreezedClassFormArrayBuilder2< formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1200,32 +1283,48 @@ class ReactiveFreezedClassFormArrayBuilder2< } class ReactiveFreezedClassFormFormGroupArrayBuilder< - ReactiveFreezedClassFormFormGroupArrayBuilderT> extends StatelessWidget { + ReactiveFreezedClassFormFormGroupArrayBuilderT +> + extends StatelessWidget { const ReactiveFreezedClassFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? extended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(FreezedClassForm formModel)? + getExtended; - final ExtendedControl?>, - List> - Function(FreezedClassForm formModel)? getExtended; - - final Widget Function(BuildContext context, List itemList, - FreezedClassForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + FreezedClassForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveFreezedClassFormFormGroupArrayBuilderT? item, - FreezedClassForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveFreezedClassFormFormGroupArrayBuilderT? item, + FreezedClassForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -1240,26 +1339,18 @@ class ReactiveFreezedClassFormFormGroupArrayBuilder< return StreamBuilder?>?>( stream: value.control.valueChanges, builder: (context, snapshot) { - final itemList = (value.value() ?? - []) - .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) - .values - .toList(); - - return builder?.call( - context, - itemList, - formModel, - ) ?? + final itemList = + (value.value() ?? + []) + .asMap() + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) + .values + .toList(); + + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/generator_tests/test/doc/generic_output_test.dart b/packages/generator_tests/test/doc/generic_output_test.dart index 9d613148..8a6be633 100644 --- a/packages/generator_tests/test/doc/generic_output_test.dart +++ b/packages/generator_tests/test/doc/generic_output_test.dart @@ -46,16 +46,17 @@ part of 'generic_output.dart'; // ************************************************************************** class ReactiveTagsOFormConsumer extends StatelessWidget { - const ReactiveTagsOFormConsumer({ - Key? key, - required this.builder, - this.child, - }) : super(key: key); + const ReactiveTagsOFormConsumer({Key? key, required this.builder, this.child}) + : super(key: key); final Widget? child; final Widget Function( - BuildContext context, TagsOForm formModel, Widget? child) builder; + BuildContext context, + TagsOForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -74,11 +75,7 @@ class TagsOFormInheritedStreamer extends InheritedStreamer { required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final TagsOForm form; } @@ -100,18 +97,17 @@ class ReactiveTagsOForm extends StatelessWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - static TagsOForm? of( - BuildContext context, { - bool listen = true, - }) { + static TagsOForm? of(BuildContext context, {bool listen = true}) { if (listen) { return context .dependOnInheritedWidgetOfExactType>() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - TagsOFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + TagsOFormInheritedStreamer + >(); return element == null ? null : (element.widget as TagsOFormInheritedStreamer).form; @@ -158,7 +154,11 @@ class TagsOFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, TagsOForm formModel, Widget? child) builder; + BuildContext context, + TagsOForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, TagsOForm formModel)? initState; @@ -173,7 +173,11 @@ class _TagsOFormBuilderState extends State> { @override void initState() { - _formModel = TagsOForm(TagsOForm.formElements(widget.model), null); + _formModel = TagsOForm( + TagsOForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -250,10 +254,7 @@ class _TagsOFormBuilderState extends State> { final _logTagsOForm = Logger.detached('TagsOForm'); class TagsOForm implements FormModel, TagsOOutput> { - TagsOForm( - this.form, - this.path, - ); + TagsOForm(this.form, this.path, this._formModel) : initial = form.rawValue; static const String tagsControlName = "tags"; @@ -261,16 +262,20 @@ class TagsOForm implements FormModel, TagsOOutput> { final String? path; + // ignore: unused_field + final FormModel? _formModel; + final Map _disabled = {}; + @override + final Map initial; + String tagsControlPath() => pathBuilder(tagsControlName); - List? get _tagsValue => tagsControl.value; + List? get _tagsValue => containsTags ? tagsControl.value : null; - List? get _tagsRawValue => tagsControl.value; + List? get _tagsRawValue => containsTags ? tagsControl.value : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsTags { try { form.control(tagsControlPath()); @@ -284,12 +289,7 @@ class TagsOForm implements FormModel, TagsOOutput> { void get tagsFocus => form.focus(tagsControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void tagsRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void tagsRemove({bool updateParent = true, bool emitEvent = true}) { if (containsTags) { final controlPath = path; if (controlPath == null) { @@ -317,8 +317,11 @@ class TagsOForm implements FormModel, TagsOOutput> { bool updateParent = true, bool emitEvent = true, }) { - tagsControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + tagsControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void tagsValuePatch( @@ -326,8 +329,11 @@ class TagsOForm implements FormModel, TagsOOutput> { bool updateParent = true, bool emitEvent = true, }) { - tagsControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + tagsControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void tagsValueReset( @@ -336,14 +342,13 @@ class TagsOForm implements FormModel, TagsOOutput> { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - tagsControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => tagsControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl> get tagsControl => form.control(tagsControlPath()) as FormControl>; @@ -387,27 +392,20 @@ class TagsOForm implements FormModel, TagsOOutput> { } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { - final currentFormInstance = currentForm; - - if (currentFormInstance is! FormGroup) { - return; - } - + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { - currentFormInstance.controls[key]?.markAsEnabled( + currentForm.controls[key]?.markAsEnabled( updateParent: updateParent, emitEvent: emitEvent, ); @@ -423,9 +421,7 @@ class TagsOForm implements FormModel, TagsOOutput> { final currentForm = this.currentForm; return const DeepCollectionEquality().equals( - currentForm is FormControlCollection - ? currentForm.rawValue - : currentForm.value, + currentForm.rawValue, TagsOForm.formElements(other).rawValue, ); } @@ -446,8 +442,16 @@ class TagsOForm implements FormModel, TagsOOutput> { } @override - AbstractControl get currentForm { - return path == null ? form : form.control(path!); + bool get hasChanged { + return !const DeepCollectionEquality().equals( + currentForm.rawValue, + initial, + ); + } + + @override + FormGroup get currentForm { + return path == null ? form : form.control(path!) as FormGroup; } @override @@ -455,37 +459,102 @@ class TagsOForm implements FormModel, TagsOOutput> { TagsO? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(TagsOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + TagsOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void upsertValue( + TagsO? value, { + bool updateParent = true, + bool emitEvent = true, + }) { + final formElements = TagsOForm.formElements(value); + + currentForm.addAll(formElements.controls); + } @override void reset({ TagsO? value, bool updateParent = true, bool emitEvent = true, - }) => - form.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void updateInitial(Map? value, String? path) { + if (_formModel != null) { + _formModel?.updateInitial(currentForm.rawValue, path); + return; + } + + if (value == null) return; + + if (path == null || path.isEmpty) { + initial.addAll(value); + return; + } + + final keys = path.split('.'); + Object? current = initial; + for (var i = 0; i < keys.length - 1; i++) { + final key = keys[i]; + + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current = current[index]; + continue; + } + } + + if (current is Map) { + if (!current.containsKey(key)) { + current[key] = {}; + } + current = current[key]; + continue; + } + + return; + } + + final key = keys.last; + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current[index] = value; + } + } else if (current is Map) { + current[key] = value; + } + } String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(TagsO? tagsO) => FormGroup({ - tagsControlName: FormControl>( - value: tagsO?.tags, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(TagsO? tagsO) => FormGroup( + { + tagsControlName: FormControl>( + value: tagsO?.tags, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } @freezed @@ -505,30 +574,39 @@ class ReactiveTagsOFormArrayBuilder required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - TagsOForm formModel)? control; + TagsOForm formModel, + )? + control; final Widget Function( - BuildContext context, List itemList, TagsOForm formModel)? - builder; + BuildContext context, + List itemList, + TagsOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveTagsOFormArrayBuilderT? item, - TagsOForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveTagsOFormArrayBuilderT? item, + TagsOForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function(FormControl control)? - controlFilter; + controlFilter; @override Widget build(BuildContext context) { @@ -544,18 +622,9 @@ class ReactiveTagsOFormArrayBuilder return ReactiveFormArrayItemBuilder( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -573,35 +642,41 @@ class ReactiveTagsOFormArrayBuilder2 required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - TagsOForm formModel)? control; + TagsOForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - TagsOForm formModel - }) params)? builder; + ({BuildContext context, List itemList, TagsOForm formModel}) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveTagsOFormArrayBuilderT? item, - TagsOForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveTagsOFormArrayBuilderT? item, + TagsOForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function(FormControl control)? - controlFilter; + controlFilter; @override Widget build(BuildContext context) { @@ -618,23 +693,17 @@ class ReactiveTagsOFormArrayBuilder2 formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -643,33 +712,49 @@ class ReactiveTagsOFormArrayBuilder2 } class ReactiveTagsOFormFormGroupArrayBuilder< - ReactiveTagsOFormFormGroupArrayBuilderT, T> extends StatelessWidget { + ReactiveTagsOFormFormGroupArrayBuilderT, + T +> + extends StatelessWidget { const ReactiveTagsOFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? extended; - - final ExtendedControl?>, - List> - Function(TagsOForm formModel)? getExtended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(TagsOForm formModel)? + getExtended; final Widget Function( - BuildContext context, List itemList, TagsOForm formModel)? - builder; + BuildContext context, + List itemList, + TagsOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveTagsOFormFormGroupArrayBuilderT? item, - TagsOForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveTagsOFormFormGroupArrayBuilderT? item, + TagsOForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -687,23 +772,14 @@ class ReactiveTagsOFormFormGroupArrayBuilder< final itemList = (value.value() ?? []) .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) .values .toList(); - return builder?.call( - context, - itemList, - formModel, - ) ?? + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/generator_tests/test/doc/generic_status_list_output_test.dart b/packages/generator_tests/test/doc/generic_status_list_output_test.dart index b2bb779d..052ceb22 100644 --- a/packages/generator_tests/test/doc/generic_status_list_output_test.dart +++ b/packages/generator_tests/test/doc/generic_status_list_output_test.dart @@ -52,8 +52,11 @@ class ReactiveStatusListOFormConsumer extends StatelessWidget { final Widget? child; final Widget Function( - BuildContext context, StatusListOForm formModel, Widget? child) - builder; + BuildContext context, + StatusListOForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -73,11 +76,7 @@ class StatusListOFormInheritedStreamer required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final StatusListOForm form; } @@ -106,12 +105,15 @@ class ReactiveStatusListOForm extends StatelessWidget { if (listen) { return context .dependOnInheritedWidgetOfExactType< - StatusListOFormInheritedStreamer>() + StatusListOFormInheritedStreamer + >() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - StatusListOFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + StatusListOFormInheritedStreamer + >(); return element == null ? null : (element.widget as StatusListOFormInheritedStreamer).form; @@ -159,11 +161,14 @@ class StatusListOFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, StatusListOForm formModel, Widget? child) - builder; + BuildContext context, + StatusListOForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, StatusListOForm formModel)? - initState; + initState; @override _StatusListOFormBuilderState createState() => @@ -178,8 +183,11 @@ class _StatusListOFormBuilderState @override void initState() { - _formModel = - StatusListOForm(StatusListOForm.formElements(widget.model), null); + _formModel = StatusListOForm( + StatusListOForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -257,10 +265,8 @@ final _logStatusListOForm = Logger.detached('StatusListOForm'); class StatusListOForm implements FormModel, StatusListOOutput> { - StatusListOForm( - this.form, - this.path, - ); + StatusListOForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String listControlName = "list"; @@ -268,16 +274,20 @@ class StatusListOForm final String? path; + // ignore: unused_field + final FormModel? _formModel; + final Map _disabled = {}; + @override + final Map initial; + String listControlPath() => pathBuilder(listControlName); List get _listValue => listControl.rawValue.whereType().toList(); List get _listRawValue => listControl.rawValue.whereType().toList(); - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsList { try { form.control(listControlPath()); @@ -296,8 +306,11 @@ class StatusListOForm bool updateParent = true, bool emitEvent = true, }) { - listControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + listControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void listValuePatch( @@ -305,8 +318,11 @@ class StatusListOForm bool updateParent = true, bool emitEvent = true, }) { - listControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + listControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void listValueReset( @@ -315,14 +331,13 @@ class StatusListOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - listControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => listControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormArray get listControl => form.control(listControlPath()) as FormArray; @@ -379,13 +394,15 @@ class StatusListOForm break; } - listControl.add(FormControl( - value: value, - validators: resultingValidators, - asyncValidators: resultingAsyncValidators, - asyncValidatorsDebounceTime: asyncValidatorsDebounceTime ?? 250, - disabled: disabled ?? false, - )); + listControl.add( + FormControl( + value: value, + validators: resultingValidators, + asyncValidators: resultingAsyncValidators, + asyncValidatorsDebounceTime: asyncValidatorsDebounceTime ?? 250, + disabled: disabled ?? false, + ), + ); } @override @@ -409,27 +426,20 @@ class StatusListOForm } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { - final currentFormInstance = currentForm; - - if (currentFormInstance is! FormGroup) { - return; - } - + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { - currentFormInstance.controls[key]?.markAsEnabled( + currentForm.controls[key]?.markAsEnabled( updateParent: updateParent, emitEvent: emitEvent, ); @@ -445,9 +455,7 @@ class StatusListOForm final currentForm = this.currentForm; return const DeepCollectionEquality().equals( - currentForm is FormControlCollection - ? currentForm.rawValue - : currentForm.value, + currentForm.rawValue, StatusListOForm.formElements(other).rawValue, ); } @@ -468,8 +476,16 @@ class StatusListOForm } @override - AbstractControl get currentForm { - return path == null ? form : form.control(path!); + bool get hasChanged { + return !const DeepCollectionEquality().equals( + currentForm.rawValue, + initial, + ); + } + + @override + FormGroup get currentForm { + return path == null ? form : form.control(path!) as FormGroup; } @override @@ -477,45 +493,112 @@ class StatusListOForm StatusListO? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(StatusListOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + StatusListOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void upsertValue( + StatusListO? value, { + bool updateParent = true, + bool emitEvent = true, + }) { + final formElements = StatusListOForm.formElements(value); + + currentForm.addAll(formElements.controls); + } @override void reset({ StatusListO? value, bool updateParent = true, bool emitEvent = true, - }) => - form.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void updateInitial(Map? value, String? path) { + if (_formModel != null) { + _formModel?.updateInitial(currentForm.rawValue, path); + return; + } + + if (value == null) return; + + if (path == null || path.isEmpty) { + initial.addAll(value); + return; + } + + final keys = path.split('.'); + Object? current = initial; + for (var i = 0; i < keys.length - 1; i++) { + final key = keys[i]; + + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current = current[index]; + continue; + } + } + + if (current is Map) { + if (!current.containsKey(key)) { + current[key] = {}; + } + current = current[key]; + continue; + } + + return; + } + + final key = keys.last; + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current[index] = value; + } + } else if (current is Map) { + current[key] = value; + } + } String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); static FormGroup formElements(StatusListO? statusListO) => - FormGroup({ - listControlName: FormArray( + FormGroup( + { + listControlName: FormArray( (statusListO?.list ?? []) - .map((e) => FormControl( - value: e, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - )) + .map( + (e) => FormControl( + value: e, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ), + ) .toList(), validators: [], asyncValidators: [], asyncValidatorsDebounceTime: 250, - disabled: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + disabled: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } @Rf(output: true) @@ -524,8 +607,11 @@ class StatusListOOutput { StatusListOOutput({@RfArray() this.list = const []}); } -class ReactiveStatusListOFormArrayBuilder extends StatelessWidget { +class ReactiveStatusListOFormArrayBuilder< + ReactiveStatusListOFormArrayBuilderT, + T extends Enum +> + extends StatelessWidget { const ReactiveStatusListOFormArrayBuilder({ Key? key, this.control, @@ -534,29 +620,41 @@ class ReactiveStatusListOFormArrayBuilder? formControl; final FormArray? Function( - StatusListOForm formModel)? control; + StatusListOForm formModel, + )? + control; - final Widget Function(BuildContext context, List itemList, - StatusListOForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + StatusListOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveStatusListOFormArrayBuilderT? item, - StatusListOForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveStatusListOFormArrayBuilderT? item, + StatusListOForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -572,18 +670,9 @@ class ReactiveStatusListOFormArrayBuilder( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -591,8 +680,11 @@ class ReactiveStatusListOFormArrayBuilder extends StatelessWidget { +class ReactiveStatusListOFormArrayBuilder2< + ReactiveStatusListOFormArrayBuilderT, + T extends Enum +> + extends StatelessWidget { const ReactiveStatusListOFormArrayBuilder2({ Key? key, this.control, @@ -601,35 +693,47 @@ class ReactiveStatusListOFormArrayBuilder2? formControl; final FormArray? Function( - StatusListOForm formModel)? control; + StatusListOForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - StatusListOForm formModel - }) params)? builder; + ({ + BuildContext context, + List itemList, + StatusListOForm formModel, + }) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveStatusListOFormArrayBuilderT? item, - StatusListOForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveStatusListOFormArrayBuilderT? item, + StatusListOForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -646,23 +750,17 @@ class ReactiveStatusListOFormArrayBuilder2 builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -671,33 +769,49 @@ class ReactiveStatusListOFormArrayBuilder2 extends StatelessWidget { + ReactiveStatusListOFormFormGroupArrayBuilderT, + T extends Enum +> + extends StatelessWidget { const ReactiveStatusListOFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(StatusListOForm formModel)? + getExtended; - final ExtendedControl?>, - List>? extended; - - final ExtendedControl?>, - List> - Function(StatusListOForm formModel)? getExtended; - - final Widget Function(BuildContext context, List itemList, - StatusListOForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + StatusListOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveStatusListOFormFormGroupArrayBuilderT? item, - StatusListOForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveStatusListOFormFormGroupArrayBuilderT? item, + StatusListOForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -715,23 +829,14 @@ class ReactiveStatusListOFormFormGroupArrayBuilder< final itemList = (value.value() ?? []) .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) .values .toList(); - return builder?.call( - context, - itemList, - formModel, - ) ?? + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/generator_tests/test/doc/generic_status_list_test.dart b/packages/generator_tests/test/doc/generic_status_list_test.dart index 507af705..a8e83782 100644 --- a/packages/generator_tests/test/doc/generic_status_list_test.dart +++ b/packages/generator_tests/test/doc/generic_status_list_test.dart @@ -52,7 +52,11 @@ class ReactiveStatusListFormConsumer extends StatelessWidget { final Widget? child; final Widget Function( - BuildContext context, StatusListForm formModel, Widget? child) builder; + BuildContext context, + StatusListForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -72,11 +76,7 @@ class StatusListFormInheritedStreamer required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final StatusListForm form; } @@ -105,12 +105,15 @@ class ReactiveStatusListForm extends StatelessWidget { if (listen) { return context .dependOnInheritedWidgetOfExactType< - StatusListFormInheritedStreamer>() + StatusListFormInheritedStreamer + >() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - StatusListFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + StatusListFormInheritedStreamer + >(); return element == null ? null : (element.widget as StatusListFormInheritedStreamer).form; @@ -158,10 +161,14 @@ class StatusListFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, StatusListForm formModel, Widget? child) builder; + BuildContext context, + StatusListForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, StatusListForm formModel)? - initState; + initState; @override _StatusListFormBuilderState createState() => @@ -176,8 +183,11 @@ class _StatusListFormBuilderState @override void initState() { - _formModel = - StatusListForm(StatusListForm.formElements(widget.model), null); + _formModel = StatusListForm( + StatusListForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -255,10 +265,8 @@ final _logStatusListForm = Logger.detached('StatusListForm'); class StatusListForm implements FormModel, StatusList> { - StatusListForm( - this.form, - this.path, - ); + StatusListForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String listControlName = "list"; @@ -266,16 +274,20 @@ class StatusListForm final String? path; + // ignore: unused_field + final FormModel? _formModel; + final Map _disabled = {}; + @override + final Map initial; + String listControlPath() => pathBuilder(listControlName); List get _listValue => listControl.rawValue.whereType().toList(); List get _listRawValue => listControl.rawValue.whereType().toList(); - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsList { try { form.control(listControlPath()); @@ -294,8 +306,11 @@ class StatusListForm bool updateParent = true, bool emitEvent = true, }) { - listControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + listControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void listValuePatch( @@ -303,8 +318,11 @@ class StatusListForm bool updateParent = true, bool emitEvent = true, }) { - listControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + listControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void listValueReset( @@ -313,14 +331,13 @@ class StatusListForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - listControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => listControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormArray get listControl => form.control(listControlPath()) as FormArray; @@ -377,13 +394,15 @@ class StatusListForm break; } - listControl.add(FormControl( - value: value, - validators: resultingValidators, - asyncValidators: resultingAsyncValidators, - asyncValidatorsDebounceTime: asyncValidatorsDebounceTime ?? 250, - disabled: disabled ?? false, - )); + listControl.add( + FormControl( + value: value, + validators: resultingValidators, + asyncValidators: resultingAsyncValidators, + asyncValidatorsDebounceTime: asyncValidatorsDebounceTime ?? 250, + disabled: disabled ?? false, + ), + ); } @override @@ -406,27 +425,20 @@ class StatusListForm } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { - final currentFormInstance = currentForm; - - if (currentFormInstance is! FormGroup) { - return; - } - + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { - currentFormInstance.controls[key]?.markAsEnabled( + currentForm.controls[key]?.markAsEnabled( updateParent: updateParent, emitEvent: emitEvent, ); @@ -442,9 +454,7 @@ class StatusListForm final currentForm = this.currentForm; return const DeepCollectionEquality().equals( - currentForm is FormControlCollection - ? currentForm.rawValue - : currentForm.value, + currentForm.rawValue, StatusListForm.formElements(other).rawValue, ); } @@ -465,8 +475,16 @@ class StatusListForm } @override - AbstractControl get currentForm { - return path == null ? form : form.control(path!); + bool get hasChanged { + return !const DeepCollectionEquality().equals( + currentForm.rawValue, + initial, + ); + } + + @override + FormGroup get currentForm { + return path == null ? form : form.control(path!) as FormGroup; } @override @@ -474,49 +492,119 @@ class StatusListForm StatusList? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(StatusListForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + StatusListForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void upsertValue( + StatusList? value, { + bool updateParent = true, + bool emitEvent = true, + }) { + final formElements = StatusListForm.formElements(value); + + currentForm.addAll(formElements.controls); + } @override void reset({ StatusList? value, bool updateParent = true, bool emitEvent = true, - }) => - form.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void updateInitial(Map? value, String? path) { + if (_formModel != null) { + _formModel?.updateInitial(currentForm.rawValue, path); + return; + } + + if (value == null) return; + + if (path == null || path.isEmpty) { + initial.addAll(value); + return; + } + + final keys = path.split('.'); + Object? current = initial; + for (var i = 0; i < keys.length - 1; i++) { + final key = keys[i]; + + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current = current[index]; + continue; + } + } + + if (current is Map) { + if (!current.containsKey(key)) { + current[key] = {}; + } + current = current[key]; + continue; + } + + return; + } + + final key = keys.last; + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current[index] = value; + } + } else if (current is Map) { + current[key] = value; + } + } String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); static FormGroup formElements(StatusList? statusList) => - FormGroup({ - listControlName: FormArray( + FormGroup( + { + listControlName: FormArray( (statusList?.list ?? []) - .map((e) => FormControl( - value: e, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - )) + .map( + (e) => FormControl( + value: e, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ), + ) .toList(), validators: [], asyncValidators: [], asyncValidatorsDebounceTime: 250, - disabled: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + disabled: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } -class ReactiveStatusListFormArrayBuilder extends StatelessWidget { +class ReactiveStatusListFormArrayBuilder< + ReactiveStatusListFormArrayBuilderT, + T extends Enum +> + extends StatelessWidget { const ReactiveStatusListFormArrayBuilder({ Key? key, this.control, @@ -525,29 +613,39 @@ class ReactiveStatusListFormArrayBuilder? formControl; final FormArray? Function( - StatusListForm formModel)? control; + StatusListForm formModel, + )? + control; - final Widget Function(BuildContext context, List itemList, - StatusListForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + StatusListForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveStatusListFormArrayBuilderT? item, - StatusListForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveStatusListFormArrayBuilderT? item, + StatusListForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function(FormControl control)? - controlFilter; + controlFilter; @override Widget build(BuildContext context) { @@ -563,18 +661,9 @@ class ReactiveStatusListFormArrayBuilder( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -582,8 +671,11 @@ class ReactiveStatusListFormArrayBuilder extends StatelessWidget { +class ReactiveStatusListFormArrayBuilder2< + ReactiveStatusListFormArrayBuilderT, + T extends Enum +> + extends StatelessWidget { const ReactiveStatusListFormArrayBuilder2({ Key? key, this.control, @@ -592,35 +684,41 @@ class ReactiveStatusListFormArrayBuilder2? formControl; final FormArray? Function( - StatusListForm formModel)? control; + StatusListForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - StatusListForm formModel - }) params)? builder; + ({BuildContext context, List itemList, StatusListForm formModel}) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveStatusListFormArrayBuilderT? item, - StatusListForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveStatusListFormArrayBuilderT? item, + StatusListForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function(FormControl control)? - controlFilter; + controlFilter; @override Widget build(BuildContext context) { @@ -637,23 +735,17 @@ class ReactiveStatusListFormArrayBuilder2 builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -662,33 +754,49 @@ class ReactiveStatusListFormArrayBuilder2 extends StatelessWidget { + ReactiveStatusListFormFormGroupArrayBuilderT, + T extends Enum +> + extends StatelessWidget { const ReactiveStatusListFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? extended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(StatusListForm formModel)? + getExtended; - final ExtendedControl?>, - List> - Function(StatusListForm formModel)? getExtended; - - final Widget Function(BuildContext context, List itemList, - StatusListForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + StatusListForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveStatusListFormFormGroupArrayBuilderT? item, - StatusListForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveStatusListFormFormGroupArrayBuilderT? item, + StatusListForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -706,23 +814,14 @@ class ReactiveStatusListFormFormGroupArrayBuilder< final itemList = (value.value() ?? []) .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) .values .toList(); - return builder?.call( - context, - itemList, - formModel, - ) ?? + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/generator_tests/test/doc/generic_test.dart b/packages/generator_tests/test/doc/generic_test.dart index ef51ba9f..13d7816c 100644 --- a/packages/generator_tests/test/doc/generic_test.dart +++ b/packages/generator_tests/test/doc/generic_test.dart @@ -46,16 +46,17 @@ part of 'generic.dart'; // ************************************************************************** class ReactiveTagsFormConsumer extends StatelessWidget { - const ReactiveTagsFormConsumer({ - Key? key, - required this.builder, - this.child, - }) : super(key: key); + const ReactiveTagsFormConsumer({Key? key, required this.builder, this.child}) + : super(key: key); final Widget? child; final Widget Function( - BuildContext context, TagsForm formModel, Widget? child) builder; + BuildContext context, + TagsForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -74,11 +75,7 @@ class TagsFormInheritedStreamer extends InheritedStreamer { required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final TagsForm form; } @@ -100,18 +97,17 @@ class ReactiveTagsForm extends StatelessWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - static TagsForm? of( - BuildContext context, { - bool listen = true, - }) { + static TagsForm? of(BuildContext context, {bool listen = true}) { if (listen) { return context .dependOnInheritedWidgetOfExactType>() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - TagsFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + TagsFormInheritedStreamer + >(); return element == null ? null : (element.widget as TagsFormInheritedStreamer).form; @@ -157,7 +153,11 @@ class TagsFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, TagsForm formModel, Widget? child) builder; + BuildContext context, + TagsForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, TagsForm formModel)? initState; @@ -172,7 +172,11 @@ class _TagsFormBuilderState extends State> { @override void initState() { - _formModel = TagsForm(TagsForm.formElements(widget.model), null); + _formModel = TagsForm( + TagsForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -249,10 +253,7 @@ class _TagsFormBuilderState extends State> { final _logTagsForm = Logger.detached('TagsForm'); class TagsForm implements FormModel, Tags> { - TagsForm( - this.form, - this.path, - ); + TagsForm(this.form, this.path, this._formModel) : initial = form.rawValue; static const String tagsControlName = "tags"; @@ -260,16 +261,20 @@ class TagsForm implements FormModel, Tags> { final String? path; + // ignore: unused_field + final FormModel? _formModel; + final Map _disabled = {}; + @override + final Map initial; + String tagsControlPath() => pathBuilder(tagsControlName); - List? get _tagsValue => tagsControl.value; + List? get _tagsValue => containsTags ? tagsControl.value : null; - List? get _tagsRawValue => tagsControl.value; + List? get _tagsRawValue => containsTags ? tagsControl.value : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsTags { try { form.control(tagsControlPath()); @@ -283,12 +288,7 @@ class TagsForm implements FormModel, Tags> { void get tagsFocus => form.focus(tagsControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void tagsRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void tagsRemove({bool updateParent = true, bool emitEvent = true}) { if (containsTags) { final controlPath = path; if (controlPath == null) { @@ -316,8 +316,11 @@ class TagsForm implements FormModel, Tags> { bool updateParent = true, bool emitEvent = true, }) { - tagsControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + tagsControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void tagsValuePatch( @@ -325,8 +328,11 @@ class TagsForm implements FormModel, Tags> { bool updateParent = true, bool emitEvent = true, }) { - tagsControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + tagsControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void tagsValueReset( @@ -335,14 +341,13 @@ class TagsForm implements FormModel, Tags> { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - tagsControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => tagsControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl> get tagsControl => form.control(tagsControlPath()) as FormControl>; @@ -385,27 +390,20 @@ class TagsForm implements FormModel, Tags> { } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { - final currentFormInstance = currentForm; - - if (currentFormInstance is! FormGroup) { - return; - } - + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { - currentFormInstance.controls[key]?.markAsEnabled( + currentForm.controls[key]?.markAsEnabled( updateParent: updateParent, emitEvent: emitEvent, ); @@ -421,9 +419,7 @@ class TagsForm implements FormModel, Tags> { final currentForm = this.currentForm; return const DeepCollectionEquality().equals( - currentForm is FormControlCollection - ? currentForm.rawValue - : currentForm.value, + currentForm.rawValue, TagsForm.formElements(other).rawValue, ); } @@ -444,8 +440,16 @@ class TagsForm implements FormModel, Tags> { } @override - AbstractControl get currentForm { - return path == null ? form : form.control(path!); + bool get hasChanged { + return !const DeepCollectionEquality().equals( + currentForm.rawValue, + initial, + ); + } + + @override + FormGroup get currentForm { + return path == null ? form : form.control(path!) as FormGroup; } @override @@ -453,37 +457,102 @@ class TagsForm implements FormModel, Tags> { Tags? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(TagsForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + TagsForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void upsertValue( + Tags? value, { + bool updateParent = true, + bool emitEvent = true, + }) { + final formElements = TagsForm.formElements(value); + + currentForm.addAll(formElements.controls); + } @override void reset({ Tags? value, bool updateParent = true, bool emitEvent = true, - }) => - form.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void updateInitial(Map? value, String? path) { + if (_formModel != null) { + _formModel?.updateInitial(currentForm.rawValue, path); + return; + } + + if (value == null) return; + + if (path == null || path.isEmpty) { + initial.addAll(value); + return; + } + + final keys = path.split('.'); + Object? current = initial; + for (var i = 0; i < keys.length - 1; i++) { + final key = keys[i]; + + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current = current[index]; + continue; + } + } + + if (current is Map) { + if (!current.containsKey(key)) { + current[key] = {}; + } + current = current[key]; + continue; + } + + return; + } + + final key = keys.last; + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current[index] = value; + } + } else if (current is Map) { + current[key] = value; + } + } String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(Tags? tags) => FormGroup({ - tagsControlName: FormControl>( - value: tags?.tags, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(Tags? tags) => FormGroup( + { + tagsControlName: FormControl>( + value: tags?.tags, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } class ReactiveTagsFormArrayBuilder @@ -496,30 +565,39 @@ class ReactiveTagsFormArrayBuilder required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - TagsForm formModel)? control; + TagsForm formModel, + )? + control; final Widget Function( - BuildContext context, List itemList, TagsForm formModel)? - builder; + BuildContext context, + List itemList, + TagsForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveTagsFormArrayBuilderT? item, - TagsForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveTagsFormArrayBuilderT? item, + TagsForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function(FormControl control)? - controlFilter; + controlFilter; @override Widget build(BuildContext context) { @@ -535,18 +613,9 @@ class ReactiveTagsFormArrayBuilder return ReactiveFormArrayItemBuilder( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -564,35 +633,41 @@ class ReactiveTagsFormArrayBuilder2 required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - TagsForm formModel)? control; + TagsForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - TagsForm formModel - }) params)? builder; + ({BuildContext context, List itemList, TagsForm formModel}) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveTagsFormArrayBuilderT? item, - TagsForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveTagsFormArrayBuilderT? item, + TagsForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function(FormControl control)? - controlFilter; + controlFilter; @override Widget build(BuildContext context) { @@ -609,23 +684,17 @@ class ReactiveTagsFormArrayBuilder2 formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -634,33 +703,49 @@ class ReactiveTagsFormArrayBuilder2 } class ReactiveTagsFormFormGroupArrayBuilder< - ReactiveTagsFormFormGroupArrayBuilderT, T> extends StatelessWidget { + ReactiveTagsFormFormGroupArrayBuilderT, + T +> + extends StatelessWidget { const ReactiveTagsFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? extended; - - final ExtendedControl?>, - List> - Function(TagsForm formModel)? getExtended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(TagsForm formModel)? + getExtended; final Widget Function( - BuildContext context, List itemList, TagsForm formModel)? - builder; + BuildContext context, + List itemList, + TagsForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveTagsFormFormGroupArrayBuilderT? item, - TagsForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveTagsFormFormGroupArrayBuilderT? item, + TagsForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -678,23 +763,14 @@ class ReactiveTagsFormFormGroupArrayBuilder< final itemList = (value.value() ?? []) .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) .values .toList(); - return builder?.call( - context, - itemList, - formModel, - ) ?? + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/generator_tests/test/doc/group_output_test.dart b/packages/generator_tests/test/doc/group_output_test.dart index e75bbae1..34844d31 100644 --- a/packages/generator_tests/test/doc/group_output_test.dart +++ b/packages/generator_tests/test/doc/group_output_test.dart @@ -102,7 +102,11 @@ class ReactiveGroupOFormConsumer extends StatelessWidget { final Widget? child; final Widget Function( - BuildContext context, GroupOForm formModel, Widget? child) builder; + BuildContext context, + GroupOForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -121,11 +125,7 @@ class GroupOFormInheritedStreamer extends InheritedStreamer { required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final GroupOForm form; } @@ -147,10 +147,7 @@ class ReactiveGroupOForm extends StatelessWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - static GroupOForm? of( - BuildContext context, { - bool listen = true, - }) { + static GroupOForm? of(BuildContext context, {bool listen = true}) { if (listen) { return context .dependOnInheritedWidgetOfExactType() @@ -204,7 +201,11 @@ class GroupOFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, GroupOForm formModel, Widget? child) builder; + BuildContext context, + GroupOForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, GroupOForm formModel)? initState; @@ -296,11 +297,7 @@ class _GroupOFormBuilderState extends State { final _logGroupOForm = Logger.detached('GroupOForm'); class GroupOForm implements FormModel { - GroupOForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + GroupOForm(this.form, this.path, this._formModel) : initial = form.rawValue; static const String personalControlName = "personal"; @@ -314,7 +311,7 @@ class GroupOForm implements FormModel { final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -330,24 +327,28 @@ class GroupOForm implements FormModel { String address2ControlPath() => pathBuilder(address2ControlName); - PersonalOOutput? get _personalValue => personalForm.model; + PersonalOOutput? get _personalValue => + containsPersonal ? personalForm.model : null; - PhoneOOutput? get _phoneValue => phoneForm.model; + PhoneOOutput? get _phoneValue => containsPhone ? phoneForm.model : null; - AddressOOutput? get _addressValue => addressForm.model; + AddressOOutput? get _addressValue => + containsAddress ? addressForm.model : null; - AddressOOutput? get _address2Value => address2Form.model; + AddressOOutput? get _address2Value => + containsAddress2 ? address2Form.model : null; - PersonalO? get _personalRawValue => personalForm.rawModel; + PersonalO? get _personalRawValue => + containsPersonal ? personalForm.rawModel : null; - PhoneO? get _phoneRawValue => phoneForm.rawModel; + PhoneO? get _phoneRawValue => containsPhone ? phoneForm.rawModel : null; - AddressO? get _addressRawValue => addressForm.rawModel; + AddressO? get _addressRawValue => + containsAddress ? addressForm.rawModel : null; - AddressO? get _address2RawValue => address2Form.rawModel; + AddressO? get _address2RawValue => + containsAddress2 ? address2Form.rawModel : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsPersonal { try { form.control(personalControlPath()); @@ -357,8 +358,6 @@ class GroupOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsPhone { try { form.control(phoneControlPath()); @@ -368,8 +367,6 @@ class GroupOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsAddress { try { form.control(addressControlPath()); @@ -379,8 +376,6 @@ class GroupOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsAddress2 { try { form.control(address2ControlPath()); @@ -406,12 +401,7 @@ class GroupOForm implements FormModel { void get address2Focus => form.focus(address2ControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void personalRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void personalRemove({bool updateParent = true, bool emitEvent = true}) { if (containsPersonal) { final controlPath = path; if (controlPath == null) { @@ -434,12 +424,7 @@ class GroupOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void phoneRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void phoneRemove({bool updateParent = true, bool emitEvent = true}) { if (containsPhone) { final controlPath = path; if (controlPath == null) { @@ -462,12 +447,7 @@ class GroupOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void addressRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void addressRemove({bool updateParent = true, bool emitEvent = true}) { if (containsAddress) { final controlPath = path; if (controlPath == null) { @@ -490,12 +470,7 @@ class GroupOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void address2Remove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void address2Remove({bool updateParent = true, bool emitEvent = true}) { if (containsAddress2) { final controlPath = path; if (controlPath == null) { @@ -523,8 +498,11 @@ class GroupOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - personalControl.updateValue(PersonalOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + personalControl.updateValue( + PersonalOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void phoneValueUpdate( @@ -532,8 +510,11 @@ class GroupOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - phoneControl.updateValue(PhoneOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + phoneControl.updateValue( + PhoneOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void addressValueUpdate( @@ -541,8 +522,11 @@ class GroupOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - addressControl.updateValue(AddressOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + addressControl.updateValue( + AddressOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void address2ValueUpdate( @@ -550,8 +534,11 @@ class GroupOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - address2Control.updateValue(AddressOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + address2Control.updateValue( + AddressOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void personalValuePatch( @@ -559,8 +546,11 @@ class GroupOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - personalControl.updateValue(PersonalOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + personalControl.updateValue( + PersonalOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void phoneValuePatch( @@ -568,8 +558,11 @@ class GroupOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - phoneControl.updateValue(PhoneOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + phoneControl.updateValue( + PhoneOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void addressValuePatch( @@ -577,8 +570,11 @@ class GroupOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - addressControl.updateValue(AddressOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + addressControl.updateValue( + AddressOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void address2ValuePatch( @@ -586,8 +582,11 @@ class GroupOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - address2Control.updateValue(AddressOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + address2Control.updateValue( + AddressOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void personalValueReset( @@ -596,11 +595,11 @@ class GroupOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - personalControl.reset( - value: PersonalOForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => personalControl.reset( + value: PersonalOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); void phoneValueReset( PhoneO? value, { @@ -608,11 +607,11 @@ class GroupOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - phoneControl.reset( - value: PhoneOForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => phoneControl.reset( + value: PhoneOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); void addressValueReset( AddressO? value, { @@ -620,11 +619,11 @@ class GroupOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - addressControl.reset( - value: AddressOForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => addressControl.reset( + value: AddressOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); void address2ValueReset( AddressO? value, { @@ -632,11 +631,11 @@ class GroupOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - address2Control.reset( - value: AddressOForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => address2Control.reset( + value: AddressOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); FormGroup get personalControl => form.control(personalControlPath()) as FormGroup; @@ -746,26 +745,25 @@ class GroupOForm implements FormModel { ); } return GroupOOutput( - personal: _personalValue, - phone: _phoneValue, - address: _addressValue, - address2: _address2Value); + personal: _personalValue, + phone: _phoneValue, + address: _addressValue, + address2: _address2Value, + ); } @override GroupO get rawModel { return GroupO( - personal: _personalRawValue, - phone: _phoneRawValue, - address: _addressRawValue, - address2: _address2RawValue); + personal: _personalRawValue, + phone: _phoneRawValue, + address: _addressRawValue, + address2: _address2RawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; @@ -776,7 +774,9 @@ class GroupOForm implements FormModel { addressForm.toggleDisabled(); address2Form.toggleDisabled(); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { personalForm.toggleDisabled(); phoneForm.toggleDisabled(); @@ -838,9 +838,11 @@ class GroupOForm implements FormModel { GroupO? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(GroupOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + GroupOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -858,17 +860,14 @@ class GroupOForm implements FormModel { GroupO? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -919,26 +918,25 @@ class GroupOForm implements FormModel { String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(GroupO? groupO) => FormGroup({ - personalControlName: PersonalOForm.formElements(groupO?.personal), - phoneControlName: PhoneOForm.formElements(groupO?.phone), - addressControlName: AddressOForm.formElements(groupO?.address), - address2ControlName: AddressOForm.formElements(groupO?.address2) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(GroupO? groupO) => FormGroup( + { + personalControlName: PersonalOForm.formElements(groupO?.personal), + phoneControlName: PhoneOForm.formElements(groupO?.phone), + addressControlName: AddressOForm.formElements(groupO?.address), + address2ControlName: AddressOForm.formElements(groupO?.address2), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } final _logPersonalOForm = Logger.detached('PersonalOForm'); class PersonalOForm implements FormModel { - PersonalOForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + PersonalOForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String nameControlName = "name"; @@ -948,7 +946,7 @@ class PersonalOForm implements FormModel { final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -960,16 +958,14 @@ class PersonalOForm implements FormModel { String emailControlPath() => pathBuilder(emailControlName); - String? get _nameValue => nameControl.value; + String? get _nameValue => containsName ? nameControl.value : null; - String? get _emailValue => emailControl.value; + String? get _emailValue => containsEmail ? emailControl.value : null; - String? get _nameRawValue => nameControl.value; + String? get _nameRawValue => containsName ? nameControl.value : null; - String? get _emailRawValue => emailControl.value; + String? get _emailRawValue => containsEmail ? emailControl.value : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsName { try { form.control(nameControlPath()); @@ -979,8 +975,6 @@ class PersonalOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsEmail { try { form.control(emailControlPath()); @@ -998,12 +992,7 @@ class PersonalOForm implements FormModel { void get emailFocus => form.focus(emailControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void nameRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void nameRemove({bool updateParent = true, bool emitEvent = true}) { if (containsName) { final controlPath = path; if (controlPath == null) { @@ -1026,12 +1015,7 @@ class PersonalOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void emailRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void emailRemove({bool updateParent = true, bool emitEvent = true}) { if (containsEmail) { final controlPath = path; if (controlPath == null) { @@ -1059,8 +1043,11 @@ class PersonalOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - nameControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + nameControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailValueUpdate( @@ -1068,8 +1055,11 @@ class PersonalOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - emailControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void nameValuePatch( @@ -1077,8 +1067,11 @@ class PersonalOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - nameControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + nameControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailValuePatch( @@ -1086,8 +1079,11 @@ class PersonalOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - emailControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void nameValueReset( @@ -1096,14 +1092,13 @@ class PersonalOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - nameControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => nameControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void emailValueReset( String? value, { @@ -1111,14 +1106,13 @@ class PersonalOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - emailControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => emailControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get nameControl => form.control(nameControlPath()) as FormControl; @@ -1183,17 +1177,16 @@ class PersonalOForm implements FormModel { } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { @@ -1251,9 +1244,11 @@ class PersonalOForm implements FormModel { PersonalO? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(PersonalOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + PersonalOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -1271,17 +1266,14 @@ class PersonalOForm implements FormModel { PersonalO? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -1332,36 +1324,36 @@ class PersonalOForm implements FormModel { String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(PersonalO? personalO) => FormGroup({ - nameControlName: FormControl( - value: personalO?.name, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - emailControlName: FormControl( - value: personalO?.email, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(PersonalO? personalO) => FormGroup( + { + nameControlName: FormControl( + value: personalO?.name, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + emailControlName: FormControl( + value: personalO?.email, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } final _logPhoneOForm = Logger.detached('PhoneOForm'); class PhoneOForm implements FormModel { - PhoneOForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + PhoneOForm(this.form, this.path, this._formModel) : initial = form.rawValue; static const String phoneNumberControlName = "phoneNumber"; @@ -1371,7 +1363,7 @@ class PhoneOForm implements FormModel { final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -1383,16 +1375,18 @@ class PhoneOForm implements FormModel { String countryIsoControlPath() => pathBuilder(countryIsoControlName); - String? get _phoneNumberValue => phoneNumberControl.value; + String? get _phoneNumberValue => + containsPhoneNumber ? phoneNumberControl.value : null; - String? get _countryIsoValue => countryIsoControl.value; + String? get _countryIsoValue => + containsCountryIso ? countryIsoControl.value : null; - String? get _phoneNumberRawValue => phoneNumberControl.value; + String? get _phoneNumberRawValue => + containsPhoneNumber ? phoneNumberControl.value : null; - String? get _countryIsoRawValue => countryIsoControl.value; + String? get _countryIsoRawValue => + containsCountryIso ? countryIsoControl.value : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsPhoneNumber { try { form.control(phoneNumberControlPath()); @@ -1402,8 +1396,6 @@ class PhoneOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsCountryIso { try { form.control(countryIsoControlPath()); @@ -1421,12 +1413,7 @@ class PhoneOForm implements FormModel { void get countryIsoFocus => form.focus(countryIsoControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void phoneNumberRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void phoneNumberRemove({bool updateParent = true, bool emitEvent = true}) { if (containsPhoneNumber) { final controlPath = path; if (controlPath == null) { @@ -1449,12 +1436,7 @@ class PhoneOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void countryIsoRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void countryIsoRemove({bool updateParent = true, bool emitEvent = true}) { if (containsCountryIso) { final controlPath = path; if (controlPath == null) { @@ -1482,8 +1464,11 @@ class PhoneOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - phoneNumberControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + phoneNumberControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void countryIsoValueUpdate( @@ -1491,8 +1476,11 @@ class PhoneOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - countryIsoControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + countryIsoControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void phoneNumberValuePatch( @@ -1500,8 +1488,11 @@ class PhoneOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - phoneNumberControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + phoneNumberControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void countryIsoValuePatch( @@ -1509,8 +1500,11 @@ class PhoneOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - countryIsoControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + countryIsoControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void phoneNumberValueReset( @@ -1519,14 +1513,13 @@ class PhoneOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - phoneNumberControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => phoneNumberControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void countryIsoValueReset( String? value, { @@ -1534,14 +1527,13 @@ class PhoneOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - countryIsoControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => countryIsoControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get phoneNumberControl => form.control(phoneNumberControlPath()) as FormControl; @@ -1598,27 +1590,30 @@ class PhoneOForm implements FormModel { ); } return PhoneOOutput( - phoneNumber: _phoneNumberValue, countryIso: _countryIsoValue); + phoneNumber: _phoneNumberValue, + countryIso: _countryIsoValue, + ); } @override PhoneO get rawModel { return PhoneO( - phoneNumber: _phoneNumberRawValue, countryIso: _countryIsoRawValue); + phoneNumber: _phoneNumberRawValue, + countryIso: _countryIsoRawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { @@ -1676,9 +1671,11 @@ class PhoneOForm implements FormModel { PhoneO? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(PhoneOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + PhoneOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -1696,17 +1693,14 @@ class PhoneOForm implements FormModel { PhoneO? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -1757,36 +1751,36 @@ class PhoneOForm implements FormModel { String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(PhoneO? phoneO) => FormGroup({ - phoneNumberControlName: FormControl( - value: phoneO?.phoneNumber, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - countryIsoControlName: FormControl( - value: phoneO?.countryIso, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(PhoneO? phoneO) => FormGroup( + { + phoneNumberControlName: FormControl( + value: phoneO?.phoneNumber, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + countryIsoControlName: FormControl( + value: phoneO?.countryIso, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } final _logAddressOForm = Logger.detached('AddressOForm'); class AddressOForm implements FormModel { - AddressOForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + AddressOForm(this.form, this.path, this._formModel) : initial = form.rawValue; static const String streetControlName = "street"; @@ -1798,7 +1792,7 @@ class AddressOForm implements FormModel { final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -1812,20 +1806,18 @@ class AddressOForm implements FormModel { String zipControlPath() => pathBuilder(zipControlName); - String? get _streetValue => streetControl.value; + String? get _streetValue => containsStreet ? streetControl.value : null; - String? get _cityValue => cityControl.value; + String? get _cityValue => containsCity ? cityControl.value : null; - String? get _zipValue => zipControl.value; + String? get _zipValue => containsZip ? zipControl.value : null; - String? get _streetRawValue => streetControl.value; + String? get _streetRawValue => containsStreet ? streetControl.value : null; - String? get _cityRawValue => cityControl.value; + String? get _cityRawValue => containsCity ? cityControl.value : null; - String? get _zipRawValue => zipControl.value; + String? get _zipRawValue => containsZip ? zipControl.value : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsStreet { try { form.control(streetControlPath()); @@ -1835,8 +1827,6 @@ class AddressOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsCity { try { form.control(cityControlPath()); @@ -1846,8 +1836,6 @@ class AddressOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsZip { try { form.control(zipControlPath()); @@ -1869,12 +1857,7 @@ class AddressOForm implements FormModel { void get zipFocus => form.focus(zipControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void streetRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void streetRemove({bool updateParent = true, bool emitEvent = true}) { if (containsStreet) { final controlPath = path; if (controlPath == null) { @@ -1897,12 +1880,7 @@ class AddressOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void cityRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void cityRemove({bool updateParent = true, bool emitEvent = true}) { if (containsCity) { final controlPath = path; if (controlPath == null) { @@ -1925,12 +1903,7 @@ class AddressOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void zipRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void zipRemove({bool updateParent = true, bool emitEvent = true}) { if (containsZip) { final controlPath = path; if (controlPath == null) { @@ -1958,8 +1931,11 @@ class AddressOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - streetControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + streetControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void cityValueUpdate( @@ -1967,8 +1943,11 @@ class AddressOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - cityControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + cityControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void zipValueUpdate( @@ -1976,8 +1955,11 @@ class AddressOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - zipControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + zipControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void streetValuePatch( @@ -1985,8 +1967,11 @@ class AddressOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - streetControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + streetControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void cityValuePatch( @@ -1994,8 +1979,11 @@ class AddressOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - cityControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + cityControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void zipValuePatch( @@ -2003,8 +1991,11 @@ class AddressOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - zipControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + zipControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void streetValueReset( @@ -2013,14 +2004,13 @@ class AddressOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - streetControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => streetControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void cityValueReset( String? value, { @@ -2028,14 +2018,13 @@ class AddressOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - cityControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => cityControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void zipValueReset( String? value, { @@ -2043,14 +2032,13 @@ class AddressOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - zipControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => zipControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get streetControl => form.control(streetControlPath()) as FormControl; @@ -2128,27 +2116,32 @@ class AddressOForm implements FormModel { ); } return AddressOOutput( - street: _streetValue, city: _cityValue, zip: _zipValue); + street: _streetValue, + city: _cityValue, + zip: _zipValue, + ); } @override AddressO get rawModel { return AddressO( - street: _streetRawValue, city: _cityRawValue, zip: _zipRawValue); + street: _streetRawValue, + city: _cityRawValue, + zip: _zipRawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { @@ -2206,9 +2199,11 @@ class AddressOForm implements FormModel { AddressO? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(AddressOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + AddressOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -2226,17 +2221,14 @@ class AddressOForm implements FormModel { AddressO? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -2287,33 +2279,38 @@ class AddressOForm implements FormModel { String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(AddressO? addressO) => FormGroup({ - streetControlName: FormControl( - value: addressO?.street, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - cityControlName: FormControl( - value: addressO?.city, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - zipControlName: FormControl( - value: addressO?.zip, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(AddressO? addressO) => FormGroup( + { + streetControlName: FormControl( + value: addressO?.street, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + cityControlName: FormControl( + value: addressO?.city, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + zipControlName: FormControl( + value: addressO?.zip, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } @Rf(output: true) @@ -2329,17 +2326,20 @@ class GroupOOutput { class PersonalOOutput { final String? name; final String? email; - PersonalOOutput( - {@RfControl() this.name, @RfControl() this.email}); + PersonalOOutput({ + @RfControl() this.name, + @RfControl() this.email, + }); } @RfGroup() class PhoneOOutput { final String? phoneNumber; final String? countryIso; - PhoneOOutput( - {@RfControl() this.phoneNumber, - @RfControl() this.countryIso}); + PhoneOOutput({ + @RfControl() this.phoneNumber, + @RfControl() this.countryIso, + }); } @RfGroup() @@ -2347,10 +2347,11 @@ class AddressOOutput { final String? street; final String? city; final String? zip; - AddressOOutput( - {@RfControl() this.street, - @RfControl() this.city, - @RfControl() this.zip}); + AddressOOutput({ + @RfControl() this.street, + @RfControl() this.city, + @RfControl() this.zip, + }); } class ReactiveGroupOFormArrayBuilder @@ -2363,30 +2364,39 @@ class ReactiveGroupOFormArrayBuilder required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - GroupOForm formModel)? control; + GroupOForm formModel, + )? + control; final Widget Function( - BuildContext context, List itemList, GroupOForm formModel)? - builder; + BuildContext context, + List itemList, + GroupOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveGroupOFormArrayBuilderT? item, - GroupOForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveGroupOFormArrayBuilderT? item, + GroupOForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function(FormControl control)? - controlFilter; + controlFilter; @override Widget build(BuildContext context) { @@ -2402,18 +2412,9 @@ class ReactiveGroupOFormArrayBuilder return ReactiveFormArrayItemBuilder( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -2431,35 +2432,41 @@ class ReactiveGroupOFormArrayBuilder2 required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - GroupOForm formModel)? control; + GroupOForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - GroupOForm formModel - }) params)? builder; + ({BuildContext context, List itemList, GroupOForm formModel}) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveGroupOFormArrayBuilderT? item, - GroupOForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveGroupOFormArrayBuilderT? item, + GroupOForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function(FormControl control)? - controlFilter; + controlFilter; @override Widget build(BuildContext context) { @@ -2476,23 +2483,17 @@ class ReactiveGroupOFormArrayBuilder2 formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -2501,33 +2502,48 @@ class ReactiveGroupOFormArrayBuilder2 } class ReactiveGroupOFormFormGroupArrayBuilder< - ReactiveGroupOFormFormGroupArrayBuilderT> extends StatelessWidget { + ReactiveGroupOFormFormGroupArrayBuilderT +> + extends StatelessWidget { const ReactiveGroupOFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? extended; - - final ExtendedControl?>, - List> - Function(GroupOForm formModel)? getExtended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(GroupOForm formModel)? + getExtended; final Widget Function( - BuildContext context, List itemList, GroupOForm formModel)? - builder; + BuildContext context, + List itemList, + GroupOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveGroupOFormFormGroupArrayBuilderT? item, - GroupOForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveGroupOFormFormGroupArrayBuilderT? item, + GroupOForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -2545,23 +2561,14 @@ class ReactiveGroupOFormFormGroupArrayBuilder< final itemList = (value.value() ?? []) .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) .values .toList(); - return builder?.call( - context, - itemList, - formModel, - ) ?? + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/generator_tests/test/doc/group_test.dart b/packages/generator_tests/test/doc/group_test.dart index e258062c..5befe174 100644 --- a/packages/generator_tests/test/doc/group_test.dart +++ b/packages/generator_tests/test/doc/group_test.dart @@ -93,16 +93,17 @@ part of 'group.dart'; // ************************************************************************** class ReactiveGroupFormConsumer extends StatelessWidget { - const ReactiveGroupFormConsumer({ - Key? key, - required this.builder, - this.child, - }) : super(key: key); + const ReactiveGroupFormConsumer({Key? key, required this.builder, this.child}) + : super(key: key); final Widget? child; final Widget Function( - BuildContext context, GroupForm formModel, Widget? child) builder; + BuildContext context, + GroupForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -121,11 +122,7 @@ class GroupFormInheritedStreamer extends InheritedStreamer { required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final GroupForm form; } @@ -147,10 +144,7 @@ class ReactiveGroupForm extends StatelessWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - static GroupForm? of( - BuildContext context, { - bool listen = true, - }) { + static GroupForm? of(BuildContext context, {bool listen = true}) { if (listen) { return context .dependOnInheritedWidgetOfExactType() @@ -204,7 +198,11 @@ class GroupFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, GroupForm formModel, Widget? child) builder; + BuildContext context, + GroupForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, GroupForm formModel)? initState; @@ -296,11 +294,7 @@ class _GroupFormBuilderState extends State { final _logGroupForm = Logger.detached('GroupForm'); class GroupForm implements FormModel { - GroupForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + GroupForm(this.form, this.path, this._formModel) : initial = form.rawValue; static const String personalControlName = "personal"; @@ -314,7 +308,7 @@ class GroupForm implements FormModel { final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -330,24 +324,25 @@ class GroupForm implements FormModel { String address2ControlPath() => pathBuilder(address2ControlName); - Personal? get _personalValue => personalForm.model; + Personal? get _personalValue => containsPersonal ? personalForm.model : null; - Phone? get _phoneValue => phoneForm.model; + Phone? get _phoneValue => containsPhone ? phoneForm.model : null; - Address? get _addressValue => addressForm.model; + Address? get _addressValue => containsAddress ? addressForm.model : null; - Address? get _address2Value => address2Form.model; + Address? get _address2Value => containsAddress2 ? address2Form.model : null; - Personal? get _personalRawValue => personalForm.rawModel; + Personal? get _personalRawValue => + containsPersonal ? personalForm.rawModel : null; - Phone? get _phoneRawValue => phoneForm.rawModel; + Phone? get _phoneRawValue => containsPhone ? phoneForm.rawModel : null; - Address? get _addressRawValue => addressForm.rawModel; + Address? get _addressRawValue => + containsAddress ? addressForm.rawModel : null; - Address? get _address2RawValue => address2Form.rawModel; + Address? get _address2RawValue => + containsAddress2 ? address2Form.rawModel : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsPersonal { try { form.control(personalControlPath()); @@ -357,8 +352,6 @@ class GroupForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsPhone { try { form.control(phoneControlPath()); @@ -368,8 +361,6 @@ class GroupForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsAddress { try { form.control(addressControlPath()); @@ -379,8 +370,6 @@ class GroupForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsAddress2 { try { form.control(address2ControlPath()); @@ -406,12 +395,7 @@ class GroupForm implements FormModel { void get address2Focus => form.focus(address2ControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void personalRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void personalRemove({bool updateParent = true, bool emitEvent = true}) { if (containsPersonal) { final controlPath = path; if (controlPath == null) { @@ -434,12 +418,7 @@ class GroupForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void phoneRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void phoneRemove({bool updateParent = true, bool emitEvent = true}) { if (containsPhone) { final controlPath = path; if (controlPath == null) { @@ -462,12 +441,7 @@ class GroupForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void addressRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void addressRemove({bool updateParent = true, bool emitEvent = true}) { if (containsAddress) { final controlPath = path; if (controlPath == null) { @@ -490,12 +464,7 @@ class GroupForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void address2Remove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void address2Remove({bool updateParent = true, bool emitEvent = true}) { if (containsAddress2) { final controlPath = path; if (controlPath == null) { @@ -523,8 +492,11 @@ class GroupForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - personalControl.updateValue(PersonalForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + personalControl.updateValue( + PersonalForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void phoneValueUpdate( @@ -532,8 +504,11 @@ class GroupForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - phoneControl.updateValue(PhoneForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + phoneControl.updateValue( + PhoneForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void addressValueUpdate( @@ -541,8 +516,11 @@ class GroupForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - addressControl.updateValue(AddressForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + addressControl.updateValue( + AddressForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void address2ValueUpdate( @@ -550,8 +528,11 @@ class GroupForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - address2Control.updateValue(AddressForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + address2Control.updateValue( + AddressForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void personalValuePatch( @@ -559,8 +540,11 @@ class GroupForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - personalControl.updateValue(PersonalForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + personalControl.updateValue( + PersonalForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void phoneValuePatch( @@ -568,8 +552,11 @@ class GroupForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - phoneControl.updateValue(PhoneForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + phoneControl.updateValue( + PhoneForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void addressValuePatch( @@ -577,8 +564,11 @@ class GroupForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - addressControl.updateValue(AddressForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + addressControl.updateValue( + AddressForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void address2ValuePatch( @@ -586,8 +576,11 @@ class GroupForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - address2Control.updateValue(AddressForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + address2Control.updateValue( + AddressForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void personalValueReset( @@ -596,11 +589,11 @@ class GroupForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - personalControl.reset( - value: PersonalForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => personalControl.reset( + value: PersonalForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); void phoneValueReset( Phone? value, { @@ -608,11 +601,11 @@ class GroupForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - phoneControl.reset( - value: PhoneForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => phoneControl.reset( + value: PhoneForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); void addressValueReset( Address? value, { @@ -620,11 +613,11 @@ class GroupForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - addressControl.reset( - value: AddressForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => addressControl.reset( + value: AddressForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); void address2ValueReset( Address? value, { @@ -632,11 +625,11 @@ class GroupForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - address2Control.reset( - value: AddressForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => address2Control.reset( + value: AddressForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); FormGroup get personalControl => form.control(personalControlPath()) as FormGroup; @@ -745,26 +738,25 @@ class GroupForm implements FormModel { ); } return Group( - personal: _personalValue, - phone: _phoneValue, - address: _addressValue, - address2: _address2Value); + personal: _personalValue, + phone: _phoneValue, + address: _addressValue, + address2: _address2Value, + ); } @override Group get rawModel { return Group( - personal: _personalRawValue, - phone: _phoneRawValue, - address: _addressRawValue, - address2: _address2RawValue); + personal: _personalRawValue, + phone: _phoneRawValue, + address: _addressRawValue, + address2: _address2RawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; @@ -775,7 +767,9 @@ class GroupForm implements FormModel { addressForm.toggleDisabled(); address2Form.toggleDisabled(); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { personalForm.toggleDisabled(); phoneForm.toggleDisabled(); @@ -837,9 +831,11 @@ class GroupForm implements FormModel { Group? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(GroupForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + GroupForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -853,21 +849,15 @@ class GroupForm implements FormModel { } @override - void reset({ - Group? value, - bool updateParent = true, - bool emitEvent = true, - }) => + void reset({Group? value, bool updateParent = true, bool emitEvent = true}) => currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -918,26 +908,24 @@ class GroupForm implements FormModel { String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(Group? group) => FormGroup({ - personalControlName: PersonalForm.formElements(group?.personal), - phoneControlName: PhoneForm.formElements(group?.phone), - addressControlName: AddressForm.formElements(group?.address), - address2ControlName: AddressForm.formElements(group?.address2) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(Group? group) => FormGroup( + { + personalControlName: PersonalForm.formElements(group?.personal), + phoneControlName: PhoneForm.formElements(group?.phone), + addressControlName: AddressForm.formElements(group?.address), + address2ControlName: AddressForm.formElements(group?.address2), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } final _logPersonalForm = Logger.detached('PersonalForm'); class PersonalForm implements FormModel { - PersonalForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + PersonalForm(this.form, this.path, this._formModel) : initial = form.rawValue; static const String nameControlName = "name"; @@ -947,7 +935,7 @@ class PersonalForm implements FormModel { final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -959,16 +947,14 @@ class PersonalForm implements FormModel { String emailControlPath() => pathBuilder(emailControlName); - String? get _nameValue => nameControl.value; + String? get _nameValue => containsName ? nameControl.value : null; - String? get _emailValue => emailControl.value; + String? get _emailValue => containsEmail ? emailControl.value : null; - String? get _nameRawValue => nameControl.value; + String? get _nameRawValue => containsName ? nameControl.value : null; - String? get _emailRawValue => emailControl.value; + String? get _emailRawValue => containsEmail ? emailControl.value : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsName { try { form.control(nameControlPath()); @@ -978,8 +964,6 @@ class PersonalForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsEmail { try { form.control(emailControlPath()); @@ -997,12 +981,7 @@ class PersonalForm implements FormModel { void get emailFocus => form.focus(emailControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void nameRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void nameRemove({bool updateParent = true, bool emitEvent = true}) { if (containsName) { final controlPath = path; if (controlPath == null) { @@ -1025,12 +1004,7 @@ class PersonalForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void emailRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void emailRemove({bool updateParent = true, bool emitEvent = true}) { if (containsEmail) { final controlPath = path; if (controlPath == null) { @@ -1058,8 +1032,11 @@ class PersonalForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - nameControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + nameControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailValueUpdate( @@ -1067,8 +1044,11 @@ class PersonalForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - emailControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void nameValuePatch( @@ -1076,8 +1056,11 @@ class PersonalForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - nameControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + nameControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailValuePatch( @@ -1085,8 +1068,11 @@ class PersonalForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - emailControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void nameValueReset( @@ -1095,14 +1081,13 @@ class PersonalForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - nameControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => nameControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void emailValueReset( String? value, { @@ -1110,14 +1095,13 @@ class PersonalForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - emailControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => emailControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get nameControl => form.control(nameControlPath()) as FormControl; @@ -1181,17 +1165,16 @@ class PersonalForm implements FormModel { } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { @@ -1249,9 +1232,11 @@ class PersonalForm implements FormModel { Personal? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(PersonalForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + PersonalForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -1269,17 +1254,14 @@ class PersonalForm implements FormModel { Personal? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -1330,36 +1312,36 @@ class PersonalForm implements FormModel { String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(Personal? personal) => FormGroup({ - nameControlName: FormControl( - value: personal?.name, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - emailControlName: FormControl( - value: personal?.email, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(Personal? personal) => FormGroup( + { + nameControlName: FormControl( + value: personal?.name, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + emailControlName: FormControl( + value: personal?.email, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } final _logPhoneForm = Logger.detached('PhoneForm'); class PhoneForm implements FormModel { - PhoneForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + PhoneForm(this.form, this.path, this._formModel) : initial = form.rawValue; static const String phoneNumberControlName = "phoneNumber"; @@ -1369,7 +1351,7 @@ class PhoneForm implements FormModel { final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -1381,16 +1363,18 @@ class PhoneForm implements FormModel { String countryIsoControlPath() => pathBuilder(countryIsoControlName); - String? get _phoneNumberValue => phoneNumberControl.value; + String? get _phoneNumberValue => + containsPhoneNumber ? phoneNumberControl.value : null; - String? get _countryIsoValue => countryIsoControl.value; + String? get _countryIsoValue => + containsCountryIso ? countryIsoControl.value : null; - String? get _phoneNumberRawValue => phoneNumberControl.value; + String? get _phoneNumberRawValue => + containsPhoneNumber ? phoneNumberControl.value : null; - String? get _countryIsoRawValue => countryIsoControl.value; + String? get _countryIsoRawValue => + containsCountryIso ? countryIsoControl.value : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsPhoneNumber { try { form.control(phoneNumberControlPath()); @@ -1400,8 +1384,6 @@ class PhoneForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsCountryIso { try { form.control(countryIsoControlPath()); @@ -1419,12 +1401,7 @@ class PhoneForm implements FormModel { void get countryIsoFocus => form.focus(countryIsoControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void phoneNumberRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void phoneNumberRemove({bool updateParent = true, bool emitEvent = true}) { if (containsPhoneNumber) { final controlPath = path; if (controlPath == null) { @@ -1447,12 +1424,7 @@ class PhoneForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void countryIsoRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void countryIsoRemove({bool updateParent = true, bool emitEvent = true}) { if (containsCountryIso) { final controlPath = path; if (controlPath == null) { @@ -1480,8 +1452,11 @@ class PhoneForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - phoneNumberControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + phoneNumberControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void countryIsoValueUpdate( @@ -1489,8 +1464,11 @@ class PhoneForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - countryIsoControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + countryIsoControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void phoneNumberValuePatch( @@ -1498,8 +1476,11 @@ class PhoneForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - phoneNumberControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + phoneNumberControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void countryIsoValuePatch( @@ -1507,8 +1488,11 @@ class PhoneForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - countryIsoControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + countryIsoControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void phoneNumberValueReset( @@ -1517,14 +1501,13 @@ class PhoneForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - phoneNumberControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => phoneNumberControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void countryIsoValueReset( String? value, { @@ -1532,14 +1515,13 @@ class PhoneForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - countryIsoControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => countryIsoControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get phoneNumberControl => form.control(phoneNumberControlPath()) as FormControl; @@ -1600,21 +1582,22 @@ class PhoneForm implements FormModel { @override Phone get rawModel { return Phone( - phoneNumber: _phoneNumberRawValue, countryIso: _countryIsoRawValue); + phoneNumber: _phoneNumberRawValue, + countryIso: _countryIsoRawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { @@ -1672,9 +1655,11 @@ class PhoneForm implements FormModel { Phone? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(PhoneForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + PhoneForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -1688,21 +1673,15 @@ class PhoneForm implements FormModel { } @override - void reset({ - Phone? value, - bool updateParent = true, - bool emitEvent = true, - }) => + void reset({Phone? value, bool updateParent = true, bool emitEvent = true}) => currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -1753,36 +1732,36 @@ class PhoneForm implements FormModel { String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(Phone? phone) => FormGroup({ - phoneNumberControlName: FormControl( - value: phone?.phoneNumber, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - countryIsoControlName: FormControl( - value: phone?.countryIso, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(Phone? phone) => FormGroup( + { + phoneNumberControlName: FormControl( + value: phone?.phoneNumber, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + countryIsoControlName: FormControl( + value: phone?.countryIso, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } final _logAddressForm = Logger.detached('AddressForm'); class AddressForm implements FormModel { - AddressForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + AddressForm(this.form, this.path, this._formModel) : initial = form.rawValue; static const String streetControlName = "street"; @@ -1794,7 +1773,7 @@ class AddressForm implements FormModel { final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -1808,20 +1787,18 @@ class AddressForm implements FormModel { String zipControlPath() => pathBuilder(zipControlName); - String? get _streetValue => streetControl.value; + String? get _streetValue => containsStreet ? streetControl.value : null; - String? get _cityValue => cityControl.value; + String? get _cityValue => containsCity ? cityControl.value : null; - String? get _zipValue => zipControl.value; + String? get _zipValue => containsZip ? zipControl.value : null; - String? get _streetRawValue => streetControl.value; + String? get _streetRawValue => containsStreet ? streetControl.value : null; - String? get _cityRawValue => cityControl.value; + String? get _cityRawValue => containsCity ? cityControl.value : null; - String? get _zipRawValue => zipControl.value; + String? get _zipRawValue => containsZip ? zipControl.value : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsStreet { try { form.control(streetControlPath()); @@ -1831,8 +1808,6 @@ class AddressForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsCity { try { form.control(cityControlPath()); @@ -1842,8 +1817,6 @@ class AddressForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsZip { try { form.control(zipControlPath()); @@ -1865,12 +1838,7 @@ class AddressForm implements FormModel { void get zipFocus => form.focus(zipControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void streetRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void streetRemove({bool updateParent = true, bool emitEvent = true}) { if (containsStreet) { final controlPath = path; if (controlPath == null) { @@ -1893,12 +1861,7 @@ class AddressForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void cityRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void cityRemove({bool updateParent = true, bool emitEvent = true}) { if (containsCity) { final controlPath = path; if (controlPath == null) { @@ -1921,12 +1884,7 @@ class AddressForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void zipRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void zipRemove({bool updateParent = true, bool emitEvent = true}) { if (containsZip) { final controlPath = path; if (controlPath == null) { @@ -1954,8 +1912,11 @@ class AddressForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - streetControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + streetControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void cityValueUpdate( @@ -1963,8 +1924,11 @@ class AddressForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - cityControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + cityControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void zipValueUpdate( @@ -1972,8 +1936,11 @@ class AddressForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - zipControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + zipControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void streetValuePatch( @@ -1981,8 +1948,11 @@ class AddressForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - streetControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + streetControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void cityValuePatch( @@ -1990,8 +1960,11 @@ class AddressForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - cityControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + cityControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void zipValuePatch( @@ -1999,8 +1972,11 @@ class AddressForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - zipControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + zipControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void streetValueReset( @@ -2009,14 +1985,13 @@ class AddressForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - streetControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => streetControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void cityValueReset( String? value, { @@ -2024,14 +1999,13 @@ class AddressForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - cityControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => cityControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void zipValueReset( String? value, { @@ -2039,14 +2013,13 @@ class AddressForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - zipControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => zipControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get streetControl => form.control(streetControlPath()) as FormControl; @@ -2128,21 +2101,23 @@ class AddressForm implements FormModel { @override Address get rawModel { return Address( - street: _streetRawValue, city: _cityRawValue, zip: _zipRawValue); + street: _streetRawValue, + city: _cityRawValue, + zip: _zipRawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { @@ -2200,9 +2175,11 @@ class AddressForm implements FormModel { Address? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(AddressForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + AddressForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -2220,17 +2197,14 @@ class AddressForm implements FormModel { Address? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -2281,33 +2255,38 @@ class AddressForm implements FormModel { String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(Address? address) => FormGroup({ - streetControlName: FormControl( - value: address?.street, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - cityControlName: FormControl( - value: address?.city, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - zipControlName: FormControl( - value: address?.zip, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(Address? address) => FormGroup( + { + streetControlName: FormControl( + value: address?.street, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + cityControlName: FormControl( + value: address?.city, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + zipControlName: FormControl( + value: address?.zip, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } class ReactiveGroupFormArrayBuilder @@ -2320,30 +2299,39 @@ class ReactiveGroupFormArrayBuilder required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - GroupForm formModel)? control; + GroupForm formModel, + )? + control; final Widget Function( - BuildContext context, List itemList, GroupForm formModel)? - builder; + BuildContext context, + List itemList, + GroupForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveGroupFormArrayBuilderT? item, - GroupForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveGroupFormArrayBuilderT? item, + GroupForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function(FormControl control)? - controlFilter; + controlFilter; @override Widget build(BuildContext context) { @@ -2359,18 +2347,9 @@ class ReactiveGroupFormArrayBuilder return ReactiveFormArrayItemBuilder( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -2388,35 +2367,40 @@ class ReactiveGroupFormArrayBuilder2 required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - GroupForm formModel)? control; + GroupForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - GroupForm formModel - }) params)? builder; + ({BuildContext context, List itemList, GroupForm formModel}) params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveGroupFormArrayBuilderT? item, - GroupForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveGroupFormArrayBuilderT? item, + GroupForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function(FormControl control)? - controlFilter; + controlFilter; @override Widget build(BuildContext context) { @@ -2433,23 +2417,17 @@ class ReactiveGroupFormArrayBuilder2 formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -2458,33 +2436,48 @@ class ReactiveGroupFormArrayBuilder2 } class ReactiveGroupFormFormGroupArrayBuilder< - ReactiveGroupFormFormGroupArrayBuilderT> extends StatelessWidget { + ReactiveGroupFormFormGroupArrayBuilderT +> + extends StatelessWidget { const ReactiveGroupFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? extended; - - final ExtendedControl?>, - List> - Function(GroupForm formModel)? getExtended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(GroupForm formModel)? + getExtended; final Widget Function( - BuildContext context, List itemList, GroupForm formModel)? - builder; + BuildContext context, + List itemList, + GroupForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveGroupFormFormGroupArrayBuilderT? item, - GroupForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveGroupFormFormGroupArrayBuilderT? item, + GroupForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -2502,23 +2495,14 @@ class ReactiveGroupFormFormGroupArrayBuilder< final itemList = (value.value() ?? []) .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) .values .toList(); - return builder?.call( - context, - itemList, - formModel, - ) ?? + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/generator_tests/test/doc/login_extended_nullable_output_test.dart b/packages/generator_tests/test/doc/login_extended_nullable_output_test.dart index 94392a28..7c46276a 100644 --- a/packages/generator_tests/test/doc/login_extended_nullable_output_test.dart +++ b/packages/generator_tests/test/doc/login_extended_nullable_output_test.dart @@ -73,8 +73,12 @@ class ReactiveLoginExtendedNullableOFormConsumer extends StatelessWidget { final Widget? child; - final Widget Function(BuildContext context, - LoginExtendedNullableOForm formModel, Widget? child) builder; + final Widget Function( + BuildContext context, + LoginExtendedNullableOForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -94,11 +98,7 @@ class LoginExtendedNullableOFormInheritedStreamer required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final LoginExtendedNullableOForm form; } @@ -127,12 +127,15 @@ class ReactiveLoginExtendedNullableOForm extends StatelessWidget { if (listen) { return context .dependOnInheritedWidgetOfExactType< - LoginExtendedNullableOFormInheritedStreamer>() + LoginExtendedNullableOFormInheritedStreamer + >() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - LoginExtendedNullableOFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + LoginExtendedNullableOFormInheritedStreamer + >(); return element == null ? null : (element.widget as LoginExtendedNullableOFormInheritedStreamer).form; @@ -179,11 +182,18 @@ class LoginExtendedNullableOFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - final Widget Function(BuildContext context, - LoginExtendedNullableOForm formModel, Widget? child) builder; + final Widget Function( + BuildContext context, + LoginExtendedNullableOForm formModel, + Widget? child, + ) + builder; final void Function( - BuildContext context, LoginExtendedNullableOForm formModel)? initState; + BuildContext context, + LoginExtendedNullableOForm formModel, + )? + initState; @override _LoginExtendedNullableOFormBuilderState createState() => @@ -199,7 +209,10 @@ class _LoginExtendedNullableOFormBuilderState @override void initState() { _formModel = LoginExtendedNullableOForm( - LoginExtendedNullableOForm.formElements(widget.model), null); + LoginExtendedNullableOForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -207,8 +220,9 @@ class _LoginExtendedNullableOFormBuilderState widget.initState?.call(context, _formModel); - _logSubscription = - _logLoginExtendedNullableOForm.onRecord.listen((LogRecord e) { + _logSubscription = _logLoginExtendedNullableOForm.onRecord.listen(( + LogRecord e, + ) { // use `dumpErrorToConsole` for severe messages to ensure that severe // exceptions are formatted consistently with other Flutter examples and // avoids printing duplicate exceptions @@ -274,15 +288,14 @@ class _LoginExtendedNullableOFormBuilderState } } -final _logLoginExtendedNullableOForm = - Logger.detached('LoginExtendedNullableOForm'); +final _logLoginExtendedNullableOForm = Logger.detached( + 'LoginExtendedNullableOForm', +); class LoginExtendedNullableOForm implements FormModel { - LoginExtendedNullableOForm( - this.form, - this.path, - ); + LoginExtendedNullableOForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String emailControlName = "email"; @@ -302,8 +315,14 @@ class LoginExtendedNullableOForm final String? path; + // ignore: unused_field + final FormModel? _formModel; + final Map _disabled = {}; + @override + final Map initial; + String emailControlPath() => pathBuilder(emailControlName); String passwordControlPath() => pathBuilder(passwordControlName); @@ -318,36 +337,37 @@ class LoginExtendedNullableOForm String heightControlPath() => pathBuilder(heightControlName); - String? get _emailValue => emailControl.value; + String? get _emailValue => containsEmail ? emailControl.value : null; - String? get _passwordValue => passwordControl.value; + String? get _passwordValue => containsPassword ? passwordControl.value : null; - bool? get _rememberMeValue => rememberMeControl.value; + bool? get _rememberMeValue => + containsRememberMe ? rememberMeControl.value : null; - String? get _themeValue => themeControl.value; + String? get _themeValue => containsTheme ? themeControl.value : null; - UserMode? get _modeValue => modeControl.value; + UserMode? get _modeValue => containsMode ? modeControl.value : null; - int? get _timeoutValue => timeoutControl.value; + int? get _timeoutValue => containsTimeout ? timeoutControl.value : null; - double? get _heightValue => heightControl.value; + double? get _heightValue => containsHeight ? heightControl.value : null; - String? get _emailRawValue => emailControl.value; + String? get _emailRawValue => containsEmail ? emailControl.value : null; - String? get _passwordRawValue => passwordControl.value; + String? get _passwordRawValue => + containsPassword ? passwordControl.value : null; - bool? get _rememberMeRawValue => rememberMeControl.value; + bool? get _rememberMeRawValue => + containsRememberMe ? rememberMeControl.value : null; - String? get _themeRawValue => themeControl.value; + String? get _themeRawValue => containsTheme ? themeControl.value : null; - UserMode? get _modeRawValue => modeControl.value; + UserMode? get _modeRawValue => containsMode ? modeControl.value : null; - int? get _timeoutRawValue => timeoutControl.value; + int? get _timeoutRawValue => containsTimeout ? timeoutControl.value : null; - double? get _heightRawValue => heightControl.value; + double? get _heightRawValue => containsHeight ? heightControl.value : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsEmail { try { form.control(emailControlPath()); @@ -357,8 +377,6 @@ class LoginExtendedNullableOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsPassword { try { form.control(passwordControlPath()); @@ -368,8 +386,6 @@ class LoginExtendedNullableOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsRememberMe { try { form.control(rememberMeControlPath()); @@ -379,8 +395,6 @@ class LoginExtendedNullableOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsTheme { try { form.control(themeControlPath()); @@ -390,8 +404,6 @@ class LoginExtendedNullableOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsMode { try { form.control(modeControlPath()); @@ -401,8 +413,6 @@ class LoginExtendedNullableOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsTimeout { try { form.control(timeoutControlPath()); @@ -412,8 +422,6 @@ class LoginExtendedNullableOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsHeight { try { form.control(heightControlPath()); @@ -451,12 +459,7 @@ class LoginExtendedNullableOForm void get heightFocus => form.focus(heightControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void emailRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void emailRemove({bool updateParent = true, bool emitEvent = true}) { if (containsEmail) { final controlPath = path; if (controlPath == null) { @@ -479,12 +482,7 @@ class LoginExtendedNullableOForm } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void passwordRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void passwordRemove({bool updateParent = true, bool emitEvent = true}) { if (containsPassword) { final controlPath = path; if (controlPath == null) { @@ -507,12 +505,7 @@ class LoginExtendedNullableOForm } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void rememberMeRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void rememberMeRemove({bool updateParent = true, bool emitEvent = true}) { if (containsRememberMe) { final controlPath = path; if (controlPath == null) { @@ -535,12 +528,7 @@ class LoginExtendedNullableOForm } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void themeRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void themeRemove({bool updateParent = true, bool emitEvent = true}) { if (containsTheme) { final controlPath = path; if (controlPath == null) { @@ -563,12 +551,7 @@ class LoginExtendedNullableOForm } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void modeRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void modeRemove({bool updateParent = true, bool emitEvent = true}) { if (containsMode) { final controlPath = path; if (controlPath == null) { @@ -591,12 +574,7 @@ class LoginExtendedNullableOForm } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void timeoutRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void timeoutRemove({bool updateParent = true, bool emitEvent = true}) { if (containsTimeout) { final controlPath = path; if (controlPath == null) { @@ -619,12 +597,7 @@ class LoginExtendedNullableOForm } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void heightRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void heightRemove({bool updateParent = true, bool emitEvent = true}) { if (containsHeight) { final controlPath = path; if (controlPath == null) { @@ -652,8 +625,11 @@ class LoginExtendedNullableOForm bool updateParent = true, bool emitEvent = true, }) { - emailControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void passwordValueUpdate( @@ -661,8 +637,11 @@ class LoginExtendedNullableOForm bool updateParent = true, bool emitEvent = true, }) { - passwordControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + passwordControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void rememberMeValueUpdate( @@ -670,8 +649,11 @@ class LoginExtendedNullableOForm bool updateParent = true, bool emitEvent = true, }) { - rememberMeControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + rememberMeControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void themeValueUpdate( @@ -679,8 +661,11 @@ class LoginExtendedNullableOForm bool updateParent = true, bool emitEvent = true, }) { - themeControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + themeControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void modeValueUpdate( @@ -688,8 +673,11 @@ class LoginExtendedNullableOForm bool updateParent = true, bool emitEvent = true, }) { - modeControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + modeControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void timeoutValueUpdate( @@ -697,8 +685,11 @@ class LoginExtendedNullableOForm bool updateParent = true, bool emitEvent = true, }) { - timeoutControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + timeoutControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void heightValueUpdate( @@ -706,8 +697,11 @@ class LoginExtendedNullableOForm bool updateParent = true, bool emitEvent = true, }) { - heightControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + heightControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailValuePatch( @@ -715,8 +709,11 @@ class LoginExtendedNullableOForm bool updateParent = true, bool emitEvent = true, }) { - emailControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void passwordValuePatch( @@ -724,8 +721,11 @@ class LoginExtendedNullableOForm bool updateParent = true, bool emitEvent = true, }) { - passwordControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + passwordControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void rememberMeValuePatch( @@ -733,8 +733,11 @@ class LoginExtendedNullableOForm bool updateParent = true, bool emitEvent = true, }) { - rememberMeControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + rememberMeControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void themeValuePatch( @@ -742,8 +745,11 @@ class LoginExtendedNullableOForm bool updateParent = true, bool emitEvent = true, }) { - themeControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + themeControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void modeValuePatch( @@ -751,8 +757,11 @@ class LoginExtendedNullableOForm bool updateParent = true, bool emitEvent = true, }) { - modeControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + modeControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void timeoutValuePatch( @@ -760,8 +769,11 @@ class LoginExtendedNullableOForm bool updateParent = true, bool emitEvent = true, }) { - timeoutControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + timeoutControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void heightValuePatch( @@ -769,8 +781,11 @@ class LoginExtendedNullableOForm bool updateParent = true, bool emitEvent = true, }) { - heightControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + heightControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailValueReset( @@ -779,14 +794,13 @@ class LoginExtendedNullableOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - emailControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => emailControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void passwordValueReset( String? value, { @@ -794,14 +808,13 @@ class LoginExtendedNullableOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - passwordControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => passwordControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void rememberMeValueReset( bool? value, { @@ -809,14 +822,13 @@ class LoginExtendedNullableOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - rememberMeControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => rememberMeControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void themeValueReset( String? value, { @@ -824,14 +836,13 @@ class LoginExtendedNullableOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - themeControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => themeControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void modeValueReset( UserMode? value, { @@ -839,14 +850,13 @@ class LoginExtendedNullableOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - modeControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => modeControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void timeoutValueReset( int? value, { @@ -854,14 +864,13 @@ class LoginExtendedNullableOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - timeoutControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => timeoutControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void heightValueReset( double? value, { @@ -869,14 +878,13 @@ class LoginExtendedNullableOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - heightControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => heightControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get emailControl => form.control(emailControlPath()) as FormControl; @@ -1038,49 +1046,44 @@ class LoginExtendedNullableOForm ); } return LoginExtendedNullableOOutput( - email: _emailValue, - password: _passwordValue, - rememberMe: _rememberMeValue, - theme: _themeValue, - mode: _modeValue, - timeout: _timeoutValue, - height: _heightValue); + email: _emailValue, + password: _passwordValue, + rememberMe: _rememberMeValue, + theme: _themeValue, + mode: _modeValue, + timeout: _timeoutValue, + height: _heightValue, + ); } @override LoginExtendedNullableO get rawModel { return LoginExtendedNullableO( - email: _emailRawValue, - password: _passwordRawValue, - rememberMe: _rememberMeRawValue, - theme: _themeRawValue, - mode: _modeRawValue, - timeout: _timeoutRawValue, - height: _heightRawValue); + email: _emailRawValue, + password: _passwordRawValue, + rememberMe: _rememberMeRawValue, + theme: _themeRawValue, + mode: _modeRawValue, + timeout: _timeoutRawValue, + height: _heightRawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { - final currentFormInstance = currentForm; - - if (currentFormInstance is! FormGroup) { - return; - } - + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { - currentFormInstance.controls[key]?.markAsEnabled( + currentForm.controls[key]?.markAsEnabled( updateParent: updateParent, emitEvent: emitEvent, ); @@ -1096,9 +1099,7 @@ class LoginExtendedNullableOForm final currentForm = this.currentForm; return const DeepCollectionEquality().equals( - currentForm is FormControlCollection - ? currentForm.rawValue - : currentForm.value, + currentForm.rawValue, LoginExtendedNullableOForm.formElements(other).rawValue, ); } @@ -1119,8 +1120,16 @@ class LoginExtendedNullableOForm } @override - AbstractControl get currentForm { - return path == null ? form : form.control(path!); + bool get hasChanged { + return !const DeepCollectionEquality().equals( + currentForm.rawValue, + initial, + ); + } + + @override + FormGroup get currentForm { + return path == null ? form : form.control(path!) as FormGroup; } @override @@ -1128,83 +1137,152 @@ class LoginExtendedNullableOForm LoginExtendedNullableO? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue( - LoginExtendedNullableOForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.updateValue( + LoginExtendedNullableOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void upsertValue( + LoginExtendedNullableO? value, { + bool updateParent = true, + bool emitEvent = true, + }) { + final formElements = LoginExtendedNullableOForm.formElements(value); + + currentForm.addAll(formElements.controls); + } @override void reset({ LoginExtendedNullableO? value, bool updateParent = true, bool emitEvent = true, - }) => - form.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void updateInitial(Map? value, String? path) { + if (_formModel != null) { + _formModel?.updateInitial(currentForm.rawValue, path); + return; + } + + if (value == null) return; + + if (path == null || path.isEmpty) { + initial.addAll(value); + return; + } + + final keys = path.split('.'); + Object? current = initial; + for (var i = 0; i < keys.length - 1; i++) { + final key = keys[i]; + + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current = current[index]; + continue; + } + } + + if (current is Map) { + if (!current.containsKey(key)) { + current[key] = {}; + } + current = current[key]; + continue; + } + + return; + } + + final key = keys.last; + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current[index] = value; + } + } else if (current is Map) { + current[key] = value; + } + } String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); static FormGroup formElements( - LoginExtendedNullableO? loginExtendedNullableO) => - FormGroup({ - emailControlName: FormControl( - value: loginExtendedNullableO?.email, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - passwordControlName: FormControl( - value: loginExtendedNullableO?.password, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - rememberMeControlName: FormControl( - value: loginExtendedNullableO?.rememberMe, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - themeControlName: FormControl( - value: loginExtendedNullableO?.theme, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - modeControlName: FormControl( - value: loginExtendedNullableO?.mode, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - timeoutControlName: FormControl( - value: loginExtendedNullableO?.timeout, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - heightControlName: FormControl( - value: loginExtendedNullableO?.height, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + LoginExtendedNullableO? loginExtendedNullableO, + ) => FormGroup( + { + emailControlName: FormControl( + value: loginExtendedNullableO?.email, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + passwordControlName: FormControl( + value: loginExtendedNullableO?.password, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + rememberMeControlName: FormControl( + value: loginExtendedNullableO?.rememberMe, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + themeControlName: FormControl( + value: loginExtendedNullableO?.theme, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + modeControlName: FormControl( + value: loginExtendedNullableO?.mode, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + timeoutControlName: FormControl( + value: loginExtendedNullableO?.timeout, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + heightControlName: FormControl( + value: loginExtendedNullableO?.height, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } @Rf(output: true) @@ -1216,18 +1294,21 @@ class LoginExtendedNullableOOutput { final UserMode? mode; final int? timeout; final double? height; - LoginExtendedNullableOOutput( - {@RfControl(validators: []) this.email, - @RfControl() this.password, - @RfControl() this.rememberMe, - @RfControl() this.theme, - @RfControl() this.mode, - @RfControl() this.timeout, - @RfControl() this.height}); + LoginExtendedNullableOOutput({ + @RfControl(validators: []) this.email, + @RfControl() this.password, + @RfControl() this.rememberMe, + @RfControl() this.theme, + @RfControl() this.mode, + @RfControl() this.timeout, + @RfControl() this.height, + }); } class ReactiveLoginExtendedNullableOFormArrayBuilder< - ReactiveLoginExtendedNullableOFormArrayBuilderT> extends StatelessWidget { + ReactiveLoginExtendedNullableOFormArrayBuilderT +> + extends StatelessWidget { const ReactiveLoginExtendedNullableOFormArrayBuilder({ Key? key, this.control, @@ -1236,30 +1317,41 @@ class ReactiveLoginExtendedNullableOFormArrayBuilder< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - LoginExtendedNullableOForm formModel)? control; + LoginExtendedNullableOForm formModel, + )? + control; - final Widget Function(BuildContext context, List itemList, - LoginExtendedNullableOForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + LoginExtendedNullableOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveLoginExtendedNullableOFormArrayBuilderT? item, - LoginExtendedNullableOForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveLoginExtendedNullableOFormArrayBuilderT? item, + LoginExtendedNullableOForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -1273,21 +1365,13 @@ class ReactiveLoginExtendedNullableOFormArrayBuilder< final itemBuilder = this.itemBuilder; return ReactiveFormArrayItemBuilder< - ReactiveLoginExtendedNullableOFormArrayBuilderT>( + ReactiveLoginExtendedNullableOFormArrayBuilderT + >( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1296,7 +1380,9 @@ class ReactiveLoginExtendedNullableOFormArrayBuilder< } class ReactiveLoginExtendedNullableOFormArrayBuilder2< - ReactiveLoginExtendedNullableOFormArrayBuilderT> extends StatelessWidget { + ReactiveLoginExtendedNullableOFormArrayBuilderT +> + extends StatelessWidget { const ReactiveLoginExtendedNullableOFormArrayBuilder2({ Key? key, this.control, @@ -1305,36 +1391,47 @@ class ReactiveLoginExtendedNullableOFormArrayBuilder2< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - LoginExtendedNullableOForm formModel)? control; + LoginExtendedNullableOForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - LoginExtendedNullableOForm formModel - }) params)? builder; + ({ + BuildContext context, + List itemList, + LoginExtendedNullableOForm formModel, + }) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveLoginExtendedNullableOFormArrayBuilderT? item, - LoginExtendedNullableOForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveLoginExtendedNullableOFormArrayBuilderT? item, + LoginExtendedNullableOForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -1348,27 +1445,22 @@ class ReactiveLoginExtendedNullableOFormArrayBuilder2< final itemBuilder = this.itemBuilder; return ReactiveFormArrayItemBuilder< - ReactiveLoginExtendedNullableOFormArrayBuilderT>( + ReactiveLoginExtendedNullableOFormArrayBuilderT + >( formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1377,7 +1469,8 @@ class ReactiveLoginExtendedNullableOFormArrayBuilder2< } class ReactiveLoginExtendedNullableOFormFormGroupArrayBuilder< - ReactiveLoginExtendedNullableOFormFormGroupArrayBuilderT> + ReactiveLoginExtendedNullableOFormFormGroupArrayBuilderT +> extends StatelessWidget { const ReactiveLoginExtendedNullableOFormFormGroupArrayBuilder({ Key? key, @@ -1385,25 +1478,39 @@ class ReactiveLoginExtendedNullableOFormFormGroupArrayBuilder< this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? extended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(LoginExtendedNullableOForm formModel)? + getExtended; - final ExtendedControl?>, - List> - Function(LoginExtendedNullableOForm formModel)? getExtended; - - final Widget Function(BuildContext context, List itemList, - LoginExtendedNullableOForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + LoginExtendedNullableOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveLoginExtendedNullableOFormFormGroupArrayBuilderT? item, - LoginExtendedNullableOForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveLoginExtendedNullableOFormFormGroupArrayBuilderT? item, + LoginExtendedNullableOForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -1418,26 +1525,20 @@ class ReactiveLoginExtendedNullableOFormFormGroupArrayBuilder< return StreamBuilder?>?>( stream: value.control.valueChanges, builder: (context, snapshot) { - final itemList = (value.value() ?? - []) - .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) - .values - .toList(); - - return builder?.call( - context, - itemList, - formModel, - ) ?? + final itemList = + (value.value() ?? + < + ReactiveLoginExtendedNullableOFormFormGroupArrayBuilderT + >[]) + .asMap() + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) + .values + .toList(); + + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/generator_tests/test/doc/login_extended_nullable_test.dart b/packages/generator_tests/test/doc/login_extended_nullable_test.dart index 6e1b8908..2cf420d1 100644 --- a/packages/generator_tests/test/doc/login_extended_nullable_test.dart +++ b/packages/generator_tests/test/doc/login_extended_nullable_test.dart @@ -73,8 +73,12 @@ class ReactiveLoginExtendedNullableFormConsumer extends StatelessWidget { final Widget? child; - final Widget Function(BuildContext context, - LoginExtendedNullableForm formModel, Widget? child) builder; + final Widget Function( + BuildContext context, + LoginExtendedNullableForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -94,11 +98,7 @@ class LoginExtendedNullableFormInheritedStreamer required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final LoginExtendedNullableForm form; } @@ -127,12 +127,15 @@ class ReactiveLoginExtendedNullableForm extends StatelessWidget { if (listen) { return context .dependOnInheritedWidgetOfExactType< - LoginExtendedNullableFormInheritedStreamer>() + LoginExtendedNullableFormInheritedStreamer + >() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - LoginExtendedNullableFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + LoginExtendedNullableFormInheritedStreamer + >(); return element == null ? null : (element.widget as LoginExtendedNullableFormInheritedStreamer).form; @@ -179,11 +182,18 @@ class LoginExtendedNullableFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - final Widget Function(BuildContext context, - LoginExtendedNullableForm formModel, Widget? child) builder; + final Widget Function( + BuildContext context, + LoginExtendedNullableForm formModel, + Widget? child, + ) + builder; final void Function( - BuildContext context, LoginExtendedNullableForm formModel)? initState; + BuildContext context, + LoginExtendedNullableForm formModel, + )? + initState; @override _LoginExtendedNullableFormBuilderState createState() => @@ -199,7 +209,10 @@ class _LoginExtendedNullableFormBuilderState @override void initState() { _formModel = LoginExtendedNullableForm( - LoginExtendedNullableForm.formElements(widget.model), null); + LoginExtendedNullableForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -207,8 +220,9 @@ class _LoginExtendedNullableFormBuilderState widget.initState?.call(context, _formModel); - _logSubscription = - _logLoginExtendedNullableForm.onRecord.listen((LogRecord e) { + _logSubscription = _logLoginExtendedNullableForm.onRecord.listen(( + LogRecord e, + ) { // use `dumpErrorToConsole` for severe messages to ensure that severe // exceptions are formatted consistently with other Flutter examples and // avoids printing duplicate exceptions @@ -274,15 +288,14 @@ class _LoginExtendedNullableFormBuilderState } } -final _logLoginExtendedNullableForm = - Logger.detached('LoginExtendedNullableForm'); +final _logLoginExtendedNullableForm = Logger.detached( + 'LoginExtendedNullableForm', +); class LoginExtendedNullableForm implements FormModel { - LoginExtendedNullableForm( - this.form, - this.path, - ); + LoginExtendedNullableForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String emailControlName = "email"; @@ -302,8 +315,14 @@ class LoginExtendedNullableForm final String? path; + // ignore: unused_field + final FormModel? _formModel; + final Map _disabled = {}; + @override + final Map initial; + String emailControlPath() => pathBuilder(emailControlName); String passwordControlPath() => pathBuilder(passwordControlName); @@ -318,36 +337,37 @@ class LoginExtendedNullableForm String heightControlPath() => pathBuilder(heightControlName); - String? get _emailValue => emailControl.value; + String? get _emailValue => containsEmail ? emailControl.value : null; - String? get _passwordValue => passwordControl.value; + String? get _passwordValue => containsPassword ? passwordControl.value : null; - bool? get _rememberMeValue => rememberMeControl.value; + bool? get _rememberMeValue => + containsRememberMe ? rememberMeControl.value : null; - String? get _themeValue => themeControl.value; + String? get _themeValue => containsTheme ? themeControl.value : null; - UserMode? get _modeValue => modeControl.value; + UserMode? get _modeValue => containsMode ? modeControl.value : null; - int? get _timeoutValue => timeoutControl.value; + int? get _timeoutValue => containsTimeout ? timeoutControl.value : null; - double? get _heightValue => heightControl.value; + double? get _heightValue => containsHeight ? heightControl.value : null; - String? get _emailRawValue => emailControl.value; + String? get _emailRawValue => containsEmail ? emailControl.value : null; - String? get _passwordRawValue => passwordControl.value; + String? get _passwordRawValue => + containsPassword ? passwordControl.value : null; - bool? get _rememberMeRawValue => rememberMeControl.value; + bool? get _rememberMeRawValue => + containsRememberMe ? rememberMeControl.value : null; - String? get _themeRawValue => themeControl.value; + String? get _themeRawValue => containsTheme ? themeControl.value : null; - UserMode? get _modeRawValue => modeControl.value; + UserMode? get _modeRawValue => containsMode ? modeControl.value : null; - int? get _timeoutRawValue => timeoutControl.value; + int? get _timeoutRawValue => containsTimeout ? timeoutControl.value : null; - double? get _heightRawValue => heightControl.value; + double? get _heightRawValue => containsHeight ? heightControl.value : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsEmail { try { form.control(emailControlPath()); @@ -357,8 +377,6 @@ class LoginExtendedNullableForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsPassword { try { form.control(passwordControlPath()); @@ -368,8 +386,6 @@ class LoginExtendedNullableForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsRememberMe { try { form.control(rememberMeControlPath()); @@ -379,8 +395,6 @@ class LoginExtendedNullableForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsTheme { try { form.control(themeControlPath()); @@ -390,8 +404,6 @@ class LoginExtendedNullableForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsMode { try { form.control(modeControlPath()); @@ -401,8 +413,6 @@ class LoginExtendedNullableForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsTimeout { try { form.control(timeoutControlPath()); @@ -412,8 +422,6 @@ class LoginExtendedNullableForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsHeight { try { form.control(heightControlPath()); @@ -451,12 +459,7 @@ class LoginExtendedNullableForm void get heightFocus => form.focus(heightControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void emailRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void emailRemove({bool updateParent = true, bool emitEvent = true}) { if (containsEmail) { final controlPath = path; if (controlPath == null) { @@ -479,12 +482,7 @@ class LoginExtendedNullableForm } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void passwordRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void passwordRemove({bool updateParent = true, bool emitEvent = true}) { if (containsPassword) { final controlPath = path; if (controlPath == null) { @@ -507,12 +505,7 @@ class LoginExtendedNullableForm } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void rememberMeRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void rememberMeRemove({bool updateParent = true, bool emitEvent = true}) { if (containsRememberMe) { final controlPath = path; if (controlPath == null) { @@ -535,12 +528,7 @@ class LoginExtendedNullableForm } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void themeRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void themeRemove({bool updateParent = true, bool emitEvent = true}) { if (containsTheme) { final controlPath = path; if (controlPath == null) { @@ -563,12 +551,7 @@ class LoginExtendedNullableForm } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void modeRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void modeRemove({bool updateParent = true, bool emitEvent = true}) { if (containsMode) { final controlPath = path; if (controlPath == null) { @@ -591,12 +574,7 @@ class LoginExtendedNullableForm } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void timeoutRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void timeoutRemove({bool updateParent = true, bool emitEvent = true}) { if (containsTimeout) { final controlPath = path; if (controlPath == null) { @@ -619,12 +597,7 @@ class LoginExtendedNullableForm } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void heightRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void heightRemove({bool updateParent = true, bool emitEvent = true}) { if (containsHeight) { final controlPath = path; if (controlPath == null) { @@ -652,8 +625,11 @@ class LoginExtendedNullableForm bool updateParent = true, bool emitEvent = true, }) { - emailControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void passwordValueUpdate( @@ -661,8 +637,11 @@ class LoginExtendedNullableForm bool updateParent = true, bool emitEvent = true, }) { - passwordControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + passwordControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void rememberMeValueUpdate( @@ -670,8 +649,11 @@ class LoginExtendedNullableForm bool updateParent = true, bool emitEvent = true, }) { - rememberMeControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + rememberMeControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void themeValueUpdate( @@ -679,8 +661,11 @@ class LoginExtendedNullableForm bool updateParent = true, bool emitEvent = true, }) { - themeControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + themeControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void modeValueUpdate( @@ -688,8 +673,11 @@ class LoginExtendedNullableForm bool updateParent = true, bool emitEvent = true, }) { - modeControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + modeControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void timeoutValueUpdate( @@ -697,8 +685,11 @@ class LoginExtendedNullableForm bool updateParent = true, bool emitEvent = true, }) { - timeoutControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + timeoutControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void heightValueUpdate( @@ -706,8 +697,11 @@ class LoginExtendedNullableForm bool updateParent = true, bool emitEvent = true, }) { - heightControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + heightControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailValuePatch( @@ -715,8 +709,11 @@ class LoginExtendedNullableForm bool updateParent = true, bool emitEvent = true, }) { - emailControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void passwordValuePatch( @@ -724,8 +721,11 @@ class LoginExtendedNullableForm bool updateParent = true, bool emitEvent = true, }) { - passwordControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + passwordControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void rememberMeValuePatch( @@ -733,8 +733,11 @@ class LoginExtendedNullableForm bool updateParent = true, bool emitEvent = true, }) { - rememberMeControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + rememberMeControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void themeValuePatch( @@ -742,8 +745,11 @@ class LoginExtendedNullableForm bool updateParent = true, bool emitEvent = true, }) { - themeControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + themeControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void modeValuePatch( @@ -751,8 +757,11 @@ class LoginExtendedNullableForm bool updateParent = true, bool emitEvent = true, }) { - modeControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + modeControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void timeoutValuePatch( @@ -760,8 +769,11 @@ class LoginExtendedNullableForm bool updateParent = true, bool emitEvent = true, }) { - timeoutControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + timeoutControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void heightValuePatch( @@ -769,8 +781,11 @@ class LoginExtendedNullableForm bool updateParent = true, bool emitEvent = true, }) { - heightControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + heightControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailValueReset( @@ -779,14 +794,13 @@ class LoginExtendedNullableForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - emailControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => emailControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void passwordValueReset( String? value, { @@ -794,14 +808,13 @@ class LoginExtendedNullableForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - passwordControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => passwordControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void rememberMeValueReset( bool? value, { @@ -809,14 +822,13 @@ class LoginExtendedNullableForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - rememberMeControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => rememberMeControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void themeValueReset( String? value, { @@ -824,14 +836,13 @@ class LoginExtendedNullableForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - themeControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => themeControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void modeValueReset( UserMode? value, { @@ -839,14 +850,13 @@ class LoginExtendedNullableForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - modeControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => modeControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void timeoutValueReset( int? value, { @@ -854,14 +864,13 @@ class LoginExtendedNullableForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - timeoutControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => timeoutControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void heightValueReset( double? value, { @@ -869,14 +878,13 @@ class LoginExtendedNullableForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - heightControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => heightControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get emailControl => form.control(emailControlPath()) as FormControl; @@ -1037,49 +1045,44 @@ class LoginExtendedNullableForm ); } return LoginExtendedNullable( - email: _emailValue, - password: _passwordValue, - rememberMe: _rememberMeValue, - theme: _themeValue, - mode: _modeValue, - timeout: _timeoutValue, - height: _heightValue); + email: _emailValue, + password: _passwordValue, + rememberMe: _rememberMeValue, + theme: _themeValue, + mode: _modeValue, + timeout: _timeoutValue, + height: _heightValue, + ); } @override LoginExtendedNullable get rawModel { return LoginExtendedNullable( - email: _emailRawValue, - password: _passwordRawValue, - rememberMe: _rememberMeRawValue, - theme: _themeRawValue, - mode: _modeRawValue, - timeout: _timeoutRawValue, - height: _heightRawValue); + email: _emailRawValue, + password: _passwordRawValue, + rememberMe: _rememberMeRawValue, + theme: _themeRawValue, + mode: _modeRawValue, + timeout: _timeoutRawValue, + height: _heightRawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { - final currentFormInstance = currentForm; - - if (currentFormInstance is! FormGroup) { - return; - } - + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { - currentFormInstance.controls[key]?.markAsEnabled( + currentForm.controls[key]?.markAsEnabled( updateParent: updateParent, emitEvent: emitEvent, ); @@ -1095,9 +1098,7 @@ class LoginExtendedNullableForm final currentForm = this.currentForm; return const DeepCollectionEquality().equals( - currentForm is FormControlCollection - ? currentForm.rawValue - : currentForm.value, + currentForm.rawValue, LoginExtendedNullableForm.formElements(other).rawValue, ); } @@ -1118,8 +1119,16 @@ class LoginExtendedNullableForm } @override - AbstractControl get currentForm { - return path == null ? form : form.control(path!); + bool get hasChanged { + return !const DeepCollectionEquality().equals( + currentForm.rawValue, + initial, + ); + } + + @override + FormGroup get currentForm { + return path == null ? form : form.control(path!) as FormGroup; } @override @@ -1127,86 +1136,157 @@ class LoginExtendedNullableForm LoginExtendedNullable? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue( - LoginExtendedNullableForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.updateValue( + LoginExtendedNullableForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void upsertValue( + LoginExtendedNullable? value, { + bool updateParent = true, + bool emitEvent = true, + }) { + final formElements = LoginExtendedNullableForm.formElements(value); + + currentForm.addAll(formElements.controls); + } @override void reset({ LoginExtendedNullable? value, bool updateParent = true, bool emitEvent = true, - }) => - form.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void updateInitial(Map? value, String? path) { + if (_formModel != null) { + _formModel?.updateInitial(currentForm.rawValue, path); + return; + } + + if (value == null) return; + + if (path == null || path.isEmpty) { + initial.addAll(value); + return; + } + + final keys = path.split('.'); + Object? current = initial; + for (var i = 0; i < keys.length - 1; i++) { + final key = keys[i]; + + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current = current[index]; + continue; + } + } + + if (current is Map) { + if (!current.containsKey(key)) { + current[key] = {}; + } + current = current[key]; + continue; + } + + return; + } + + final key = keys.last; + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current[index] = value; + } + } else if (current is Map) { + current[key] = value; + } + } String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); static FormGroup formElements(LoginExtendedNullable? loginExtendedNullable) => - FormGroup({ - emailControlName: FormControl( + FormGroup( + { + emailControlName: FormControl( value: loginExtendedNullable?.email, validators: [], asyncValidators: [], asyncValidatorsDebounceTime: 250, disabled: false, - touched: false), - passwordControlName: FormControl( + touched: false, + ), + passwordControlName: FormControl( value: loginExtendedNullable?.password, validators: [], asyncValidators: [], asyncValidatorsDebounceTime: 250, disabled: false, - touched: false), - rememberMeControlName: FormControl( + touched: false, + ), + rememberMeControlName: FormControl( value: loginExtendedNullable?.rememberMe, validators: [], asyncValidators: [], asyncValidatorsDebounceTime: 250, disabled: false, - touched: false), - themeControlName: FormControl( + touched: false, + ), + themeControlName: FormControl( value: loginExtendedNullable?.theme, validators: [], asyncValidators: [], asyncValidatorsDebounceTime: 250, disabled: false, - touched: false), - modeControlName: FormControl( + touched: false, + ), + modeControlName: FormControl( value: loginExtendedNullable?.mode, validators: [], asyncValidators: [], asyncValidatorsDebounceTime: 250, disabled: false, - touched: false), - timeoutControlName: FormControl( + touched: false, + ), + timeoutControlName: FormControl( value: loginExtendedNullable?.timeout, validators: [], asyncValidators: [], asyncValidatorsDebounceTime: 250, disabled: false, - touched: false), - heightControlName: FormControl( + touched: false, + ), + heightControlName: FormControl( value: loginExtendedNullable?.height, validators: [], asyncValidators: [], asyncValidatorsDebounceTime: 250, disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } class ReactiveLoginExtendedNullableFormArrayBuilder< - ReactiveLoginExtendedNullableFormArrayBuilderT> extends StatelessWidget { + ReactiveLoginExtendedNullableFormArrayBuilderT +> + extends StatelessWidget { const ReactiveLoginExtendedNullableFormArrayBuilder({ Key? key, this.control, @@ -1215,30 +1295,41 @@ class ReactiveLoginExtendedNullableFormArrayBuilder< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - LoginExtendedNullableForm formModel)? control; + LoginExtendedNullableForm formModel, + )? + control; - final Widget Function(BuildContext context, List itemList, - LoginExtendedNullableForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + LoginExtendedNullableForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveLoginExtendedNullableFormArrayBuilderT? item, - LoginExtendedNullableForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveLoginExtendedNullableFormArrayBuilderT? item, + LoginExtendedNullableForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -1252,21 +1343,13 @@ class ReactiveLoginExtendedNullableFormArrayBuilder< final itemBuilder = this.itemBuilder; return ReactiveFormArrayItemBuilder< - ReactiveLoginExtendedNullableFormArrayBuilderT>( + ReactiveLoginExtendedNullableFormArrayBuilderT + >( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1275,7 +1358,9 @@ class ReactiveLoginExtendedNullableFormArrayBuilder< } class ReactiveLoginExtendedNullableFormArrayBuilder2< - ReactiveLoginExtendedNullableFormArrayBuilderT> extends StatelessWidget { + ReactiveLoginExtendedNullableFormArrayBuilderT +> + extends StatelessWidget { const ReactiveLoginExtendedNullableFormArrayBuilder2({ Key? key, this.control, @@ -1284,36 +1369,47 @@ class ReactiveLoginExtendedNullableFormArrayBuilder2< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - LoginExtendedNullableForm formModel)? control; + LoginExtendedNullableForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - LoginExtendedNullableForm formModel - }) params)? builder; + ({ + BuildContext context, + List itemList, + LoginExtendedNullableForm formModel, + }) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveLoginExtendedNullableFormArrayBuilderT? item, - LoginExtendedNullableForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveLoginExtendedNullableFormArrayBuilderT? item, + LoginExtendedNullableForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -1327,27 +1423,22 @@ class ReactiveLoginExtendedNullableFormArrayBuilder2< final itemBuilder = this.itemBuilder; return ReactiveFormArrayItemBuilder< - ReactiveLoginExtendedNullableFormArrayBuilderT>( + ReactiveLoginExtendedNullableFormArrayBuilderT + >( formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1356,7 +1447,8 @@ class ReactiveLoginExtendedNullableFormArrayBuilder2< } class ReactiveLoginExtendedNullableFormFormGroupArrayBuilder< - ReactiveLoginExtendedNullableFormFormGroupArrayBuilderT> + ReactiveLoginExtendedNullableFormFormGroupArrayBuilderT +> extends StatelessWidget { const ReactiveLoginExtendedNullableFormFormGroupArrayBuilder({ Key? key, @@ -1364,25 +1456,39 @@ class ReactiveLoginExtendedNullableFormFormGroupArrayBuilder< this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? extended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(LoginExtendedNullableForm formModel)? + getExtended; - final ExtendedControl?>, - List> - Function(LoginExtendedNullableForm formModel)? getExtended; - - final Widget Function(BuildContext context, List itemList, - LoginExtendedNullableForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + LoginExtendedNullableForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveLoginExtendedNullableFormFormGroupArrayBuilderT? item, - LoginExtendedNullableForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveLoginExtendedNullableFormFormGroupArrayBuilderT? item, + LoginExtendedNullableForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -1397,26 +1503,18 @@ class ReactiveLoginExtendedNullableFormFormGroupArrayBuilder< return StreamBuilder?>?>( stream: value.control.valueChanges, builder: (context, snapshot) { - final itemList = (value.value() ?? - []) - .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) - .values - .toList(); - - return builder?.call( - context, - itemList, - formModel, - ) ?? + final itemList = + (value.value() ?? + []) + .asMap() + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) + .values + .toList(); + + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/generator_tests/test/doc/login_extended_output_test.dart b/packages/generator_tests/test/doc/login_extended_output_test.dart index f5370d84..82b132bd 100644 --- a/packages/generator_tests/test/doc/login_extended_output_test.dart +++ b/packages/generator_tests/test/doc/login_extended_output_test.dart @@ -129,8 +129,11 @@ class ReactiveLoginExtendedOFormConsumer extends StatelessWidget { final Widget? child; final Widget Function( - BuildContext context, LoginExtendedOForm formModel, Widget? child) - builder; + BuildContext context, + LoginExtendedOForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -149,11 +152,7 @@ class LoginExtendedOFormInheritedStreamer extends InheritedStreamer { required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final LoginExtendedOForm form; } @@ -175,19 +174,19 @@ class ReactiveLoginExtendedOForm extends StatelessWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - static LoginExtendedOForm? of( - BuildContext context, { - bool listen = true, - }) { + static LoginExtendedOForm? of(BuildContext context, {bool listen = true}) { if (listen) { return context .dependOnInheritedWidgetOfExactType< - LoginExtendedOFormInheritedStreamer>() + LoginExtendedOFormInheritedStreamer + >() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - LoginExtendedOFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + LoginExtendedOFormInheritedStreamer + >(); return element == null ? null : (element.widget as LoginExtendedOFormInheritedStreamer).form; @@ -235,11 +234,14 @@ class LoginExtendedOFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, LoginExtendedOForm formModel, Widget? child) - builder; + BuildContext context, + LoginExtendedOForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, LoginExtendedOForm formModel)? - initState; + initState; @override _LoginExtendedOFormBuilderState createState() => @@ -253,8 +255,11 @@ class _LoginExtendedOFormBuilderState extends State { @override void initState() { - _formModel = - LoginExtendedOForm(LoginExtendedOForm.formElements(widget.model), null); + _formModel = LoginExtendedOForm( + LoginExtendedOForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -332,10 +337,8 @@ final _logLoginExtendedOForm = Logger.detached('LoginExtendedOForm'); class LoginExtendedOForm implements FormModel { - LoginExtendedOForm( - this.form, - this.path, - ); + LoginExtendedOForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String emailControlName = "email"; @@ -361,8 +364,14 @@ class LoginExtendedOForm final String? path; + // ignore: unused_field + final FormModel? _formModel; + final Map _disabled = {}; + @override + final Map initial; + String emailControlPath() => pathBuilder(emailControlName); String email2ControlPath() => pathBuilder(email2ControlName); @@ -399,11 +408,12 @@ class LoginExtendedOForm double get _heightValue => heightControl.value as double; - String? get _unAnnotatedValue => unAnnotatedControl.value; + String? get _unAnnotatedValue => + containsUnAnnotated ? unAnnotatedControl.value : null; List get _someIntListValue => someIntListControl.value ?? const []; - String? get _emailRawValue => emailControl.value; + String? get _emailRawValue => containsEmail ? emailControl.value : null; String get _email2RawValue => email2Control.value ?? ""; @@ -419,12 +429,11 @@ class LoginExtendedOForm double get _heightRawValue => heightControl.value as double; - String? get _unAnnotatedRawValue => unAnnotatedControl.value; + String? get _unAnnotatedRawValue => + containsUnAnnotated ? unAnnotatedControl.value : null; List get _someIntListRawValue => someIntListControl.value ?? const []; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsEmail { try { form.control(emailControlPath()); @@ -434,8 +443,6 @@ class LoginExtendedOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsEmail2 { try { form.control(email2ControlPath()); @@ -445,8 +452,6 @@ class LoginExtendedOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsPassword { try { form.control(passwordControlPath()); @@ -456,8 +461,6 @@ class LoginExtendedOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsRememberMe { try { form.control(rememberMeControlPath()); @@ -467,8 +470,6 @@ class LoginExtendedOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsTheme { try { form.control(themeControlPath()); @@ -478,8 +479,6 @@ class LoginExtendedOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsMode { try { form.control(modeControlPath()); @@ -489,8 +488,6 @@ class LoginExtendedOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsTimeout { try { form.control(timeoutControlPath()); @@ -500,8 +497,6 @@ class LoginExtendedOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsHeight { try { form.control(heightControlPath()); @@ -511,8 +506,6 @@ class LoginExtendedOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsUnAnnotated { try { form.control(unAnnotatedControlPath()); @@ -522,8 +515,6 @@ class LoginExtendedOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsSomeIntList { try { form.control(someIntListControlPath()); @@ -573,12 +564,7 @@ class LoginExtendedOForm void get someIntListFocus => form.focus(someIntListControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void emailRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void emailRemove({bool updateParent = true, bool emitEvent = true}) { if (containsEmail) { final controlPath = path; if (controlPath == null) { @@ -601,12 +587,7 @@ class LoginExtendedOForm } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void unAnnotatedRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void unAnnotatedRemove({bool updateParent = true, bool emitEvent = true}) { if (containsUnAnnotated) { final controlPath = path; if (controlPath == null) { @@ -634,8 +615,11 @@ class LoginExtendedOForm bool updateParent = true, bool emitEvent = true, }) { - emailControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void email2ValueUpdate( @@ -643,8 +627,11 @@ class LoginExtendedOForm bool updateParent = true, bool emitEvent = true, }) { - email2Control.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + email2Control.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void passwordValueUpdate( @@ -652,8 +639,11 @@ class LoginExtendedOForm bool updateParent = true, bool emitEvent = true, }) { - passwordControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + passwordControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void rememberMeValueUpdate( @@ -661,8 +651,11 @@ class LoginExtendedOForm bool updateParent = true, bool emitEvent = true, }) { - rememberMeControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + rememberMeControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void themeValueUpdate( @@ -670,8 +663,11 @@ class LoginExtendedOForm bool updateParent = true, bool emitEvent = true, }) { - themeControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + themeControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void modeValueUpdate( @@ -679,8 +675,11 @@ class LoginExtendedOForm bool updateParent = true, bool emitEvent = true, }) { - modeControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + modeControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void timeoutValueUpdate( @@ -688,8 +687,11 @@ class LoginExtendedOForm bool updateParent = true, bool emitEvent = true, }) { - timeoutControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + timeoutControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void heightValueUpdate( @@ -697,8 +699,11 @@ class LoginExtendedOForm bool updateParent = true, bool emitEvent = true, }) { - heightControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + heightControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void unAnnotatedValueUpdate( @@ -706,8 +711,11 @@ class LoginExtendedOForm bool updateParent = true, bool emitEvent = true, }) { - unAnnotatedControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + unAnnotatedControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void someIntListValueUpdate( @@ -715,8 +723,11 @@ class LoginExtendedOForm bool updateParent = true, bool emitEvent = true, }) { - someIntListControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + someIntListControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailValuePatch( @@ -724,8 +735,11 @@ class LoginExtendedOForm bool updateParent = true, bool emitEvent = true, }) { - emailControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void email2ValuePatch( @@ -733,8 +747,11 @@ class LoginExtendedOForm bool updateParent = true, bool emitEvent = true, }) { - email2Control.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + email2Control.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void passwordValuePatch( @@ -742,8 +759,11 @@ class LoginExtendedOForm bool updateParent = true, bool emitEvent = true, }) { - passwordControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + passwordControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void rememberMeValuePatch( @@ -751,8 +771,11 @@ class LoginExtendedOForm bool updateParent = true, bool emitEvent = true, }) { - rememberMeControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + rememberMeControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void themeValuePatch( @@ -760,8 +783,11 @@ class LoginExtendedOForm bool updateParent = true, bool emitEvent = true, }) { - themeControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + themeControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void modeValuePatch( @@ -769,8 +795,11 @@ class LoginExtendedOForm bool updateParent = true, bool emitEvent = true, }) { - modeControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + modeControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void timeoutValuePatch( @@ -778,8 +807,11 @@ class LoginExtendedOForm bool updateParent = true, bool emitEvent = true, }) { - timeoutControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + timeoutControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void heightValuePatch( @@ -787,8 +819,11 @@ class LoginExtendedOForm bool updateParent = true, bool emitEvent = true, }) { - heightControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + heightControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void unAnnotatedValuePatch( @@ -796,8 +831,11 @@ class LoginExtendedOForm bool updateParent = true, bool emitEvent = true, }) { - unAnnotatedControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + unAnnotatedControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void someIntListValuePatch( @@ -805,8 +843,11 @@ class LoginExtendedOForm bool updateParent = true, bool emitEvent = true, }) { - someIntListControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + someIntListControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailValueReset( @@ -815,14 +856,13 @@ class LoginExtendedOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - emailControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => emailControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void email2ValueReset( String value, { @@ -830,14 +870,13 @@ class LoginExtendedOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - email2Control.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => email2Control.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void passwordValueReset( String value, { @@ -845,14 +884,13 @@ class LoginExtendedOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - passwordControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => passwordControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void rememberMeValueReset( bool value, { @@ -860,14 +898,13 @@ class LoginExtendedOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - rememberMeControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => rememberMeControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void themeValueReset( String value, { @@ -875,14 +912,13 @@ class LoginExtendedOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - themeControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => themeControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void modeValueReset( UserMode value, { @@ -890,14 +926,13 @@ class LoginExtendedOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - modeControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => modeControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void timeoutValueReset( int value, { @@ -905,14 +940,13 @@ class LoginExtendedOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - timeoutControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => timeoutControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void heightValueReset( double value, { @@ -920,14 +954,13 @@ class LoginExtendedOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - heightControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => heightControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void unAnnotatedValueReset( String? value, { @@ -935,14 +968,13 @@ class LoginExtendedOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - unAnnotatedControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => unAnnotatedControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void someIntListValueReset( List value, { @@ -950,14 +982,13 @@ class LoginExtendedOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - someIntListControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => someIntListControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get emailControl => form.control(emailControlPath()) as FormControl; @@ -1182,55 +1213,50 @@ class LoginExtendedOForm ); } return LoginExtendedOOutput( - email: _emailValue, - email2: _email2Value, - password: _passwordValue, - rememberMe: _rememberMeValue, - theme: _themeValue, - mode: _modeValue, - timeout: _timeoutValue, - height: _heightValue, - unAnnotated: _unAnnotatedValue, - someIntList: _someIntListValue); + email: _emailValue, + email2: _email2Value, + password: _passwordValue, + rememberMe: _rememberMeValue, + theme: _themeValue, + mode: _modeValue, + timeout: _timeoutValue, + height: _heightValue, + unAnnotated: _unAnnotatedValue, + someIntList: _someIntListValue, + ); } @override LoginExtendedO get rawModel { return LoginExtendedO( - email: _emailRawValue, - email2: _email2RawValue, - password: _passwordRawValue, - rememberMe: _rememberMeRawValue, - theme: _themeRawValue, - mode: _modeRawValue, - timeout: _timeoutRawValue, - height: _heightRawValue, - unAnnotated: _unAnnotatedRawValue, - someIntList: _someIntListRawValue); + email: _emailRawValue, + email2: _email2RawValue, + password: _passwordRawValue, + rememberMe: _rememberMeRawValue, + theme: _themeRawValue, + mode: _modeRawValue, + timeout: _timeoutRawValue, + height: _heightRawValue, + unAnnotated: _unAnnotatedRawValue, + someIntList: _someIntListRawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { - final currentFormInstance = currentForm; - - if (currentFormInstance is! FormGroup) { - return; - } - + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { - currentFormInstance.controls[key]?.markAsEnabled( + currentForm.controls[key]?.markAsEnabled( updateParent: updateParent, emitEvent: emitEvent, ); @@ -1246,9 +1272,7 @@ class LoginExtendedOForm final currentForm = this.currentForm; return const DeepCollectionEquality().equals( - currentForm is FormControlCollection - ? currentForm.rawValue - : currentForm.value, + currentForm.rawValue, LoginExtendedOForm.formElements(other).rawValue, ); } @@ -1269,8 +1293,16 @@ class LoginExtendedOForm } @override - AbstractControl get currentForm { - return path == null ? form : form.control(path!); + bool get hasChanged { + return !const DeepCollectionEquality().equals( + currentForm.rawValue, + initial, + ); + } + + @override + FormGroup get currentForm { + return path == null ? form : form.control(path!) as FormGroup; } @override @@ -1278,102 +1310,174 @@ class LoginExtendedOForm LoginExtendedO? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(LoginExtendedOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + LoginExtendedOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void upsertValue( + LoginExtendedO? value, { + bool updateParent = true, + bool emitEvent = true, + }) { + final formElements = LoginExtendedOForm.formElements(value); + + currentForm.addAll(formElements.controls); + } @override void reset({ LoginExtendedO? value, bool updateParent = true, bool emitEvent = true, - }) => - form.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void updateInitial(Map? value, String? path) { + if (_formModel != null) { + _formModel?.updateInitial(currentForm.rawValue, path); + return; + } + + if (value == null) return; + + if (path == null || path.isEmpty) { + initial.addAll(value); + return; + } + + final keys = path.split('.'); + Object? current = initial; + for (var i = 0; i < keys.length - 1; i++) { + final key = keys[i]; + + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current = current[index]; + continue; + } + } + + if (current is Map) { + if (!current.containsKey(key)) { + current[key] = {}; + } + current = current[key]; + continue; + } + + return; + } + + final key = keys.last; + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current[index] = value; + } + } else if (current is Map) { + current[key] = value; + } + } String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(LoginExtendedO? loginExtendedO) => FormGroup({ - emailControlName: FormControl( - value: loginExtendedO?.email, - validators: [RequiredValidator()], - asyncValidators: [UniqueEmailAsyncValidator()], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - email2ControlName: FormControl( - value: loginExtendedO?.email2, - validators: [RequiredValidator()], - asyncValidators: [UniqueEmailAsyncValidator()], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - passwordControlName: FormControl( - value: loginExtendedO?.password, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - rememberMeControlName: FormControl( - value: loginExtendedO?.rememberMe, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - themeControlName: FormControl( - value: loginExtendedO?.theme, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - modeControlName: FormControl( - value: loginExtendedO?.mode, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - timeoutControlName: FormControl( - value: loginExtendedO?.timeout, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - heightControlName: FormControl( - value: loginExtendedO?.height, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - unAnnotatedControlName: FormControl( - value: loginExtendedO?.unAnnotated, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - someIntListControlName: FormControl>( - value: loginExtendedO?.someIntList, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [ - AllFieldsRequired() - ], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(LoginExtendedO? loginExtendedO) => FormGroup( + { + emailControlName: FormControl( + value: loginExtendedO?.email, + validators: [RequiredValidator()], + asyncValidators: [UniqueEmailAsyncValidator()], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + email2ControlName: FormControl( + value: loginExtendedO?.email2, + validators: [RequiredValidator()], + asyncValidators: [UniqueEmailAsyncValidator()], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + passwordControlName: FormControl( + value: loginExtendedO?.password, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + rememberMeControlName: FormControl( + value: loginExtendedO?.rememberMe, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + themeControlName: FormControl( + value: loginExtendedO?.theme, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + modeControlName: FormControl( + value: loginExtendedO?.mode, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + timeoutControlName: FormControl( + value: loginExtendedO?.timeout, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + heightControlName: FormControl( + value: loginExtendedO?.height, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + unAnnotatedControlName: FormControl( + value: loginExtendedO?.unAnnotated, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + someIntListControlName: FormControl>( + value: loginExtendedO?.someIntList, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [AllFieldsRequired()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } @Rf(output: true) @@ -1389,27 +1493,32 @@ class LoginExtendedOOutput { final double height; final String? unAnnotated; final List someIntList; - LoginExtendedOOutput( - {@RfControl( - validators: [RequiredValidator()], - asyncValidators: [UniqueEmailAsyncValidator()]) - required this.email, - @RfControl( - validators: [RequiredValidator()], - asyncValidators: [UniqueEmailAsyncValidator()]) - required this.email2, - @RfControl(validators: [RequiredValidator()]) required this.password, - @RfControl(validators: [RequiredValidator()]) required this.rememberMe, - @RfControl(validators: [RequiredValidator()]) required this.theme, - @RfControl(validators: [RequiredValidator()]) required this.mode, - @RfControl(validators: [RequiredValidator()]) required this.timeout, - @RfControl(validators: [RequiredValidator()]) required this.height, - this.unAnnotated, - this.someIntList = const []}); + LoginExtendedOOutput({ + @RfControl( + validators: [RequiredValidator()], + asyncValidators: [UniqueEmailAsyncValidator()], + ) + required this.email, + @RfControl( + validators: [RequiredValidator()], + asyncValidators: [UniqueEmailAsyncValidator()], + ) + required this.email2, + @RfControl(validators: [RequiredValidator()]) required this.password, + @RfControl(validators: [RequiredValidator()]) required this.rememberMe, + @RfControl(validators: [RequiredValidator()]) required this.theme, + @RfControl(validators: [RequiredValidator()]) required this.mode, + @RfControl(validators: [RequiredValidator()]) required this.timeout, + @RfControl(validators: [RequiredValidator()]) required this.height, + this.unAnnotated, + this.someIntList = const [], + }); } class ReactiveLoginExtendedOFormArrayBuilder< - ReactiveLoginExtendedOFormArrayBuilderT> extends StatelessWidget { + ReactiveLoginExtendedOFormArrayBuilderT +> + extends StatelessWidget { const ReactiveLoginExtendedOFormArrayBuilder({ Key? key, this.control, @@ -1418,30 +1527,41 @@ class ReactiveLoginExtendedOFormArrayBuilder< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - LoginExtendedOForm formModel)? control; + LoginExtendedOForm formModel, + )? + control; - final Widget Function(BuildContext context, List itemList, - LoginExtendedOForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + LoginExtendedOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveLoginExtendedOFormArrayBuilderT? item, - LoginExtendedOForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveLoginExtendedOFormArrayBuilderT? item, + LoginExtendedOForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -1455,21 +1575,13 @@ class ReactiveLoginExtendedOFormArrayBuilder< final itemBuilder = this.itemBuilder; return ReactiveFormArrayItemBuilder< - ReactiveLoginExtendedOFormArrayBuilderT>( + ReactiveLoginExtendedOFormArrayBuilderT + >( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1478,7 +1590,9 @@ class ReactiveLoginExtendedOFormArrayBuilder< } class ReactiveLoginExtendedOFormArrayBuilder2< - ReactiveLoginExtendedOFormArrayBuilderT> extends StatelessWidget { + ReactiveLoginExtendedOFormArrayBuilderT +> + extends StatelessWidget { const ReactiveLoginExtendedOFormArrayBuilder2({ Key? key, this.control, @@ -1487,36 +1601,47 @@ class ReactiveLoginExtendedOFormArrayBuilder2< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - LoginExtendedOForm formModel)? control; + LoginExtendedOForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - LoginExtendedOForm formModel - }) params)? builder; + ({ + BuildContext context, + List itemList, + LoginExtendedOForm formModel, + }) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveLoginExtendedOFormArrayBuilderT? item, - LoginExtendedOForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveLoginExtendedOFormArrayBuilderT? item, + LoginExtendedOForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -1530,27 +1655,22 @@ class ReactiveLoginExtendedOFormArrayBuilder2< final itemBuilder = this.itemBuilder; return ReactiveFormArrayItemBuilder< - ReactiveLoginExtendedOFormArrayBuilderT>( + ReactiveLoginExtendedOFormArrayBuilderT + >( formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1559,32 +1679,48 @@ class ReactiveLoginExtendedOFormArrayBuilder2< } class ReactiveLoginExtendedOFormFormGroupArrayBuilder< - ReactiveLoginExtendedOFormFormGroupArrayBuilderT> extends StatelessWidget { + ReactiveLoginExtendedOFormFormGroupArrayBuilderT +> + extends StatelessWidget { const ReactiveLoginExtendedOFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? extended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(LoginExtendedOForm formModel)? + getExtended; - final ExtendedControl?>, - List> - Function(LoginExtendedOForm formModel)? getExtended; - - final Widget Function(BuildContext context, List itemList, - LoginExtendedOForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + LoginExtendedOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveLoginExtendedOFormFormGroupArrayBuilderT? item, - LoginExtendedOForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveLoginExtendedOFormFormGroupArrayBuilderT? item, + LoginExtendedOForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -1599,26 +1735,18 @@ class ReactiveLoginExtendedOFormFormGroupArrayBuilder< return StreamBuilder?>?>( stream: value.control.valueChanges, builder: (context, snapshot) { - final itemList = (value.value() ?? - []) - .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) - .values - .toList(); - - return builder?.call( - context, - itemList, - formModel, - ) ?? + final itemList = + (value.value() ?? + []) + .asMap() + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) + .values + .toList(); + + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/generator_tests/test/doc/login_extended_test.dart b/packages/generator_tests/test/doc/login_extended_test.dart index 2111ef41..4832a2ef 100644 --- a/packages/generator_tests/test/doc/login_extended_test.dart +++ b/packages/generator_tests/test/doc/login_extended_test.dart @@ -124,7 +124,11 @@ class ReactiveLoginExtendedFormConsumer extends StatelessWidget { final Widget? child; final Widget Function( - BuildContext context, LoginExtendedForm formModel, Widget? child) builder; + BuildContext context, + LoginExtendedForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -143,11 +147,7 @@ class LoginExtendedFormInheritedStreamer extends InheritedStreamer { required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final LoginExtendedForm form; } @@ -169,19 +169,19 @@ class ReactiveLoginExtendedForm extends StatelessWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - static LoginExtendedForm? of( - BuildContext context, { - bool listen = true, - }) { + static LoginExtendedForm? of(BuildContext context, {bool listen = true}) { if (listen) { return context .dependOnInheritedWidgetOfExactType< - LoginExtendedFormInheritedStreamer>() + LoginExtendedFormInheritedStreamer + >() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - LoginExtendedFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + LoginExtendedFormInheritedStreamer + >(); return element == null ? null : (element.widget as LoginExtendedFormInheritedStreamer).form; @@ -229,10 +229,14 @@ class LoginExtendedFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, LoginExtendedForm formModel, Widget? child) builder; + BuildContext context, + LoginExtendedForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, LoginExtendedForm formModel)? - initState; + initState; @override _LoginExtendedFormBuilderState createState() => @@ -246,8 +250,11 @@ class _LoginExtendedFormBuilderState extends State { @override void initState() { - _formModel = - LoginExtendedForm(LoginExtendedForm.formElements(widget.model), null); + _formModel = LoginExtendedForm( + LoginExtendedForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -324,10 +331,8 @@ class _LoginExtendedFormBuilderState extends State { final _logLoginExtendedForm = Logger.detached('LoginExtendedForm'); class LoginExtendedForm implements FormModel { - LoginExtendedForm( - this.form, - this.path, - ); + LoginExtendedForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String emailControlName = "email"; @@ -351,8 +356,14 @@ class LoginExtendedForm implements FormModel { final String? path; + // ignore: unused_field + final FormModel? _formModel; + final Map _disabled = {}; + @override + final Map initial; + String emailControlPath() => pathBuilder(emailControlName); String passwordControlPath() => pathBuilder(passwordControlName); @@ -385,7 +396,8 @@ class LoginExtendedForm implements FormModel { double get _heightValue => heightControl.value as double; - String? get _unAnnotatedValue => unAnnotatedControl.value; + String? get _unAnnotatedValue => + containsUnAnnotated ? unAnnotatedControl.value : null; List get _someIntListValue => someIntListControl.value ?? const []; @@ -403,12 +415,11 @@ class LoginExtendedForm implements FormModel { double get _heightRawValue => heightControl.value as double; - String? get _unAnnotatedRawValue => unAnnotatedControl.value; + String? get _unAnnotatedRawValue => + containsUnAnnotated ? unAnnotatedControl.value : null; List get _someIntListRawValue => someIntListControl.value ?? const []; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsEmail { try { form.control(emailControlPath()); @@ -418,8 +429,6 @@ class LoginExtendedForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsPassword { try { form.control(passwordControlPath()); @@ -429,8 +438,6 @@ class LoginExtendedForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsRememberMe { try { form.control(rememberMeControlPath()); @@ -440,8 +447,6 @@ class LoginExtendedForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsTheme { try { form.control(themeControlPath()); @@ -451,8 +456,6 @@ class LoginExtendedForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsMode { try { form.control(modeControlPath()); @@ -462,8 +465,6 @@ class LoginExtendedForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsTimeout { try { form.control(timeoutControlPath()); @@ -473,8 +474,6 @@ class LoginExtendedForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsHeight { try { form.control(heightControlPath()); @@ -484,8 +483,6 @@ class LoginExtendedForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsUnAnnotated { try { form.control(unAnnotatedControlPath()); @@ -495,8 +492,6 @@ class LoginExtendedForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsSomeIntList { try { form.control(someIntListControlPath()); @@ -542,12 +537,7 @@ class LoginExtendedForm implements FormModel { void get someIntListFocus => form.focus(someIntListControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void unAnnotatedRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void unAnnotatedRemove({bool updateParent = true, bool emitEvent = true}) { if (containsUnAnnotated) { final controlPath = path; if (controlPath == null) { @@ -575,8 +565,11 @@ class LoginExtendedForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - emailControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void passwordValueUpdate( @@ -584,8 +577,11 @@ class LoginExtendedForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - passwordControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + passwordControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void rememberMeValueUpdate( @@ -593,8 +589,11 @@ class LoginExtendedForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - rememberMeControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + rememberMeControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void themeValueUpdate( @@ -602,8 +601,11 @@ class LoginExtendedForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - themeControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + themeControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void modeValueUpdate( @@ -611,8 +613,11 @@ class LoginExtendedForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - modeControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + modeControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void timeoutValueUpdate( @@ -620,8 +625,11 @@ class LoginExtendedForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - timeoutControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + timeoutControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void heightValueUpdate( @@ -629,8 +637,11 @@ class LoginExtendedForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - heightControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + heightControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void unAnnotatedValueUpdate( @@ -638,8 +649,11 @@ class LoginExtendedForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - unAnnotatedControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + unAnnotatedControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void someIntListValueUpdate( @@ -647,8 +661,11 @@ class LoginExtendedForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - someIntListControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + someIntListControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailValuePatch( @@ -656,8 +673,11 @@ class LoginExtendedForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - emailControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void passwordValuePatch( @@ -665,8 +685,11 @@ class LoginExtendedForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - passwordControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + passwordControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void rememberMeValuePatch( @@ -674,8 +697,11 @@ class LoginExtendedForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - rememberMeControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + rememberMeControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void themeValuePatch( @@ -683,8 +709,11 @@ class LoginExtendedForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - themeControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + themeControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void modeValuePatch( @@ -692,8 +721,11 @@ class LoginExtendedForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - modeControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + modeControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void timeoutValuePatch( @@ -701,8 +733,11 @@ class LoginExtendedForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - timeoutControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + timeoutControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void heightValuePatch( @@ -710,8 +745,11 @@ class LoginExtendedForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - heightControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + heightControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void unAnnotatedValuePatch( @@ -719,8 +757,11 @@ class LoginExtendedForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - unAnnotatedControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + unAnnotatedControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void someIntListValuePatch( @@ -728,8 +769,11 @@ class LoginExtendedForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - someIntListControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + someIntListControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailValueReset( @@ -738,14 +782,13 @@ class LoginExtendedForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - emailControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => emailControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void passwordValueReset( String value, { @@ -753,14 +796,13 @@ class LoginExtendedForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - passwordControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => passwordControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void rememberMeValueReset( bool value, { @@ -768,14 +810,13 @@ class LoginExtendedForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - rememberMeControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => rememberMeControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void themeValueReset( String value, { @@ -783,14 +824,13 @@ class LoginExtendedForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - themeControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => themeControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void modeValueReset( UserMode value, { @@ -798,14 +838,13 @@ class LoginExtendedForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - modeControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => modeControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void timeoutValueReset( int value, { @@ -813,14 +852,13 @@ class LoginExtendedForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - timeoutControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => timeoutControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void heightValueReset( double value, { @@ -828,14 +866,13 @@ class LoginExtendedForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - heightControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => heightControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void unAnnotatedValueReset( String? value, { @@ -843,14 +880,13 @@ class LoginExtendedForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - unAnnotatedControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => unAnnotatedControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void someIntListValueReset( List value, { @@ -858,14 +894,13 @@ class LoginExtendedForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - someIntListControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => someIntListControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get emailControl => form.control(emailControlPath()) as FormControl; @@ -1068,53 +1103,48 @@ class LoginExtendedForm implements FormModel { ); } return LoginExtended( - email: _emailValue, - password: _passwordValue, - rememberMe: _rememberMeValue, - theme: _themeValue, - mode: _modeValue, - timeout: _timeoutValue, - height: _heightValue, - unAnnotated: _unAnnotatedValue, - someIntList: _someIntListValue); + email: _emailValue, + password: _passwordValue, + rememberMe: _rememberMeValue, + theme: _themeValue, + mode: _modeValue, + timeout: _timeoutValue, + height: _heightValue, + unAnnotated: _unAnnotatedValue, + someIntList: _someIntListValue, + ); } @override LoginExtended get rawModel { return LoginExtended( - email: _emailRawValue, - password: _passwordRawValue, - rememberMe: _rememberMeRawValue, - theme: _themeRawValue, - mode: _modeRawValue, - timeout: _timeoutRawValue, - height: _heightRawValue, - unAnnotated: _unAnnotatedRawValue, - someIntList: _someIntListRawValue); + email: _emailRawValue, + password: _passwordRawValue, + rememberMe: _rememberMeRawValue, + theme: _themeRawValue, + mode: _modeRawValue, + timeout: _timeoutRawValue, + height: _heightRawValue, + unAnnotated: _unAnnotatedRawValue, + someIntList: _someIntListRawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { - final currentFormInstance = currentForm; - - if (currentFormInstance is! FormGroup) { - return; - } - + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { - currentFormInstance.controls[key]?.markAsEnabled( + currentForm.controls[key]?.markAsEnabled( updateParent: updateParent, emitEvent: emitEvent, ); @@ -1130,9 +1160,7 @@ class LoginExtendedForm implements FormModel { final currentForm = this.currentForm; return const DeepCollectionEquality().equals( - currentForm is FormControlCollection - ? currentForm.rawValue - : currentForm.value, + currentForm.rawValue, LoginExtendedForm.formElements(other).rawValue, ); } @@ -1153,8 +1181,16 @@ class LoginExtendedForm implements FormModel { } @override - AbstractControl get currentForm { - return path == null ? form : form.control(path!); + bool get hasChanged { + return !const DeepCollectionEquality().equals( + currentForm.rawValue, + initial, + ); + } + + @override + FormGroup get currentForm { + return path == null ? form : form.control(path!) as FormGroup; } @override @@ -1162,99 +1198,172 @@ class LoginExtendedForm implements FormModel { LoginExtended? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(LoginExtendedForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + LoginExtendedForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void upsertValue( + LoginExtended? value, { + bool updateParent = true, + bool emitEvent = true, + }) { + final formElements = LoginExtendedForm.formElements(value); + + currentForm.addAll(formElements.controls); + } @override void reset({ LoginExtended? value, bool updateParent = true, bool emitEvent = true, - }) => - form.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void updateInitial(Map? value, String? path) { + if (_formModel != null) { + _formModel?.updateInitial(currentForm.rawValue, path); + return; + } + + if (value == null) return; + + if (path == null || path.isEmpty) { + initial.addAll(value); + return; + } + + final keys = path.split('.'); + Object? current = initial; + for (var i = 0; i < keys.length - 1; i++) { + final key = keys[i]; + + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current = current[index]; + continue; + } + } + + if (current is Map) { + if (!current.containsKey(key)) { + current[key] = {}; + } + current = current[key]; + continue; + } + + return; + } + + final key = keys.last; + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current[index] = value; + } + } else if (current is Map) { + current[key] = value; + } + } String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(LoginExtended? loginExtended) => FormGroup({ - emailControlName: FormControl( - value: loginExtended?.email, - validators: [RequiredValidator()], - asyncValidators: [UniqueEmailAsyncValidator()], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - passwordControlName: FormControl( - value: loginExtended?.password, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - rememberMeControlName: FormControl( - value: loginExtended?.rememberMe, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - themeControlName: FormControl( - value: loginExtended?.theme, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - modeControlName: FormControl( - value: loginExtended?.mode, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - timeoutControlName: FormControl( - value: loginExtended?.timeout, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - heightControlName: FormControl( - value: loginExtended?.height, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - unAnnotatedControlName: FormControl( - value: loginExtended?.unAnnotated, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - someIntListControlName: FormControl>( - value: loginExtended?.someIntList, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [ - AllFieldsRequired() - ], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(LoginExtended? loginExtended) => FormGroup( + { + emailControlName: FormControl( + value: loginExtended?.email, + validators: [RequiredValidator()], + asyncValidators: [UniqueEmailAsyncValidator()], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + passwordControlName: FormControl( + value: loginExtended?.password, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + rememberMeControlName: FormControl( + value: loginExtended?.rememberMe, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + themeControlName: FormControl( + value: loginExtended?.theme, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + modeControlName: FormControl( + value: loginExtended?.mode, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + timeoutControlName: FormControl( + value: loginExtended?.timeout, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + heightControlName: FormControl( + value: loginExtended?.height, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + unAnnotatedControlName: FormControl( + value: loginExtended?.unAnnotated, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + someIntListControlName: FormControl>( + value: loginExtended?.someIntList, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [AllFieldsRequired()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } class ReactiveLoginExtendedFormArrayBuilder< - ReactiveLoginExtendedFormArrayBuilderT> extends StatelessWidget { + ReactiveLoginExtendedFormArrayBuilderT +> + extends StatelessWidget { const ReactiveLoginExtendedFormArrayBuilder({ Key? key, this.control, @@ -1263,30 +1372,41 @@ class ReactiveLoginExtendedFormArrayBuilder< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - LoginExtendedForm formModel)? control; + LoginExtendedForm formModel, + )? + control; - final Widget Function(BuildContext context, List itemList, - LoginExtendedForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + LoginExtendedForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveLoginExtendedFormArrayBuilderT? item, - LoginExtendedForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveLoginExtendedFormArrayBuilderT? item, + LoginExtendedForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -1302,18 +1422,9 @@ class ReactiveLoginExtendedFormArrayBuilder< return ReactiveFormArrayItemBuilder( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1322,7 +1433,9 @@ class ReactiveLoginExtendedFormArrayBuilder< } class ReactiveLoginExtendedFormArrayBuilder2< - ReactiveLoginExtendedFormArrayBuilderT> extends StatelessWidget { + ReactiveLoginExtendedFormArrayBuilderT +> + extends StatelessWidget { const ReactiveLoginExtendedFormArrayBuilder2({ Key? key, this.control, @@ -1331,36 +1444,43 @@ class ReactiveLoginExtendedFormArrayBuilder2< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - LoginExtendedForm formModel)? control; + LoginExtendedForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - LoginExtendedForm formModel - }) params)? builder; + ({BuildContext context, List itemList, LoginExtendedForm formModel}) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveLoginExtendedFormArrayBuilderT? item, - LoginExtendedForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveLoginExtendedFormArrayBuilderT? item, + LoginExtendedForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -1377,23 +1497,17 @@ class ReactiveLoginExtendedFormArrayBuilder2< formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1402,32 +1516,48 @@ class ReactiveLoginExtendedFormArrayBuilder2< } class ReactiveLoginExtendedFormFormGroupArrayBuilder< - ReactiveLoginExtendedFormFormGroupArrayBuilderT> extends StatelessWidget { + ReactiveLoginExtendedFormFormGroupArrayBuilderT +> + extends StatelessWidget { const ReactiveLoginExtendedFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? extended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(LoginExtendedForm formModel)? + getExtended; - final ExtendedControl?>, - List> - Function(LoginExtendedForm formModel)? getExtended; - - final Widget Function(BuildContext context, List itemList, - LoginExtendedForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + LoginExtendedForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveLoginExtendedFormFormGroupArrayBuilderT? item, - LoginExtendedForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveLoginExtendedFormFormGroupArrayBuilderT? item, + LoginExtendedForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -1442,26 +1572,18 @@ class ReactiveLoginExtendedFormFormGroupArrayBuilder< return StreamBuilder?>?>( stream: value.control.valueChanges, builder: (context, snapshot) { - final itemList = (value.value() ?? - []) - .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) - .values - .toList(); - - return builder?.call( - context, - itemList, - formModel, - ) ?? + final itemList = + (value.value() ?? + []) + .asMap() + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) + .values + .toList(); + + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/generator_tests/test/doc/login_output_test.dart b/packages/generator_tests/test/doc/login_output_test.dart index 64beb083..1e28ca45 100644 --- a/packages/generator_tests/test/doc/login_output_test.dart +++ b/packages/generator_tests/test/doc/login_output_test.dart @@ -90,7 +90,11 @@ class ReactiveLoginOFormConsumer extends StatelessWidget { final Widget? child; final Widget Function( - BuildContext context, LoginOForm formModel, Widget? child) builder; + BuildContext context, + LoginOForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -109,11 +113,7 @@ class LoginOFormInheritedStreamer extends InheritedStreamer { required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final LoginOForm form; } @@ -135,10 +135,7 @@ class ReactiveLoginOForm extends StatelessWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - static LoginOForm? of( - BuildContext context, { - bool listen = true, - }) { + static LoginOForm? of(BuildContext context, {bool listen = true}) { if (listen) { return context .dependOnInheritedWidgetOfExactType() @@ -192,7 +189,11 @@ class LoginOFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, LoginOForm formModel, Widget? child) builder; + BuildContext context, + LoginOForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, LoginOForm formModel)? initState; @@ -207,7 +208,7 @@ class _LoginOFormBuilderState extends State { @override void initState() { - _formModel = LoginOForm(LoginOForm.formElements(widget.model), null); + _formModel = LoginOForm(LoginOForm.formElements(widget.model), null, null); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -284,10 +285,7 @@ class _LoginOFormBuilderState extends State { final _logLoginOForm = Logger.detached('LoginOForm'); class LoginOForm implements FormModel { - LoginOForm( - this.form, - this.path, - ); + LoginOForm(this.form, this.path, this._formModel) : initial = form.rawValue; static const String emailControlName = "email"; @@ -297,8 +295,14 @@ class LoginOForm implements FormModel { final String? path; + // ignore: unused_field + final FormModel? _formModel; + final Map _disabled = {}; + @override + final Map initial; + String emailControlPath() => pathBuilder(emailControlName); String passwordControlPath() => pathBuilder(passwordControlName); @@ -307,12 +311,11 @@ class LoginOForm implements FormModel { String get _passwordValue => passwordControl.value as String; - String? get _emailRawValue => emailControl.value; + String? get _emailRawValue => containsEmail ? emailControl.value : null; - String? get _passwordRawValue => passwordControl.value; + String? get _passwordRawValue => + containsPassword ? passwordControl.value : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsEmail { try { form.control(emailControlPath()); @@ -322,8 +325,6 @@ class LoginOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsPassword { try { form.control(passwordControlPath()); @@ -341,12 +342,7 @@ class LoginOForm implements FormModel { void get passwordFocus => form.focus(passwordControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void emailRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void emailRemove({bool updateParent = true, bool emitEvent = true}) { if (containsEmail) { final controlPath = path; if (controlPath == null) { @@ -369,12 +365,7 @@ class LoginOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void passwordRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void passwordRemove({bool updateParent = true, bool emitEvent = true}) { if (containsPassword) { final controlPath = path; if (controlPath == null) { @@ -402,8 +393,11 @@ class LoginOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - emailControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void passwordValueUpdate( @@ -411,8 +405,11 @@ class LoginOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - passwordControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + passwordControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailValuePatch( @@ -420,8 +417,11 @@ class LoginOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - emailControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void passwordValuePatch( @@ -429,8 +429,11 @@ class LoginOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - passwordControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + passwordControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailValueReset( @@ -439,14 +442,13 @@ class LoginOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - emailControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => emailControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void passwordValueReset( String? value, { @@ -454,14 +456,13 @@ class LoginOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - passwordControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => passwordControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get emailControl => form.control(emailControlPath()) as FormControl; @@ -526,27 +527,20 @@ class LoginOForm implements FormModel { } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { - final currentFormInstance = currentForm; - - if (currentFormInstance is! FormGroup) { - return; - } - + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { - currentFormInstance.controls[key]?.markAsEnabled( + currentForm.controls[key]?.markAsEnabled( updateParent: updateParent, emitEvent: emitEvent, ); @@ -562,9 +556,7 @@ class LoginOForm implements FormModel { final currentForm = this.currentForm; return const DeepCollectionEquality().equals( - currentForm is FormControlCollection - ? currentForm.rawValue - : currentForm.value, + currentForm.rawValue, LoginOForm.formElements(other).rawValue, ); } @@ -585,8 +577,16 @@ class LoginOForm implements FormModel { } @override - AbstractControl get currentForm { - return path == null ? form : form.control(path!); + bool get hasChanged { + return !const DeepCollectionEquality().equals( + currentForm.rawValue, + initial, + ); + } + + @override + FormGroup get currentForm { + return path == null ? form : form.control(path!) as FormGroup; } @override @@ -594,59 +594,125 @@ class LoginOForm implements FormModel { LoginO? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(LoginOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + LoginOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void upsertValue( + LoginO? value, { + bool updateParent = true, + bool emitEvent = true, + }) { + final formElements = LoginOForm.formElements(value); + + currentForm.addAll(formElements.controls); + } @override void reset({ LoginO? value, bool updateParent = true, bool emitEvent = true, - }) => - form.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void updateInitial(Map? value, String? path) { + if (_formModel != null) { + _formModel?.updateInitial(currentForm.rawValue, path); + return; + } + + if (value == null) return; + + if (path == null || path.isEmpty) { + initial.addAll(value); + return; + } + + final keys = path.split('.'); + Object? current = initial; + for (var i = 0; i < keys.length - 1; i++) { + final key = keys[i]; + + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current = current[index]; + continue; + } + } + + if (current is Map) { + if (!current.containsKey(key)) { + current[key] = {}; + } + current = current[key]; + continue; + } + + return; + } + + final key = keys.last; + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current[index] = value; + } + } else if (current is Map) { + current[key] = value; + } + } String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(LoginO? loginO) => FormGroup({ - emailControlName: FormControl( - value: loginO?.email, - validators: [RequiredValidator(), RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - passwordControlName: FormControl( - value: loginO?.password, - validators: [RequiredValidator2()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [ - MustMatchValidator() - ], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(LoginO? loginO) => FormGroup( + { + emailControlName: FormControl( + value: loginO?.email, + validators: [RequiredValidator(), RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + passwordControlName: FormControl( + value: loginO?.password, + validators: [RequiredValidator2()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [MustMatchValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } @Rf( - output: true, - requiredValidators: [...defaultRequiredValidators, 'RequiredValidator2()']) + output: true, + requiredValidators: [...defaultRequiredValidators, 'RequiredValidator2()'], +) @RfGroup(validators: [MustMatchValidator()]) class LoginOOutput extends Equatable { final String email; final String password; - const LoginOOutput( - {@RfControl(validators: [RequiredValidator(), RequiredValidator()]) - required this.email, - @RfControl(validators: [RequiredValidator2()]) required this.password}); + const LoginOOutput({ + @RfControl(validators: [RequiredValidator(), RequiredValidator()]) + required this.email, + @RfControl(validators: [RequiredValidator2()]) required this.password, + }); @override List get props => [email, password]; } @@ -661,30 +727,39 @@ class ReactiveLoginOFormArrayBuilder required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - LoginOForm formModel)? control; + LoginOForm formModel, + )? + control; final Widget Function( - BuildContext context, List itemList, LoginOForm formModel)? - builder; + BuildContext context, + List itemList, + LoginOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveLoginOFormArrayBuilderT? item, - LoginOForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveLoginOFormArrayBuilderT? item, + LoginOForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function(FormControl control)? - controlFilter; + controlFilter; @override Widget build(BuildContext context) { @@ -700,18 +775,9 @@ class ReactiveLoginOFormArrayBuilder return ReactiveFormArrayItemBuilder( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -729,35 +795,41 @@ class ReactiveLoginOFormArrayBuilder2 required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - LoginOForm formModel)? control; + LoginOForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - LoginOForm formModel - }) params)? builder; + ({BuildContext context, List itemList, LoginOForm formModel}) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveLoginOFormArrayBuilderT? item, - LoginOForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveLoginOFormArrayBuilderT? item, + LoginOForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function(FormControl control)? - controlFilter; + controlFilter; @override Widget build(BuildContext context) { @@ -774,23 +846,17 @@ class ReactiveLoginOFormArrayBuilder2 formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -799,33 +865,48 @@ class ReactiveLoginOFormArrayBuilder2 } class ReactiveLoginOFormFormGroupArrayBuilder< - ReactiveLoginOFormFormGroupArrayBuilderT> extends StatelessWidget { + ReactiveLoginOFormFormGroupArrayBuilderT +> + extends StatelessWidget { const ReactiveLoginOFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? extended; - - final ExtendedControl?>, - List> - Function(LoginOForm formModel)? getExtended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(LoginOForm formModel)? + getExtended; final Widget Function( - BuildContext context, List itemList, LoginOForm formModel)? - builder; + BuildContext context, + List itemList, + LoginOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveLoginOFormFormGroupArrayBuilderT? item, - LoginOForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveLoginOFormFormGroupArrayBuilderT? item, + LoginOForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -843,23 +924,14 @@ class ReactiveLoginOFormFormGroupArrayBuilder< final itemList = (value.value() ?? []) .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) .values .toList(); - return builder?.call( - context, - itemList, - formModel, - ) ?? + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/generator_tests/test/doc/login_test.dart b/packages/generator_tests/test/doc/login_test.dart index 2b675da0..9261b6b7 100644 --- a/packages/generator_tests/test/doc/login_test.dart +++ b/packages/generator_tests/test/doc/login_test.dart @@ -34,7 +34,7 @@ void main() { validators: [MustMatchValidator()], ) class Login { - final String email; + final String? email; final String password; @@ -42,7 +42,7 @@ void main() { @RfControl( validators: [RequiredValidator(), RequiredValidator()], ) - this.email = "", + this.email, @RfControl( validators: [RequiredValidator()], ) @@ -68,16 +68,17 @@ part of 'login.dart'; // ************************************************************************** class ReactiveLoginFormConsumer extends StatelessWidget { - const ReactiveLoginFormConsumer({ - Key? key, - required this.builder, - this.child, - }) : super(key: key); + const ReactiveLoginFormConsumer({Key? key, required this.builder, this.child}) + : super(key: key); final Widget? child; final Widget Function( - BuildContext context, LoginForm formModel, Widget? child) builder; + BuildContext context, + LoginForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -96,11 +97,7 @@ class LoginFormInheritedStreamer extends InheritedStreamer { required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final LoginForm form; } @@ -122,10 +119,7 @@ class ReactiveLoginForm extends StatelessWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - static LoginForm? of( - BuildContext context, { - bool listen = true, - }) { + static LoginForm? of(BuildContext context, {bool listen = true}) { if (listen) { return context .dependOnInheritedWidgetOfExactType() @@ -179,7 +173,11 @@ class LoginFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, LoginForm formModel, Widget? child) builder; + BuildContext context, + LoginForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, LoginForm formModel)? initState; @@ -194,7 +192,7 @@ class _LoginFormBuilderState extends State { @override void initState() { - _formModel = LoginForm(LoginForm.formElements(widget.model), null); + _formModel = LoginForm(LoginForm.formElements(widget.model), null, null); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -271,10 +269,7 @@ class _LoginFormBuilderState extends State { final _logLoginForm = Logger.detached('LoginForm'); class LoginForm implements FormModel { - LoginForm( - this.form, - this.path, - ); + LoginForm(this.form, this.path, this._formModel) : initial = form.rawValue; static const String emailControlName = "email"; @@ -284,22 +279,26 @@ class LoginForm implements FormModel { final String? path; + // ignore: unused_field + final FormModel? _formModel; + final Map _disabled = {}; + @override + final Map initial; + String emailControlPath() => pathBuilder(emailControlName); String passwordControlPath() => pathBuilder(passwordControlName); - String get _emailValue => emailControl.value ?? ""; + String? get _emailValue => containsEmail ? emailControl.value : null; String get _passwordValue => passwordControl.value ?? ""; - String get _emailRawValue => emailControl.value ?? ""; + String? get _emailRawValue => containsEmail ? emailControl.value : null; String get _passwordRawValue => passwordControl.value ?? ""; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsEmail { try { form.control(emailControlPath()); @@ -309,8 +308,6 @@ class LoginForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsPassword { try { form.control(passwordControlPath()); @@ -320,7 +317,7 @@ class LoginForm implements FormModel { } } - Map get emailErrors => emailControl.errors; + Map? get emailErrors => emailControl.errors; Map get passwordErrors => passwordControl.errors; @@ -328,13 +325,39 @@ class LoginForm implements FormModel { void get passwordFocus => form.focus(passwordControlPath()); + void emailRemove({bool updateParent = true, bool emitEvent = true}) { + if (containsEmail) { + final controlPath = path; + if (controlPath == null) { + form.removeControl( + emailControlName, + updateParent: updateParent, + emitEvent: emitEvent, + ); + } else { + final formGroup = form.control(controlPath); + + if (formGroup is FormGroup) { + formGroup.removeControl( + emailControlName, + updateParent: updateParent, + emitEvent: emitEvent, + ); + } + } + } + } + void emailValueUpdate( - String value, { + String? value, { bool updateParent = true, bool emitEvent = true, }) { - emailControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void passwordValueUpdate( @@ -342,17 +365,23 @@ class LoginForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - passwordControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + passwordControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailValuePatch( - String value, { + String? value, { bool updateParent = true, bool emitEvent = true, }) { - emailControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void passwordValuePatch( @@ -360,24 +389,26 @@ class LoginForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - passwordControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + passwordControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailValueReset( - String value, { + String? value, { bool updateParent = true, bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - emailControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => emailControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void passwordValueReset( String value, { @@ -385,14 +416,13 @@ class LoginForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - passwordControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => passwordControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get emailControl => form.control(emailControlPath()) as FormControl; @@ -456,27 +486,20 @@ class LoginForm implements FormModel { } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { - final currentFormInstance = currentForm; - - if (currentFormInstance is! FormGroup) { - return; - } - + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { - currentFormInstance.controls[key]?.markAsEnabled( + currentForm.controls[key]?.markAsEnabled( updateParent: updateParent, emitEvent: emitEvent, ); @@ -492,9 +515,7 @@ class LoginForm implements FormModel { final currentForm = this.currentForm; return const DeepCollectionEquality().equals( - currentForm is FormControlCollection - ? currentForm.rawValue - : currentForm.value, + currentForm.rawValue, LoginForm.formElements(other).rawValue, ); } @@ -515,8 +536,16 @@ class LoginForm implements FormModel { } @override - AbstractControl get currentForm { - return path == null ? form : form.control(path!); + bool get hasChanged { + return !const DeepCollectionEquality().equals( + currentForm.rawValue, + initial, + ); + } + + @override + FormGroup get currentForm { + return path == null ? form : form.control(path!) as FormGroup; } @override @@ -524,46 +553,107 @@ class LoginForm implements FormModel { Login? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(LoginForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + LoginForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void reset({ - Login? value, + void upsertValue( + Login? value, { bool updateParent = true, bool emitEvent = true, - }) => - form.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) { + final formElements = LoginForm.formElements(value); + + currentForm.addAll(formElements.controls); + } + + @override + void reset({Login? value, bool updateParent = true, bool emitEvent = true}) => + currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void updateInitial(Map? value, String? path) { + if (_formModel != null) { + _formModel?.updateInitial(currentForm.rawValue, path); + return; + } + + if (value == null) return; + + if (path == null || path.isEmpty) { + initial.addAll(value); + return; + } + + final keys = path.split('.'); + Object? current = initial; + for (var i = 0; i < keys.length - 1; i++) { + final key = keys[i]; + + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current = current[index]; + continue; + } + } + + if (current is Map) { + if (!current.containsKey(key)) { + current[key] = {}; + } + current = current[key]; + continue; + } + + return; + } + + final key = keys.last; + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current[index] = value; + } + } else if (current is Map) { + current[key] = value; + } + } String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(Login? login) => FormGroup({ - emailControlName: FormControl( - value: login?.email, - validators: [RequiredValidator(), RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - passwordControlName: FormControl( - value: login?.password, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [ - MustMatchValidator() - ], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(Login? login) => FormGroup( + { + emailControlName: FormControl( + value: login?.email, + validators: [RequiredValidator(), RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + passwordControlName: FormControl( + value: login?.password, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [MustMatchValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } class ReactiveLoginFormArrayBuilder @@ -576,30 +666,39 @@ class ReactiveLoginFormArrayBuilder required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - LoginForm formModel)? control; + LoginForm formModel, + )? + control; final Widget Function( - BuildContext context, List itemList, LoginForm formModel)? - builder; + BuildContext context, + List itemList, + LoginForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveLoginFormArrayBuilderT? item, - LoginForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveLoginFormArrayBuilderT? item, + LoginForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function(FormControl control)? - controlFilter; + controlFilter; @override Widget build(BuildContext context) { @@ -615,18 +714,9 @@ class ReactiveLoginFormArrayBuilder return ReactiveFormArrayItemBuilder( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -644,35 +734,40 @@ class ReactiveLoginFormArrayBuilder2 required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - LoginForm formModel)? control; + LoginForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - LoginForm formModel - }) params)? builder; + ({BuildContext context, List itemList, LoginForm formModel}) params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveLoginFormArrayBuilderT? item, - LoginForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveLoginFormArrayBuilderT? item, + LoginForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function(FormControl control)? - controlFilter; + controlFilter; @override Widget build(BuildContext context) { @@ -689,23 +784,17 @@ class ReactiveLoginFormArrayBuilder2 formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -714,33 +803,48 @@ class ReactiveLoginFormArrayBuilder2 } class ReactiveLoginFormFormGroupArrayBuilder< - ReactiveLoginFormFormGroupArrayBuilderT> extends StatelessWidget { + ReactiveLoginFormFormGroupArrayBuilderT +> + extends StatelessWidget { const ReactiveLoginFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? extended; - - final ExtendedControl?>, - List> - Function(LoginForm formModel)? getExtended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(LoginForm formModel)? + getExtended; final Widget Function( - BuildContext context, List itemList, LoginForm formModel)? - builder; + BuildContext context, + List itemList, + LoginForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveLoginFormFormGroupArrayBuilderT? item, - LoginForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveLoginFormFormGroupArrayBuilderT? item, + LoginForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -758,23 +862,14 @@ class ReactiveLoginFormFormGroupArrayBuilder< final itemList = (value.value() ?? []) .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) .values .toList(); - return builder?.call( - context, - itemList, - formModel, - ) ?? + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/generator_tests/test/doc/mailing_list_output_test.dart b/packages/generator_tests/test/doc/mailing_list_output_test.dart index 556dd2ed..b27181e1 100644 --- a/packages/generator_tests/test/doc/mailing_list_output_test.dart +++ b/packages/generator_tests/test/doc/mailing_list_output_test.dart @@ -68,7 +68,11 @@ class ReactiveMailingListOFormConsumer extends StatelessWidget { final Widget? child; final Widget Function( - BuildContext context, MailingListOForm formModel, Widget? child) builder; + BuildContext context, + MailingListOForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -87,11 +91,7 @@ class MailingListOFormInheritedStreamer extends InheritedStreamer { required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final MailingListOForm form; } @@ -113,19 +113,19 @@ class ReactiveMailingListOForm extends StatelessWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - static MailingListOForm? of( - BuildContext context, { - bool listen = true, - }) { + static MailingListOForm? of(BuildContext context, {bool listen = true}) { if (listen) { return context .dependOnInheritedWidgetOfExactType< - MailingListOFormInheritedStreamer>() + MailingListOFormInheritedStreamer + >() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - MailingListOFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + MailingListOFormInheritedStreamer + >(); return element == null ? null : (element.widget as MailingListOFormInheritedStreamer).form; @@ -173,10 +173,14 @@ class MailingListOFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, MailingListOForm formModel, Widget? child) builder; + BuildContext context, + MailingListOForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, MailingListOForm formModel)? - initState; + initState; @override _MailingListOFormBuilderState createState() => @@ -190,8 +194,11 @@ class _MailingListOFormBuilderState extends State { @override void initState() { - _formModel = - MailingListOForm(MailingListOForm.formElements(widget.model), null); + _formModel = MailingListOForm( + MailingListOForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -268,10 +275,8 @@ class _MailingListOFormBuilderState extends State { final _logMailingListOForm = Logger.detached('MailingListOForm'); class MailingListOForm implements FormModel { - MailingListOForm( - this.form, - this.path, - ); + MailingListOForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String emailListControlName = "emailList"; @@ -279,8 +284,14 @@ class MailingListOForm implements FormModel { final String? path; + // ignore: unused_field + final FormModel? _formModel; + final Map _disabled = {}; + @override + final Map initial; + String emailListControlPath() => pathBuilder(emailListControlName); List get _emailListValue => @@ -289,8 +300,6 @@ class MailingListOForm implements FormModel { List get _emailListRawValue => emailListControl.rawValue.whereType().toList(); - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsEmailList { try { form.control(emailListControlPath()); @@ -309,8 +318,11 @@ class MailingListOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - emailListControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailListControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailListValuePatch( @@ -318,8 +330,11 @@ class MailingListOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - emailListControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailListControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailListValueReset( @@ -328,14 +343,13 @@ class MailingListOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - emailListControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => emailListControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormArray get emailListControl => form.control(emailListControlPath()) as FormArray; @@ -392,13 +406,15 @@ class MailingListOForm implements FormModel { break; } - emailListControl.add(FormControl( - value: value, - validators: resultingValidators, - asyncValidators: resultingAsyncValidators, - asyncValidatorsDebounceTime: asyncValidatorsDebounceTime ?? 250, - disabled: disabled ?? false, - )); + emailListControl.add( + FormControl( + value: value, + validators: resultingValidators, + asyncValidators: resultingAsyncValidators, + asyncValidatorsDebounceTime: asyncValidatorsDebounceTime ?? 250, + disabled: disabled ?? false, + ), + ); } @override @@ -422,27 +438,20 @@ class MailingListOForm implements FormModel { } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { - final currentFormInstance = currentForm; - - if (currentFormInstance is! FormGroup) { - return; - } - + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { - currentFormInstance.controls[key]?.markAsEnabled( + currentForm.controls[key]?.markAsEnabled( updateParent: updateParent, emitEvent: emitEvent, ); @@ -458,9 +467,7 @@ class MailingListOForm implements FormModel { final currentForm = this.currentForm; return const DeepCollectionEquality().equals( - currentForm is FormControlCollection - ? currentForm.rawValue - : currentForm.value, + currentForm.rawValue, MailingListOForm.formElements(other).rawValue, ); } @@ -481,8 +488,16 @@ class MailingListOForm implements FormModel { } @override - AbstractControl get currentForm { - return path == null ? form : form.control(path!); + bool get hasChanged { + return !const DeepCollectionEquality().equals( + currentForm.rawValue, + initial, + ); + } + + @override + FormGroup get currentForm { + return path == null ? form : form.control(path!) as FormGroup; } @override @@ -490,58 +505,129 @@ class MailingListOForm implements FormModel { MailingListO? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(MailingListOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + MailingListOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void upsertValue( + MailingListO? value, { + bool updateParent = true, + bool emitEvent = true, + }) { + final formElements = MailingListOForm.formElements(value); + + currentForm.addAll(formElements.controls); + } @override void reset({ MailingListO? value, bool updateParent = true, bool emitEvent = true, - }) => - form.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void updateInitial(Map? value, String? path) { + if (_formModel != null) { + _formModel?.updateInitial(currentForm.rawValue, path); + return; + } + + if (value == null) return; + + if (path == null || path.isEmpty) { + initial.addAll(value); + return; + } + + final keys = path.split('.'); + Object? current = initial; + for (var i = 0; i < keys.length - 1; i++) { + final key = keys[i]; + + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current = current[index]; + continue; + } + } + + if (current is Map) { + if (!current.containsKey(key)) { + current[key] = {}; + } + current = current[key]; + continue; + } + + return; + } + + final key = keys.last; + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current[index] = value; + } + } else if (current is Map) { + current[key] = value; + } + } String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(MailingListO? mailingListO) => FormGroup({ - emailListControlName: FormArray( - (mailingListO?.emailList ?? []) - .map((e) => FormControl( - value: e, - validators: [EmailValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - )) - .toList(), - validators: [MailingListValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(MailingListO? mailingListO) => FormGroup( + { + emailListControlName: FormArray( + (mailingListO?.emailList ?? []) + .map( + (e) => FormControl( + value: e, + validators: [EmailValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ), + ) + .toList(), + validators: [MailingListValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } @Rf(output: true) class MailingListOOutput { final List emailList; - MailingListOOutput( - {@RfArray( - validators: [MailingListValidator()], - itemValidators: [EmailValidator()]) - this.emailList = const []}); + MailingListOOutput({ + @RfArray( + validators: [MailingListValidator()], + itemValidators: [EmailValidator()], + ) + this.emailList = const [], + }); } class ReactiveMailingListOFormArrayBuilder< - ReactiveMailingListOFormArrayBuilderT> extends StatelessWidget { + ReactiveMailingListOFormArrayBuilderT +> + extends StatelessWidget { const ReactiveMailingListOFormArrayBuilder({ Key? key, this.control, @@ -550,30 +636,41 @@ class ReactiveMailingListOFormArrayBuilder< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - MailingListOForm formModel)? control; + MailingListOForm formModel, + )? + control; - final Widget Function(BuildContext context, List itemList, - MailingListOForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + MailingListOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveMailingListOFormArrayBuilderT? item, - MailingListOForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveMailingListOFormArrayBuilderT? item, + MailingListOForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -589,18 +686,9 @@ class ReactiveMailingListOFormArrayBuilder< return ReactiveFormArrayItemBuilder( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -609,7 +697,9 @@ class ReactiveMailingListOFormArrayBuilder< } class ReactiveMailingListOFormArrayBuilder2< - ReactiveMailingListOFormArrayBuilderT> extends StatelessWidget { + ReactiveMailingListOFormArrayBuilderT +> + extends StatelessWidget { const ReactiveMailingListOFormArrayBuilder2({ Key? key, this.control, @@ -618,36 +708,43 @@ class ReactiveMailingListOFormArrayBuilder2< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - MailingListOForm formModel)? control; + MailingListOForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - MailingListOForm formModel - }) params)? builder; + ({BuildContext context, List itemList, MailingListOForm formModel}) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveMailingListOFormArrayBuilderT? item, - MailingListOForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveMailingListOFormArrayBuilderT? item, + MailingListOForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -664,23 +761,17 @@ class ReactiveMailingListOFormArrayBuilder2< formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -689,32 +780,48 @@ class ReactiveMailingListOFormArrayBuilder2< } class ReactiveMailingListOFormFormGroupArrayBuilder< - ReactiveMailingListOFormFormGroupArrayBuilderT> extends StatelessWidget { + ReactiveMailingListOFormFormGroupArrayBuilderT +> + extends StatelessWidget { const ReactiveMailingListOFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? extended; - - final ExtendedControl?>, - List> - Function(MailingListOForm formModel)? getExtended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(MailingListOForm formModel)? + getExtended; - final Widget Function(BuildContext context, List itemList, - MailingListOForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + MailingListOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveMailingListOFormFormGroupArrayBuilderT? item, - MailingListOForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveMailingListOFormFormGroupArrayBuilderT? item, + MailingListOForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -729,26 +836,18 @@ class ReactiveMailingListOFormFormGroupArrayBuilder< return StreamBuilder?>?>( stream: value.control.valueChanges, builder: (context, snapshot) { - final itemList = (value.value() ?? - []) - .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) - .values - .toList(); - - return builder?.call( - context, - itemList, - formModel, - ) ?? + final itemList = + (value.value() ?? + []) + .asMap() + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) + .values + .toList(); + + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/generator_tests/test/doc/mailing_list_test.dart b/packages/generator_tests/test/doc/mailing_list_test.dart index cf492d5d..9c4b1fd1 100644 --- a/packages/generator_tests/test/doc/mailing_list_test.dart +++ b/packages/generator_tests/test/doc/mailing_list_test.dart @@ -68,7 +68,11 @@ class ReactiveMailingListFormConsumer extends StatelessWidget { final Widget? child; final Widget Function( - BuildContext context, MailingListForm formModel, Widget? child) builder; + BuildContext context, + MailingListForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -87,11 +91,7 @@ class MailingListFormInheritedStreamer extends InheritedStreamer { required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final MailingListForm form; } @@ -113,19 +113,19 @@ class ReactiveMailingListForm extends StatelessWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - static MailingListForm? of( - BuildContext context, { - bool listen = true, - }) { + static MailingListForm? of(BuildContext context, {bool listen = true}) { if (listen) { return context .dependOnInheritedWidgetOfExactType< - MailingListFormInheritedStreamer>() + MailingListFormInheritedStreamer + >() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - MailingListFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + MailingListFormInheritedStreamer + >(); return element == null ? null : (element.widget as MailingListFormInheritedStreamer).form; @@ -172,10 +172,14 @@ class MailingListFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, MailingListForm formModel, Widget? child) builder; + BuildContext context, + MailingListForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, MailingListForm formModel)? - initState; + initState; @override _MailingListFormBuilderState createState() => _MailingListFormBuilderState(); @@ -188,8 +192,11 @@ class _MailingListFormBuilderState extends State { @override void initState() { - _formModel = - MailingListForm(MailingListForm.formElements(widget.model), null); + _formModel = MailingListForm( + MailingListForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -266,10 +273,8 @@ class _MailingListFormBuilderState extends State { final _logMailingListForm = Logger.detached('MailingListForm'); class MailingListForm implements FormModel { - MailingListForm( - this.form, - this.path, - ); + MailingListForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String emailListControlName = "emailList"; @@ -277,8 +282,14 @@ class MailingListForm implements FormModel { final String? path; + // ignore: unused_field + final FormModel? _formModel; + final Map _disabled = {}; + @override + final Map initial; + String emailListControlPath() => pathBuilder(emailListControlName); List get _emailListValue => @@ -287,8 +298,6 @@ class MailingListForm implements FormModel { List get _emailListRawValue => emailListControl.rawValue.whereType().toList(); - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsEmailList { try { form.control(emailListControlPath()); @@ -307,8 +316,11 @@ class MailingListForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - emailListControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailListControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailListValuePatch( @@ -316,8 +328,11 @@ class MailingListForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - emailListControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailListControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailListValueReset( @@ -326,14 +341,13 @@ class MailingListForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - emailListControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => emailListControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormArray get emailListControl => form.control(emailListControlPath()) as FormArray; @@ -390,13 +404,15 @@ class MailingListForm implements FormModel { break; } - emailListControl.add(FormControl( - value: value, - validators: resultingValidators, - asyncValidators: resultingAsyncValidators, - asyncValidatorsDebounceTime: asyncValidatorsDebounceTime ?? 250, - disabled: disabled ?? false, - )); + emailListControl.add( + FormControl( + value: value, + validators: resultingValidators, + asyncValidators: resultingAsyncValidators, + asyncValidatorsDebounceTime: asyncValidatorsDebounceTime ?? 250, + disabled: disabled ?? false, + ), + ); } @override @@ -419,27 +435,20 @@ class MailingListForm implements FormModel { } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { - final currentFormInstance = currentForm; - - if (currentFormInstance is! FormGroup) { - return; - } - + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { - currentFormInstance.controls[key]?.markAsEnabled( + currentForm.controls[key]?.markAsEnabled( updateParent: updateParent, emitEvent: emitEvent, ); @@ -455,9 +464,7 @@ class MailingListForm implements FormModel { final currentForm = this.currentForm; return const DeepCollectionEquality().equals( - currentForm is FormControlCollection - ? currentForm.rawValue - : currentForm.value, + currentForm.rawValue, MailingListForm.formElements(other).rawValue, ); } @@ -478,8 +485,16 @@ class MailingListForm implements FormModel { } @override - AbstractControl get currentForm { - return path == null ? form : form.control(path!); + bool get hasChanged { + return !const DeepCollectionEquality().equals( + currentForm.rawValue, + initial, + ); + } + + @override + FormGroup get currentForm { + return path == null ? form : form.control(path!) as FormGroup; } @override @@ -487,44 +502,111 @@ class MailingListForm implements FormModel { MailingList? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(MailingListForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + MailingListForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void upsertValue( + MailingList? value, { + bool updateParent = true, + bool emitEvent = true, + }) { + final formElements = MailingListForm.formElements(value); + + currentForm.addAll(formElements.controls); + } @override void reset({ MailingList? value, bool updateParent = true, bool emitEvent = true, - }) => - form.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void updateInitial(Map? value, String? path) { + if (_formModel != null) { + _formModel?.updateInitial(currentForm.rawValue, path); + return; + } + + if (value == null) return; + + if (path == null || path.isEmpty) { + initial.addAll(value); + return; + } + + final keys = path.split('.'); + Object? current = initial; + for (var i = 0; i < keys.length - 1; i++) { + final key = keys[i]; + + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current = current[index]; + continue; + } + } + + if (current is Map) { + if (!current.containsKey(key)) { + current[key] = {}; + } + current = current[key]; + continue; + } + + return; + } + + final key = keys.last; + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current[index] = value; + } + } else if (current is Map) { + current[key] = value; + } + } String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(MailingList? mailingList) => FormGroup({ - emailListControlName: FormArray( - (mailingList?.emailList ?? []) - .map((e) => FormControl( - value: e, - validators: [EmailValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - )) - .toList(), - validators: [MailingListValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(MailingList? mailingList) => FormGroup( + { + emailListControlName: FormArray( + (mailingList?.emailList ?? []) + .map( + (e) => FormControl( + value: e, + validators: [EmailValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ), + ) + .toList(), + validators: [MailingListValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } class ReactiveMailingListFormArrayBuilder @@ -537,29 +619,41 @@ class ReactiveMailingListFormArrayBuilder required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - MailingListForm formModel)? control; + MailingListForm formModel, + )? + control; - final Widget Function(BuildContext context, List itemList, - MailingListForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + MailingListForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveMailingListFormArrayBuilderT? item, - MailingListForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveMailingListFormArrayBuilderT? item, + MailingListForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -575,18 +669,9 @@ class ReactiveMailingListFormArrayBuilder return ReactiveFormArrayItemBuilder( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -604,35 +689,43 @@ class ReactiveMailingListFormArrayBuilder2 required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - MailingListForm formModel)? control; + MailingListForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - MailingListForm formModel - }) params)? builder; + ({BuildContext context, List itemList, MailingListForm formModel}) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveMailingListFormArrayBuilderT? item, - MailingListForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveMailingListFormArrayBuilderT? item, + MailingListForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -649,23 +742,17 @@ class ReactiveMailingListFormArrayBuilder2 formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -674,32 +761,48 @@ class ReactiveMailingListFormArrayBuilder2 } class ReactiveMailingListFormFormGroupArrayBuilder< - ReactiveMailingListFormFormGroupArrayBuilderT> extends StatelessWidget { + ReactiveMailingListFormFormGroupArrayBuilderT +> + extends StatelessWidget { const ReactiveMailingListFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? extended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(MailingListForm formModel)? + getExtended; - final ExtendedControl?>, - List> - Function(MailingListForm formModel)? getExtended; - - final Widget Function(BuildContext context, List itemList, - MailingListForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + MailingListForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveMailingListFormFormGroupArrayBuilderT? item, - MailingListForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveMailingListFormFormGroupArrayBuilderT? item, + MailingListForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -717,23 +820,14 @@ class ReactiveMailingListFormFormGroupArrayBuilder< final itemList = (value.value() ?? []) .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) .values .toList(); - return builder?.call( - context, - itemList, - formModel, - ) ?? + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/generator_tests/test/doc/nested_generics_output_test.dart b/packages/generator_tests/test/doc/nested_generics_output_test.dart index ebd422f7..144b90a3 100644 --- a/packages/generator_tests/test/doc/nested_generics_output_test.dart +++ b/packages/generator_tests/test/doc/nested_generics_output_test.dart @@ -89,8 +89,12 @@ class ReactiveProductDetailsOFormConsumer

final Widget? child; - final Widget Function(BuildContext context, - ProductDetailsOForm formModel, Widget? child) builder; + final Widget Function( + BuildContext context, + ProductDetailsOForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -110,11 +114,7 @@ class ProductDetailsOFormInheritedStreamer

required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final ProductDetailsOForm form; } @@ -144,12 +144,15 @@ class ReactiveProductDetailsOForm

if (listen) { return context .dependOnInheritedWidgetOfExactType< - ProductDetailsOFormInheritedStreamer>() + ProductDetailsOFormInheritedStreamer + >() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - ProductDetailsOFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + ProductDetailsOFormInheritedStreamer + >(); return element == null ? null : (element.widget as ProductDetailsOFormInheritedStreamer).form; @@ -171,12 +174,12 @@ class ReactiveProductDetailsOForm

extension ReactiveReactiveProductDetailsOFormExt on BuildContext { ProductDetailsOForm? - productDetailsOFormWatch

() => - ReactiveProductDetailsOForm.of(this); + productDetailsOFormWatch

() => + ReactiveProductDetailsOForm.of(this); ProductDetailsOForm? - productDetailsOFormRead

() => - ReactiveProductDetailsOForm.of(this, listen: false); + productDetailsOFormRead

() => + ReactiveProductDetailsOForm.of(this, listen: false); } class ProductDetailsOFormBuilder

@@ -199,11 +202,18 @@ class ProductDetailsOFormBuilder

final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - final Widget Function(BuildContext context, - ProductDetailsOForm formModel, Widget? child) builder; + final Widget Function( + BuildContext context, + ProductDetailsOForm formModel, + Widget? child, + ) + builder; final void Function( - BuildContext context, ProductDetailsOForm formModel)? initState; + BuildContext context, + ProductDetailsOForm formModel, + )? + initState; @override _ProductDetailsOFormBuilderState createState() => @@ -219,7 +229,10 @@ class _ProductDetailsOFormBuilderState

@override void initState() { _formModel = ProductDetailsOForm( - ProductDetailsOForm.formElements(widget.model), null); + ProductDetailsOForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -297,10 +310,8 @@ final _logProductDetailsOForm = Logger.detached('ProductDetailsOForm'); class ProductDetailsOForm

implements FormModel, ProductDetailsOOutput> { - ProductDetailsOForm( - this.form, - this.path, - ); + ProductDetailsOForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String descriptionControlName = "description"; @@ -310,22 +321,28 @@ class ProductDetailsOForm

final String? path; + // ignore: unused_field + final FormModel? _formModel; + final Map _disabled = {}; + @override + final Map initial; + String descriptionControlPath() => pathBuilder(descriptionControlName); String idControlPath() => pathBuilder(idControlName); - String? get _descriptionValue => descriptionControl.value; + String? get _descriptionValue => + containsDescription ? descriptionControl.value : null; - IdOOutput? get _idValue => idForm.model; + IdOOutput? get _idValue => containsId ? idForm.model : null; - String? get _descriptionRawValue => descriptionControl.value; + String? get _descriptionRawValue => + containsDescription ? descriptionControl.value : null; - IdO? get _idRawValue => idForm.rawModel; + IdO? get _idRawValue => containsId ? idForm.rawModel : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsDescription { try { form.control(descriptionControlPath()); @@ -335,8 +352,6 @@ class ProductDetailsOForm

} } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsId { try { form.control(idControlPath()); @@ -354,12 +369,7 @@ class ProductDetailsOForm

void get idFocus => form.focus(idControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void descriptionRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void descriptionRemove({bool updateParent = true, bool emitEvent = true}) { if (containsDescription) { final controlPath = path; if (controlPath == null) { @@ -382,12 +392,7 @@ class ProductDetailsOForm

} } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void idRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void idRemove({bool updateParent = true, bool emitEvent = true}) { if (containsId) { final controlPath = path; if (controlPath == null) { @@ -415,8 +420,11 @@ class ProductDetailsOForm

bool updateParent = true, bool emitEvent = true, }) { - descriptionControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + descriptionControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void idValueUpdate( @@ -424,8 +432,11 @@ class ProductDetailsOForm

bool updateParent = true, bool emitEvent = true, }) { - idControl.updateValue(IdOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + idControl.updateValue( + IdOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void descriptionValuePatch( @@ -433,8 +444,11 @@ class ProductDetailsOForm

bool updateParent = true, bool emitEvent = true, }) { - descriptionControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + descriptionControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void idValuePatch( @@ -442,8 +456,11 @@ class ProductDetailsOForm

bool updateParent = true, bool emitEvent = true, }) { - idControl.updateValue(IdOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + idControl.updateValue( + IdOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void descriptionValueReset( @@ -452,14 +469,13 @@ class ProductDetailsOForm

bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - descriptionControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => descriptionControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void idValueReset( IdO? value, { @@ -467,18 +483,19 @@ class ProductDetailsOForm

bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - idControl.reset( - value: IdOForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => idControl.reset( + value: IdOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); FormControl get descriptionControl => form.control(descriptionControlPath()) as FormControl; FormGroup get idControl => form.control(idControlPath()) as FormGroup; - IdOForm get idForm => IdOForm(form, pathBuilder('id')); + IdOForm get idForm => + IdOForm(form, pathBuilder('id'), _formModel ?? this); void descriptionSetDisabled( bool disabled, { @@ -509,10 +526,7 @@ class ProductDetailsOForm

emitEvent: emitEvent, ); } else { - idControl.markAsEnabled( - updateParent: updateParent, - emitEvent: emitEvent, - ); + idControl.markAsEnabled(updateParent: updateParent, emitEvent: emitEvent); } } @@ -529,39 +543,36 @@ class ProductDetailsOForm

); } return ProductDetailsOOutput( - description: _descriptionValue, id: _idValue); + description: _descriptionValue, + id: _idValue, + ); } @override ProductDetailsO get rawModel { return ProductDetailsO( - description: _descriptionRawValue, id: _idRawValue); + description: _descriptionRawValue, + id: _idRawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { - final currentFormInstance = currentForm; - - if (currentFormInstance is! FormGroup) { - return; - } - + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); idForm.toggleDisabled(); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { idForm.toggleDisabled(); - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { - currentFormInstance.controls[key]?.markAsEnabled( + currentForm.controls[key]?.markAsEnabled( updateParent: updateParent, emitEvent: emitEvent, ); @@ -577,9 +588,7 @@ class ProductDetailsOForm

final currentForm = this.currentForm; return const DeepCollectionEquality().equals( - currentForm is FormControlCollection - ? currentForm.rawValue - : currentForm.value, + currentForm.rawValue, ProductDetailsOForm.formElements(other).rawValue, ); } @@ -600,8 +609,16 @@ class ProductDetailsOForm

} @override - AbstractControl get currentForm { - return path == null ? form : form.control(path!); + bool get hasChanged { + return !const DeepCollectionEquality().equals( + currentForm.rawValue, + initial, + ); + } + + @override + FormGroup get currentForm { + return path == null ? form : form.control(path!) as FormGroup; } @override @@ -609,50 +626,112 @@ class ProductDetailsOForm

ProductDetailsO? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(ProductDetailsOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + ProductDetailsOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void upsertValue( + ProductDetailsO? value, { + bool updateParent = true, + bool emitEvent = true, + }) { + final formElements = ProductDetailsOForm.formElements(value); + + currentForm.addAll(formElements.controls); + } @override void reset({ ProductDetailsO? value, bool updateParent = true, bool emitEvent = true, - }) => - form.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void updateInitial(Map? value, String? path) { + if (_formModel != null) { + _formModel?.updateInitial(currentForm.rawValue, path); + return; + } + + if (value == null) return; + + if (path == null || path.isEmpty) { + initial.addAll(value); + return; + } + + final keys = path.split('.'); + Object? current = initial; + for (var i = 0; i < keys.length - 1; i++) { + final key = keys[i]; + + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current = current[index]; + continue; + } + } + + if (current is Map) { + if (!current.containsKey(key)) { + current[key] = {}; + } + current = current[key]; + continue; + } + + return; + } + + final key = keys.last; + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current[index] = value; + } + } else if (current is Map) { + current[key] = value; + } + } String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); static FormGroup formElements

( - ProductDetailsO? productDetailsO) => - FormGroup({ - descriptionControlName: FormControl( - value: productDetailsO?.description, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - idControlName: IdOForm.formElements(productDetailsO?.id) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + ProductDetailsO? productDetailsO, + ) => FormGroup( + { + descriptionControlName: FormControl( + value: productDetailsO?.description, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + idControlName: IdOForm.formElements(productDetailsO?.id), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } final _logIdOForm = Logger.detached('IdOForm'); class IdOForm

implements FormModel, IdOOutput> { - IdOForm( - this.form, - this.path, - ); + IdOForm(this.form, this.path, this._formModel) : initial = form.rawValue; static const String companyNameControlName = "companyName"; @@ -662,22 +741,28 @@ class IdOForm

final String? path; + // ignore: unused_field + final FormModel? _formModel; + final Map _disabled = {}; + @override + final Map initial; + String companyNameControlPath() => pathBuilder(companyNameControlName); String nameControlPath() => pathBuilder(nameControlName); - String? get _companyNameValue => companyNameControl.value; + String? get _companyNameValue => + containsCompanyName ? companyNameControl.value : null; - String? get _nameValue => nameControl.value; + String? get _nameValue => containsName ? nameControl.value : null; - String? get _companyNameRawValue => companyNameControl.value; + String? get _companyNameRawValue => + containsCompanyName ? companyNameControl.value : null; - String? get _nameRawValue => nameControl.value; + String? get _nameRawValue => containsName ? nameControl.value : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsCompanyName { try { form.control(companyNameControlPath()); @@ -687,8 +772,6 @@ class IdOForm

} } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsName { try { form.control(nameControlPath()); @@ -706,12 +789,7 @@ class IdOForm

void get nameFocus => form.focus(nameControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void companyNameRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void companyNameRemove({bool updateParent = true, bool emitEvent = true}) { if (containsCompanyName) { final controlPath = path; if (controlPath == null) { @@ -734,12 +812,7 @@ class IdOForm

} } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void nameRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void nameRemove({bool updateParent = true, bool emitEvent = true}) { if (containsName) { final controlPath = path; if (controlPath == null) { @@ -767,8 +840,11 @@ class IdOForm

bool updateParent = true, bool emitEvent = true, }) { - companyNameControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + companyNameControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void nameValueUpdate( @@ -776,8 +852,11 @@ class IdOForm

bool updateParent = true, bool emitEvent = true, }) { - nameControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + nameControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void companyNameValuePatch( @@ -785,8 +864,11 @@ class IdOForm

bool updateParent = true, bool emitEvent = true, }) { - companyNameControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + companyNameControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void nameValuePatch( @@ -794,8 +876,11 @@ class IdOForm

bool updateParent = true, bool emitEvent = true, }) { - nameControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + nameControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void companyNameValueReset( @@ -804,14 +889,13 @@ class IdOForm

bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - companyNameControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => companyNameControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void nameValueReset( String? value, { @@ -819,14 +903,13 @@ class IdOForm

bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - nameControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => nameControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get companyNameControl => form.control(companyNameControlPath()) as FormControl; @@ -891,27 +974,20 @@ class IdOForm

} @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { - final currentFormInstance = currentForm; - - if (currentFormInstance is! FormGroup) { - return; - } - + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { - currentFormInstance.controls[key]?.markAsEnabled( + currentForm.controls[key]?.markAsEnabled( updateParent: updateParent, emitEvent: emitEvent, ); @@ -927,9 +1003,7 @@ class IdOForm

final currentForm = this.currentForm; return const DeepCollectionEquality().equals( - currentForm is FormControlCollection - ? currentForm.rawValue - : currentForm.value, + currentForm.rawValue, IdOForm.formElements(other).rawValue, ); } @@ -950,8 +1024,16 @@ class IdOForm

} @override - AbstractControl get currentForm { - return path == null ? form : form.control(path!); + bool get hasChanged { + return !const DeepCollectionEquality().equals( + currentForm.rawValue, + initial, + ); + } + + @override + FormGroup get currentForm { + return path == null ? form : form.control(path!) as FormGroup; } @override @@ -959,55 +1041,122 @@ class IdOForm

IdO? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(IdOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + IdOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void upsertValue( + IdO? value, { + bool updateParent = true, + bool emitEvent = true, + }) { + final formElements = IdOForm.formElements(value); + + currentForm.addAll(formElements.controls); + } @override void reset({ IdO? value, bool updateParent = true, bool emitEvent = true, - }) => - form.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void updateInitial(Map? value, String? path) { + if (_formModel != null) { + _formModel?.updateInitial(currentForm.rawValue, path); + return; + } + + if (value == null) return; + + if (path == null || path.isEmpty) { + initial.addAll(value); + return; + } + + final keys = path.split('.'); + Object? current = initial; + for (var i = 0; i < keys.length - 1; i++) { + final key = keys[i]; + + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current = current[index]; + continue; + } + } + + if (current is Map) { + if (!current.containsKey(key)) { + current[key] = {}; + } + current = current[key]; + continue; + } + + return; + } + + final key = keys.last; + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current[index] = value; + } + } else if (current is Map) { + current[key] = value; + } + } String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); static FormGroup formElements

( - IdO? idO) => - FormGroup({ - companyNameControlName: FormControl( - value: idO?.companyName, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - nameControlName: FormControl( - value: idO?.name, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + IdO? idO, + ) => FormGroup( + { + companyNameControlName: FormControl( + value: idO?.companyName, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + nameControlName: FormControl( + value: idO?.name, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } @freezed @Rf(output: true) abstract class ProductDetailsOOutput

with _$ProductDetailsOOutput { - factory ProductDetailsOOutput( - {@RfControl() String? description, - @Rf(output: false) IdOOutput? id}) = _ProductDetailsOOutput; + factory ProductDetailsOOutput({ + @RfControl() String? description, + @Rf(output: false) IdOOutput? id, + }) = _ProductDetailsOOutput; ProductDetailsOOutput._(); } @@ -1016,16 +1165,19 @@ abstract class ProductDetailsOOutput

@RfGroup() abstract class IdOOutput

with _$IdOOutput { - factory IdOOutput( - {@RfControl() String? companyName, - @RfControl() String? name}) = _IdOOutput; + factory IdOOutput({ + @RfControl() String? companyName, + @RfControl() String? name, + }) = _IdOOutput; IdOOutput._(); } class ReactiveProductDetailsOFormArrayBuilder< - ReactiveProductDetailsOFormArrayBuilderT, - P extends Product, - C extends Cart> extends StatelessWidget { + ReactiveProductDetailsOFormArrayBuilderT, + P extends Product, + C extends Cart +> + extends StatelessWidget { const ReactiveProductDetailsOFormArrayBuilder({ Key? key, this.control, @@ -1034,30 +1186,41 @@ class ReactiveProductDetailsOFormArrayBuilder< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - ProductDetailsOForm formModel)? control; + ProductDetailsOForm formModel, + )? + control; - final Widget Function(BuildContext context, List itemList, - ProductDetailsOForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + ProductDetailsOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveProductDetailsOFormArrayBuilderT? item, - ProductDetailsOForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveProductDetailsOFormArrayBuilderT? item, + ProductDetailsOForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -1071,21 +1234,13 @@ class ReactiveProductDetailsOFormArrayBuilder< final itemBuilder = this.itemBuilder; return ReactiveFormArrayItemBuilder< - ReactiveProductDetailsOFormArrayBuilderT>( + ReactiveProductDetailsOFormArrayBuilderT + >( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1094,9 +1249,11 @@ class ReactiveProductDetailsOFormArrayBuilder< } class ReactiveProductDetailsOFormArrayBuilder2< - ReactiveProductDetailsOFormArrayBuilderT, - P extends Product, - C extends Cart> extends StatelessWidget { + ReactiveProductDetailsOFormArrayBuilderT, + P extends Product, + C extends Cart +> + extends StatelessWidget { const ReactiveProductDetailsOFormArrayBuilder2({ Key? key, this.control, @@ -1105,36 +1262,47 @@ class ReactiveProductDetailsOFormArrayBuilder2< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - ProductDetailsOForm formModel)? control; + ProductDetailsOForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - ProductDetailsOForm formModel - }) params)? builder; + ({ + BuildContext context, + List itemList, + ProductDetailsOForm formModel, + }) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveProductDetailsOFormArrayBuilderT? item, - ProductDetailsOForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveProductDetailsOFormArrayBuilderT? item, + ProductDetailsOForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -1148,27 +1316,22 @@ class ReactiveProductDetailsOFormArrayBuilder2< final itemBuilder = this.itemBuilder; return ReactiveFormArrayItemBuilder< - ReactiveProductDetailsOFormArrayBuilderT>( + ReactiveProductDetailsOFormArrayBuilderT + >( formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1177,34 +1340,50 @@ class ReactiveProductDetailsOFormArrayBuilder2< } class ReactiveProductDetailsOFormFormGroupArrayBuilder< - ReactiveProductDetailsOFormFormGroupArrayBuilderT, - P extends Product, - C extends Cart> extends StatelessWidget { + ReactiveProductDetailsOFormFormGroupArrayBuilderT, + P extends Product, + C extends Cart +> + extends StatelessWidget { const ReactiveProductDetailsOFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? extended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(ProductDetailsOForm formModel)? + getExtended; - final ExtendedControl?>, - List> - Function(ProductDetailsOForm formModel)? getExtended; - - final Widget Function(BuildContext context, List itemList, - ProductDetailsOForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + ProductDetailsOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveProductDetailsOFormFormGroupArrayBuilderT? item, - ProductDetailsOForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveProductDetailsOFormFormGroupArrayBuilderT? item, + ProductDetailsOForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -1219,26 +1398,18 @@ class ReactiveProductDetailsOFormFormGroupArrayBuilder< return StreamBuilder?>?>( stream: value.control.valueChanges, builder: (context, snapshot) { - final itemList = (value.value() ?? - []) - .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) - .values - .toList(); - - return builder?.call( - context, - itemList, - formModel, - ) ?? + final itemList = + (value.value() ?? + []) + .asMap() + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) + .values + .toList(); + + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); @@ -1247,16 +1418,17 @@ class ReactiveProductDetailsOFormFormGroupArrayBuilder< class ReactiveIdOFormConsumer

extends StatelessWidget { - const ReactiveIdOFormConsumer({ - Key? key, - required this.builder, - this.child, - }) : super(key: key); + const ReactiveIdOFormConsumer({Key? key, required this.builder, this.child}) + : super(key: key); final Widget? child; final Widget Function( - BuildContext context, IdOForm formModel, Widget? child) builder; + BuildContext context, + IdOForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -1276,11 +1448,7 @@ class IdOFormInheritedStreamer

required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final IdOForm form; } @@ -1313,8 +1481,10 @@ class ReactiveIdOForm

?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - IdOFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + IdOFormInheritedStreamer + >(); return element == null ? null : (element.widget as IdOFormInheritedStreamer).form; @@ -1362,7 +1532,11 @@ class IdOFormBuilder

extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, IdOForm formModel, Widget? child) builder; + BuildContext context, + IdOForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, IdOForm formModel)? initState; @@ -1378,7 +1552,11 @@ class _IdOFormBuilderState

@override void initState() { - _formModel = IdOForm(IdOForm.formElements(widget.model), null); + _formModel = IdOForm( + IdOForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -1452,8 +1630,12 @@ class _IdOFormBuilderState

} } -class ReactiveIdOFormArrayBuilder extends StatelessWidget { +class ReactiveIdOFormArrayBuilder< + ReactiveIdOFormArrayBuilderT, + P extends Product, + C extends Cart +> + extends StatelessWidget { const ReactiveIdOFormArrayBuilder({ Key? key, this.control, @@ -1462,30 +1644,39 @@ class ReactiveIdOFormArrayBuilder? formControl; final FormArray? Function( - IdOForm formModel)? control; + IdOForm formModel, + )? + control; final Widget Function( - BuildContext context, List itemList, IdOForm formModel)? - builder; + BuildContext context, + List itemList, + IdOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveIdOFormArrayBuilderT? item, - IdOForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveIdOFormArrayBuilderT? item, + IdOForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function(FormControl control)? - controlFilter; + controlFilter; @override Widget build(BuildContext context) { @@ -1501,18 +1692,9 @@ class ReactiveIdOFormArrayBuilder( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1520,8 +1702,12 @@ class ReactiveIdOFormArrayBuilder extends StatelessWidget { +class ReactiveIdOFormArrayBuilder2< + ReactiveIdOFormArrayBuilderT, + P extends Product, + C extends Cart +> + extends StatelessWidget { const ReactiveIdOFormArrayBuilder2({ Key? key, this.control, @@ -1530,35 +1716,41 @@ class ReactiveIdOFormArrayBuilder2? formControl; final FormArray? Function( - IdOForm formModel)? control; + IdOForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - IdOForm formModel - }) params)? builder; + ({BuildContext context, List itemList, IdOForm formModel}) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveIdOFormArrayBuilderT? item, - IdOForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveIdOFormArrayBuilderT? item, + IdOForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function(FormControl control)? - controlFilter; + controlFilter; @override Widget build(BuildContext context) { @@ -1575,23 +1767,17 @@ class ReactiveIdOFormArrayBuilder2 builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1600,35 +1786,50 @@ class ReactiveIdOFormArrayBuilder2 extends StatelessWidget { + ReactiveIdOFormFormGroupArrayBuilderT, + P extends Product, + C extends Cart +> + extends StatelessWidget { const ReactiveIdOFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? extended; - - final ExtendedControl?>, - List> - Function(IdOForm formModel)? getExtended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(IdOForm formModel)? + getExtended; final Widget Function( - BuildContext context, List itemList, IdOForm formModel)? - builder; + BuildContext context, + List itemList, + IdOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveIdOFormFormGroupArrayBuilderT? item, - IdOForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveIdOFormFormGroupArrayBuilderT? item, + IdOForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -1646,23 +1847,14 @@ class ReactiveIdOFormFormGroupArrayBuilder< final itemList = (value.value() ?? []) .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) .values .toList(); - return builder?.call( - context, - itemList, - formModel, - ) ?? + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/generator_tests/test/doc/nested_generics_test.dart b/packages/generator_tests/test/doc/nested_generics_test.dart index cab22399..94116b23 100644 --- a/packages/generator_tests/test/doc/nested_generics_test.dart +++ b/packages/generator_tests/test/doc/nested_generics_test.dart @@ -89,8 +89,12 @@ class ReactiveProductDetailsFormConsumer

final Widget? child; - final Widget Function(BuildContext context, - ProductDetailsForm formModel, Widget? child) builder; + final Widget Function( + BuildContext context, + ProductDetailsForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -110,11 +114,7 @@ class ProductDetailsFormInheritedStreamer

required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final ProductDetailsForm form; } @@ -144,12 +144,15 @@ class ReactiveProductDetailsForm

if (listen) { return context .dependOnInheritedWidgetOfExactType< - ProductDetailsFormInheritedStreamer>() + ProductDetailsFormInheritedStreamer + >() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - ProductDetailsFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + ProductDetailsFormInheritedStreamer + >(); return element == null ? null : (element.widget as ProductDetailsFormInheritedStreamer).form; @@ -171,12 +174,12 @@ class ReactiveProductDetailsForm

extension ReactiveReactiveProductDetailsFormExt on BuildContext { ProductDetailsForm? - productDetailsFormWatch

() => - ReactiveProductDetailsForm.of(this); + productDetailsFormWatch

() => + ReactiveProductDetailsForm.of(this); ProductDetailsForm? - productDetailsFormRead

() => - ReactiveProductDetailsForm.of(this, listen: false); + productDetailsFormRead

() => + ReactiveProductDetailsForm.of(this, listen: false); } class ProductDetailsFormBuilder

@@ -199,11 +202,15 @@ class ProductDetailsFormBuilder

final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - final Widget Function(BuildContext context, - ProductDetailsForm formModel, Widget? child) builder; + final Widget Function( + BuildContext context, + ProductDetailsForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, ProductDetailsForm formModel)? - initState; + initState; @override _ProductDetailsFormBuilderState createState() => @@ -219,7 +226,10 @@ class _ProductDetailsFormBuilderState

@override void initState() { _formModel = ProductDetailsForm( - ProductDetailsForm.formElements(widget.model), null); + ProductDetailsForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -297,10 +307,8 @@ final _logProductDetailsForm = Logger.detached('ProductDetailsForm'); class ProductDetailsForm

implements FormModel, ProductDetails> { - ProductDetailsForm( - this.form, - this.path, - ); + ProductDetailsForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String descriptionControlName = "description"; @@ -310,22 +318,28 @@ class ProductDetailsForm

final String? path; + // ignore: unused_field + final FormModel? _formModel; + final Map _disabled = {}; + @override + final Map initial; + String descriptionControlPath() => pathBuilder(descriptionControlName); String idControlPath() => pathBuilder(idControlName); - String? get _descriptionValue => descriptionControl.value; + String? get _descriptionValue => + containsDescription ? descriptionControl.value : null; - Id? get _idValue => idForm.model; + Id? get _idValue => containsId ? idForm.model : null; - String? get _descriptionRawValue => descriptionControl.value; + String? get _descriptionRawValue => + containsDescription ? descriptionControl.value : null; - Id? get _idRawValue => idForm.rawModel; + Id? get _idRawValue => containsId ? idForm.rawModel : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsDescription { try { form.control(descriptionControlPath()); @@ -335,8 +349,6 @@ class ProductDetailsForm

} } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsId { try { form.control(idControlPath()); @@ -354,12 +366,7 @@ class ProductDetailsForm

void get idFocus => form.focus(idControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void descriptionRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void descriptionRemove({bool updateParent = true, bool emitEvent = true}) { if (containsDescription) { final controlPath = path; if (controlPath == null) { @@ -382,12 +389,7 @@ class ProductDetailsForm

} } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void idRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void idRemove({bool updateParent = true, bool emitEvent = true}) { if (containsId) { final controlPath = path; if (controlPath == null) { @@ -415,8 +417,11 @@ class ProductDetailsForm

bool updateParent = true, bool emitEvent = true, }) { - descriptionControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + descriptionControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void idValueUpdate( @@ -424,8 +429,11 @@ class ProductDetailsForm

bool updateParent = true, bool emitEvent = true, }) { - idControl.updateValue(IdForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + idControl.updateValue( + IdForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void descriptionValuePatch( @@ -433,8 +441,11 @@ class ProductDetailsForm

bool updateParent = true, bool emitEvent = true, }) { - descriptionControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + descriptionControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void idValuePatch( @@ -442,8 +453,11 @@ class ProductDetailsForm

bool updateParent = true, bool emitEvent = true, }) { - idControl.updateValue(IdForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + idControl.updateValue( + IdForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void descriptionValueReset( @@ -452,14 +466,13 @@ class ProductDetailsForm

bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - descriptionControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => descriptionControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void idValueReset( Id? value, { @@ -467,18 +480,19 @@ class ProductDetailsForm

bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - idControl.reset( - value: IdForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => idControl.reset( + value: IdForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); FormControl get descriptionControl => form.control(descriptionControlPath()) as FormControl; FormGroup get idControl => form.control(idControlPath()) as FormGroup; - IdForm get idForm => IdForm(form, pathBuilder('id')); + IdForm get idForm => + IdForm(form, pathBuilder('id'), _formModel ?? this); void descriptionSetDisabled( bool disabled, { @@ -509,10 +523,7 @@ class ProductDetailsForm

emitEvent: emitEvent, ); } else { - idControl.markAsEnabled( - updateParent: updateParent, - emitEvent: emitEvent, - ); + idControl.markAsEnabled(updateParent: updateParent, emitEvent: emitEvent); } } @@ -533,33 +544,28 @@ class ProductDetailsForm

@override ProductDetails get rawModel { return ProductDetails( - description: _descriptionRawValue, id: _idRawValue); + description: _descriptionRawValue, + id: _idRawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { - final currentFormInstance = currentForm; - - if (currentFormInstance is! FormGroup) { - return; - } - + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); idForm.toggleDisabled(); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { idForm.toggleDisabled(); - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { - currentFormInstance.controls[key]?.markAsEnabled( + currentForm.controls[key]?.markAsEnabled( updateParent: updateParent, emitEvent: emitEvent, ); @@ -575,9 +581,7 @@ class ProductDetailsForm

final currentForm = this.currentForm; return const DeepCollectionEquality().equals( - currentForm is FormControlCollection - ? currentForm.rawValue - : currentForm.value, + currentForm.rawValue, ProductDetailsForm.formElements(other).rawValue, ); } @@ -598,8 +602,16 @@ class ProductDetailsForm

} @override - AbstractControl get currentForm { - return path == null ? form : form.control(path!); + bool get hasChanged { + return !const DeepCollectionEquality().equals( + currentForm.rawValue, + initial, + ); + } + + @override + FormGroup get currentForm { + return path == null ? form : form.control(path!) as FormGroup; } @override @@ -607,50 +619,112 @@ class ProductDetailsForm

ProductDetails? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(ProductDetailsForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + ProductDetailsForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void upsertValue( + ProductDetails? value, { + bool updateParent = true, + bool emitEvent = true, + }) { + final formElements = ProductDetailsForm.formElements(value); + + currentForm.addAll(formElements.controls); + } @override void reset({ ProductDetails? value, bool updateParent = true, bool emitEvent = true, - }) => - form.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void updateInitial(Map? value, String? path) { + if (_formModel != null) { + _formModel?.updateInitial(currentForm.rawValue, path); + return; + } + + if (value == null) return; + + if (path == null || path.isEmpty) { + initial.addAll(value); + return; + } + + final keys = path.split('.'); + Object? current = initial; + for (var i = 0; i < keys.length - 1; i++) { + final key = keys[i]; + + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current = current[index]; + continue; + } + } + + if (current is Map) { + if (!current.containsKey(key)) { + current[key] = {}; + } + current = current[key]; + continue; + } + + return; + } + + final key = keys.last; + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current[index] = value; + } + } else if (current is Map) { + current[key] = value; + } + } String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); static FormGroup formElements

( - ProductDetails? productDetails) => - FormGroup({ - descriptionControlName: FormControl( - value: productDetails?.description, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - idControlName: IdForm.formElements(productDetails?.id) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + ProductDetails? productDetails, + ) => FormGroup( + { + descriptionControlName: FormControl( + value: productDetails?.description, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + idControlName: IdForm.formElements(productDetails?.id), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } final _logIdForm = Logger.detached('IdForm'); class IdForm

implements FormModel, Id> { - IdForm( - this.form, - this.path, - ); + IdForm(this.form, this.path, this._formModel) : initial = form.rawValue; static const String companyNameControlName = "companyName"; @@ -660,22 +734,28 @@ class IdForm

final String? path; + // ignore: unused_field + final FormModel? _formModel; + final Map _disabled = {}; + @override + final Map initial; + String companyNameControlPath() => pathBuilder(companyNameControlName); String nameControlPath() => pathBuilder(nameControlName); - String? get _companyNameValue => companyNameControl.value; + String? get _companyNameValue => + containsCompanyName ? companyNameControl.value : null; - String? get _nameValue => nameControl.value; + String? get _nameValue => containsName ? nameControl.value : null; - String? get _companyNameRawValue => companyNameControl.value; + String? get _companyNameRawValue => + containsCompanyName ? companyNameControl.value : null; - String? get _nameRawValue => nameControl.value; + String? get _nameRawValue => containsName ? nameControl.value : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsCompanyName { try { form.control(companyNameControlPath()); @@ -685,8 +765,6 @@ class IdForm

} } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsName { try { form.control(nameControlPath()); @@ -704,12 +782,7 @@ class IdForm

void get nameFocus => form.focus(nameControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void companyNameRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void companyNameRemove({bool updateParent = true, bool emitEvent = true}) { if (containsCompanyName) { final controlPath = path; if (controlPath == null) { @@ -732,12 +805,7 @@ class IdForm

} } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void nameRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void nameRemove({bool updateParent = true, bool emitEvent = true}) { if (containsName) { final controlPath = path; if (controlPath == null) { @@ -765,8 +833,11 @@ class IdForm

bool updateParent = true, bool emitEvent = true, }) { - companyNameControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + companyNameControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void nameValueUpdate( @@ -774,8 +845,11 @@ class IdForm

bool updateParent = true, bool emitEvent = true, }) { - nameControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + nameControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void companyNameValuePatch( @@ -783,8 +857,11 @@ class IdForm

bool updateParent = true, bool emitEvent = true, }) { - companyNameControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + companyNameControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void nameValuePatch( @@ -792,8 +869,11 @@ class IdForm

bool updateParent = true, bool emitEvent = true, }) { - nameControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + nameControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void companyNameValueReset( @@ -802,14 +882,13 @@ class IdForm

bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - companyNameControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => companyNameControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void nameValueReset( String? value, { @@ -817,14 +896,13 @@ class IdForm

bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - nameControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => nameControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get companyNameControl => form.control(companyNameControlPath()) as FormControl; @@ -888,27 +966,20 @@ class IdForm

} @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { - final currentFormInstance = currentForm; - - if (currentFormInstance is! FormGroup) { - return; - } - + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { - currentFormInstance.controls[key]?.markAsEnabled( + currentForm.controls[key]?.markAsEnabled( updateParent: updateParent, emitEvent: emitEvent, ); @@ -924,9 +995,7 @@ class IdForm

final currentForm = this.currentForm; return const DeepCollectionEquality().equals( - currentForm is FormControlCollection - ? currentForm.rawValue - : currentForm.value, + currentForm.rawValue, IdForm.formElements(other).rawValue, ); } @@ -947,8 +1016,16 @@ class IdForm

} @override - AbstractControl get currentForm { - return path == null ? form : form.control(path!); + bool get hasChanged { + return !const DeepCollectionEquality().equals( + currentForm.rawValue, + initial, + ); + } + + @override + FormGroup get currentForm { + return path == null ? form : form.control(path!) as FormGroup; } @override @@ -956,52 +1033,120 @@ class IdForm

Id? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(IdForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + IdForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void upsertValue( + Id? value, { + bool updateParent = true, + bool emitEvent = true, + }) { + final formElements = IdForm.formElements(value); + + currentForm.addAll(formElements.controls); + } @override void reset({ Id? value, bool updateParent = true, bool emitEvent = true, - }) => - form.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void updateInitial(Map? value, String? path) { + if (_formModel != null) { + _formModel?.updateInitial(currentForm.rawValue, path); + return; + } + + if (value == null) return; + + if (path == null || path.isEmpty) { + initial.addAll(value); + return; + } + + final keys = path.split('.'); + Object? current = initial; + for (var i = 0; i < keys.length - 1; i++) { + final key = keys[i]; + + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current = current[index]; + continue; + } + } + + if (current is Map) { + if (!current.containsKey(key)) { + current[key] = {}; + } + current = current[key]; + continue; + } + + return; + } + + final key = keys.last; + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current[index] = value; + } + } else if (current is Map) { + current[key] = value; + } + } String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); static FormGroup formElements

( - Id? id) => - FormGroup({ - companyNameControlName: FormControl( - value: id?.companyName, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - nameControlName: FormControl( - value: id?.name, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + Id? id, + ) => FormGroup( + { + companyNameControlName: FormControl( + value: id?.companyName, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + nameControlName: FormControl( + value: id?.name, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } class ReactiveProductDetailsFormArrayBuilder< - ReactiveProductDetailsFormArrayBuilderT, - P extends Product, - C extends Cart> extends StatelessWidget { + ReactiveProductDetailsFormArrayBuilderT, + P extends Product, + C extends Cart +> + extends StatelessWidget { const ReactiveProductDetailsFormArrayBuilder({ Key? key, this.control, @@ -1010,30 +1155,41 @@ class ReactiveProductDetailsFormArrayBuilder< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - ProductDetailsForm formModel)? control; + ProductDetailsForm formModel, + )? + control; - final Widget Function(BuildContext context, List itemList, - ProductDetailsForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + ProductDetailsForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveProductDetailsFormArrayBuilderT? item, - ProductDetailsForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveProductDetailsFormArrayBuilderT? item, + ProductDetailsForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -1047,21 +1203,13 @@ class ReactiveProductDetailsFormArrayBuilder< final itemBuilder = this.itemBuilder; return ReactiveFormArrayItemBuilder< - ReactiveProductDetailsFormArrayBuilderT>( + ReactiveProductDetailsFormArrayBuilderT + >( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1070,9 +1218,11 @@ class ReactiveProductDetailsFormArrayBuilder< } class ReactiveProductDetailsFormArrayBuilder2< - ReactiveProductDetailsFormArrayBuilderT, - P extends Product, - C extends Cart> extends StatelessWidget { + ReactiveProductDetailsFormArrayBuilderT, + P extends Product, + C extends Cart +> + extends StatelessWidget { const ReactiveProductDetailsFormArrayBuilder2({ Key? key, this.control, @@ -1081,36 +1231,47 @@ class ReactiveProductDetailsFormArrayBuilder2< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - ProductDetailsForm formModel)? control; + ProductDetailsForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - ProductDetailsForm formModel - }) params)? builder; + ({ + BuildContext context, + List itemList, + ProductDetailsForm formModel, + }) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveProductDetailsFormArrayBuilderT? item, - ProductDetailsForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveProductDetailsFormArrayBuilderT? item, + ProductDetailsForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -1124,27 +1285,22 @@ class ReactiveProductDetailsFormArrayBuilder2< final itemBuilder = this.itemBuilder; return ReactiveFormArrayItemBuilder< - ReactiveProductDetailsFormArrayBuilderT>( + ReactiveProductDetailsFormArrayBuilderT + >( formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1153,34 +1309,50 @@ class ReactiveProductDetailsFormArrayBuilder2< } class ReactiveProductDetailsFormFormGroupArrayBuilder< - ReactiveProductDetailsFormFormGroupArrayBuilderT, - P extends Product, - C extends Cart> extends StatelessWidget { + ReactiveProductDetailsFormFormGroupArrayBuilderT, + P extends Product, + C extends Cart +> + extends StatelessWidget { const ReactiveProductDetailsFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? extended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(ProductDetailsForm formModel)? + getExtended; - final ExtendedControl?>, - List> - Function(ProductDetailsForm formModel)? getExtended; - - final Widget Function(BuildContext context, List itemList, - ProductDetailsForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + ProductDetailsForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveProductDetailsFormFormGroupArrayBuilderT? item, - ProductDetailsForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveProductDetailsFormFormGroupArrayBuilderT? item, + ProductDetailsForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -1195,26 +1367,18 @@ class ReactiveProductDetailsFormFormGroupArrayBuilder< return StreamBuilder?>?>( stream: value.control.valueChanges, builder: (context, snapshot) { - final itemList = (value.value() ?? - []) - .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) - .values - .toList(); - - return builder?.call( - context, - itemList, - formModel, - ) ?? + final itemList = + (value.value() ?? + []) + .asMap() + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) + .values + .toList(); + + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); @@ -1223,16 +1387,17 @@ class ReactiveProductDetailsFormFormGroupArrayBuilder< class ReactiveIdFormConsumer

extends StatelessWidget { - const ReactiveIdFormConsumer({ - Key? key, - required this.builder, - this.child, - }) : super(key: key); + const ReactiveIdFormConsumer({Key? key, required this.builder, this.child}) + : super(key: key); final Widget? child; final Widget Function( - BuildContext context, IdForm formModel, Widget? child) builder; + BuildContext context, + IdForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -1252,11 +1417,7 @@ class IdFormInheritedStreamer

required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final IdForm form; } @@ -1289,8 +1450,10 @@ class ReactiveIdForm

?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - IdFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + IdFormInheritedStreamer + >(); return element == null ? null : (element.widget as IdFormInheritedStreamer).form; @@ -1338,7 +1501,11 @@ class IdFormBuilder

extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, IdForm formModel, Widget? child) builder; + BuildContext context, + IdForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, IdForm formModel)? initState; @@ -1354,7 +1521,11 @@ class _IdFormBuilderState

@override void initState() { - _formModel = IdForm(IdForm.formElements(widget.model), null); + _formModel = IdForm( + IdForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -1428,8 +1599,12 @@ class _IdFormBuilderState

} } -class ReactiveIdFormArrayBuilder extends StatelessWidget { +class ReactiveIdFormArrayBuilder< + ReactiveIdFormArrayBuilderT, + P extends Product, + C extends Cart +> + extends StatelessWidget { const ReactiveIdFormArrayBuilder({ Key? key, this.control, @@ -1438,30 +1613,39 @@ class ReactiveIdFormArrayBuilder? formControl; final FormArray? Function( - IdForm formModel)? control; + IdForm formModel, + )? + control; final Widget Function( - BuildContext context, List itemList, IdForm formModel)? - builder; + BuildContext context, + List itemList, + IdForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveIdFormArrayBuilderT? item, - IdForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveIdFormArrayBuilderT? item, + IdForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function(FormControl control)? - controlFilter; + controlFilter; @override Widget build(BuildContext context) { @@ -1477,18 +1661,9 @@ class ReactiveIdFormArrayBuilder( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1496,8 +1671,12 @@ class ReactiveIdFormArrayBuilder extends StatelessWidget { +class ReactiveIdFormArrayBuilder2< + ReactiveIdFormArrayBuilderT, + P extends Product, + C extends Cart +> + extends StatelessWidget { const ReactiveIdFormArrayBuilder2({ Key? key, this.control, @@ -1506,35 +1685,41 @@ class ReactiveIdFormArrayBuilder2? formControl; final FormArray? Function( - IdForm formModel)? control; + IdForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - IdForm formModel - }) params)? builder; + ({BuildContext context, List itemList, IdForm formModel}) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveIdFormArrayBuilderT? item, - IdForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveIdFormArrayBuilderT? item, + IdForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function(FormControl control)? - controlFilter; + controlFilter; @override Widget build(BuildContext context) { @@ -1551,23 +1736,17 @@ class ReactiveIdFormArrayBuilder2 builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1575,34 +1754,51 @@ class ReactiveIdFormArrayBuilder2 extends StatelessWidget { +class ReactiveIdFormFormGroupArrayBuilder< + ReactiveIdFormFormGroupArrayBuilderT, + P extends Product, + C extends Cart +> + extends StatelessWidget { const ReactiveIdFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? extended; - - final ExtendedControl?>, - List> - Function(IdForm formModel)? getExtended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(IdForm formModel)? + getExtended; final Widget Function( - BuildContext context, List itemList, IdForm formModel)? - builder; + BuildContext context, + List itemList, + IdForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveIdFormFormGroupArrayBuilderT? item, - IdForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveIdFormFormGroupArrayBuilderT? item, + IdForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -1620,23 +1816,14 @@ class ReactiveIdFormFormGroupArrayBuilder[]) .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) .values .toList(); - return builder?.call( - context, - itemList, - formModel, - ) ?? + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/generator_tests/test/doc/nested_output.test.dart b/packages/generator_tests/test/doc/nested_output.test.dart index b4209b39..27d4fc33 100644 --- a/packages/generator_tests/test/doc/nested_output.test.dart +++ b/packages/generator_tests/test/doc/nested_output.test.dart @@ -84,7 +84,11 @@ class ReactiveSubGroupOFormConsumer extends StatelessWidget { final Widget? child; final Widget Function( - BuildContext context, SubGroupOForm formModel, Widget? child) builder; + BuildContext context, + SubGroupOForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -103,11 +107,7 @@ class SubGroupOFormInheritedStreamer extends InheritedStreamer { required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final SubGroupOForm form; } @@ -129,18 +129,17 @@ class ReactiveSubGroupOForm extends StatelessWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - static SubGroupOForm? of( - BuildContext context, { - bool listen = true, - }) { + static SubGroupOForm? of(BuildContext context, {bool listen = true}) { if (listen) { return context .dependOnInheritedWidgetOfExactType() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - SubGroupOFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + SubGroupOFormInheritedStreamer + >(); return element == null ? null : (element.widget as SubGroupOFormInheritedStreamer).form; @@ -187,7 +186,11 @@ class SubGroupOFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, SubGroupOForm formModel, Widget? child) builder; + BuildContext context, + SubGroupOForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, SubGroupOForm formModel)? initState; @@ -202,7 +205,11 @@ class _SubGroupOFormBuilderState extends State { @override void initState() { - _formModel = SubGroupOForm(SubGroupOForm.formElements(widget.model), null); + _formModel = SubGroupOForm( + SubGroupOForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -279,10 +286,8 @@ class _SubGroupOFormBuilderState extends State { final _logSubGroupOForm = Logger.detached('SubGroupOForm'); class SubGroupOForm implements FormModel { - SubGroupOForm( - this.form, - this.path, - ); + SubGroupOForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String idControlName = "id"; @@ -290,16 +295,20 @@ class SubGroupOForm implements FormModel { final String? path; + // ignore: unused_field + final FormModel? _formModel; + final Map _disabled = {}; + @override + final Map initial; + String idControlPath() => pathBuilder(idControlName); String get _idValue => idControl.value as String; String get _idRawValue => idControl.value as String; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsId { try { form.control(idControlPath()); @@ -318,8 +327,11 @@ class SubGroupOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - idControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + idControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void idValuePatch( @@ -327,8 +339,11 @@ class SubGroupOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - idControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + idControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void idValueReset( @@ -337,14 +352,13 @@ class SubGroupOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - idControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => idControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get idControl => form.control(idControlPath()) as FormControl; @@ -360,10 +374,7 @@ class SubGroupOForm implements FormModel { emitEvent: emitEvent, ); } else { - idControl.markAsEnabled( - updateParent: updateParent, - emitEvent: emitEvent, - ); + idControl.markAsEnabled(updateParent: updateParent, emitEvent: emitEvent); } } @@ -388,27 +399,20 @@ class SubGroupOForm implements FormModel { } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { - final currentFormInstance = currentForm; - - if (currentFormInstance is! FormGroup) { - return; - } - + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { - currentFormInstance.controls[key]?.markAsEnabled( + currentForm.controls[key]?.markAsEnabled( updateParent: updateParent, emitEvent: emitEvent, ); @@ -424,9 +428,7 @@ class SubGroupOForm implements FormModel { final currentForm = this.currentForm; return const DeepCollectionEquality().equals( - currentForm is FormControlCollection - ? currentForm.rawValue - : currentForm.value, + currentForm.rawValue, SubGroupOForm.formElements(other).rawValue, ); } @@ -447,8 +449,16 @@ class SubGroupOForm implements FormModel { } @override - AbstractControl get currentForm { - return path == null ? form : form.control(path!); + bool get hasChanged { + return !const DeepCollectionEquality().equals( + currentForm.rawValue, + initial, + ); + } + + @override + FormGroup get currentForm { + return path == null ? form : form.control(path!) as FormGroup; } @override @@ -456,37 +466,102 @@ class SubGroupOForm implements FormModel { SubGroupO? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(SubGroupOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + SubGroupOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void upsertValue( + SubGroupO? value, { + bool updateParent = true, + bool emitEvent = true, + }) { + final formElements = SubGroupOForm.formElements(value); + + currentForm.addAll(formElements.controls); + } @override void reset({ SubGroupO? value, bool updateParent = true, bool emitEvent = true, - }) => - form.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void updateInitial(Map? value, String? path) { + if (_formModel != null) { + _formModel?.updateInitial(currentForm.rawValue, path); + return; + } + + if (value == null) return; + + if (path == null || path.isEmpty) { + initial.addAll(value); + return; + } + + final keys = path.split('.'); + Object? current = initial; + for (var i = 0; i < keys.length - 1; i++) { + final key = keys[i]; + + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current = current[index]; + continue; + } + } + + if (current is Map) { + if (!current.containsKey(key)) { + current[key] = {}; + } + current = current[key]; + continue; + } + + return; + } + + final key = keys.last; + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current[index] = value; + } + } else if (current is Map) { + current[key] = value; + } + } String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(SubGroupO? subGroupO) => FormGroup({ - idControlName: FormControl( - value: subGroupO?.id, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(SubGroupO? subGroupO) => FormGroup( + { + idControlName: FormControl( + value: subGroupO?.id, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } @Rf(output: true) @@ -503,10 +578,10 @@ abstract class SubGroupOOutput with _$SubGroupOOutput { @RfGroup() @freezed abstract class GroupOOutput with _$GroupOOutput { - const factory GroupOOutput( - {@RfControl() required String id, - @RfArray() required List subGroupList}) = - _GroupOOutput; + const factory GroupOOutput({ + @RfControl() required String id, + @RfArray() required List subGroupList, + }) = _GroupOOutput; factory GroupOOutput.fromJson(Map json) => _$GroupOOutputFromJson(json); } @@ -515,9 +590,9 @@ abstract class GroupOOutput with _$GroupOOutput { @RfGroup() @freezed abstract class NestedOOutput with _$NestedOOutput { - const factory NestedOOutput( - {@RfArray() required List groupList}) = - _NestedOOutput; + const factory NestedOOutput({ + @RfArray() required List groupList, + }) = _NestedOOutput; factory NestedOOutput.fromJson(Map json) => _$NestedOOutputFromJson(json); } @@ -532,30 +607,39 @@ class ReactiveSubGroupOFormArrayBuilder required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - SubGroupOForm formModel)? control; + SubGroupOForm formModel, + )? + control; final Widget Function( - BuildContext context, List itemList, SubGroupOForm formModel)? - builder; + BuildContext context, + List itemList, + SubGroupOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveSubGroupOFormArrayBuilderT? item, - SubGroupOForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveSubGroupOFormArrayBuilderT? item, + SubGroupOForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function(FormControl control)? - controlFilter; + controlFilter; @override Widget build(BuildContext context) { @@ -571,18 +655,9 @@ class ReactiveSubGroupOFormArrayBuilder return ReactiveFormArrayItemBuilder( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -600,35 +675,41 @@ class ReactiveSubGroupOFormArrayBuilder2 required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - SubGroupOForm formModel)? control; + SubGroupOForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - SubGroupOForm formModel - }) params)? builder; + ({BuildContext context, List itemList, SubGroupOForm formModel}) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveSubGroupOFormArrayBuilderT? item, - SubGroupOForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveSubGroupOFormArrayBuilderT? item, + SubGroupOForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function(FormControl control)? - controlFilter; + controlFilter; @override Widget build(BuildContext context) { @@ -645,23 +726,17 @@ class ReactiveSubGroupOFormArrayBuilder2 formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -670,33 +745,48 @@ class ReactiveSubGroupOFormArrayBuilder2 } class ReactiveSubGroupOFormFormGroupArrayBuilder< - ReactiveSubGroupOFormFormGroupArrayBuilderT> extends StatelessWidget { + ReactiveSubGroupOFormFormGroupArrayBuilderT +> + extends StatelessWidget { const ReactiveSubGroupOFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? extended; - - final ExtendedControl?>, - List> - Function(SubGroupOForm formModel)? getExtended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(SubGroupOForm formModel)? + getExtended; final Widget Function( - BuildContext context, List itemList, SubGroupOForm formModel)? - builder; + BuildContext context, + List itemList, + SubGroupOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveSubGroupOFormFormGroupArrayBuilderT? item, - SubGroupOForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveSubGroupOFormFormGroupArrayBuilderT? item, + SubGroupOForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -714,23 +804,14 @@ class ReactiveSubGroupOFormFormGroupArrayBuilder< final itemList = (value.value() ?? []) .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) .values .toList(); - return builder?.call( - context, - itemList, - formModel, - ) ?? + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); @@ -747,7 +828,11 @@ class ReactiveGroupOFormConsumer extends StatelessWidget { final Widget? child; final Widget Function( - BuildContext context, GroupOForm formModel, Widget? child) builder; + BuildContext context, + GroupOForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -766,11 +851,7 @@ class GroupOFormInheritedStreamer extends InheritedStreamer { required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final GroupOForm form; } @@ -792,10 +873,7 @@ class ReactiveGroupOForm extends StatelessWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - static GroupOForm? of( - BuildContext context, { - bool listen = true, - }) { + static GroupOForm? of(BuildContext context, {bool listen = true}) { if (listen) { return context .dependOnInheritedWidgetOfExactType() @@ -849,7 +927,11 @@ class GroupOFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, GroupOForm formModel, Widget? child) builder; + BuildContext context, + GroupOForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, GroupOForm formModel)? initState; @@ -864,7 +946,7 @@ class _GroupOFormBuilderState extends State { @override void initState() { - _formModel = GroupOForm(GroupOForm.formElements(widget.model), null); + _formModel = GroupOForm(GroupOForm.formElements(widget.model), null, null); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -941,10 +1023,7 @@ class _GroupOFormBuilderState extends State { final _logGroupOForm = Logger.detached('GroupOForm'); class GroupOForm implements FormModel { - GroupOForm( - this.form, - this.path, - ); + GroupOForm(this.form, this.path, this._formModel) : initial = form.rawValue; static const String idControlName = "id"; @@ -954,8 +1033,14 @@ class GroupOForm implements FormModel { final String? path; + // ignore: unused_field + final FormModel? _formModel; + final Map _disabled = {}; + @override + final Map initial; + String idControlPath() => pathBuilder(idControlName); String subGroupListControlPath() => pathBuilder(subGroupListControlName); @@ -970,8 +1055,6 @@ class GroupOForm implements FormModel { List get _subGroupListRawValue => subGroupListSubGroupOForm.map((e) => e.rawModel).toList(); - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsId { try { form.control(idControlPath()); @@ -981,8 +1064,6 @@ class GroupOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsSubGroupList { try { form.control(subGroupListControlPath()); @@ -1005,8 +1086,11 @@ class GroupOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - idControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + idControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void subGroupListValueUpdate( @@ -1037,15 +1121,19 @@ class GroupOForm implements FormModel { if (toUpdate.isNotEmpty) { subGroupListControl.updateValue( - toUpdate.map((e) => SubGroupOForm.formElements(e).rawValue).toList(), - updateParent: updateParent, - emitEvent: emitEvent); + toUpdate.map((e) => SubGroupOForm.formElements(e).rawValue).toList(), + updateParent: updateParent, + emitEvent: emitEvent, + ); } if (toAdd.isNotEmpty) { toAdd.forEach((e) { - subGroupListControl.add(SubGroupOForm.formElements(e), - updateParent: updateParent, emitEvent: emitEvent); + subGroupListControl.add( + SubGroupOForm.formElements(e), + updateParent: updateParent, + emitEvent: emitEvent, + ); }); } } @@ -1070,10 +1158,7 @@ class GroupOForm implements FormModel { ); } - void subGroupListClear({ - bool updateParent = true, - bool emitEvent = true, - }) { + void subGroupListClear({bool updateParent = true, bool emitEvent = true}) { subGroupListSubGroupOForm.clear(); subGroupListControl.clear(updateParent: updateParent, emitEvent: emitEvent); } @@ -1083,8 +1168,11 @@ class GroupOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - idControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + idControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void subGroupListValuePatch( @@ -1095,18 +1183,17 @@ class GroupOForm implements FormModel { final keys = subGroupListSubGroupOForm.asMap().keys; final toPatch = []; - (value).asMap().forEach( - (k, v) { - if (keys.contains(k)) { - toPatch.add(v); - } - }, - ); + (value).asMap().forEach((k, v) { + if (keys.contains(k)) { + toPatch.add(v); + } + }); subGroupListControl.patchValue( - toPatch.map((e) => SubGroupOForm.formElements(e).rawValue).toList(), - updateParent: updateParent, - emitEvent: emitEvent); + toPatch.map((e) => SubGroupOForm.formElements(e).rawValue).toList(), + updateParent: updateParent, + emitEvent: emitEvent, + ); } void idValueReset( @@ -1115,14 +1202,13 @@ class GroupOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - idControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => idControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void subGroupListValueReset( List value, { @@ -1130,12 +1216,11 @@ class GroupOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - subGroupListControl.reset( - value: - value.map((e) => SubGroupOForm.formElements(e).rawValue).toList(), - updateParent: updateParent, - emitEvent: emitEvent); + }) => subGroupListControl.reset( + value: value.map((e) => SubGroupOForm.formElements(e).rawValue).toList(), + updateParent: updateParent, + emitEvent: emitEvent, + ); FormControl get idControl => form.control(idControlPath()) as FormControl; @@ -1149,8 +1234,16 @@ class GroupOForm implements FormModel { return values .asMap() - .map((k, v) => - MapEntry(k, SubGroupOForm(form, pathBuilder("subGroupList.$k")))) + .map( + (k, v) => MapEntry( + k, + SubGroupOForm( + form, + pathBuilder("subGroupList.$k"), + _formModel ?? this, + ), + ), + ) .values .toList(); } @@ -1166,10 +1259,7 @@ class GroupOForm implements FormModel { emitEvent: emitEvent, ); } else { - idControl.markAsEnabled( - updateParent: updateParent, - emitEvent: emitEvent, - ); + idControl.markAsEnabled(updateParent: updateParent, emitEvent: emitEvent); } } @@ -1192,11 +1282,12 @@ class GroupOForm implements FormModel { } ExtendedControl?>, List> - get subGroupListExtendedControl => - ExtendedControl?>, List>( - form.control(subGroupListControlPath()) - as FormArray>, - () => subGroupListSubGroupOForm); + get subGroupListExtendedControl => + ExtendedControl?>, List>( + form.control(subGroupListControlPath()) + as FormArray>, + () => subGroupListSubGroupOForm, + ); void addSubGroupListItem(SubGroupO value) { subGroupListControl.add(SubGroupOForm.formElements(value)); @@ -1233,31 +1324,24 @@ class GroupOForm implements FormModel { } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { - final currentFormInstance = currentForm; - - if (currentFormInstance is! FormGroup) { - return; - } - + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); subGroupListSubGroupOForm.forEach((e) => e.toggleDisabled()); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { subGroupListSubGroupOForm.forEach((e) => e.toggleDisabled()); - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { - currentFormInstance.controls[key]?.markAsEnabled( + currentForm.controls[key]?.markAsEnabled( updateParent: updateParent, emitEvent: emitEvent, ); @@ -1273,9 +1357,7 @@ class GroupOForm implements FormModel { final currentForm = this.currentForm; return const DeepCollectionEquality().equals( - currentForm is FormControlCollection - ? currentForm.rawValue - : currentForm.value, + currentForm.rawValue, GroupOForm.formElements(other).rawValue, ); } @@ -1296,8 +1378,16 @@ class GroupOForm implements FormModel { } @override - AbstractControl get currentForm { - return path == null ? form : form.control(path!); + bool get hasChanged { + return !const DeepCollectionEquality().equals( + currentForm.rawValue, + initial, + ); + } + + @override + FormGroup get currentForm { + return path == null ? form : form.control(path!) as FormGroup; } @override @@ -1305,45 +1395,111 @@ class GroupOForm implements FormModel { GroupO? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(GroupOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + GroupOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void upsertValue( + GroupO? value, { + bool updateParent = true, + bool emitEvent = true, + }) { + final formElements = GroupOForm.formElements(value); + + currentForm.addAll(formElements.controls); + } @override void reset({ GroupO? value, bool updateParent = true, bool emitEvent = true, - }) => - form.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void updateInitial(Map? value, String? path) { + if (_formModel != null) { + _formModel?.updateInitial(currentForm.rawValue, path); + return; + } + + if (value == null) return; + + if (path == null || path.isEmpty) { + initial.addAll(value); + return; + } + + final keys = path.split('.'); + Object? current = initial; + for (var i = 0; i < keys.length - 1; i++) { + final key = keys[i]; + + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current = current[index]; + continue; + } + } + + if (current is Map) { + if (!current.containsKey(key)) { + current[key] = {}; + } + current = current[key]; + continue; + } + + return; + } + + final key = keys.last; + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current[index] = value; + } + } else if (current is Map) { + current[key] = value; + } + } String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(GroupO? groupO) => FormGroup({ - idControlName: FormControl( - value: groupO?.id, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - subGroupListControlName: FormArray( - (groupO?.subGroupList ?? []) - .map((e) => SubGroupOForm.formElements(e)) - .toList(), - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(GroupO? groupO) => FormGroup( + { + idControlName: FormControl( + value: groupO?.id, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + subGroupListControlName: FormArray( + (groupO?.subGroupList ?? []) + .map((e) => SubGroupOForm.formElements(e)) + .toList(), + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } class ReactiveGroupOFormArrayBuilder @@ -1356,30 +1512,39 @@ class ReactiveGroupOFormArrayBuilder required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - GroupOForm formModel)? control; + GroupOForm formModel, + )? + control; final Widget Function( - BuildContext context, List itemList, GroupOForm formModel)? - builder; + BuildContext context, + List itemList, + GroupOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveGroupOFormArrayBuilderT? item, - GroupOForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveGroupOFormArrayBuilderT? item, + GroupOForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function(FormControl control)? - controlFilter; + controlFilter; @override Widget build(BuildContext context) { @@ -1395,18 +1560,9 @@ class ReactiveGroupOFormArrayBuilder return ReactiveFormArrayItemBuilder( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1424,35 +1580,41 @@ class ReactiveGroupOFormArrayBuilder2 required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - GroupOForm formModel)? control; + GroupOForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - GroupOForm formModel - }) params)? builder; + ({BuildContext context, List itemList, GroupOForm formModel}) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveGroupOFormArrayBuilderT? item, - GroupOForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveGroupOFormArrayBuilderT? item, + GroupOForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function(FormControl control)? - controlFilter; + controlFilter; @override Widget build(BuildContext context) { @@ -1469,23 +1631,17 @@ class ReactiveGroupOFormArrayBuilder2 formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1494,33 +1650,48 @@ class ReactiveGroupOFormArrayBuilder2 } class ReactiveGroupOFormFormGroupArrayBuilder< - ReactiveGroupOFormFormGroupArrayBuilderT> extends StatelessWidget { + ReactiveGroupOFormFormGroupArrayBuilderT +> + extends StatelessWidget { const ReactiveGroupOFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? extended; - - final ExtendedControl?>, - List> - Function(GroupOForm formModel)? getExtended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(GroupOForm formModel)? + getExtended; final Widget Function( - BuildContext context, List itemList, GroupOForm formModel)? - builder; + BuildContext context, + List itemList, + GroupOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveGroupOFormFormGroupArrayBuilderT? item, - GroupOForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveGroupOFormFormGroupArrayBuilderT? item, + GroupOForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -1538,23 +1709,14 @@ class ReactiveGroupOFormFormGroupArrayBuilder< final itemList = (value.value() ?? []) .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) .values .toList(); - return builder?.call( - context, - itemList, - formModel, - ) ?? + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); @@ -1571,7 +1733,11 @@ class ReactiveNestedOFormConsumer extends StatelessWidget { final Widget? child; final Widget Function( - BuildContext context, NestedOForm formModel, Widget? child) builder; + BuildContext context, + NestedOForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -1590,11 +1756,7 @@ class NestedOFormInheritedStreamer extends InheritedStreamer { required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final NestedOForm form; } @@ -1616,18 +1778,17 @@ class ReactiveNestedOForm extends StatelessWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - static NestedOForm? of( - BuildContext context, { - bool listen = true, - }) { + static NestedOForm? of(BuildContext context, {bool listen = true}) { if (listen) { return context .dependOnInheritedWidgetOfExactType() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - NestedOFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + NestedOFormInheritedStreamer + >(); return element == null ? null : (element.widget as NestedOFormInheritedStreamer).form; @@ -1673,7 +1834,11 @@ class NestedOFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, NestedOForm formModel, Widget? child) builder; + BuildContext context, + NestedOForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, NestedOForm formModel)? initState; @@ -1688,7 +1853,11 @@ class _NestedOFormBuilderState extends State { @override void initState() { - _formModel = NestedOForm(NestedOForm.formElements(widget.model), null); + _formModel = NestedOForm( + NestedOForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -1765,10 +1934,7 @@ class _NestedOFormBuilderState extends State { final _logNestedOForm = Logger.detached('NestedOForm'); class NestedOForm implements FormModel { - NestedOForm( - this.form, - this.path, - ); + NestedOForm(this.form, this.path, this._formModel) : initial = form.rawValue; static const String groupListControlName = "groupList"; @@ -1776,8 +1942,14 @@ class NestedOForm implements FormModel { final String? path; + // ignore: unused_field + final FormModel? _formModel; + final Map _disabled = {}; + @override + final Map initial; + String groupListControlPath() => pathBuilder(groupListControlName); List get _groupListValue => @@ -1786,8 +1958,6 @@ class NestedOForm implements FormModel { List get _groupListRawValue => groupListGroupOForm.map((e) => e.rawModel).toList(); - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsGroupList { try { form.control(groupListControlPath()); @@ -1829,15 +1999,19 @@ class NestedOForm implements FormModel { if (toUpdate.isNotEmpty) { groupListControl.updateValue( - toUpdate.map((e) => GroupOForm.formElements(e).rawValue).toList(), - updateParent: updateParent, - emitEvent: emitEvent); + toUpdate.map((e) => GroupOForm.formElements(e).rawValue).toList(), + updateParent: updateParent, + emitEvent: emitEvent, + ); } if (toAdd.isNotEmpty) { toAdd.forEach((e) { - groupListControl.add(GroupOForm.formElements(e), - updateParent: updateParent, emitEvent: emitEvent); + groupListControl.add( + GroupOForm.formElements(e), + updateParent: updateParent, + emitEvent: emitEvent, + ); }); } } @@ -1862,10 +2036,7 @@ class NestedOForm implements FormModel { ); } - void groupListClear({ - bool updateParent = true, - bool emitEvent = true, - }) { + void groupListClear({bool updateParent = true, bool emitEvent = true}) { groupListGroupOForm.clear(); groupListControl.clear(updateParent: updateParent, emitEvent: emitEvent); } @@ -1878,18 +2049,17 @@ class NestedOForm implements FormModel { final keys = groupListGroupOForm.asMap().keys; final toPatch = []; - (value).asMap().forEach( - (k, v) { - if (keys.contains(k)) { - toPatch.add(v); - } - }, - ); + (value).asMap().forEach((k, v) { + if (keys.contains(k)) { + toPatch.add(v); + } + }); groupListControl.patchValue( - toPatch.map((e) => GroupOForm.formElements(e).rawValue).toList(), - updateParent: updateParent, - emitEvent: emitEvent); + toPatch.map((e) => GroupOForm.formElements(e).rawValue).toList(), + updateParent: updateParent, + emitEvent: emitEvent, + ); } void groupListValueReset( @@ -1898,11 +2068,11 @@ class NestedOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - groupListControl.reset( - value: value.map((e) => GroupOForm.formElements(e).rawValue).toList(), - updateParent: updateParent, - emitEvent: emitEvent); + }) => groupListControl.reset( + value: value.map((e) => GroupOForm.formElements(e).rawValue).toList(), + updateParent: updateParent, + emitEvent: emitEvent, + ); FormArray> get groupListControl => form.control(groupListControlPath()) as FormArray>; @@ -1912,8 +2082,12 @@ class NestedOForm implements FormModel { return values .asMap() - .map((k, v) => - MapEntry(k, GroupOForm(form, pathBuilder("groupList.$k")))) + .map( + (k, v) => MapEntry( + k, + GroupOForm(form, pathBuilder("groupList.$k"), _formModel ?? this), + ), + ) .values .toList(); } @@ -1937,11 +2111,11 @@ class NestedOForm implements FormModel { } ExtendedControl?>, List> - get groupListExtendedControl => - ExtendedControl?>, List>( - form.control(groupListControlPath()) - as FormArray>, - () => groupListGroupOForm); + get groupListExtendedControl => + ExtendedControl?>, List>( + form.control(groupListControlPath()) as FormArray>, + () => groupListGroupOForm, + ); void addGroupListItem(GroupO value) { groupListControl.add(GroupOForm.formElements(value)); @@ -1978,31 +2152,24 @@ class NestedOForm implements FormModel { } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { - final currentFormInstance = currentForm; - - if (currentFormInstance is! FormGroup) { - return; - } - + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); groupListGroupOForm.forEach((e) => e.toggleDisabled()); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { groupListGroupOForm.forEach((e) => e.toggleDisabled()); - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { - currentFormInstance.controls[key]?.markAsEnabled( + currentForm.controls[key]?.markAsEnabled( updateParent: updateParent, emitEvent: emitEvent, ); @@ -2018,9 +2185,7 @@ class NestedOForm implements FormModel { final currentForm = this.currentForm; return const DeepCollectionEquality().equals( - currentForm is FormControlCollection - ? currentForm.rawValue - : currentForm.value, + currentForm.rawValue, NestedOForm.formElements(other).rawValue, ); } @@ -2041,8 +2206,16 @@ class NestedOForm implements FormModel { } @override - AbstractControl get currentForm { - return path == null ? form : form.control(path!); + bool get hasChanged { + return !const DeepCollectionEquality().equals( + currentForm.rawValue, + initial, + ); + } + + @override + FormGroup get currentForm { + return path == null ? form : form.control(path!) as FormGroup; } @override @@ -2050,38 +2223,103 @@ class NestedOForm implements FormModel { NestedO? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(NestedOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + NestedOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void upsertValue( + NestedO? value, { + bool updateParent = true, + bool emitEvent = true, + }) { + final formElements = NestedOForm.formElements(value); + + currentForm.addAll(formElements.controls); + } @override void reset({ NestedO? value, bool updateParent = true, bool emitEvent = true, - }) => - form.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void updateInitial(Map? value, String? path) { + if (_formModel != null) { + _formModel?.updateInitial(currentForm.rawValue, path); + return; + } + + if (value == null) return; + + if (path == null || path.isEmpty) { + initial.addAll(value); + return; + } + + final keys = path.split('.'); + Object? current = initial; + for (var i = 0; i < keys.length - 1; i++) { + final key = keys[i]; + + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current = current[index]; + continue; + } + } + + if (current is Map) { + if (!current.containsKey(key)) { + current[key] = {}; + } + current = current[key]; + continue; + } + + return; + } + + final key = keys.last; + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current[index] = value; + } + } else if (current is Map) { + current[key] = value; + } + } String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(NestedO? nestedO) => FormGroup({ - groupListControlName: FormArray( - (nestedO?.groupList ?? []) - .map((e) => GroupOForm.formElements(e)) - .toList(), - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(NestedO? nestedO) => FormGroup( + { + groupListControlName: FormArray( + (nestedO?.groupList ?? []) + .map((e) => GroupOForm.formElements(e)) + .toList(), + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } class ReactiveNestedOFormArrayBuilder @@ -2094,30 +2332,39 @@ class ReactiveNestedOFormArrayBuilder required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - NestedOForm formModel)? control; + NestedOForm formModel, + )? + control; final Widget Function( - BuildContext context, List itemList, NestedOForm formModel)? - builder; + BuildContext context, + List itemList, + NestedOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveNestedOFormArrayBuilderT? item, - NestedOForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveNestedOFormArrayBuilderT? item, + NestedOForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function(FormControl control)? - controlFilter; + controlFilter; @override Widget build(BuildContext context) { @@ -2133,18 +2380,9 @@ class ReactiveNestedOFormArrayBuilder return ReactiveFormArrayItemBuilder( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -2162,35 +2400,41 @@ class ReactiveNestedOFormArrayBuilder2 required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - NestedOForm formModel)? control; + NestedOForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - NestedOForm formModel - }) params)? builder; + ({BuildContext context, List itemList, NestedOForm formModel}) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveNestedOFormArrayBuilderT? item, - NestedOForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveNestedOFormArrayBuilderT? item, + NestedOForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function(FormControl control)? - controlFilter; + controlFilter; @override Widget build(BuildContext context) { @@ -2207,23 +2451,17 @@ class ReactiveNestedOFormArrayBuilder2 formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -2232,33 +2470,48 @@ class ReactiveNestedOFormArrayBuilder2 } class ReactiveNestedOFormFormGroupArrayBuilder< - ReactiveNestedOFormFormGroupArrayBuilderT> extends StatelessWidget { + ReactiveNestedOFormFormGroupArrayBuilderT +> + extends StatelessWidget { const ReactiveNestedOFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? extended; - - final ExtendedControl?>, - List> - Function(NestedOForm formModel)? getExtended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(NestedOForm formModel)? + getExtended; final Widget Function( - BuildContext context, List itemList, NestedOForm formModel)? - builder; + BuildContext context, + List itemList, + NestedOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveNestedOFormFormGroupArrayBuilderT? item, - NestedOForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveNestedOFormFormGroupArrayBuilderT? item, + NestedOForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -2276,23 +2529,14 @@ class ReactiveNestedOFormFormGroupArrayBuilder< final itemList = (value.value() ?? []) .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) .values .toList(); - return builder?.call( - context, - itemList, - formModel, - ) ?? + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/generator_tests/test/doc/profile_output_test.dart b/packages/generator_tests/test/doc/profile_output_test.dart index b631b5b8..a7c85b44 100644 --- a/packages/generator_tests/test/doc/profile_output_test.dart +++ b/packages/generator_tests/test/doc/profile_output_test.dart @@ -305,7 +305,11 @@ class ReactiveProfileOFormConsumer extends StatelessWidget { final Widget? child; final Widget Function( - BuildContext context, ProfileOForm formModel, Widget? child) builder; + BuildContext context, + ProfileOForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -324,11 +328,7 @@ class ProfileOFormInheritedStreamer extends InheritedStreamer { required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final ProfileOForm form; } @@ -350,18 +350,17 @@ class ReactiveProfileOForm extends StatelessWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - static ProfileOForm? of( - BuildContext context, { - bool listen = true, - }) { + static ProfileOForm? of(BuildContext context, {bool listen = true}) { if (listen) { return context .dependOnInheritedWidgetOfExactType() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - ProfileOFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + ProfileOFormInheritedStreamer + >(); return element == null ? null : (element.widget as ProfileOFormInheritedStreamer).form; @@ -408,7 +407,11 @@ class ProfileOFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, ProfileOForm formModel, Widget? child) builder; + BuildContext context, + ProfileOForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, ProfileOForm formModel)? initState; @@ -423,7 +426,11 @@ class _ProfileOFormBuilderState extends State { @override void initState() { - _formModel = ProfileOForm(ProfileOForm.formElements(widget.model), null); + _formModel = ProfileOForm( + ProfileOForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -500,10 +507,7 @@ class _ProfileOFormBuilderState extends State { final _logProfileOForm = Logger.detached('ProfileOForm'); class ProfileOForm implements FormModel { - ProfileOForm( - this.form, - this.path, - ); + ProfileOForm(this.form, this.path, this._formModel) : initial = form.rawValue; static const String idControlName = "id"; @@ -529,8 +533,14 @@ class ProfileOForm implements FormModel { final String? path; + // ignore: unused_field + final FormModel? _formModel; + final Map _disabled = {}; + @override + final Map initial; + String idControlPath() => pathBuilder(idControlName); String anotherIdControlPath() => pathBuilder(anotherIdControlName); @@ -600,8 +610,6 @@ class ProfileOForm implements FormModel { TimerSettingO get _timerRawValue => timerForm.rawModel; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsId { try { form.control(idControlPath()); @@ -611,8 +619,6 @@ class ProfileOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsAnotherId { try { form.control(anotherIdControlPath()); @@ -622,8 +628,6 @@ class ProfileOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsName { try { form.control(nameControlPath()); @@ -633,8 +637,6 @@ class ProfileOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsChartingOrder { try { form.control(chartingOrderControlPath()); @@ -644,8 +646,6 @@ class ProfileOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsNumberingStandard { try { form.control(numberingStandardControlPath()); @@ -655,8 +655,6 @@ class ProfileOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsMeasurementType { try { form.control(measurementTypeControlPath()); @@ -666,8 +664,6 @@ class ProfileOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsAudioGuidance { try { form.control(audioGuidanceControlPath()); @@ -677,8 +673,6 @@ class ProfileOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsIncidenceFilter { try { form.control(incidenceFilterControlPath()); @@ -688,8 +682,6 @@ class ProfileOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsThreshold { try { form.control(thresholdControlPath()); @@ -699,8 +691,6 @@ class ProfileOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsTimer { try { form.control(timerControlPath()); @@ -758,8 +748,11 @@ class ProfileOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - idControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + idControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void anotherIdValueUpdate( @@ -767,8 +760,11 @@ class ProfileOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - anotherIdControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + anotherIdControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void nameValueUpdate( @@ -776,8 +772,11 @@ class ProfileOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - nameControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + nameControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void chartingOrderValueUpdate( @@ -785,8 +784,11 @@ class ProfileOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - chartingOrderControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + chartingOrderControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void numberingStandardValueUpdate( @@ -794,8 +796,11 @@ class ProfileOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - numberingStandardControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + numberingStandardControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void measurementTypeValueUpdate( @@ -803,8 +808,11 @@ class ProfileOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - measurementTypeControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + measurementTypeControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void audioGuidanceValueUpdate( @@ -812,8 +820,11 @@ class ProfileOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - audioGuidanceControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + audioGuidanceControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void incidenceFilterValueUpdate( @@ -822,9 +833,10 @@ class ProfileOForm implements FormModel { bool emitEvent = true, }) { incidenceFilterControl.updateValue( - IncidenceFilterOForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + IncidenceFilterOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void thresholdValueUpdate( @@ -833,9 +845,10 @@ class ProfileOForm implements FormModel { bool emitEvent = true, }) { thresholdControl.updateValue( - ThresholdSettingOForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + ThresholdSettingOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void timerValueUpdate( @@ -843,8 +856,11 @@ class ProfileOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - timerControl.updateValue(TimerSettingOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + timerControl.updateValue( + TimerSettingOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void idValuePatch( @@ -852,8 +868,11 @@ class ProfileOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - idControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + idControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void anotherIdValuePatch( @@ -861,8 +880,11 @@ class ProfileOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - anotherIdControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + anotherIdControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void nameValuePatch( @@ -870,8 +892,11 @@ class ProfileOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - nameControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + nameControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void chartingOrderValuePatch( @@ -879,8 +904,11 @@ class ProfileOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - chartingOrderControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + chartingOrderControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void numberingStandardValuePatch( @@ -888,8 +916,11 @@ class ProfileOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - numberingStandardControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + numberingStandardControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void measurementTypeValuePatch( @@ -897,8 +928,11 @@ class ProfileOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - measurementTypeControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + measurementTypeControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void audioGuidanceValuePatch( @@ -906,8 +940,11 @@ class ProfileOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - audioGuidanceControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + audioGuidanceControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void incidenceFilterValuePatch( @@ -916,9 +953,10 @@ class ProfileOForm implements FormModel { bool emitEvent = true, }) { incidenceFilterControl.updateValue( - IncidenceFilterOForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + IncidenceFilterOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void thresholdValuePatch( @@ -927,9 +965,10 @@ class ProfileOForm implements FormModel { bool emitEvent = true, }) { thresholdControl.updateValue( - ThresholdSettingOForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + ThresholdSettingOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void timerValuePatch( @@ -937,8 +976,11 @@ class ProfileOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - timerControl.updateValue(TimerSettingOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + timerControl.updateValue( + TimerSettingOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void idValueReset( @@ -947,14 +989,13 @@ class ProfileOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - idControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => idControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void anotherIdValueReset( String value, { @@ -962,14 +1003,13 @@ class ProfileOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - anotherIdControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => anotherIdControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void nameValueReset( String value, { @@ -977,14 +1017,13 @@ class ProfileOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - nameControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => nameControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void chartingOrderValueReset( ChartingOrderValue value, { @@ -992,14 +1031,13 @@ class ProfileOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - chartingOrderControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => chartingOrderControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void numberingStandardValueReset( NumberingStandard value, { @@ -1007,14 +1045,13 @@ class ProfileOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - numberingStandardControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => numberingStandardControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void measurementTypeValueReset( MeasurementType value, { @@ -1022,14 +1059,13 @@ class ProfileOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - measurementTypeControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => measurementTypeControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void audioGuidanceValueReset( bool value, { @@ -1037,14 +1073,13 @@ class ProfileOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - audioGuidanceControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => audioGuidanceControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void incidenceFilterValueReset( IncidenceFilterO value, { @@ -1052,11 +1087,11 @@ class ProfileOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - incidenceFilterControl.reset( - value: IncidenceFilterOForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => incidenceFilterControl.reset( + value: IncidenceFilterOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); void thresholdValueReset( ThresholdSettingO value, { @@ -1064,11 +1099,11 @@ class ProfileOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - thresholdControl.reset( - value: ThresholdSettingOForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => thresholdControl.reset( + value: ThresholdSettingOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); void timerValueReset( TimerSettingO value, { @@ -1076,11 +1111,11 @@ class ProfileOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - timerControl.reset( - value: TimerSettingOForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => timerControl.reset( + value: TimerSettingOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); FormControl get idControl => form.control(idControlPath()) as FormControl; @@ -1114,14 +1149,17 @@ class ProfileOForm implements FormModel { FormGroup get timerControl => form.control(timerControlPath()) as FormGroup; - IncidenceFilterOForm get incidenceFilterForm => - IncidenceFilterOForm(form, pathBuilder('incidenceFilter')); + IncidenceFilterOForm get incidenceFilterForm => IncidenceFilterOForm( + form, + pathBuilder('incidenceFilter'), + _formModel ?? this, + ); ThresholdSettingOForm get thresholdForm => - ThresholdSettingOForm(form, pathBuilder('threshold')); + ThresholdSettingOForm(form, pathBuilder('threshold'), _formModel ?? this); TimerSettingOForm get timerForm => - TimerSettingOForm(form, pathBuilder('timer')); + TimerSettingOForm(form, pathBuilder('timer'), _formModel ?? this); void idSetDisabled( bool disabled, { @@ -1134,10 +1172,7 @@ class ProfileOForm implements FormModel { emitEvent: emitEvent, ); } else { - idControl.markAsEnabled( - updateParent: updateParent, - emitEvent: emitEvent, - ); + idControl.markAsEnabled(updateParent: updateParent, emitEvent: emitEvent); } } @@ -1315,45 +1350,40 @@ class ProfileOForm implements FormModel { StackTrace.current, ); } - return ProfileOOutput(_idValue, - anotherId: _anotherIdValue, - name: _nameValue, - chartingOrder: _chartingOrderValue, - numberingStandard: _numberingStandardValue, - incidenceFilter: _incidenceFilterValue, - measurementType: _measurementTypeValue, - threshold: _thresholdValue, - timer: _timerValue, - audioGuidance: _audioGuidanceValue); + return ProfileOOutput( + _idValue, + anotherId: _anotherIdValue, + name: _nameValue, + chartingOrder: _chartingOrderValue, + numberingStandard: _numberingStandardValue, + incidenceFilter: _incidenceFilterValue, + measurementType: _measurementTypeValue, + threshold: _thresholdValue, + timer: _timerValue, + audioGuidance: _audioGuidanceValue, + ); } @override ProfileO get rawModel { - return ProfileO(_idRawValue, - anotherId: _anotherIdRawValue, - name: _nameRawValue, - chartingOrder: _chartingOrderRawValue, - numberingStandard: _numberingStandardRawValue, - incidenceFilter: _incidenceFilterRawValue, - measurementType: _measurementTypeRawValue, - threshold: _thresholdRawValue, - timer: _timerRawValue, - audioGuidance: _audioGuidanceRawValue); + return ProfileO( + _idRawValue, + anotherId: _anotherIdRawValue, + name: _nameRawValue, + chartingOrder: _chartingOrderRawValue, + numberingStandard: _numberingStandardRawValue, + incidenceFilter: _incidenceFilterRawValue, + measurementType: _measurementTypeRawValue, + threshold: _thresholdRawValue, + timer: _timerRawValue, + audioGuidance: _audioGuidanceRawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { - final currentFormInstance = currentForm; - - if (currentFormInstance is! FormGroup) { - return; - } - + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); @@ -1361,14 +1391,16 @@ class ProfileOForm implements FormModel { thresholdForm.toggleDisabled(); timerForm.toggleDisabled(); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { incidenceFilterForm.toggleDisabled(); thresholdForm.toggleDisabled(); timerForm.toggleDisabled(); - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { - currentFormInstance.controls[key]?.markAsEnabled( + currentForm.controls[key]?.markAsEnabled( updateParent: updateParent, emitEvent: emitEvent, ); @@ -1384,9 +1416,7 @@ class ProfileOForm implements FormModel { final currentForm = this.currentForm; return const DeepCollectionEquality().equals( - currentForm is FormControlCollection - ? currentForm.rawValue - : currentForm.value, + currentForm.rawValue, ProfileOForm.formElements(other).rawValue, ); } @@ -1407,8 +1437,16 @@ class ProfileOForm implements FormModel { } @override - AbstractControl get currentForm { - return path == null ? form : form.control(path!); + bool get hasChanged { + return !const DeepCollectionEquality().equals( + currentForm.rawValue, + initial, + ); + } + + @override + FormGroup get currentForm { + return path == null ? form : form.control(path!) as FormGroup; } @override @@ -1416,94 +1454,165 @@ class ProfileOForm implements FormModel { ProfileO? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(ProfileOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + ProfileOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void upsertValue( + ProfileO? value, { + bool updateParent = true, + bool emitEvent = true, + }) { + final formElements = ProfileOForm.formElements(value); + + currentForm.addAll(formElements.controls); + } @override void reset({ ProfileO? value, bool updateParent = true, bool emitEvent = true, - }) => - form.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void updateInitial(Map? value, String? path) { + if (_formModel != null) { + _formModel?.updateInitial(currentForm.rawValue, path); + return; + } + + if (value == null) return; + + if (path == null || path.isEmpty) { + initial.addAll(value); + return; + } + + final keys = path.split('.'); + Object? current = initial; + for (var i = 0; i < keys.length - 1; i++) { + final key = keys[i]; + + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current = current[index]; + continue; + } + } + + if (current is Map) { + if (!current.containsKey(key)) { + current[key] = {}; + } + current = current[key]; + continue; + } + + return; + } + + final key = keys.last; + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current[index] = value; + } + } else if (current is Map) { + current[key] = value; + } + } String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(ProfileO? profileO) => FormGroup({ - idControlName: FormControl( - value: profileO?.id, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - anotherIdControlName: FormControl( - value: profileO?.anotherId, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - nameControlName: FormControl( - value: profileO?.name, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - chartingOrderControlName: FormControl( - value: profileO?.chartingOrder, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - numberingStandardControlName: FormControl( - value: profileO?.numberingStandard, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - measurementTypeControlName: FormControl( - value: profileO?.measurementType, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - audioGuidanceControlName: FormControl( - value: profileO?.audioGuidance, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - incidenceFilterControlName: - IncidenceFilterOForm.formElements(profileO?.incidenceFilter), - thresholdControlName: - ThresholdSettingOForm.formElements(profileO?.threshold), - timerControlName: TimerSettingOForm.formElements(profileO?.timer) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(ProfileO? profileO) => FormGroup( + { + idControlName: FormControl( + value: profileO?.id, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + anotherIdControlName: FormControl( + value: profileO?.anotherId, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + nameControlName: FormControl( + value: profileO?.name, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + chartingOrderControlName: FormControl( + value: profileO?.chartingOrder, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + numberingStandardControlName: FormControl( + value: profileO?.numberingStandard, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + measurementTypeControlName: FormControl( + value: profileO?.measurementType, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + audioGuidanceControlName: FormControl( + value: profileO?.audioGuidance, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + incidenceFilterControlName: IncidenceFilterOForm.formElements( + profileO?.incidenceFilter, + ), + thresholdControlName: ThresholdSettingOForm.formElements( + profileO?.threshold, + ), + timerControlName: TimerSettingOForm.formElements(profileO?.timer), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } final _logIncidenceFilterOForm = Logger.detached('IncidenceFilterOForm'); class IncidenceFilterOForm implements FormModel { - IncidenceFilterOForm( - this.form, - this.path, - ); + IncidenceFilterOForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String isMobilityEnabledControlName = "isMobilityEnabled"; @@ -1521,8 +1630,14 @@ class IncidenceFilterOForm final String? path; + // ignore: unused_field + final FormModel? _formModel; + final Map _disabled = {}; + @override + final Map initial; + String isMobilityEnabledControlPath() => pathBuilder(isMobilityEnabledControlName); @@ -1568,8 +1683,6 @@ class IncidenceFilterOForm bool get _isPlaqueEnabledRawValue => isPlaqueEnabledControl.value ?? true; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsIsMobilityEnabled { try { form.control(isMobilityEnabledControlPath()); @@ -1579,8 +1692,6 @@ class IncidenceFilterOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsIsFurcationEnabled { try { form.control(isFurcationEnabledControlPath()); @@ -1590,8 +1701,6 @@ class IncidenceFilterOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsIsBleedingEnabled { try { form.control(isBleedingEnabledControlPath()); @@ -1601,8 +1710,6 @@ class IncidenceFilterOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsIsSuppurationEnabled { try { form.control(isSuppurationEnabledControlPath()); @@ -1612,8 +1719,6 @@ class IncidenceFilterOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsIsCalculusEnabled { try { form.control(isCalculusEnabledControlPath()); @@ -1623,8 +1728,6 @@ class IncidenceFilterOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsIsPlaqueEnabled { try { form.control(isPlaqueEnabledControlPath()); @@ -1671,8 +1774,11 @@ class IncidenceFilterOForm bool updateParent = true, bool emitEvent = true, }) { - isMobilityEnabledControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + isMobilityEnabledControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void isFurcationEnabledValueUpdate( @@ -1680,8 +1786,11 @@ class IncidenceFilterOForm bool updateParent = true, bool emitEvent = true, }) { - isFurcationEnabledControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + isFurcationEnabledControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void isBleedingEnabledValueUpdate( @@ -1689,8 +1798,11 @@ class IncidenceFilterOForm bool updateParent = true, bool emitEvent = true, }) { - isBleedingEnabledControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + isBleedingEnabledControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void isSuppurationEnabledValueUpdate( @@ -1698,8 +1810,11 @@ class IncidenceFilterOForm bool updateParent = true, bool emitEvent = true, }) { - isSuppurationEnabledControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + isSuppurationEnabledControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void isCalculusEnabledValueUpdate( @@ -1707,8 +1822,11 @@ class IncidenceFilterOForm bool updateParent = true, bool emitEvent = true, }) { - isCalculusEnabledControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + isCalculusEnabledControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void isPlaqueEnabledValueUpdate( @@ -1716,8 +1834,11 @@ class IncidenceFilterOForm bool updateParent = true, bool emitEvent = true, }) { - isPlaqueEnabledControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + isPlaqueEnabledControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void isMobilityEnabledValuePatch( @@ -1725,8 +1846,11 @@ class IncidenceFilterOForm bool updateParent = true, bool emitEvent = true, }) { - isMobilityEnabledControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + isMobilityEnabledControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void isFurcationEnabledValuePatch( @@ -1734,8 +1858,11 @@ class IncidenceFilterOForm bool updateParent = true, bool emitEvent = true, }) { - isFurcationEnabledControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + isFurcationEnabledControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void isBleedingEnabledValuePatch( @@ -1743,8 +1870,11 @@ class IncidenceFilterOForm bool updateParent = true, bool emitEvent = true, }) { - isBleedingEnabledControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + isBleedingEnabledControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void isSuppurationEnabledValuePatch( @@ -1752,8 +1882,11 @@ class IncidenceFilterOForm bool updateParent = true, bool emitEvent = true, }) { - isSuppurationEnabledControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + isSuppurationEnabledControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void isCalculusEnabledValuePatch( @@ -1761,8 +1894,11 @@ class IncidenceFilterOForm bool updateParent = true, bool emitEvent = true, }) { - isCalculusEnabledControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + isCalculusEnabledControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void isPlaqueEnabledValuePatch( @@ -1770,8 +1906,11 @@ class IncidenceFilterOForm bool updateParent = true, bool emitEvent = true, }) { - isPlaqueEnabledControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + isPlaqueEnabledControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void isMobilityEnabledValueReset( @@ -1780,14 +1919,13 @@ class IncidenceFilterOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - isMobilityEnabledControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => isMobilityEnabledControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void isFurcationEnabledValueReset( bool value, { @@ -1795,14 +1933,13 @@ class IncidenceFilterOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - isFurcationEnabledControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => isFurcationEnabledControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void isBleedingEnabledValueReset( bool value, { @@ -1810,14 +1947,13 @@ class IncidenceFilterOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - isBleedingEnabledControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => isBleedingEnabledControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void isSuppurationEnabledValueReset( bool value, { @@ -1825,14 +1961,13 @@ class IncidenceFilterOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - isSuppurationEnabledControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => isSuppurationEnabledControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void isCalculusEnabledValueReset( bool value, { @@ -1840,14 +1975,13 @@ class IncidenceFilterOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - isCalculusEnabledControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => isCalculusEnabledControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void isPlaqueEnabledValueReset( bool value, { @@ -1855,14 +1989,13 @@ class IncidenceFilterOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - isPlaqueEnabledControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => isPlaqueEnabledControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get isMobilityEnabledControl => form.control(isMobilityEnabledControlPath()) as FormControl; @@ -2003,47 +2136,42 @@ class IncidenceFilterOForm ); } return IncidenceFilterOOutput( - isMobilityEnabled: _isMobilityEnabledValue, - isFurcationEnabled: _isFurcationEnabledValue, - isBleedingEnabled: _isBleedingEnabledValue, - isSuppurationEnabled: _isSuppurationEnabledValue, - isCalculusEnabled: _isCalculusEnabledValue, - isPlaqueEnabled: _isPlaqueEnabledValue); + isMobilityEnabled: _isMobilityEnabledValue, + isFurcationEnabled: _isFurcationEnabledValue, + isBleedingEnabled: _isBleedingEnabledValue, + isSuppurationEnabled: _isSuppurationEnabledValue, + isCalculusEnabled: _isCalculusEnabledValue, + isPlaqueEnabled: _isPlaqueEnabledValue, + ); } @override IncidenceFilterO get rawModel { return IncidenceFilterO( - isMobilityEnabled: _isMobilityEnabledRawValue, - isFurcationEnabled: _isFurcationEnabledRawValue, - isBleedingEnabled: _isBleedingEnabledRawValue, - isSuppurationEnabled: _isSuppurationEnabledRawValue, - isCalculusEnabled: _isCalculusEnabledRawValue, - isPlaqueEnabled: _isPlaqueEnabledRawValue); + isMobilityEnabled: _isMobilityEnabledRawValue, + isFurcationEnabled: _isFurcationEnabledRawValue, + isBleedingEnabled: _isBleedingEnabledRawValue, + isSuppurationEnabled: _isSuppurationEnabledRawValue, + isCalculusEnabled: _isCalculusEnabledRawValue, + isPlaqueEnabled: _isPlaqueEnabledRawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { - final currentFormInstance = currentForm; - - if (currentFormInstance is! FormGroup) { - return; - } - + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { - currentFormInstance.controls[key]?.markAsEnabled( + currentForm.controls[key]?.markAsEnabled( updateParent: updateParent, emitEvent: emitEvent, ); @@ -2059,9 +2187,7 @@ class IncidenceFilterOForm final currentForm = this.currentForm; return const DeepCollectionEquality().equals( - currentForm is FormControlCollection - ? currentForm.rawValue - : currentForm.value, + currentForm.rawValue, IncidenceFilterOForm.formElements(other).rawValue, ); } @@ -2082,8 +2208,16 @@ class IncidenceFilterOForm } @override - AbstractControl get currentForm { - return path == null ? form : form.control(path!); + bool get hasChanged { + return !const DeepCollectionEquality().equals( + currentForm.rawValue, + initial, + ); + } + + @override + FormGroup get currentForm { + return path == null ? form : form.control(path!) as FormGroup; } @override @@ -2091,83 +2225,151 @@ class IncidenceFilterOForm IncidenceFilterO? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(IncidenceFilterOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + IncidenceFilterOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void upsertValue( + IncidenceFilterO? value, { + bool updateParent = true, + bool emitEvent = true, + }) { + final formElements = IncidenceFilterOForm.formElements(value); + + currentForm.addAll(formElements.controls); + } @override void reset({ IncidenceFilterO? value, bool updateParent = true, bool emitEvent = true, - }) => - form.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void updateInitial(Map? value, String? path) { + if (_formModel != null) { + _formModel?.updateInitial(currentForm.rawValue, path); + return; + } + + if (value == null) return; + + if (path == null || path.isEmpty) { + initial.addAll(value); + return; + } + + final keys = path.split('.'); + Object? current = initial; + for (var i = 0; i < keys.length - 1; i++) { + final key = keys[i]; + + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current = current[index]; + continue; + } + } + + if (current is Map) { + if (!current.containsKey(key)) { + current[key] = {}; + } + current = current[key]; + continue; + } + + return; + } + + final key = keys.last; + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current[index] = value; + } + } else if (current is Map) { + current[key] = value; + } + } String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); static FormGroup formElements(IncidenceFilterO? incidenceFilterO) => - FormGroup({ - isMobilityEnabledControlName: FormControl( + FormGroup( + { + isMobilityEnabledControlName: FormControl( value: incidenceFilterO?.isMobilityEnabled, validators: [], asyncValidators: [], asyncValidatorsDebounceTime: 250, disabled: false, - touched: false), - isFurcationEnabledControlName: FormControl( + touched: false, + ), + isFurcationEnabledControlName: FormControl( value: incidenceFilterO?.isFurcationEnabled, validators: [], asyncValidators: [], asyncValidatorsDebounceTime: 250, disabled: false, - touched: false), - isBleedingEnabledControlName: FormControl( + touched: false, + ), + isBleedingEnabledControlName: FormControl( value: incidenceFilterO?.isBleedingEnabled, validators: [], asyncValidators: [], asyncValidatorsDebounceTime: 250, disabled: false, - touched: false), - isSuppurationEnabledControlName: FormControl( + touched: false, + ), + isSuppurationEnabledControlName: FormControl( value: incidenceFilterO?.isSuppurationEnabled, validators: [], asyncValidators: [], asyncValidatorsDebounceTime: 250, disabled: false, - touched: false), - isCalculusEnabledControlName: FormControl( + touched: false, + ), + isCalculusEnabledControlName: FormControl( value: incidenceFilterO?.isCalculusEnabled, validators: [], asyncValidators: [], asyncValidatorsDebounceTime: 250, disabled: false, - touched: false), - isPlaqueEnabledControlName: FormControl( + touched: false, + ), + isPlaqueEnabledControlName: FormControl( value: incidenceFilterO?.isPlaqueEnabled, validators: [], asyncValidators: [], asyncValidatorsDebounceTime: 250, disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } final _logThresholdSettingOForm = Logger.detached('ThresholdSettingOForm'); class ThresholdSettingOForm implements FormModel { - ThresholdSettingOForm( - this.form, - this.path, - ); + ThresholdSettingOForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String isEnabledControlName = "isEnabled"; @@ -2177,8 +2379,14 @@ class ThresholdSettingOForm final String? path; + // ignore: unused_field + final FormModel? _formModel; + final Map _disabled = {}; + @override + final Map initial; + String isEnabledControlPath() => pathBuilder(isEnabledControlName); String valueControlPath() => pathBuilder(valueControlName); @@ -2191,8 +2399,6 @@ class ThresholdSettingOForm int get _valueRawValue => valueControl.value ?? 2; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsIsEnabled { try { form.control(isEnabledControlPath()); @@ -2202,8 +2408,6 @@ class ThresholdSettingOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsValue { try { form.control(valueControlPath()); @@ -2226,8 +2430,11 @@ class ThresholdSettingOForm bool updateParent = true, bool emitEvent = true, }) { - isEnabledControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + isEnabledControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void valueValueUpdate( @@ -2235,8 +2442,11 @@ class ThresholdSettingOForm bool updateParent = true, bool emitEvent = true, }) { - valueControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + valueControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void isEnabledValuePatch( @@ -2244,8 +2454,11 @@ class ThresholdSettingOForm bool updateParent = true, bool emitEvent = true, }) { - isEnabledControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + isEnabledControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void valueValuePatch( @@ -2253,8 +2466,11 @@ class ThresholdSettingOForm bool updateParent = true, bool emitEvent = true, }) { - valueControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + valueControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void isEnabledValueReset( @@ -2263,14 +2479,13 @@ class ThresholdSettingOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - isEnabledControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => isEnabledControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void valueValueReset( int value, { @@ -2278,14 +2493,13 @@ class ThresholdSettingOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - valueControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => valueControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get isEnabledControl => form.control(isEnabledControlPath()) as FormControl; @@ -2342,37 +2556,34 @@ class ThresholdSettingOForm ); } return ThresholdSettingOOutput( - isEnabled: _isEnabledValue, value: _valueValue); + isEnabled: _isEnabledValue, + value: _valueValue, + ); } @override ThresholdSettingO get rawModel { return ThresholdSettingO( - isEnabled: _isEnabledRawValue, value: _valueRawValue); + isEnabled: _isEnabledRawValue, + value: _valueRawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { - final currentFormInstance = currentForm; - - if (currentFormInstance is! FormGroup) { - return; - } - + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { - currentFormInstance.controls[key]?.markAsEnabled( + currentForm.controls[key]?.markAsEnabled( updateParent: updateParent, emitEvent: emitEvent, ); @@ -2388,9 +2599,7 @@ class ThresholdSettingOForm final currentForm = this.currentForm; return const DeepCollectionEquality().equals( - currentForm is FormControlCollection - ? currentForm.rawValue - : currentForm.value, + currentForm.rawValue, ThresholdSettingOForm.formElements(other).rawValue, ); } @@ -2411,8 +2620,16 @@ class ThresholdSettingOForm } @override - AbstractControl get currentForm { - return path == null ? form : form.control(path!); + bool get hasChanged { + return !const DeepCollectionEquality().equals( + currentForm.rawValue, + initial, + ); + } + + @override + FormGroup get currentForm { + return path == null ? form : form.control(path!) as FormGroup; } @override @@ -2420,57 +2637,119 @@ class ThresholdSettingOForm ThresholdSettingO? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue( - ThresholdSettingOForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.updateValue( + ThresholdSettingOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void upsertValue( + ThresholdSettingO? value, { + bool updateParent = true, + bool emitEvent = true, + }) { + final formElements = ThresholdSettingOForm.formElements(value); + + currentForm.addAll(formElements.controls); + } @override void reset({ ThresholdSettingO? value, bool updateParent = true, bool emitEvent = true, - }) => - form.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void updateInitial(Map? value, String? path) { + if (_formModel != null) { + _formModel?.updateInitial(currentForm.rawValue, path); + return; + } + + if (value == null) return; + + if (path == null || path.isEmpty) { + initial.addAll(value); + return; + } + + final keys = path.split('.'); + Object? current = initial; + for (var i = 0; i < keys.length - 1; i++) { + final key = keys[i]; + + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current = current[index]; + continue; + } + } + + if (current is Map) { + if (!current.containsKey(key)) { + current[key] = {}; + } + current = current[key]; + continue; + } + + return; + } + + final key = keys.last; + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current[index] = value; + } + } else if (current is Map) { + current[key] = value; + } + } String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); static FormGroup formElements(ThresholdSettingO? thresholdSettingO) => - FormGroup({ - isEnabledControlName: FormControl( + FormGroup( + { + isEnabledControlName: FormControl( value: thresholdSettingO?.isEnabled, validators: [], asyncValidators: [], asyncValidatorsDebounceTime: 250, disabled: false, - touched: false), - valueControlName: FormControl( + touched: false, + ), + valueControlName: FormControl( value: thresholdSettingO?.value, validators: [], asyncValidators: [], asyncValidatorsDebounceTime: 250, disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } final _logTimerSettingOForm = Logger.detached('TimerSettingOForm'); class TimerSettingOForm implements FormModel { - TimerSettingOForm( - this.form, - this.path, - ); + TimerSettingOForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String isEnabledControlName = "isEnabled"; @@ -2480,8 +2759,14 @@ class TimerSettingOForm final String? path; + // ignore: unused_field + final FormModel? _formModel; + final Map _disabled = {}; + @override + final Map initial; + String isEnabledControlPath() => pathBuilder(isEnabledControlName); String valueControlPath() => pathBuilder(valueControlName); @@ -2494,8 +2779,6 @@ class TimerSettingOForm int get _valueRawValue => valueControl.value ?? 5; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsIsEnabled { try { form.control(isEnabledControlPath()); @@ -2505,8 +2788,6 @@ class TimerSettingOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsValue { try { form.control(valueControlPath()); @@ -2529,8 +2810,11 @@ class TimerSettingOForm bool updateParent = true, bool emitEvent = true, }) { - isEnabledControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + isEnabledControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void valueValueUpdate( @@ -2538,8 +2822,11 @@ class TimerSettingOForm bool updateParent = true, bool emitEvent = true, }) { - valueControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + valueControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void isEnabledValuePatch( @@ -2547,8 +2834,11 @@ class TimerSettingOForm bool updateParent = true, bool emitEvent = true, }) { - isEnabledControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + isEnabledControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void valueValuePatch( @@ -2556,8 +2846,11 @@ class TimerSettingOForm bool updateParent = true, bool emitEvent = true, }) { - valueControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + valueControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void isEnabledValueReset( @@ -2566,14 +2859,13 @@ class TimerSettingOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - isEnabledControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => isEnabledControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void valueValueReset( int value, { @@ -2581,14 +2873,13 @@ class TimerSettingOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - valueControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => valueControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get isEnabledControl => form.control(isEnabledControlPath()) as FormControl; @@ -2653,27 +2944,20 @@ class TimerSettingOForm } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { - final currentFormInstance = currentForm; - - if (currentFormInstance is! FormGroup) { - return; - } - + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { - currentFormInstance.controls[key]?.markAsEnabled( + currentForm.controls[key]?.markAsEnabled( updateParent: updateParent, emitEvent: emitEvent, ); @@ -2689,9 +2973,7 @@ class TimerSettingOForm final currentForm = this.currentForm; return const DeepCollectionEquality().equals( - currentForm is FormControlCollection - ? currentForm.rawValue - : currentForm.value, + currentForm.rawValue, TimerSettingOForm.formElements(other).rawValue, ); } @@ -2712,8 +2994,16 @@ class TimerSettingOForm } @override - AbstractControl get currentForm { - return path == null ? form : form.control(path!); + bool get hasChanged { + return !const DeepCollectionEquality().equals( + currentForm.rawValue, + initial, + ); + } + + @override + FormGroup get currentForm { + return path == null ? form : form.control(path!) as FormGroup; } @override @@ -2721,62 +3011,129 @@ class TimerSettingOForm TimerSettingO? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(TimerSettingOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + TimerSettingOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void upsertValue( + TimerSettingO? value, { + bool updateParent = true, + bool emitEvent = true, + }) { + final formElements = TimerSettingOForm.formElements(value); + + currentForm.addAll(formElements.controls); + } @override void reset({ TimerSettingO? value, bool updateParent = true, bool emitEvent = true, - }) => - form.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void updateInitial(Map? value, String? path) { + if (_formModel != null) { + _formModel?.updateInitial(currentForm.rawValue, path); + return; + } + + if (value == null) return; + + if (path == null || path.isEmpty) { + initial.addAll(value); + return; + } + + final keys = path.split('.'); + Object? current = initial; + for (var i = 0; i < keys.length - 1; i++) { + final key = keys[i]; + + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current = current[index]; + continue; + } + } + + if (current is Map) { + if (!current.containsKey(key)) { + current[key] = {}; + } + current = current[key]; + continue; + } + + return; + } + + final key = keys.last; + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current[index] = value; + } + } else if (current is Map) { + current[key] = value; + } + } String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(TimerSettingO? timerSettingO) => FormGroup({ - isEnabledControlName: FormControl( - value: timerSettingO?.isEnabled, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - valueControlName: FormControl( - value: timerSettingO?.value, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(TimerSettingO? timerSettingO) => FormGroup( + { + isEnabledControlName: FormControl( + value: timerSettingO?.isEnabled, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + valueControlName: FormControl( + value: timerSettingO?.value, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } @freezed @Rf(output: true) abstract class ProfileOOutput with _$ProfileOOutput { const ProfileOOutput._(); - factory ProfileOOutput(String id, - {required String anotherId, - @RfControl() required String name, - @RfControl() - required ChartingOrderValue chartingOrder, - @RfControl() - required NumberingStandard numberingStandard, - required IncidenceFilterOOutput incidenceFilter, - @RfControl() required MeasurementType measurementType, - required ThresholdSettingOOutput threshold, - required TimerSettingOOutput timer, - @RfControl() required bool audioGuidance}) = _ProfileOOutput; + factory ProfileOOutput( + String id, { + required String anotherId, + @RfControl() required String name, + @RfControl() required ChartingOrderValue chartingOrder, + @RfControl() + required NumberingStandard numberingStandard, + required IncidenceFilterOOutput incidenceFilter, + @RfControl() required MeasurementType measurementType, + required ThresholdSettingOOutput threshold, + required TimerSettingOOutput timer, + @RfControl() required bool audioGuidance, + }) = _ProfileOOutput; factory ProfileOOutput.fromJson(Map json) => _$ProfileOOutputFromJson(json); } @@ -2785,9 +3142,10 @@ abstract class ProfileOOutput with _$ProfileOOutput { @RfGroup() abstract class ThresholdSettingOOutput with _$ThresholdSettingOOutput { static const values = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; - const factory ThresholdSettingOOutput( - {@RfControl() @Default(true) bool isEnabled, - @RfControl() @Default(2) int value}) = _ThresholdSettingOOutput; + const factory ThresholdSettingOOutput({ + @RfControl() @Default(true) bool isEnabled, + @RfControl() @Default(2) int value, + }) = _ThresholdSettingOOutput; factory ThresholdSettingOOutput.fromJson(Map json) => _$ThresholdSettingOOutputFromJson(json); } @@ -2796,9 +3154,10 @@ abstract class ThresholdSettingOOutput with _$ThresholdSettingOOutput { @RfGroup() abstract class TimerSettingOOutput with _$TimerSettingOOutput { static const values = [1, 2, 3, 4, 5, 6]; - const factory TimerSettingOOutput( - {@RfControl() @Default(false) bool isEnabled, - @RfControl() @Default(5) int value}) = _TimerSettingOOutput; + const factory TimerSettingOOutput({ + @RfControl() @Default(false) bool isEnabled, + @RfControl() @Default(5) int value, + }) = _TimerSettingOOutput; factory TimerSettingOOutput.fromJson(Map json) => _$TimerSettingOOutputFromJson(json); } @@ -2806,14 +3165,14 @@ abstract class TimerSettingOOutput with _$TimerSettingOOutput { @RfGroup() @freezed abstract class IncidenceFilterOOutput with _$IncidenceFilterOOutput { - const factory IncidenceFilterOOutput( - {@RfControl() @Default(true) bool isMobilityEnabled, - @RfControl() @Default(true) bool isFurcationEnabled, - @RfControl() @Default(true) bool isBleedingEnabled, - @RfControl() @Default(true) bool isSuppurationEnabled, - @RfControl() @Default(true) bool isCalculusEnabled, - @RfControl() @Default(true) bool isPlaqueEnabled}) = - _IncidenceFilterOOutput; + const factory IncidenceFilterOOutput({ + @RfControl() @Default(true) bool isMobilityEnabled, + @RfControl() @Default(true) bool isFurcationEnabled, + @RfControl() @Default(true) bool isBleedingEnabled, + @RfControl() @Default(true) bool isSuppurationEnabled, + @RfControl() @Default(true) bool isCalculusEnabled, + @RfControl() @Default(true) bool isPlaqueEnabled, + }) = _IncidenceFilterOOutput; factory IncidenceFilterOOutput.fromJson(Map json) => _$IncidenceFilterOOutputFromJson(json); } @@ -2828,30 +3187,39 @@ class ReactiveProfileOFormArrayBuilder required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - ProfileOForm formModel)? control; + ProfileOForm formModel, + )? + control; final Widget Function( - BuildContext context, List itemList, ProfileOForm formModel)? - builder; + BuildContext context, + List itemList, + ProfileOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveProfileOFormArrayBuilderT? item, - ProfileOForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveProfileOFormArrayBuilderT? item, + ProfileOForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function(FormControl control)? - controlFilter; + controlFilter; @override Widget build(BuildContext context) { @@ -2867,18 +3235,9 @@ class ReactiveProfileOFormArrayBuilder return ReactiveFormArrayItemBuilder( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -2896,35 +3255,41 @@ class ReactiveProfileOFormArrayBuilder2 required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - ProfileOForm formModel)? control; + ProfileOForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - ProfileOForm formModel - }) params)? builder; + ({BuildContext context, List itemList, ProfileOForm formModel}) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveProfileOFormArrayBuilderT? item, - ProfileOForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveProfileOFormArrayBuilderT? item, + ProfileOForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function(FormControl control)? - controlFilter; + controlFilter; @override Widget build(BuildContext context) { @@ -2941,23 +3306,17 @@ class ReactiveProfileOFormArrayBuilder2 formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -2966,33 +3325,48 @@ class ReactiveProfileOFormArrayBuilder2 } class ReactiveProfileOFormFormGroupArrayBuilder< - ReactiveProfileOFormFormGroupArrayBuilderT> extends StatelessWidget { + ReactiveProfileOFormFormGroupArrayBuilderT +> + extends StatelessWidget { const ReactiveProfileOFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? extended; - - final ExtendedControl?>, - List> - Function(ProfileOForm formModel)? getExtended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(ProfileOForm formModel)? + getExtended; final Widget Function( - BuildContext context, List itemList, ProfileOForm formModel)? - builder; + BuildContext context, + List itemList, + ProfileOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveProfileOFormFormGroupArrayBuilderT? item, - ProfileOForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveProfileOFormFormGroupArrayBuilderT? item, + ProfileOForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -3010,23 +3384,14 @@ class ReactiveProfileOFormFormGroupArrayBuilder< final itemList = (value.value() ?? []) .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) .values .toList(); - return builder?.call( - context, - itemList, - formModel, - ) ?? + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/generator_tests/test/doc/profile_test.dart b/packages/generator_tests/test/doc/profile_test.dart index a3ea0c1e..b42210aa 100644 --- a/packages/generator_tests/test/doc/profile_test.dart +++ b/packages/generator_tests/test/doc/profile_test.dart @@ -300,7 +300,11 @@ class ReactiveProfileFormConsumer extends StatelessWidget { final Widget? child; final Widget Function( - BuildContext context, ProfileForm formModel, Widget? child) builder; + BuildContext context, + ProfileForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -319,11 +323,7 @@ class ProfileFormInheritedStreamer extends InheritedStreamer { required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final ProfileForm form; } @@ -345,18 +345,17 @@ class ReactiveProfileForm extends StatelessWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - static ProfileForm? of( - BuildContext context, { - bool listen = true, - }) { + static ProfileForm? of(BuildContext context, {bool listen = true}) { if (listen) { return context .dependOnInheritedWidgetOfExactType() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - ProfileFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + ProfileFormInheritedStreamer + >(); return element == null ? null : (element.widget as ProfileFormInheritedStreamer).form; @@ -402,7 +401,11 @@ class ProfileFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, ProfileForm formModel, Widget? child) builder; + BuildContext context, + ProfileForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, ProfileForm formModel)? initState; @@ -417,7 +420,11 @@ class _ProfileFormBuilderState extends State { @override void initState() { - _formModel = ProfileForm(ProfileForm.formElements(widget.model), null); + _formModel = ProfileForm( + ProfileForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -494,10 +501,7 @@ class _ProfileFormBuilderState extends State { final _logProfileForm = Logger.detached('ProfileForm'); class ProfileForm implements FormModel { - ProfileForm( - this.form, - this.path, - ); + ProfileForm(this.form, this.path, this._formModel) : initial = form.rawValue; static const String idControlName = "id"; @@ -523,8 +527,14 @@ class ProfileForm implements FormModel { final String? path; + // ignore: unused_field + final FormModel? _formModel; + final Map _disabled = {}; + @override + final Map initial; + String idControlPath() => pathBuilder(idControlName); String anotherIdControlPath() => pathBuilder(anotherIdControlName); @@ -594,8 +604,6 @@ class ProfileForm implements FormModel { TimerSetting get _timerRawValue => timerForm.rawModel; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsId { try { form.control(idControlPath()); @@ -605,8 +613,6 @@ class ProfileForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsAnotherId { try { form.control(anotherIdControlPath()); @@ -616,8 +622,6 @@ class ProfileForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsName { try { form.control(nameControlPath()); @@ -627,8 +631,6 @@ class ProfileForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsChartingOrder { try { form.control(chartingOrderControlPath()); @@ -638,8 +640,6 @@ class ProfileForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsNumberingStandard { try { form.control(numberingStandardControlPath()); @@ -649,8 +649,6 @@ class ProfileForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsMeasurementType { try { form.control(measurementTypeControlPath()); @@ -660,8 +658,6 @@ class ProfileForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsAudioGuidance { try { form.control(audioGuidanceControlPath()); @@ -671,8 +667,6 @@ class ProfileForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsIncidenceFilter { try { form.control(incidenceFilterControlPath()); @@ -682,8 +676,6 @@ class ProfileForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsThreshold { try { form.control(thresholdControlPath()); @@ -693,8 +685,6 @@ class ProfileForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsTimer { try { form.control(timerControlPath()); @@ -752,8 +742,11 @@ class ProfileForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - idControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + idControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void anotherIdValueUpdate( @@ -761,8 +754,11 @@ class ProfileForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - anotherIdControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + anotherIdControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void nameValueUpdate( @@ -770,8 +766,11 @@ class ProfileForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - nameControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + nameControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void chartingOrderValueUpdate( @@ -779,8 +778,11 @@ class ProfileForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - chartingOrderControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + chartingOrderControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void numberingStandardValueUpdate( @@ -788,8 +790,11 @@ class ProfileForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - numberingStandardControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + numberingStandardControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void measurementTypeValueUpdate( @@ -797,8 +802,11 @@ class ProfileForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - measurementTypeControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + measurementTypeControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void audioGuidanceValueUpdate( @@ -806,8 +814,11 @@ class ProfileForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - audioGuidanceControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + audioGuidanceControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void incidenceFilterValueUpdate( @@ -816,9 +827,10 @@ class ProfileForm implements FormModel { bool emitEvent = true, }) { incidenceFilterControl.updateValue( - IncidenceFilterForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + IncidenceFilterForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void thresholdValueUpdate( @@ -827,9 +839,10 @@ class ProfileForm implements FormModel { bool emitEvent = true, }) { thresholdControl.updateValue( - ThresholdSettingForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + ThresholdSettingForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void timerValueUpdate( @@ -837,8 +850,11 @@ class ProfileForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - timerControl.updateValue(TimerSettingForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + timerControl.updateValue( + TimerSettingForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void idValuePatch( @@ -846,8 +862,11 @@ class ProfileForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - idControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + idControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void anotherIdValuePatch( @@ -855,8 +874,11 @@ class ProfileForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - anotherIdControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + anotherIdControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void nameValuePatch( @@ -864,8 +886,11 @@ class ProfileForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - nameControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + nameControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void chartingOrderValuePatch( @@ -873,8 +898,11 @@ class ProfileForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - chartingOrderControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + chartingOrderControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void numberingStandardValuePatch( @@ -882,8 +910,11 @@ class ProfileForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - numberingStandardControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + numberingStandardControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void measurementTypeValuePatch( @@ -891,8 +922,11 @@ class ProfileForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - measurementTypeControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + measurementTypeControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void audioGuidanceValuePatch( @@ -900,8 +934,11 @@ class ProfileForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - audioGuidanceControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + audioGuidanceControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void incidenceFilterValuePatch( @@ -910,9 +947,10 @@ class ProfileForm implements FormModel { bool emitEvent = true, }) { incidenceFilterControl.updateValue( - IncidenceFilterForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + IncidenceFilterForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void thresholdValuePatch( @@ -921,9 +959,10 @@ class ProfileForm implements FormModel { bool emitEvent = true, }) { thresholdControl.updateValue( - ThresholdSettingForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + ThresholdSettingForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void timerValuePatch( @@ -931,8 +970,11 @@ class ProfileForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - timerControl.updateValue(TimerSettingForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + timerControl.updateValue( + TimerSettingForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void idValueReset( @@ -941,14 +983,13 @@ class ProfileForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - idControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => idControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void anotherIdValueReset( String value, { @@ -956,14 +997,13 @@ class ProfileForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - anotherIdControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => anotherIdControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void nameValueReset( String value, { @@ -971,14 +1011,13 @@ class ProfileForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - nameControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => nameControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void chartingOrderValueReset( ChartingOrderValue value, { @@ -986,14 +1025,13 @@ class ProfileForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - chartingOrderControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => chartingOrderControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void numberingStandardValueReset( NumberingStandard value, { @@ -1001,14 +1039,13 @@ class ProfileForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - numberingStandardControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => numberingStandardControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void measurementTypeValueReset( MeasurementType value, { @@ -1016,14 +1053,13 @@ class ProfileForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - measurementTypeControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => measurementTypeControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void audioGuidanceValueReset( bool value, { @@ -1031,14 +1067,13 @@ class ProfileForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - audioGuidanceControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => audioGuidanceControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void incidenceFilterValueReset( IncidenceFilter value, { @@ -1046,11 +1081,11 @@ class ProfileForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - incidenceFilterControl.reset( - value: IncidenceFilterForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => incidenceFilterControl.reset( + value: IncidenceFilterForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); void thresholdValueReset( ThresholdSetting value, { @@ -1058,11 +1093,11 @@ class ProfileForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - thresholdControl.reset( - value: ThresholdSettingForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => thresholdControl.reset( + value: ThresholdSettingForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); void timerValueReset( TimerSetting value, { @@ -1070,11 +1105,11 @@ class ProfileForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - timerControl.reset( - value: TimerSettingForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => timerControl.reset( + value: TimerSettingForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); FormControl get idControl => form.control(idControlPath()) as FormControl; @@ -1108,14 +1143,17 @@ class ProfileForm implements FormModel { FormGroup get timerControl => form.control(timerControlPath()) as FormGroup; - IncidenceFilterForm get incidenceFilterForm => - IncidenceFilterForm(form, pathBuilder('incidenceFilter')); + IncidenceFilterForm get incidenceFilterForm => IncidenceFilterForm( + form, + pathBuilder('incidenceFilter'), + _formModel ?? this, + ); ThresholdSettingForm get thresholdForm => - ThresholdSettingForm(form, pathBuilder('threshold')); + ThresholdSettingForm(form, pathBuilder('threshold'), _formModel ?? this); TimerSettingForm get timerForm => - TimerSettingForm(form, pathBuilder('timer')); + TimerSettingForm(form, pathBuilder('timer'), _formModel ?? this); void idSetDisabled( bool disabled, { @@ -1128,10 +1166,7 @@ class ProfileForm implements FormModel { emitEvent: emitEvent, ); } else { - idControl.markAsEnabled( - updateParent: updateParent, - emitEvent: emitEvent, - ); + idControl.markAsEnabled(updateParent: updateParent, emitEvent: emitEvent); } } @@ -1308,45 +1343,40 @@ class ProfileForm implements FormModel { StackTrace.current, ); } - return Profile(_idValue, - anotherId: _anotherIdValue, - name: _nameValue, - chartingOrder: _chartingOrderValue, - numberingStandard: _numberingStandardValue, - incidenceFilter: _incidenceFilterValue, - measurementType: _measurementTypeValue, - threshold: _thresholdValue, - timer: _timerValue, - audioGuidance: _audioGuidanceValue); + return Profile( + _idValue, + anotherId: _anotherIdValue, + name: _nameValue, + chartingOrder: _chartingOrderValue, + numberingStandard: _numberingStandardValue, + incidenceFilter: _incidenceFilterValue, + measurementType: _measurementTypeValue, + threshold: _thresholdValue, + timer: _timerValue, + audioGuidance: _audioGuidanceValue, + ); } @override Profile get rawModel { - return Profile(_idRawValue, - anotherId: _anotherIdRawValue, - name: _nameRawValue, - chartingOrder: _chartingOrderRawValue, - numberingStandard: _numberingStandardRawValue, - incidenceFilter: _incidenceFilterRawValue, - measurementType: _measurementTypeRawValue, - threshold: _thresholdRawValue, - timer: _timerRawValue, - audioGuidance: _audioGuidanceRawValue); + return Profile( + _idRawValue, + anotherId: _anotherIdRawValue, + name: _nameRawValue, + chartingOrder: _chartingOrderRawValue, + numberingStandard: _numberingStandardRawValue, + incidenceFilter: _incidenceFilterRawValue, + measurementType: _measurementTypeRawValue, + threshold: _thresholdRawValue, + timer: _timerRawValue, + audioGuidance: _audioGuidanceRawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { - final currentFormInstance = currentForm; - - if (currentFormInstance is! FormGroup) { - return; - } - + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); @@ -1354,14 +1384,16 @@ class ProfileForm implements FormModel { thresholdForm.toggleDisabled(); timerForm.toggleDisabled(); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { incidenceFilterForm.toggleDisabled(); thresholdForm.toggleDisabled(); timerForm.toggleDisabled(); - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { - currentFormInstance.controls[key]?.markAsEnabled( + currentForm.controls[key]?.markAsEnabled( updateParent: updateParent, emitEvent: emitEvent, ); @@ -1377,9 +1409,7 @@ class ProfileForm implements FormModel { final currentForm = this.currentForm; return const DeepCollectionEquality().equals( - currentForm is FormControlCollection - ? currentForm.rawValue - : currentForm.value, + currentForm.rawValue, ProfileForm.formElements(other).rawValue, ); } @@ -1400,8 +1430,16 @@ class ProfileForm implements FormModel { } @override - AbstractControl get currentForm { - return path == null ? form : form.control(path!); + bool get hasChanged { + return !const DeepCollectionEquality().equals( + currentForm.rawValue, + initial, + ); + } + + @override + FormGroup get currentForm { + return path == null ? form : form.control(path!) as FormGroup; } @override @@ -1409,94 +1447,165 @@ class ProfileForm implements FormModel { Profile? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(ProfileForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + ProfileForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void upsertValue( + Profile? value, { + bool updateParent = true, + bool emitEvent = true, + }) { + final formElements = ProfileForm.formElements(value); + + currentForm.addAll(formElements.controls); + } @override void reset({ Profile? value, bool updateParent = true, bool emitEvent = true, - }) => - form.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void updateInitial(Map? value, String? path) { + if (_formModel != null) { + _formModel?.updateInitial(currentForm.rawValue, path); + return; + } + + if (value == null) return; + + if (path == null || path.isEmpty) { + initial.addAll(value); + return; + } + + final keys = path.split('.'); + Object? current = initial; + for (var i = 0; i < keys.length - 1; i++) { + final key = keys[i]; + + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current = current[index]; + continue; + } + } + + if (current is Map) { + if (!current.containsKey(key)) { + current[key] = {}; + } + current = current[key]; + continue; + } + + return; + } + + final key = keys.last; + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current[index] = value; + } + } else if (current is Map) { + current[key] = value; + } + } String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(Profile? profile) => FormGroup({ - idControlName: FormControl( - value: profile?.id, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - anotherIdControlName: FormControl( - value: profile?.anotherId, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - nameControlName: FormControl( - value: profile?.name, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - chartingOrderControlName: FormControl( - value: profile?.chartingOrder, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - numberingStandardControlName: FormControl( - value: profile?.numberingStandard, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - measurementTypeControlName: FormControl( - value: profile?.measurementType, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - audioGuidanceControlName: FormControl( - value: profile?.audioGuidance, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - incidenceFilterControlName: - IncidenceFilterForm.formElements(profile?.incidenceFilter), - thresholdControlName: - ThresholdSettingForm.formElements(profile?.threshold), - timerControlName: TimerSettingForm.formElements(profile?.timer) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(Profile? profile) => FormGroup( + { + idControlName: FormControl( + value: profile?.id, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + anotherIdControlName: FormControl( + value: profile?.anotherId, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + nameControlName: FormControl( + value: profile?.name, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + chartingOrderControlName: FormControl( + value: profile?.chartingOrder, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + numberingStandardControlName: FormControl( + value: profile?.numberingStandard, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + measurementTypeControlName: FormControl( + value: profile?.measurementType, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + audioGuidanceControlName: FormControl( + value: profile?.audioGuidance, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + incidenceFilterControlName: IncidenceFilterForm.formElements( + profile?.incidenceFilter, + ), + thresholdControlName: ThresholdSettingForm.formElements( + profile?.threshold, + ), + timerControlName: TimerSettingForm.formElements(profile?.timer), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } final _logIncidenceFilterForm = Logger.detached('IncidenceFilterForm'); class IncidenceFilterForm implements FormModel { - IncidenceFilterForm( - this.form, - this.path, - ); + IncidenceFilterForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String isMobilityEnabledControlName = "isMobilityEnabled"; @@ -1514,8 +1623,14 @@ class IncidenceFilterForm final String? path; + // ignore: unused_field + final FormModel? _formModel; + final Map _disabled = {}; + @override + final Map initial; + String isMobilityEnabledControlPath() => pathBuilder(isMobilityEnabledControlName); @@ -1561,8 +1676,6 @@ class IncidenceFilterForm bool get _isPlaqueEnabledRawValue => isPlaqueEnabledControl.value ?? true; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsIsMobilityEnabled { try { form.control(isMobilityEnabledControlPath()); @@ -1572,8 +1685,6 @@ class IncidenceFilterForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsIsFurcationEnabled { try { form.control(isFurcationEnabledControlPath()); @@ -1583,8 +1694,6 @@ class IncidenceFilterForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsIsBleedingEnabled { try { form.control(isBleedingEnabledControlPath()); @@ -1594,8 +1703,6 @@ class IncidenceFilterForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsIsSuppurationEnabled { try { form.control(isSuppurationEnabledControlPath()); @@ -1605,8 +1712,6 @@ class IncidenceFilterForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsIsCalculusEnabled { try { form.control(isCalculusEnabledControlPath()); @@ -1616,8 +1721,6 @@ class IncidenceFilterForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsIsPlaqueEnabled { try { form.control(isPlaqueEnabledControlPath()); @@ -1664,8 +1767,11 @@ class IncidenceFilterForm bool updateParent = true, bool emitEvent = true, }) { - isMobilityEnabledControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + isMobilityEnabledControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void isFurcationEnabledValueUpdate( @@ -1673,8 +1779,11 @@ class IncidenceFilterForm bool updateParent = true, bool emitEvent = true, }) { - isFurcationEnabledControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + isFurcationEnabledControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void isBleedingEnabledValueUpdate( @@ -1682,8 +1791,11 @@ class IncidenceFilterForm bool updateParent = true, bool emitEvent = true, }) { - isBleedingEnabledControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + isBleedingEnabledControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void isSuppurationEnabledValueUpdate( @@ -1691,8 +1803,11 @@ class IncidenceFilterForm bool updateParent = true, bool emitEvent = true, }) { - isSuppurationEnabledControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + isSuppurationEnabledControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void isCalculusEnabledValueUpdate( @@ -1700,8 +1815,11 @@ class IncidenceFilterForm bool updateParent = true, bool emitEvent = true, }) { - isCalculusEnabledControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + isCalculusEnabledControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void isPlaqueEnabledValueUpdate( @@ -1709,8 +1827,11 @@ class IncidenceFilterForm bool updateParent = true, bool emitEvent = true, }) { - isPlaqueEnabledControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + isPlaqueEnabledControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void isMobilityEnabledValuePatch( @@ -1718,8 +1839,11 @@ class IncidenceFilterForm bool updateParent = true, bool emitEvent = true, }) { - isMobilityEnabledControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + isMobilityEnabledControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void isFurcationEnabledValuePatch( @@ -1727,8 +1851,11 @@ class IncidenceFilterForm bool updateParent = true, bool emitEvent = true, }) { - isFurcationEnabledControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + isFurcationEnabledControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void isBleedingEnabledValuePatch( @@ -1736,8 +1863,11 @@ class IncidenceFilterForm bool updateParent = true, bool emitEvent = true, }) { - isBleedingEnabledControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + isBleedingEnabledControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void isSuppurationEnabledValuePatch( @@ -1745,8 +1875,11 @@ class IncidenceFilterForm bool updateParent = true, bool emitEvent = true, }) { - isSuppurationEnabledControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + isSuppurationEnabledControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void isCalculusEnabledValuePatch( @@ -1754,8 +1887,11 @@ class IncidenceFilterForm bool updateParent = true, bool emitEvent = true, }) { - isCalculusEnabledControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + isCalculusEnabledControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void isPlaqueEnabledValuePatch( @@ -1763,8 +1899,11 @@ class IncidenceFilterForm bool updateParent = true, bool emitEvent = true, }) { - isPlaqueEnabledControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + isPlaqueEnabledControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void isMobilityEnabledValueReset( @@ -1773,14 +1912,13 @@ class IncidenceFilterForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - isMobilityEnabledControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => isMobilityEnabledControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void isFurcationEnabledValueReset( bool value, { @@ -1788,14 +1926,13 @@ class IncidenceFilterForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - isFurcationEnabledControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => isFurcationEnabledControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void isBleedingEnabledValueReset( bool value, { @@ -1803,14 +1940,13 @@ class IncidenceFilterForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - isBleedingEnabledControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => isBleedingEnabledControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void isSuppurationEnabledValueReset( bool value, { @@ -1818,14 +1954,13 @@ class IncidenceFilterForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - isSuppurationEnabledControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => isSuppurationEnabledControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void isCalculusEnabledValueReset( bool value, { @@ -1833,14 +1968,13 @@ class IncidenceFilterForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - isCalculusEnabledControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => isCalculusEnabledControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void isPlaqueEnabledValueReset( bool value, { @@ -1848,14 +1982,13 @@ class IncidenceFilterForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - isPlaqueEnabledControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => isPlaqueEnabledControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get isMobilityEnabledControl => form.control(isMobilityEnabledControlPath()) as FormControl; @@ -1995,47 +2128,42 @@ class IncidenceFilterForm ); } return IncidenceFilter( - isMobilityEnabled: _isMobilityEnabledValue, - isFurcationEnabled: _isFurcationEnabledValue, - isBleedingEnabled: _isBleedingEnabledValue, - isSuppurationEnabled: _isSuppurationEnabledValue, - isCalculusEnabled: _isCalculusEnabledValue, - isPlaqueEnabled: _isPlaqueEnabledValue); + isMobilityEnabled: _isMobilityEnabledValue, + isFurcationEnabled: _isFurcationEnabledValue, + isBleedingEnabled: _isBleedingEnabledValue, + isSuppurationEnabled: _isSuppurationEnabledValue, + isCalculusEnabled: _isCalculusEnabledValue, + isPlaqueEnabled: _isPlaqueEnabledValue, + ); } @override IncidenceFilter get rawModel { return IncidenceFilter( - isMobilityEnabled: _isMobilityEnabledRawValue, - isFurcationEnabled: _isFurcationEnabledRawValue, - isBleedingEnabled: _isBleedingEnabledRawValue, - isSuppurationEnabled: _isSuppurationEnabledRawValue, - isCalculusEnabled: _isCalculusEnabledRawValue, - isPlaqueEnabled: _isPlaqueEnabledRawValue); + isMobilityEnabled: _isMobilityEnabledRawValue, + isFurcationEnabled: _isFurcationEnabledRawValue, + isBleedingEnabled: _isBleedingEnabledRawValue, + isSuppurationEnabled: _isSuppurationEnabledRawValue, + isCalculusEnabled: _isCalculusEnabledRawValue, + isPlaqueEnabled: _isPlaqueEnabledRawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { - final currentFormInstance = currentForm; - - if (currentFormInstance is! FormGroup) { - return; - } - + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { - currentFormInstance.controls[key]?.markAsEnabled( + currentForm.controls[key]?.markAsEnabled( updateParent: updateParent, emitEvent: emitEvent, ); @@ -2051,9 +2179,7 @@ class IncidenceFilterForm final currentForm = this.currentForm; return const DeepCollectionEquality().equals( - currentForm is FormControlCollection - ? currentForm.rawValue - : currentForm.value, + currentForm.rawValue, IncidenceFilterForm.formElements(other).rawValue, ); } @@ -2074,8 +2200,16 @@ class IncidenceFilterForm } @override - AbstractControl get currentForm { - return path == null ? form : form.control(path!); + bool get hasChanged { + return !const DeepCollectionEquality().equals( + currentForm.rawValue, + initial, + ); + } + + @override + FormGroup get currentForm { + return path == null ? form : form.control(path!) as FormGroup; } @override @@ -2083,82 +2217,150 @@ class IncidenceFilterForm IncidenceFilter? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(IncidenceFilterForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + IncidenceFilterForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void upsertValue( + IncidenceFilter? value, { + bool updateParent = true, + bool emitEvent = true, + }) { + final formElements = IncidenceFilterForm.formElements(value); + + currentForm.addAll(formElements.controls); + } @override void reset({ IncidenceFilter? value, bool updateParent = true, bool emitEvent = true, - }) => - form.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void updateInitial(Map? value, String? path) { + if (_formModel != null) { + _formModel?.updateInitial(currentForm.rawValue, path); + return; + } + + if (value == null) return; + + if (path == null || path.isEmpty) { + initial.addAll(value); + return; + } + + final keys = path.split('.'); + Object? current = initial; + for (var i = 0; i < keys.length - 1; i++) { + final key = keys[i]; + + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current = current[index]; + continue; + } + } + + if (current is Map) { + if (!current.containsKey(key)) { + current[key] = {}; + } + current = current[key]; + continue; + } + + return; + } + + final key = keys.last; + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current[index] = value; + } + } else if (current is Map) { + current[key] = value; + } + } String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(IncidenceFilter? incidenceFilter) => FormGroup({ - isMobilityEnabledControlName: FormControl( - value: incidenceFilter?.isMobilityEnabled, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - isFurcationEnabledControlName: FormControl( - value: incidenceFilter?.isFurcationEnabled, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - isBleedingEnabledControlName: FormControl( - value: incidenceFilter?.isBleedingEnabled, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - isSuppurationEnabledControlName: FormControl( - value: incidenceFilter?.isSuppurationEnabled, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - isCalculusEnabledControlName: FormControl( - value: incidenceFilter?.isCalculusEnabled, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - isPlaqueEnabledControlName: FormControl( - value: incidenceFilter?.isPlaqueEnabled, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(IncidenceFilter? incidenceFilter) => FormGroup( + { + isMobilityEnabledControlName: FormControl( + value: incidenceFilter?.isMobilityEnabled, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + isFurcationEnabledControlName: FormControl( + value: incidenceFilter?.isFurcationEnabled, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + isBleedingEnabledControlName: FormControl( + value: incidenceFilter?.isBleedingEnabled, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + isSuppurationEnabledControlName: FormControl( + value: incidenceFilter?.isSuppurationEnabled, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + isCalculusEnabledControlName: FormControl( + value: incidenceFilter?.isCalculusEnabled, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + isPlaqueEnabledControlName: FormControl( + value: incidenceFilter?.isPlaqueEnabled, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } final _logThresholdSettingForm = Logger.detached('ThresholdSettingForm'); class ThresholdSettingForm implements FormModel { - ThresholdSettingForm( - this.form, - this.path, - ); + ThresholdSettingForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String isEnabledControlName = "isEnabled"; @@ -2168,8 +2370,14 @@ class ThresholdSettingForm final String? path; + // ignore: unused_field + final FormModel? _formModel; + final Map _disabled = {}; + @override + final Map initial; + String isEnabledControlPath() => pathBuilder(isEnabledControlName); String valueControlPath() => pathBuilder(valueControlName); @@ -2182,8 +2390,6 @@ class ThresholdSettingForm int get _valueRawValue => valueControl.value ?? 2; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsIsEnabled { try { form.control(isEnabledControlPath()); @@ -2193,8 +2399,6 @@ class ThresholdSettingForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsValue { try { form.control(valueControlPath()); @@ -2217,8 +2421,11 @@ class ThresholdSettingForm bool updateParent = true, bool emitEvent = true, }) { - isEnabledControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + isEnabledControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void valueValueUpdate( @@ -2226,8 +2433,11 @@ class ThresholdSettingForm bool updateParent = true, bool emitEvent = true, }) { - valueControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + valueControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void isEnabledValuePatch( @@ -2235,8 +2445,11 @@ class ThresholdSettingForm bool updateParent = true, bool emitEvent = true, }) { - isEnabledControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + isEnabledControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void valueValuePatch( @@ -2244,8 +2457,11 @@ class ThresholdSettingForm bool updateParent = true, bool emitEvent = true, }) { - valueControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + valueControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void isEnabledValueReset( @@ -2254,14 +2470,13 @@ class ThresholdSettingForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - isEnabledControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => isEnabledControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void valueValueReset( int value, { @@ -2269,14 +2484,13 @@ class ThresholdSettingForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - valueControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => valueControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get isEnabledControl => form.control(isEnabledControlPath()) as FormControl; @@ -2337,31 +2551,26 @@ class ThresholdSettingForm @override ThresholdSetting get rawModel { return ThresholdSetting( - isEnabled: _isEnabledRawValue, value: _valueRawValue); + isEnabled: _isEnabledRawValue, + value: _valueRawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { - final currentFormInstance = currentForm; - - if (currentFormInstance is! FormGroup) { - return; - } - + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { - currentFormInstance.controls[key]?.markAsEnabled( + currentForm.controls[key]?.markAsEnabled( updateParent: updateParent, emitEvent: emitEvent, ); @@ -2377,9 +2586,7 @@ class ThresholdSettingForm final currentForm = this.currentForm; return const DeepCollectionEquality().equals( - currentForm is FormControlCollection - ? currentForm.rawValue - : currentForm.value, + currentForm.rawValue, ThresholdSettingForm.formElements(other).rawValue, ); } @@ -2400,8 +2607,16 @@ class ThresholdSettingForm } @override - AbstractControl get currentForm { - return path == null ? form : form.control(path!); + bool get hasChanged { + return !const DeepCollectionEquality().equals( + currentForm.rawValue, + initial, + ); + } + + @override + FormGroup get currentForm { + return path == null ? form : form.control(path!) as FormGroup; } @override @@ -2409,54 +2624,118 @@ class ThresholdSettingForm ThresholdSetting? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(ThresholdSettingForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + ThresholdSettingForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void upsertValue( + ThresholdSetting? value, { + bool updateParent = true, + bool emitEvent = true, + }) { + final formElements = ThresholdSettingForm.formElements(value); + + currentForm.addAll(formElements.controls); + } @override void reset({ ThresholdSetting? value, bool updateParent = true, bool emitEvent = true, - }) => - form.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void updateInitial(Map? value, String? path) { + if (_formModel != null) { + _formModel?.updateInitial(currentForm.rawValue, path); + return; + } + + if (value == null) return; + + if (path == null || path.isEmpty) { + initial.addAll(value); + return; + } + + final keys = path.split('.'); + Object? current = initial; + for (var i = 0; i < keys.length - 1; i++) { + final key = keys[i]; + + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current = current[index]; + continue; + } + } + + if (current is Map) { + if (!current.containsKey(key)) { + current[key] = {}; + } + current = current[key]; + continue; + } + + return; + } + + final key = keys.last; + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current[index] = value; + } + } else if (current is Map) { + current[key] = value; + } + } String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); static FormGroup formElements(ThresholdSetting? thresholdSetting) => - FormGroup({ - isEnabledControlName: FormControl( + FormGroup( + { + isEnabledControlName: FormControl( value: thresholdSetting?.isEnabled, validators: [], asyncValidators: [], asyncValidatorsDebounceTime: 250, disabled: false, - touched: false), - valueControlName: FormControl( + touched: false, + ), + valueControlName: FormControl( value: thresholdSetting?.value, validators: [], asyncValidators: [], asyncValidatorsDebounceTime: 250, disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } final _logTimerSettingForm = Logger.detached('TimerSettingForm'); class TimerSettingForm implements FormModel { - TimerSettingForm( - this.form, - this.path, - ); + TimerSettingForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String isEnabledControlName = "isEnabled"; @@ -2466,8 +2745,14 @@ class TimerSettingForm implements FormModel { final String? path; + // ignore: unused_field + final FormModel? _formModel; + final Map _disabled = {}; + @override + final Map initial; + String isEnabledControlPath() => pathBuilder(isEnabledControlName); String valueControlPath() => pathBuilder(valueControlName); @@ -2480,8 +2765,6 @@ class TimerSettingForm implements FormModel { int get _valueRawValue => valueControl.value ?? 5; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsIsEnabled { try { form.control(isEnabledControlPath()); @@ -2491,8 +2774,6 @@ class TimerSettingForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsValue { try { form.control(valueControlPath()); @@ -2515,8 +2796,11 @@ class TimerSettingForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - isEnabledControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + isEnabledControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void valueValueUpdate( @@ -2524,8 +2808,11 @@ class TimerSettingForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - valueControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + valueControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void isEnabledValuePatch( @@ -2533,8 +2820,11 @@ class TimerSettingForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - isEnabledControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + isEnabledControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void valueValuePatch( @@ -2542,8 +2832,11 @@ class TimerSettingForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - valueControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + valueControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void isEnabledValueReset( @@ -2552,14 +2845,13 @@ class TimerSettingForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - isEnabledControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => isEnabledControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void valueValueReset( int value, { @@ -2567,14 +2859,13 @@ class TimerSettingForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - valueControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => valueControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get isEnabledControl => form.control(isEnabledControlPath()) as FormControl; @@ -2638,27 +2929,20 @@ class TimerSettingForm implements FormModel { } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { - final currentFormInstance = currentForm; - - if (currentFormInstance is! FormGroup) { - return; - } - + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { - currentFormInstance.controls[key]?.markAsEnabled( + currentForm.controls[key]?.markAsEnabled( updateParent: updateParent, emitEvent: emitEvent, ); @@ -2674,9 +2958,7 @@ class TimerSettingForm implements FormModel { final currentForm = this.currentForm; return const DeepCollectionEquality().equals( - currentForm is FormControlCollection - ? currentForm.rawValue - : currentForm.value, + currentForm.rawValue, TimerSettingForm.formElements(other).rawValue, ); } @@ -2697,8 +2979,16 @@ class TimerSettingForm implements FormModel { } @override - AbstractControl get currentForm { - return path == null ? form : form.control(path!); + bool get hasChanged { + return !const DeepCollectionEquality().equals( + currentForm.rawValue, + initial, + ); + } + + @override + FormGroup get currentForm { + return path == null ? form : form.control(path!) as FormGroup; } @override @@ -2706,44 +2996,110 @@ class TimerSettingForm implements FormModel { TimerSetting? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(TimerSettingForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + TimerSettingForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void upsertValue( + TimerSetting? value, { + bool updateParent = true, + bool emitEvent = true, + }) { + final formElements = TimerSettingForm.formElements(value); + + currentForm.addAll(formElements.controls); + } @override void reset({ TimerSetting? value, bool updateParent = true, bool emitEvent = true, - }) => - form.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void updateInitial(Map? value, String? path) { + if (_formModel != null) { + _formModel?.updateInitial(currentForm.rawValue, path); + return; + } + + if (value == null) return; + + if (path == null || path.isEmpty) { + initial.addAll(value); + return; + } + + final keys = path.split('.'); + Object? current = initial; + for (var i = 0; i < keys.length - 1; i++) { + final key = keys[i]; + + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current = current[index]; + continue; + } + } + + if (current is Map) { + if (!current.containsKey(key)) { + current[key] = {}; + } + current = current[key]; + continue; + } + + return; + } + + final key = keys.last; + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current[index] = value; + } + } else if (current is Map) { + current[key] = value; + } + } String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(TimerSetting? timerSetting) => FormGroup({ - isEnabledControlName: FormControl( - value: timerSetting?.isEnabled, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - valueControlName: FormControl( - value: timerSetting?.value, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(TimerSetting? timerSetting) => FormGroup( + { + isEnabledControlName: FormControl( + value: timerSetting?.isEnabled, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + valueControlName: FormControl( + value: timerSetting?.value, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } class ReactiveProfileFormArrayBuilder @@ -2756,30 +3112,39 @@ class ReactiveProfileFormArrayBuilder required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - ProfileForm formModel)? control; + ProfileForm formModel, + )? + control; final Widget Function( - BuildContext context, List itemList, ProfileForm formModel)? - builder; + BuildContext context, + List itemList, + ProfileForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveProfileFormArrayBuilderT? item, - ProfileForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveProfileFormArrayBuilderT? item, + ProfileForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function(FormControl control)? - controlFilter; + controlFilter; @override Widget build(BuildContext context) { @@ -2795,18 +3160,9 @@ class ReactiveProfileFormArrayBuilder return ReactiveFormArrayItemBuilder( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -2824,35 +3180,41 @@ class ReactiveProfileFormArrayBuilder2 required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - ProfileForm formModel)? control; + ProfileForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - ProfileForm formModel - }) params)? builder; + ({BuildContext context, List itemList, ProfileForm formModel}) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveProfileFormArrayBuilderT? item, - ProfileForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveProfileFormArrayBuilderT? item, + ProfileForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function(FormControl control)? - controlFilter; + controlFilter; @override Widget build(BuildContext context) { @@ -2869,23 +3231,17 @@ class ReactiveProfileFormArrayBuilder2 formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -2894,33 +3250,48 @@ class ReactiveProfileFormArrayBuilder2 } class ReactiveProfileFormFormGroupArrayBuilder< - ReactiveProfileFormFormGroupArrayBuilderT> extends StatelessWidget { + ReactiveProfileFormFormGroupArrayBuilderT +> + extends StatelessWidget { const ReactiveProfileFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? extended; - - final ExtendedControl?>, - List> - Function(ProfileForm formModel)? getExtended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(ProfileForm formModel)? + getExtended; final Widget Function( - BuildContext context, List itemList, ProfileForm formModel)? - builder; + BuildContext context, + List itemList, + ProfileForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveProfileFormFormGroupArrayBuilderT? item, - ProfileForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveProfileFormFormGroupArrayBuilderT? item, + ProfileForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -2938,23 +3309,14 @@ class ReactiveProfileFormFormGroupArrayBuilder< final itemList = (value.value() ?? []) .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) .values .toList(); - return builder?.call( - context, - itemList, - formModel, - ) ?? + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/generator_tests/test/doc/recursive_test.dart b/packages/generator_tests/test/doc/recursive_test.dart index 0a28e4d2..07eb2209 100644 --- a/packages/generator_tests/test/doc/recursive_test.dart +++ b/packages/generator_tests/test/doc/recursive_test.dart @@ -75,7 +75,11 @@ class ReactiveSecuredAreaFormConsumer extends StatelessWidget { final Widget? child; final Widget Function( - BuildContext context, SecuredAreaForm formModel, Widget? child) builder; + BuildContext context, + SecuredAreaForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -94,11 +98,7 @@ class SecuredAreaFormInheritedStreamer extends InheritedStreamer { required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final SecuredAreaForm form; } @@ -120,19 +120,19 @@ class ReactiveSecuredAreaForm extends StatelessWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - static SecuredAreaForm? of( - BuildContext context, { - bool listen = true, - }) { + static SecuredAreaForm? of(BuildContext context, {bool listen = true}) { if (listen) { return context .dependOnInheritedWidgetOfExactType< - SecuredAreaFormInheritedStreamer>() + SecuredAreaFormInheritedStreamer + >() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - SecuredAreaFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + SecuredAreaFormInheritedStreamer + >(); return element == null ? null : (element.widget as SecuredAreaFormInheritedStreamer).form; @@ -179,10 +179,14 @@ class SecuredAreaFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, SecuredAreaForm formModel, Widget? child) builder; + BuildContext context, + SecuredAreaForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, SecuredAreaForm formModel)? - initState; + initState; @override _SecuredAreaFormBuilderState createState() => _SecuredAreaFormBuilderState(); @@ -195,8 +199,11 @@ class _SecuredAreaFormBuilderState extends State { @override void initState() { - _formModel = - SecuredAreaForm(SecuredAreaForm.formElements(widget.model), null); + _formModel = SecuredAreaForm( + SecuredAreaForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -273,10 +280,8 @@ class _SecuredAreaFormBuilderState extends State { final _logSecuredAreaForm = Logger.detached('SecuredAreaForm'); class SecuredAreaForm implements FormModel { - SecuredAreaForm( - this.form, - this.path, - ); + SecuredAreaForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String idControlName = "id"; @@ -290,8 +295,14 @@ class SecuredAreaForm implements FormModel { final String? path; + // ignore: unused_field + final FormModel? _formModel; + final Map _disabled = {}; + @override + final Map initial; + String idControlPath() => pathBuilder(idControlName); String securedAreaControlPath() => pathBuilder(securedAreaControlName); @@ -301,26 +312,28 @@ class SecuredAreaForm implements FormModel { String subSecuredAreasControlPath() => pathBuilder(subSecuredAreasControlName); - String? get _idValue => idControl.value; + String? get _idValue => containsId ? idControl.value : null; - SecuredArea? get _securedAreaValue => securedAreaForm.model; + SecuredArea? get _securedAreaValue => + containsSecuredArea ? securedAreaForm.model : null; - ParcelSystem? get _parcelSystemValue => parcelSystemForm.model; + ParcelSystem? get _parcelSystemValue => + containsParcelSystem ? parcelSystemForm.model : null; List get _subSecuredAreasValue => subSecuredAreasSecuredAreaForm.map((e) => e.model).toList(); - String? get _idRawValue => idControl.value; + String? get _idRawValue => containsId ? idControl.value : null; - SecuredArea? get _securedAreaRawValue => securedAreaForm.rawModel; + SecuredArea? get _securedAreaRawValue => + containsSecuredArea ? securedAreaForm.rawModel : null; - ParcelSystem? get _parcelSystemRawValue => parcelSystemForm.rawModel; + ParcelSystem? get _parcelSystemRawValue => + containsParcelSystem ? parcelSystemForm.rawModel : null; List get _subSecuredAreasRawValue => subSecuredAreasSecuredAreaForm.map((e) => e.rawModel).toList(); - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsId { try { form.control(idControlPath()); @@ -330,8 +343,6 @@ class SecuredAreaForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsSecuredArea { try { form.control(securedAreaControlPath()); @@ -341,8 +352,6 @@ class SecuredAreaForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsParcelSystem { try { form.control(parcelSystemControlPath()); @@ -352,8 +361,6 @@ class SecuredAreaForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsSubSecuredAreas { try { form.control(subSecuredAreasControlPath()); @@ -380,12 +387,7 @@ class SecuredAreaForm implements FormModel { void get subSecuredAreasFocus => form.focus(subSecuredAreasControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void idRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void idRemove({bool updateParent = true, bool emitEvent = true}) { if (containsId) { final controlPath = path; if (controlPath == null) { @@ -408,12 +410,7 @@ class SecuredAreaForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void securedAreaRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void securedAreaRemove({bool updateParent = true, bool emitEvent = true}) { if (containsSecuredArea) { final controlPath = path; if (controlPath == null) { @@ -436,12 +433,7 @@ class SecuredAreaForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void parcelSystemRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void parcelSystemRemove({bool updateParent = true, bool emitEvent = true}) { if (containsParcelSystem) { final controlPath = path; if (controlPath == null) { @@ -469,8 +461,11 @@ class SecuredAreaForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - idControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + idControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void securedAreaValueUpdate( @@ -478,8 +473,11 @@ class SecuredAreaForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - securedAreaControl.updateValue(SecuredAreaForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + securedAreaControl.updateValue( + SecuredAreaForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void parcelSystemValueUpdate( @@ -488,9 +486,10 @@ class SecuredAreaForm implements FormModel { bool emitEvent = true, }) { parcelSystemControl.updateValue( - ParcelSystemForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + ParcelSystemForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void subSecuredAreasValueUpdate( @@ -509,8 +508,9 @@ class SecuredAreaForm implements FormModel { final toAdd = []; localValue.asMap().forEach((k, v) { - final values = - subSecuredAreasControl.controls.map((e) => e.value).toList(); + final values = subSecuredAreasControl.controls + .map((e) => e.value) + .toList(); if (subSecuredAreasSecuredAreaForm.asMap().containsKey(k) && values.asMap().containsKey(k)) { @@ -522,17 +522,19 @@ class SecuredAreaForm implements FormModel { if (toUpdate.isNotEmpty) { subSecuredAreasControl.updateValue( - toUpdate - .map((e) => SecuredAreaForm.formElements(e).rawValue) - .toList(), - updateParent: updateParent, - emitEvent: emitEvent); + toUpdate.map((e) => SecuredAreaForm.formElements(e).rawValue).toList(), + updateParent: updateParent, + emitEvent: emitEvent, + ); } if (toAdd.isNotEmpty) { toAdd.forEach((e) { - subSecuredAreasControl.add(SecuredAreaForm.formElements(e), - updateParent: updateParent, emitEvent: emitEvent); + subSecuredAreasControl.add( + SecuredAreaForm.formElements(e), + updateParent: updateParent, + emitEvent: emitEvent, + ); }); } } @@ -557,13 +559,12 @@ class SecuredAreaForm implements FormModel { ); } - void subSecuredAreasClear({ - bool updateParent = true, - bool emitEvent = true, - }) { + void subSecuredAreasClear({bool updateParent = true, bool emitEvent = true}) { subSecuredAreasSecuredAreaForm.clear(); subSecuredAreasControl.clear( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } void idValuePatch( @@ -571,8 +572,11 @@ class SecuredAreaForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - idControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + idControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void securedAreaValuePatch( @@ -580,8 +584,11 @@ class SecuredAreaForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - securedAreaControl.updateValue(SecuredAreaForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + securedAreaControl.updateValue( + SecuredAreaForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void parcelSystemValuePatch( @@ -590,9 +597,10 @@ class SecuredAreaForm implements FormModel { bool emitEvent = true, }) { parcelSystemControl.updateValue( - ParcelSystemForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + ParcelSystemForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void subSecuredAreasValuePatch( @@ -603,18 +611,17 @@ class SecuredAreaForm implements FormModel { final keys = subSecuredAreasSecuredAreaForm.asMap().keys; final toPatch = []; - (value).asMap().forEach( - (k, v) { - if (keys.contains(k)) { - toPatch.add(v); - } - }, - ); + (value).asMap().forEach((k, v) { + if (keys.contains(k)) { + toPatch.add(v); + } + }); subSecuredAreasControl.patchValue( - toPatch.map((e) => SecuredAreaForm.formElements(e).rawValue).toList(), - updateParent: updateParent, - emitEvent: emitEvent); + toPatch.map((e) => SecuredAreaForm.formElements(e).rawValue).toList(), + updateParent: updateParent, + emitEvent: emitEvent, + ); } void idValueReset( @@ -623,14 +630,13 @@ class SecuredAreaForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - idControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => idControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void securedAreaValueReset( SecuredArea? value, { @@ -638,11 +644,11 @@ class SecuredAreaForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - securedAreaControl.reset( - value: SecuredAreaForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => securedAreaControl.reset( + value: SecuredAreaForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); void parcelSystemValueReset( ParcelSystem? value, { @@ -650,11 +656,11 @@ class SecuredAreaForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - parcelSystemControl.reset( - value: ParcelSystemForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => parcelSystemControl.reset( + value: ParcelSystemForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); void subSecuredAreasValueReset( List value, { @@ -662,13 +668,11 @@ class SecuredAreaForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - subSecuredAreasControl.reset( - value: value - .map((e) => SecuredAreaForm.formElements(e).rawValue) - .toList(), - updateParent: updateParent, - emitEvent: emitEvent); + }) => subSecuredAreasControl.reset( + value: value.map((e) => SecuredAreaForm.formElements(e).rawValue).toList(), + updateParent: updateParent, + emitEvent: emitEvent, + ); FormControl get idControl => form.control(idControlPath()) as FormControl; @@ -684,18 +688,26 @@ class SecuredAreaForm implements FormModel { as FormArray>; SecuredAreaForm get securedAreaForm => - SecuredAreaForm(form, pathBuilder('securedArea')); + SecuredAreaForm(form, pathBuilder('securedArea'), _formModel ?? this); ParcelSystemForm get parcelSystemForm => - ParcelSystemForm(form, pathBuilder('parcelSystem')); + ParcelSystemForm(form, pathBuilder('parcelSystem'), _formModel ?? this); List get subSecuredAreasSecuredAreaForm { final values = subSecuredAreasControl.controls.map((e) => e.value).toList(); return values .asMap() - .map((k, v) => MapEntry( - k, SecuredAreaForm(form, pathBuilder("subSecuredAreas.$k")))) + .map( + (k, v) => MapEntry( + k, + SecuredAreaForm( + form, + pathBuilder("subSecuredAreas.$k"), + _formModel ?? this, + ), + ), + ) .values .toList(); } @@ -711,10 +723,7 @@ class SecuredAreaForm implements FormModel { emitEvent: emitEvent, ); } else { - idControl.markAsEnabled( - updateParent: updateParent, - emitEvent: emitEvent, - ); + idControl.markAsEnabled(updateParent: updateParent, emitEvent: emitEvent); } } @@ -773,11 +782,12 @@ class SecuredAreaForm implements FormModel { } ExtendedControl?>, List> - get subSecuredAreasExtendedControl => - ExtendedControl?>, List>( - form.control(subSecuredAreasControlPath()) - as FormArray>, - () => subSecuredAreasSecuredAreaForm); + get subSecuredAreasExtendedControl => + ExtendedControl?>, List>( + form.control(subSecuredAreasControlPath()) + as FormArray>, + () => subSecuredAreasSecuredAreaForm, + ); void addSubSecuredAreasItem(SecuredArea value) { subSecuredAreasControl.add(SecuredAreaForm.formElements(value)); @@ -805,34 +815,27 @@ class SecuredAreaForm implements FormModel { ); } return SecuredArea( - id: _idValue, - securedArea: _securedAreaValue, - parcelSystem: _parcelSystemValue, - subSecuredAreas: _subSecuredAreasValue); + id: _idValue, + securedArea: _securedAreaValue, + parcelSystem: _parcelSystemValue, + subSecuredAreas: _subSecuredAreasValue, + ); } @override SecuredArea get rawModel { return SecuredArea( - id: _idRawValue, - securedArea: _securedAreaRawValue, - parcelSystem: _parcelSystemRawValue, - subSecuredAreas: _subSecuredAreasRawValue); + id: _idRawValue, + securedArea: _securedAreaRawValue, + parcelSystem: _parcelSystemRawValue, + subSecuredAreas: _subSecuredAreasRawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { - final currentFormInstance = currentForm; - - if (currentFormInstance is! FormGroup) { - return; - } - + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); @@ -840,14 +843,16 @@ class SecuredAreaForm implements FormModel { securedAreaForm.toggleDisabled(); parcelSystemForm.toggleDisabled(); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { subSecuredAreasSecuredAreaForm.forEach((e) => e.toggleDisabled()); securedAreaForm.toggleDisabled(); parcelSystemForm.toggleDisabled(); - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { - currentFormInstance.controls[key]?.markAsEnabled( + currentForm.controls[key]?.markAsEnabled( updateParent: updateParent, emitEvent: emitEvent, ); @@ -863,9 +868,7 @@ class SecuredAreaForm implements FormModel { final currentForm = this.currentForm; return const DeepCollectionEquality().equals( - currentForm is FormControlCollection - ? currentForm.rawValue - : currentForm.value, + currentForm.rawValue, SecuredAreaForm.formElements(other).rawValue, ); } @@ -886,8 +889,16 @@ class SecuredAreaForm implements FormModel { } @override - AbstractControl get currentForm { - return path == null ? form : form.control(path!); + bool get hasChanged { + return !const DeepCollectionEquality().equals( + currentForm.rawValue, + initial, + ); + } + + @override + FormGroup get currentForm { + return path == null ? form : form.control(path!) as FormGroup; } @override @@ -895,58 +906,124 @@ class SecuredAreaForm implements FormModel { SecuredArea? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(SecuredAreaForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + SecuredAreaForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void upsertValue( + SecuredArea? value, { + bool updateParent = true, + bool emitEvent = true, + }) { + final formElements = SecuredAreaForm.formElements(value); + + currentForm.addAll(formElements.controls); + } @override void reset({ SecuredArea? value, bool updateParent = true, bool emitEvent = true, - }) => - form.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void updateInitial(Map? value, String? path) { + if (_formModel != null) { + _formModel?.updateInitial(currentForm.rawValue, path); + return; + } + + if (value == null) return; + + if (path == null || path.isEmpty) { + initial.addAll(value); + return; + } + + final keys = path.split('.'); + Object? current = initial; + for (var i = 0; i < keys.length - 1; i++) { + final key = keys[i]; + + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current = current[index]; + continue; + } + } + + if (current is Map) { + if (!current.containsKey(key)) { + current[key] = {}; + } + current = current[key]; + continue; + } + + return; + } + + final key = keys.last; + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current[index] = value; + } + } else if (current is Map) { + current[key] = value; + } + } String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(SecuredArea? securedArea) => FormGroup({ - idControlName: FormControl( - value: securedArea?.id, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - subSecuredAreasControlName: FormArray( - (securedArea?.subSecuredAreas ?? []) - .map((e) => SecuredAreaForm.formElements(e)) - .toList(), - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false), - securedAreaControlName: - SecuredAreaForm.formElements(securedArea?.securedArea), - parcelSystemControlName: - ParcelSystemForm.formElements(securedArea?.parcelSystem) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(SecuredArea? securedArea) => FormGroup( + { + idControlName: FormControl( + value: securedArea?.id, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + subSecuredAreasControlName: FormArray( + (securedArea?.subSecuredAreas ?? []) + .map((e) => SecuredAreaForm.formElements(e)) + .toList(), + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ), + securedAreaControlName: SecuredAreaForm.formElements( + securedArea?.securedArea, + ), + parcelSystemControlName: ParcelSystemForm.formElements( + securedArea?.parcelSystem, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } final _logParcelSystemForm = Logger.detached('ParcelSystemForm'); class ParcelSystemForm implements FormModel { - ParcelSystemForm( - this.form, - this.path, - ); + ParcelSystemForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String hasParcelSystemControlName = "hasParcelSystem"; @@ -956,8 +1033,14 @@ class ParcelSystemForm implements FormModel { final String? path; + // ignore: unused_field + final FormModel? _formModel; + final Map _disabled = {}; + @override + final Map initial; + String hasParcelSystemControlPath() => pathBuilder(hasParcelSystemControlName); @@ -971,8 +1054,6 @@ class ParcelSystemForm implements FormModel { ParcelSystemData get _dataRawValue => dataForm.rawModel; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsHasParcelSystem { try { form.control(hasParcelSystemControlPath()); @@ -982,8 +1063,6 @@ class ParcelSystemForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsData { try { form.control(dataControlPath()); @@ -1007,8 +1086,11 @@ class ParcelSystemForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - hasParcelSystemControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + hasParcelSystemControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void dataValueUpdate( @@ -1016,8 +1098,11 @@ class ParcelSystemForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - dataControl.updateValue(ParcelSystemDataForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + dataControl.updateValue( + ParcelSystemDataForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void hasParcelSystemValuePatch( @@ -1025,8 +1110,11 @@ class ParcelSystemForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - hasParcelSystemControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + hasParcelSystemControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void dataValuePatch( @@ -1034,8 +1122,11 @@ class ParcelSystemForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - dataControl.updateValue(ParcelSystemDataForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + dataControl.updateValue( + ParcelSystemDataForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void hasParcelSystemValueReset( @@ -1044,14 +1135,13 @@ class ParcelSystemForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - hasParcelSystemControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => hasParcelSystemControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void dataValueReset( ParcelSystemData value, { @@ -1059,11 +1149,11 @@ class ParcelSystemForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - dataControl.reset( - value: ParcelSystemDataForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => dataControl.reset( + value: ParcelSystemDataForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); FormControl get hasParcelSystemControl => form.control(hasParcelSystemControlPath()) as FormControl; @@ -1071,7 +1161,7 @@ class ParcelSystemForm implements FormModel { FormGroup get dataControl => form.control(dataControlPath()) as FormGroup; ParcelSystemDataForm get dataForm => - ParcelSystemDataForm(form, pathBuilder('data')); + ParcelSystemDataForm(form, pathBuilder('data'), _formModel ?? this); void hasParcelSystemSetDisabled( bool disabled, { @@ -1121,39 +1211,36 @@ class ParcelSystemForm implements FormModel { ); } return ParcelSystem( - hasParcelSystem: _hasParcelSystemValue, data: _dataValue); + hasParcelSystem: _hasParcelSystemValue, + data: _dataValue, + ); } @override ParcelSystem get rawModel { return ParcelSystem( - hasParcelSystem: _hasParcelSystemRawValue, data: _dataRawValue); + hasParcelSystem: _hasParcelSystemRawValue, + data: _dataRawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { - final currentFormInstance = currentForm; - - if (currentFormInstance is! FormGroup) { - return; - } - + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); dataForm.toggleDisabled(); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { dataForm.toggleDisabled(); - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { - currentFormInstance.controls[key]?.markAsEnabled( + currentForm.controls[key]?.markAsEnabled( updateParent: updateParent, emitEvent: emitEvent, ); @@ -1169,9 +1256,7 @@ class ParcelSystemForm implements FormModel { final currentForm = this.currentForm; return const DeepCollectionEquality().equals( - currentForm is FormControlCollection - ? currentForm.rawValue - : currentForm.value, + currentForm.rawValue, ParcelSystemForm.formElements(other).rawValue, ); } @@ -1192,8 +1277,16 @@ class ParcelSystemForm implements FormModel { } @override - AbstractControl get currentForm { - return path == null ? form : form.control(path!); + bool get hasChanged { + return !const DeepCollectionEquality().equals( + currentForm.rawValue, + initial, + ); + } + + @override + FormGroup get currentForm { + return path == null ? form : form.control(path!) as FormGroup; } @override @@ -1201,48 +1294,111 @@ class ParcelSystemForm implements FormModel { ParcelSystem? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(ParcelSystemForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + ParcelSystemForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void upsertValue( + ParcelSystem? value, { + bool updateParent = true, + bool emitEvent = true, + }) { + final formElements = ParcelSystemForm.formElements(value); + + currentForm.addAll(formElements.controls); + } @override void reset({ ParcelSystem? value, bool updateParent = true, bool emitEvent = true, - }) => - form.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void updateInitial(Map? value, String? path) { + if (_formModel != null) { + _formModel?.updateInitial(currentForm.rawValue, path); + return; + } + + if (value == null) return; + + if (path == null || path.isEmpty) { + initial.addAll(value); + return; + } + + final keys = path.split('.'); + Object? current = initial; + for (var i = 0; i < keys.length - 1; i++) { + final key = keys[i]; + + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current = current[index]; + continue; + } + } + + if (current is Map) { + if (!current.containsKey(key)) { + current[key] = {}; + } + current = current[key]; + continue; + } + + return; + } + + final key = keys.last; + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current[index] = value; + } + } else if (current is Map) { + current[key] = value; + } + } String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(ParcelSystem? parcelSystem) => FormGroup({ - hasParcelSystemControlName: FormControl( - value: parcelSystem?.hasParcelSystem, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - dataControlName: ParcelSystemDataForm.formElements(parcelSystem?.data) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(ParcelSystem? parcelSystem) => FormGroup( + { + hasParcelSystemControlName: FormControl( + value: parcelSystem?.hasParcelSystem, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + dataControlName: ParcelSystemDataForm.formElements(parcelSystem?.data), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } final _logParcelSystemDataForm = Logger.detached('ParcelSystemDataForm'); class ParcelSystemDataForm implements FormModel { - ParcelSystemDataForm( - this.form, - this.path, - ); + ParcelSystemDataForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String idControlName = "id"; @@ -1250,16 +1406,20 @@ class ParcelSystemDataForm final String? path; + // ignore: unused_field + final FormModel? _formModel; + final Map _disabled = {}; + @override + final Map initial; + String idControlPath() => pathBuilder(idControlName); - String? get _idValue => idControl.value; + String? get _idValue => containsId ? idControl.value : null; - String? get _idRawValue => idControl.value; + String? get _idRawValue => containsId ? idControl.value : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsId { try { form.control(idControlPath()); @@ -1273,12 +1433,7 @@ class ParcelSystemDataForm void get idFocus => form.focus(idControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void idRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void idRemove({bool updateParent = true, bool emitEvent = true}) { if (containsId) { final controlPath = path; if (controlPath == null) { @@ -1306,8 +1461,11 @@ class ParcelSystemDataForm bool updateParent = true, bool emitEvent = true, }) { - idControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + idControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void idValuePatch( @@ -1315,8 +1473,11 @@ class ParcelSystemDataForm bool updateParent = true, bool emitEvent = true, }) { - idControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + idControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void idValueReset( @@ -1325,14 +1486,13 @@ class ParcelSystemDataForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - idControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => idControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get idControl => form.control(idControlPath()) as FormControl; @@ -1348,10 +1508,7 @@ class ParcelSystemDataForm emitEvent: emitEvent, ); } else { - idControl.markAsEnabled( - updateParent: updateParent, - emitEvent: emitEvent, - ); + idControl.markAsEnabled(updateParent: updateParent, emitEvent: emitEvent); } } @@ -1375,27 +1532,20 @@ class ParcelSystemDataForm } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { - final currentFormInstance = currentForm; - - if (currentFormInstance is! FormGroup) { - return; - } - + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { - currentFormInstance.controls[key]?.markAsEnabled( + currentForm.controls[key]?.markAsEnabled( updateParent: updateParent, emitEvent: emitEvent, ); @@ -1411,9 +1561,7 @@ class ParcelSystemDataForm final currentForm = this.currentForm; return const DeepCollectionEquality().equals( - currentForm is FormControlCollection - ? currentForm.rawValue - : currentForm.value, + currentForm.rawValue, ParcelSystemDataForm.formElements(other).rawValue, ); } @@ -1434,8 +1582,16 @@ class ParcelSystemDataForm } @override - AbstractControl get currentForm { - return path == null ? form : form.control(path!); + bool get hasChanged { + return !const DeepCollectionEquality().equals( + currentForm.rawValue, + initial, + ); + } + + @override + FormGroup get currentForm { + return path == null ? form : form.control(path!) as FormGroup; } @override @@ -1443,38 +1599,103 @@ class ParcelSystemDataForm ParcelSystemData? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(ParcelSystemDataForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + ParcelSystemDataForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void upsertValue( + ParcelSystemData? value, { + bool updateParent = true, + bool emitEvent = true, + }) { + final formElements = ParcelSystemDataForm.formElements(value); + + currentForm.addAll(formElements.controls); + } @override void reset({ ParcelSystemData? value, bool updateParent = true, bool emitEvent = true, - }) => - form.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void updateInitial(Map? value, String? path) { + if (_formModel != null) { + _formModel?.updateInitial(currentForm.rawValue, path); + return; + } + + if (value == null) return; + + if (path == null || path.isEmpty) { + initial.addAll(value); + return; + } + + final keys = path.split('.'); + Object? current = initial; + for (var i = 0; i < keys.length - 1; i++) { + final key = keys[i]; + + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current = current[index]; + continue; + } + } + + if (current is Map) { + if (!current.containsKey(key)) { + current[key] = {}; + } + current = current[key]; + continue; + } + + return; + } + + final key = keys.last; + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current[index] = value; + } + } else if (current is Map) { + current[key] = value; + } + } String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); static FormGroup formElements(ParcelSystemData? parcelSystemData) => - FormGroup({ - idControlName: FormControl( + FormGroup( + { + idControlName: FormControl( value: parcelSystemData?.id, validators: [], asyncValidators: [], asyncValidatorsDebounceTime: 250, disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } class ReactiveSecuredAreaFormArrayBuilder @@ -1487,29 +1708,41 @@ class ReactiveSecuredAreaFormArrayBuilder required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - SecuredAreaForm formModel)? control; + SecuredAreaForm formModel, + )? + control; - final Widget Function(BuildContext context, List itemList, - SecuredAreaForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + SecuredAreaForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveSecuredAreaFormArrayBuilderT? item, - SecuredAreaForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveSecuredAreaFormArrayBuilderT? item, + SecuredAreaForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -1525,18 +1758,9 @@ class ReactiveSecuredAreaFormArrayBuilder return ReactiveFormArrayItemBuilder( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1554,35 +1778,43 @@ class ReactiveSecuredAreaFormArrayBuilder2 required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - SecuredAreaForm formModel)? control; + SecuredAreaForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - SecuredAreaForm formModel - }) params)? builder; + ({BuildContext context, List itemList, SecuredAreaForm formModel}) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveSecuredAreaFormArrayBuilderT? item, - SecuredAreaForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveSecuredAreaFormArrayBuilderT? item, + SecuredAreaForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -1599,23 +1831,17 @@ class ReactiveSecuredAreaFormArrayBuilder2 formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1624,32 +1850,48 @@ class ReactiveSecuredAreaFormArrayBuilder2 } class ReactiveSecuredAreaFormFormGroupArrayBuilder< - ReactiveSecuredAreaFormFormGroupArrayBuilderT> extends StatelessWidget { + ReactiveSecuredAreaFormFormGroupArrayBuilderT +> + extends StatelessWidget { const ReactiveSecuredAreaFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? extended; - - final ExtendedControl?>, - List> - Function(SecuredAreaForm formModel)? getExtended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(SecuredAreaForm formModel)? + getExtended; - final Widget Function(BuildContext context, List itemList, - SecuredAreaForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + SecuredAreaForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveSecuredAreaFormFormGroupArrayBuilderT? item, - SecuredAreaForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveSecuredAreaFormFormGroupArrayBuilderT? item, + SecuredAreaForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -1667,23 +1909,14 @@ class ReactiveSecuredAreaFormFormGroupArrayBuilder< final itemList = (value.value() ?? []) .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) .values .toList(); - return builder?.call( - context, - itemList, - formModel, - ) ?? + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/generator_tests/test/doc/renamed_basic_output_test.dart b/packages/generator_tests/test/doc/renamed_basic_output_test.dart index 89f1fc1a..233296a7 100644 --- a/packages/generator_tests/test/doc/renamed_basic_output_test.dart +++ b/packages/generator_tests/test/doc/renamed_basic_output_test.dart @@ -65,7 +65,11 @@ class ReactiveSomeWiredNameFormConsumer extends StatelessWidget { final Widget? child; final Widget Function( - BuildContext context, SomeWiredNameForm formModel, Widget? child) builder; + BuildContext context, + SomeWiredNameForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -84,11 +88,7 @@ class SomeWiredNameFormInheritedStreamer extends InheritedStreamer { required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final SomeWiredNameForm form; } @@ -110,19 +110,19 @@ class ReactiveSomeWiredNameForm extends StatelessWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - static SomeWiredNameForm? of( - BuildContext context, { - bool listen = true, - }) { + static SomeWiredNameForm? of(BuildContext context, {bool listen = true}) { if (listen) { return context .dependOnInheritedWidgetOfExactType< - SomeWiredNameFormInheritedStreamer>() + SomeWiredNameFormInheritedStreamer + >() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - SomeWiredNameFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + SomeWiredNameFormInheritedStreamer + >(); return element == null ? null : (element.widget as SomeWiredNameFormInheritedStreamer).form; @@ -170,10 +170,14 @@ class SomeWiredNameFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, SomeWiredNameForm formModel, Widget? child) builder; + BuildContext context, + SomeWiredNameForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, SomeWiredNameForm formModel)? - initState; + initState; @override _SomeWiredNameFormBuilderState createState() => @@ -187,8 +191,11 @@ class _SomeWiredNameFormBuilderState extends State { @override void initState() { - _formModel = - SomeWiredNameForm(SomeWiredNameForm.formElements(widget.model), null); + _formModel = SomeWiredNameForm( + SomeWiredNameForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -266,10 +273,8 @@ final _logSomeWiredNameForm = Logger.detached('SomeWiredNameForm'); class SomeWiredNameForm implements FormModel { - SomeWiredNameForm( - this.form, - this.path, - ); + SomeWiredNameForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String emailControlName = "email"; @@ -279,8 +284,14 @@ class SomeWiredNameForm final String? path; + // ignore: unused_field + final FormModel? _formModel; + final Map _disabled = {}; + @override + final Map initial; + String emailControlPath() => pathBuilder(emailControlName); String passwordControlPath() => pathBuilder(passwordControlName); @@ -289,12 +300,11 @@ class SomeWiredNameForm String get _passwordValue => passwordControl.value as String; - String? get _emailRawValue => emailControl.value; + String? get _emailRawValue => containsEmail ? emailControl.value : null; - String? get _passwordRawValue => passwordControl.value; + String? get _passwordRawValue => + containsPassword ? passwordControl.value : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsEmail { try { form.control(emailControlPath()); @@ -304,8 +314,6 @@ class SomeWiredNameForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsPassword { try { form.control(passwordControlPath()); @@ -323,12 +331,7 @@ class SomeWiredNameForm void get passwordFocus => form.focus(passwordControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void emailRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void emailRemove({bool updateParent = true, bool emitEvent = true}) { if (containsEmail) { final controlPath = path; if (controlPath == null) { @@ -351,12 +354,7 @@ class SomeWiredNameForm } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void passwordRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void passwordRemove({bool updateParent = true, bool emitEvent = true}) { if (containsPassword) { final controlPath = path; if (controlPath == null) { @@ -384,8 +382,11 @@ class SomeWiredNameForm bool updateParent = true, bool emitEvent = true, }) { - emailControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void passwordValueUpdate( @@ -393,8 +394,11 @@ class SomeWiredNameForm bool updateParent = true, bool emitEvent = true, }) { - passwordControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + passwordControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailValuePatch( @@ -402,8 +406,11 @@ class SomeWiredNameForm bool updateParent = true, bool emitEvent = true, }) { - emailControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void passwordValuePatch( @@ -411,8 +418,11 @@ class SomeWiredNameForm bool updateParent = true, bool emitEvent = true, }) { - passwordControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + passwordControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailValueReset( @@ -421,14 +431,13 @@ class SomeWiredNameForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - emailControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => emailControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void passwordValueReset( String? value, { @@ -436,14 +445,13 @@ class SomeWiredNameForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - passwordControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => passwordControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get emailControl => form.control(emailControlPath()) as FormControl; @@ -508,27 +516,20 @@ class SomeWiredNameForm } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { - final currentFormInstance = currentForm; - - if (currentFormInstance is! FormGroup) { - return; - } - + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { - currentFormInstance.controls[key]?.markAsEnabled( + currentForm.controls[key]?.markAsEnabled( updateParent: updateParent, emitEvent: emitEvent, ); @@ -544,9 +545,7 @@ class SomeWiredNameForm final currentForm = this.currentForm; return const DeepCollectionEquality().equals( - currentForm is FormControlCollection - ? currentForm.rawValue - : currentForm.value, + currentForm.rawValue, SomeWiredNameForm.formElements(other).rawValue, ); } @@ -567,8 +566,16 @@ class SomeWiredNameForm } @override - AbstractControl get currentForm { - return path == null ? form : form.control(path!); + bool get hasChanged { + return !const DeepCollectionEquality().equals( + currentForm.rawValue, + initial, + ); + } + + @override + FormGroup get currentForm { + return path == null ? form : form.control(path!) as FormGroup; } @override @@ -576,57 +583,126 @@ class SomeWiredNameForm RenamedBasicO? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(SomeWiredNameForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + SomeWiredNameForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void upsertValue( + RenamedBasicO? value, { + bool updateParent = true, + bool emitEvent = true, + }) { + final formElements = SomeWiredNameForm.formElements(value); + + currentForm.addAll(formElements.controls); + } @override void reset({ RenamedBasicO? value, bool updateParent = true, bool emitEvent = true, - }) => - form.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void updateInitial(Map? value, String? path) { + if (_formModel != null) { + _formModel?.updateInitial(currentForm.rawValue, path); + return; + } + + if (value == null) return; + + if (path == null || path.isEmpty) { + initial.addAll(value); + return; + } + + final keys = path.split('.'); + Object? current = initial; + for (var i = 0; i < keys.length - 1; i++) { + final key = keys[i]; + + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current = current[index]; + continue; + } + } + + if (current is Map) { + if (!current.containsKey(key)) { + current[key] = {}; + } + current = current[key]; + continue; + } + + return; + } + + final key = keys.last; + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current[index] = value; + } + } else if (current is Map) { + current[key] = value; + } + } String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(RenamedBasicO? renamedBasicO) => FormGroup({ - emailControlName: FormControl( - value: renamedBasicO?.email, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - passwordControlName: FormControl( - value: renamedBasicO?.password, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(RenamedBasicO? renamedBasicO) => FormGroup( + { + emailControlName: FormControl( + value: renamedBasicO?.email, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + passwordControlName: FormControl( + value: renamedBasicO?.password, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } @Rf(output: true, name: 'SomeWiredName') class RenamedBasicOOutput { final String email; final String password; - RenamedBasicOOutput( - {@RfControl(validators: [RequiredValidator()]) required this.email, - @RfControl(validators: [RequiredValidator()]) required this.password}); + RenamedBasicOOutput({ + @RfControl(validators: [RequiredValidator()]) required this.email, + @RfControl(validators: [RequiredValidator()]) required this.password, + }); } class ReactiveSomeWiredNameFormArrayBuilder< - ReactiveSomeWiredNameFormArrayBuilderT> extends StatelessWidget { + ReactiveSomeWiredNameFormArrayBuilderT +> + extends StatelessWidget { const ReactiveSomeWiredNameFormArrayBuilder({ Key? key, this.control, @@ -635,30 +711,41 @@ class ReactiveSomeWiredNameFormArrayBuilder< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - SomeWiredNameForm formModel)? control; + SomeWiredNameForm formModel, + )? + control; - final Widget Function(BuildContext context, List itemList, - SomeWiredNameForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + SomeWiredNameForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveSomeWiredNameFormArrayBuilderT? item, - SomeWiredNameForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveSomeWiredNameFormArrayBuilderT? item, + SomeWiredNameForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -674,18 +761,9 @@ class ReactiveSomeWiredNameFormArrayBuilder< return ReactiveFormArrayItemBuilder( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -694,7 +772,9 @@ class ReactiveSomeWiredNameFormArrayBuilder< } class ReactiveSomeWiredNameFormArrayBuilder2< - ReactiveSomeWiredNameFormArrayBuilderT> extends StatelessWidget { + ReactiveSomeWiredNameFormArrayBuilderT +> + extends StatelessWidget { const ReactiveSomeWiredNameFormArrayBuilder2({ Key? key, this.control, @@ -703,36 +783,43 @@ class ReactiveSomeWiredNameFormArrayBuilder2< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - SomeWiredNameForm formModel)? control; + SomeWiredNameForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - SomeWiredNameForm formModel - }) params)? builder; + ({BuildContext context, List itemList, SomeWiredNameForm formModel}) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveSomeWiredNameFormArrayBuilderT? item, - SomeWiredNameForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveSomeWiredNameFormArrayBuilderT? item, + SomeWiredNameForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -749,23 +836,17 @@ class ReactiveSomeWiredNameFormArrayBuilder2< formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -774,32 +855,48 @@ class ReactiveSomeWiredNameFormArrayBuilder2< } class ReactiveSomeWiredNameFormFormGroupArrayBuilder< - ReactiveSomeWiredNameFormFormGroupArrayBuilderT> extends StatelessWidget { + ReactiveSomeWiredNameFormFormGroupArrayBuilderT +> + extends StatelessWidget { const ReactiveSomeWiredNameFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? extended; - - final ExtendedControl?>, - List> - Function(SomeWiredNameForm formModel)? getExtended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(SomeWiredNameForm formModel)? + getExtended; - final Widget Function(BuildContext context, List itemList, - SomeWiredNameForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + SomeWiredNameForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveSomeWiredNameFormFormGroupArrayBuilderT? item, - SomeWiredNameForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveSomeWiredNameFormFormGroupArrayBuilderT? item, + SomeWiredNameForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -814,26 +911,18 @@ class ReactiveSomeWiredNameFormFormGroupArrayBuilder< return StreamBuilder?>?>( stream: value.control.valueChanges, builder: (context, snapshot) { - final itemList = (value.value() ?? - []) - .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) - .values - .toList(); - - return builder?.call( - context, - itemList, - formModel, - ) ?? + final itemList = + (value.value() ?? + []) + .asMap() + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) + .values + .toList(); + + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/generator_tests/test/doc/renamed_basic_test.dart b/packages/generator_tests/test/doc/renamed_basic_test.dart index 6cc46ef8..ccb04a7a 100644 --- a/packages/generator_tests/test/doc/renamed_basic_test.dart +++ b/packages/generator_tests/test/doc/renamed_basic_test.dart @@ -65,7 +65,11 @@ class ReactiveSomeWiredNameFormConsumer extends StatelessWidget { final Widget? child; final Widget Function( - BuildContext context, SomeWiredNameForm formModel, Widget? child) builder; + BuildContext context, + SomeWiredNameForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -84,11 +88,7 @@ class SomeWiredNameFormInheritedStreamer extends InheritedStreamer { required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final SomeWiredNameForm form; } @@ -110,19 +110,19 @@ class ReactiveSomeWiredNameForm extends StatelessWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - static SomeWiredNameForm? of( - BuildContext context, { - bool listen = true, - }) { + static SomeWiredNameForm? of(BuildContext context, {bool listen = true}) { if (listen) { return context .dependOnInheritedWidgetOfExactType< - SomeWiredNameFormInheritedStreamer>() + SomeWiredNameFormInheritedStreamer + >() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - SomeWiredNameFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + SomeWiredNameFormInheritedStreamer + >(); return element == null ? null : (element.widget as SomeWiredNameFormInheritedStreamer).form; @@ -170,10 +170,14 @@ class SomeWiredNameFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, SomeWiredNameForm formModel, Widget? child) builder; + BuildContext context, + SomeWiredNameForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, SomeWiredNameForm formModel)? - initState; + initState; @override _SomeWiredNameFormBuilderState createState() => @@ -187,8 +191,11 @@ class _SomeWiredNameFormBuilderState extends State { @override void initState() { - _formModel = - SomeWiredNameForm(SomeWiredNameForm.formElements(widget.model), null); + _formModel = SomeWiredNameForm( + SomeWiredNameForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -265,10 +272,8 @@ class _SomeWiredNameFormBuilderState extends State { final _logSomeWiredNameForm = Logger.detached('SomeWiredNameForm'); class SomeWiredNameForm implements FormModel { - SomeWiredNameForm( - this.form, - this.path, - ); + SomeWiredNameForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String emailControlName = "email"; @@ -278,8 +283,14 @@ class SomeWiredNameForm implements FormModel { final String? path; + // ignore: unused_field + final FormModel? _formModel; + final Map _disabled = {}; + @override + final Map initial; + String emailControlPath() => pathBuilder(emailControlName); String passwordControlPath() => pathBuilder(passwordControlName); @@ -292,8 +303,6 @@ class SomeWiredNameForm implements FormModel { String get _passwordRawValue => passwordControl.value ?? ""; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsEmail { try { form.control(emailControlPath()); @@ -303,8 +312,6 @@ class SomeWiredNameForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsPassword { try { form.control(passwordControlPath()); @@ -327,8 +334,11 @@ class SomeWiredNameForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - emailControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void passwordValueUpdate( @@ -336,8 +346,11 @@ class SomeWiredNameForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - passwordControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + passwordControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailValuePatch( @@ -345,8 +358,11 @@ class SomeWiredNameForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - emailControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void passwordValuePatch( @@ -354,8 +370,11 @@ class SomeWiredNameForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - passwordControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + passwordControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailValueReset( @@ -364,14 +383,13 @@ class SomeWiredNameForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - emailControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => emailControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void passwordValueReset( String value, { @@ -379,14 +397,13 @@ class SomeWiredNameForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - passwordControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => passwordControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get emailControl => form.control(emailControlPath()) as FormControl; @@ -450,27 +467,20 @@ class SomeWiredNameForm implements FormModel { } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { - final currentFormInstance = currentForm; - - if (currentFormInstance is! FormGroup) { - return; - } - + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { - currentFormInstance.controls[key]?.markAsEnabled( + currentForm.controls[key]?.markAsEnabled( updateParent: updateParent, emitEvent: emitEvent, ); @@ -486,9 +496,7 @@ class SomeWiredNameForm implements FormModel { final currentForm = this.currentForm; return const DeepCollectionEquality().equals( - currentForm is FormControlCollection - ? currentForm.rawValue - : currentForm.value, + currentForm.rawValue, SomeWiredNameForm.formElements(other).rawValue, ); } @@ -509,8 +517,16 @@ class SomeWiredNameForm implements FormModel { } @override - AbstractControl get currentForm { - return path == null ? form : form.control(path!); + bool get hasChanged { + return !const DeepCollectionEquality().equals( + currentForm.rawValue, + initial, + ); + } + + @override + FormGroup get currentForm { + return path == null ? form : form.control(path!) as FormGroup; } @override @@ -518,48 +534,116 @@ class SomeWiredNameForm implements FormModel { RenamedBasic? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(SomeWiredNameForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + SomeWiredNameForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void upsertValue( + RenamedBasic? value, { + bool updateParent = true, + bool emitEvent = true, + }) { + final formElements = SomeWiredNameForm.formElements(value); + + currentForm.addAll(formElements.controls); + } @override void reset({ RenamedBasic? value, bool updateParent = true, bool emitEvent = true, - }) => - form.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void updateInitial(Map? value, String? path) { + if (_formModel != null) { + _formModel?.updateInitial(currentForm.rawValue, path); + return; + } + + if (value == null) return; + + if (path == null || path.isEmpty) { + initial.addAll(value); + return; + } + + final keys = path.split('.'); + Object? current = initial; + for (var i = 0; i < keys.length - 1; i++) { + final key = keys[i]; + + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current = current[index]; + continue; + } + } + + if (current is Map) { + if (!current.containsKey(key)) { + current[key] = {}; + } + current = current[key]; + continue; + } + + return; + } + + final key = keys.last; + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current[index] = value; + } + } else if (current is Map) { + current[key] = value; + } + } String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(RenamedBasic? renamedBasic) => FormGroup({ - emailControlName: FormControl( - value: renamedBasic?.email, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - passwordControlName: FormControl( - value: renamedBasic?.password, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(RenamedBasic? renamedBasic) => FormGroup( + { + emailControlName: FormControl( + value: renamedBasic?.email, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + passwordControlName: FormControl( + value: renamedBasic?.password, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } class ReactiveSomeWiredNameFormArrayBuilder< - ReactiveSomeWiredNameFormArrayBuilderT> extends StatelessWidget { + ReactiveSomeWiredNameFormArrayBuilderT +> + extends StatelessWidget { const ReactiveSomeWiredNameFormArrayBuilder({ Key? key, this.control, @@ -568,30 +652,41 @@ class ReactiveSomeWiredNameFormArrayBuilder< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - SomeWiredNameForm formModel)? control; + SomeWiredNameForm formModel, + )? + control; - final Widget Function(BuildContext context, List itemList, - SomeWiredNameForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + SomeWiredNameForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveSomeWiredNameFormArrayBuilderT? item, - SomeWiredNameForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveSomeWiredNameFormArrayBuilderT? item, + SomeWiredNameForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -607,18 +702,9 @@ class ReactiveSomeWiredNameFormArrayBuilder< return ReactiveFormArrayItemBuilder( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -627,7 +713,9 @@ class ReactiveSomeWiredNameFormArrayBuilder< } class ReactiveSomeWiredNameFormArrayBuilder2< - ReactiveSomeWiredNameFormArrayBuilderT> extends StatelessWidget { + ReactiveSomeWiredNameFormArrayBuilderT +> + extends StatelessWidget { const ReactiveSomeWiredNameFormArrayBuilder2({ Key? key, this.control, @@ -636,36 +724,43 @@ class ReactiveSomeWiredNameFormArrayBuilder2< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - SomeWiredNameForm formModel)? control; + SomeWiredNameForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - SomeWiredNameForm formModel - }) params)? builder; + ({BuildContext context, List itemList, SomeWiredNameForm formModel}) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveSomeWiredNameFormArrayBuilderT? item, - SomeWiredNameForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveSomeWiredNameFormArrayBuilderT? item, + SomeWiredNameForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -682,23 +777,17 @@ class ReactiveSomeWiredNameFormArrayBuilder2< formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -707,32 +796,48 @@ class ReactiveSomeWiredNameFormArrayBuilder2< } class ReactiveSomeWiredNameFormFormGroupArrayBuilder< - ReactiveSomeWiredNameFormFormGroupArrayBuilderT> extends StatelessWidget { + ReactiveSomeWiredNameFormFormGroupArrayBuilderT +> + extends StatelessWidget { const ReactiveSomeWiredNameFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? extended; - - final ExtendedControl?>, - List> - Function(SomeWiredNameForm formModel)? getExtended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(SomeWiredNameForm formModel)? + getExtended; - final Widget Function(BuildContext context, List itemList, - SomeWiredNameForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + SomeWiredNameForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveSomeWiredNameFormFormGroupArrayBuilderT? item, - SomeWiredNameForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveSomeWiredNameFormFormGroupArrayBuilderT? item, + SomeWiredNameForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -747,26 +852,18 @@ class ReactiveSomeWiredNameFormFormGroupArrayBuilder< return StreamBuilder?>?>( stream: value.control.valueChanges, builder: (context, snapshot) { - final itemList = (value.value() ?? - []) - .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) - .values - .toList(); - - return builder?.call( - context, - itemList, - formModel, - ) ?? + final itemList = + (value.value() ?? + []) + .asMap() + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) + .values + .toList(); + + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/generator_tests/test/doc/user_profile_output_test.dart b/packages/generator_tests/test/doc/user_profile_output_test.dart index aae5bcb3..6e1dfe4a 100644 --- a/packages/generator_tests/test/doc/user_profile_output_test.dart +++ b/packages/generator_tests/test/doc/user_profile_output_test.dart @@ -91,7 +91,11 @@ class ReactiveUserProfileOFormConsumer extends StatelessWidget { final Widget? child; final Widget Function( - BuildContext context, UserProfileOForm formModel, Widget? child) builder; + BuildContext context, + UserProfileOForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -110,11 +114,7 @@ class UserProfileOFormInheritedStreamer extends InheritedStreamer { required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final UserProfileOForm form; } @@ -136,19 +136,19 @@ class ReactiveUserProfileOForm extends StatelessWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - static UserProfileOForm? of( - BuildContext context, { - bool listen = true, - }) { + static UserProfileOForm? of(BuildContext context, {bool listen = true}) { if (listen) { return context .dependOnInheritedWidgetOfExactType< - UserProfileOFormInheritedStreamer>() + UserProfileOFormInheritedStreamer + >() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - UserProfileOFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + UserProfileOFormInheritedStreamer + >(); return element == null ? null : (element.widget as UserProfileOFormInheritedStreamer).form; @@ -196,10 +196,14 @@ class UserProfileOFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, UserProfileOForm formModel, Widget? child) builder; + BuildContext context, + UserProfileOForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, UserProfileOForm formModel)? - initState; + initState; @override _UserProfileOFormBuilderState createState() => @@ -213,8 +217,11 @@ class _UserProfileOFormBuilderState extends State { @override void initState() { - _formModel = - UserProfileOForm(UserProfileOForm.formElements(widget.model), null); + _formModel = UserProfileOForm( + UserProfileOForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -291,10 +298,8 @@ class _UserProfileOFormBuilderState extends State { final _logUserProfileOForm = Logger.detached('UserProfileOForm'); class UserProfileOForm implements FormModel { - UserProfileOForm( - this.form, - this.path, - ); + UserProfileOForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String idControlName = "id"; @@ -310,8 +315,14 @@ class UserProfileOForm implements FormModel { final String? path; + // ignore: unused_field + final FormModel? _formModel; + final Map _disabled = {}; + @override + final Map initial; + String idControlPath() => pathBuilder(idControlName); String firstNameControlPath() => pathBuilder(firstNameControlName); @@ -330,20 +341,20 @@ class UserProfileOForm implements FormModel { AddressOOutput get _homeValue => homeForm.model; - AddressOOutput? get _officeValue => officeForm.model; + AddressOOutput? get _officeValue => containsOffice ? officeForm.model : null; String get _idRawValue => idControl.value as String; - String? get _firstNameRawValue => firstNameControl.value; + String? get _firstNameRawValue => + containsFirstName ? firstNameControl.value : null; - String? get _lastNameRawValue => lastNameControl.value; + String? get _lastNameRawValue => + containsLastName ? lastNameControl.value : null; AddressO get _homeRawValue => homeForm.rawModel; - AddressO? get _officeRawValue => officeForm.rawModel; + AddressO? get _officeRawValue => containsOffice ? officeForm.rawModel : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsId { try { form.control(idControlPath()); @@ -353,8 +364,6 @@ class UserProfileOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsFirstName { try { form.control(firstNameControlPath()); @@ -364,8 +373,6 @@ class UserProfileOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsLastName { try { form.control(lastNameControlPath()); @@ -375,8 +382,6 @@ class UserProfileOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsHome { try { form.control(homeControlPath()); @@ -386,8 +391,6 @@ class UserProfileOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsOffice { try { form.control(officeControlPath()); @@ -417,12 +420,7 @@ class UserProfileOForm implements FormModel { void get officeFocus => form.focus(officeControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void firstNameRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void firstNameRemove({bool updateParent = true, bool emitEvent = true}) { if (containsFirstName) { final controlPath = path; if (controlPath == null) { @@ -445,12 +443,7 @@ class UserProfileOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void lastNameRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void lastNameRemove({bool updateParent = true, bool emitEvent = true}) { if (containsLastName) { final controlPath = path; if (controlPath == null) { @@ -473,12 +466,7 @@ class UserProfileOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void officeRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void officeRemove({bool updateParent = true, bool emitEvent = true}) { if (containsOffice) { final controlPath = path; if (controlPath == null) { @@ -506,8 +494,11 @@ class UserProfileOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - idControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + idControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void firstNameValueUpdate( @@ -515,8 +506,11 @@ class UserProfileOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - firstNameControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + firstNameControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void lastNameValueUpdate( @@ -524,8 +518,11 @@ class UserProfileOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - lastNameControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + lastNameControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void homeValueUpdate( @@ -533,8 +530,11 @@ class UserProfileOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - homeControl.updateValue(AddressOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + homeControl.updateValue( + AddressOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void officeValueUpdate( @@ -542,8 +542,11 @@ class UserProfileOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - officeControl.updateValue(AddressOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + officeControl.updateValue( + AddressOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void idValuePatch( @@ -551,8 +554,11 @@ class UserProfileOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - idControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + idControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void firstNameValuePatch( @@ -560,8 +566,11 @@ class UserProfileOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - firstNameControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + firstNameControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void lastNameValuePatch( @@ -569,8 +578,11 @@ class UserProfileOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - lastNameControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + lastNameControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void homeValuePatch( @@ -578,8 +590,11 @@ class UserProfileOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - homeControl.updateValue(AddressOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + homeControl.updateValue( + AddressOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void officeValuePatch( @@ -587,8 +602,11 @@ class UserProfileOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - officeControl.updateValue(AddressOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + officeControl.updateValue( + AddressOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void idValueReset( @@ -597,14 +615,13 @@ class UserProfileOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - idControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => idControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void firstNameValueReset( String? value, { @@ -612,14 +629,13 @@ class UserProfileOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - firstNameControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => firstNameControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void lastNameValueReset( String? value, { @@ -627,14 +643,13 @@ class UserProfileOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - lastNameControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => lastNameControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void homeValueReset( AddressO value, { @@ -642,11 +657,11 @@ class UserProfileOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - homeControl.reset( - value: AddressOForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => homeControl.reset( + value: AddressOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); void officeValueReset( AddressO? value, { @@ -654,11 +669,11 @@ class UserProfileOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - officeControl.reset( - value: AddressOForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => officeControl.reset( + value: AddressOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); FormControl get idControl => form.control(idControlPath()) as FormControl; @@ -673,9 +688,11 @@ class UserProfileOForm implements FormModel { FormGroup get officeControl => form.control(officeControlPath()) as FormGroup; - AddressOForm get homeForm => AddressOForm(form, pathBuilder('home')); + AddressOForm get homeForm => + AddressOForm(form, pathBuilder('home'), _formModel ?? this); - AddressOForm get officeForm => AddressOForm(form, pathBuilder('office')); + AddressOForm get officeForm => + AddressOForm(form, pathBuilder('office'), _formModel ?? this); void idSetDisabled( bool disabled, { @@ -688,10 +705,7 @@ class UserProfileOForm implements FormModel { emitEvent: emitEvent, ); } else { - idControl.markAsEnabled( - updateParent: updateParent, - emitEvent: emitEvent, - ); + idControl.markAsEnabled(updateParent: updateParent, emitEvent: emitEvent); } } @@ -780,49 +794,44 @@ class UserProfileOForm implements FormModel { ); } return UserProfileOOutput( - id: _idValue, - firstName: _firstNameValue, - lastName: _lastNameValue, - home: _homeValue, - office: _officeValue); + id: _idValue, + firstName: _firstNameValue, + lastName: _lastNameValue, + home: _homeValue, + office: _officeValue, + ); } @override UserProfileO get rawModel { return UserProfileO( - id: _idRawValue, - firstName: _firstNameRawValue, - lastName: _lastNameRawValue, - home: _homeRawValue, - office: _officeRawValue); + id: _idRawValue, + firstName: _firstNameRawValue, + lastName: _lastNameRawValue, + home: _homeRawValue, + office: _officeRawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { - final currentFormInstance = currentForm; - - if (currentFormInstance is! FormGroup) { - return; - } - + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); homeForm.toggleDisabled(); officeForm.toggleDisabled(); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { homeForm.toggleDisabled(); officeForm.toggleDisabled(); - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { - currentFormInstance.controls[key]?.markAsEnabled( + currentForm.controls[key]?.markAsEnabled( updateParent: updateParent, emitEvent: emitEvent, ); @@ -838,9 +847,7 @@ class UserProfileOForm implements FormModel { final currentForm = this.currentForm; return const DeepCollectionEquality().equals( - currentForm is FormControlCollection - ? currentForm.rawValue - : currentForm.value, + currentForm.rawValue, UserProfileOForm.formElements(other).rawValue, ); } @@ -861,8 +868,16 @@ class UserProfileOForm implements FormModel { } @override - AbstractControl get currentForm { - return path == null ? form : form.control(path!); + bool get hasChanged { + return !const DeepCollectionEquality().equals( + currentForm.rawValue, + initial, + ); + } + + @override + FormGroup get currentForm { + return path == null ? form : form.control(path!) as FormGroup; } @override @@ -870,62 +885,126 @@ class UserProfileOForm implements FormModel { UserProfileO? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(UserProfileOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + UserProfileOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void upsertValue( + UserProfileO? value, { + bool updateParent = true, + bool emitEvent = true, + }) { + final formElements = UserProfileOForm.formElements(value); + + currentForm.addAll(formElements.controls); + } @override void reset({ UserProfileO? value, bool updateParent = true, bool emitEvent = true, - }) => - form.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void updateInitial(Map? value, String? path) { + if (_formModel != null) { + _formModel?.updateInitial(currentForm.rawValue, path); + return; + } + + if (value == null) return; + + if (path == null || path.isEmpty) { + initial.addAll(value); + return; + } + + final keys = path.split('.'); + Object? current = initial; + for (var i = 0; i < keys.length - 1; i++) { + final key = keys[i]; + + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current = current[index]; + continue; + } + } + + if (current is Map) { + if (!current.containsKey(key)) { + current[key] = {}; + } + current = current[key]; + continue; + } + + return; + } + + final key = keys.last; + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current[index] = value; + } + } else if (current is Map) { + current[key] = value; + } + } String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(UserProfileO? userProfileO) => FormGroup({ - idControlName: FormControl( - value: userProfileO?.id, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - firstNameControlName: FormControl( - value: userProfileO?.firstName, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - lastNameControlName: FormControl( - value: userProfileO?.lastName, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - homeControlName: AddressOForm.formElements(userProfileO?.home), - officeControlName: AddressOForm.formElements(userProfileO?.office) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(UserProfileO? userProfileO) => FormGroup( + { + idControlName: FormControl( + value: userProfileO?.id, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + firstNameControlName: FormControl( + value: userProfileO?.firstName, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + lastNameControlName: FormControl( + value: userProfileO?.lastName, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + homeControlName: AddressOForm.formElements(userProfileO?.home), + officeControlName: AddressOForm.formElements(userProfileO?.office), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } final _logAddressOForm = Logger.detached('AddressOForm'); class AddressOForm implements FormModel { - AddressOForm( - this.form, - this.path, - ); + AddressOForm(this.form, this.path, this._formModel) : initial = form.rawValue; static const String streetControlName = "street"; @@ -937,28 +1016,32 @@ class AddressOForm implements FormModel { final String? path; + // ignore: unused_field + final FormModel? _formModel; + final Map _disabled = {}; + @override + final Map initial; + String streetControlPath() => pathBuilder(streetControlName); String cityControlPath() => pathBuilder(cityControlName); String zipControlPath() => pathBuilder(zipControlName); - String? get _streetValue => streetControl.value; + String? get _streetValue => containsStreet ? streetControl.value : null; String get _cityValue => cityControl.value as String; - String? get _zipValue => zipControl.value; + String? get _zipValue => containsZip ? zipControl.value : null; - String? get _streetRawValue => streetControl.value; + String? get _streetRawValue => containsStreet ? streetControl.value : null; - String? get _cityRawValue => cityControl.value; + String? get _cityRawValue => containsCity ? cityControl.value : null; - String? get _zipRawValue => zipControl.value; + String? get _zipRawValue => containsZip ? zipControl.value : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsStreet { try { form.control(streetControlPath()); @@ -968,8 +1051,6 @@ class AddressOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsCity { try { form.control(cityControlPath()); @@ -979,8 +1060,6 @@ class AddressOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsZip { try { form.control(zipControlPath()); @@ -1002,12 +1081,7 @@ class AddressOForm implements FormModel { void get zipFocus => form.focus(zipControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void streetRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void streetRemove({bool updateParent = true, bool emitEvent = true}) { if (containsStreet) { final controlPath = path; if (controlPath == null) { @@ -1030,12 +1104,7 @@ class AddressOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void cityRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void cityRemove({bool updateParent = true, bool emitEvent = true}) { if (containsCity) { final controlPath = path; if (controlPath == null) { @@ -1058,12 +1127,7 @@ class AddressOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void zipRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void zipRemove({bool updateParent = true, bool emitEvent = true}) { if (containsZip) { final controlPath = path; if (controlPath == null) { @@ -1091,8 +1155,11 @@ class AddressOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - streetControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + streetControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void cityValueUpdate( @@ -1100,8 +1167,11 @@ class AddressOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - cityControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + cityControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void zipValueUpdate( @@ -1109,8 +1179,11 @@ class AddressOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - zipControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + zipControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void streetValuePatch( @@ -1118,8 +1191,11 @@ class AddressOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - streetControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + streetControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void cityValuePatch( @@ -1127,8 +1203,11 @@ class AddressOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - cityControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + cityControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void zipValuePatch( @@ -1136,8 +1215,11 @@ class AddressOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - zipControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + zipControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void streetValueReset( @@ -1146,14 +1228,13 @@ class AddressOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - streetControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => streetControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void cityValueReset( String? value, { @@ -1161,14 +1242,13 @@ class AddressOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - cityControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => cityControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void zipValueReset( String? value, { @@ -1176,14 +1256,13 @@ class AddressOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - zipControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => zipControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get streetControl => form.control(streetControlPath()) as FormControl; @@ -1261,37 +1340,36 @@ class AddressOForm implements FormModel { ); } return AddressOOutput( - street: _streetValue, city: _cityValue, zip: _zipValue); + street: _streetValue, + city: _cityValue, + zip: _zipValue, + ); } @override AddressO get rawModel { return AddressO( - street: _streetRawValue, city: _cityRawValue, zip: _zipRawValue); + street: _streetRawValue, + city: _cityRawValue, + zip: _zipRawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { - final currentFormInstance = currentForm; - - if (currentFormInstance is! FormGroup) { - return; - } - + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { - currentFormInstance.controls[key]?.markAsEnabled( + currentForm.controls[key]?.markAsEnabled( updateParent: updateParent, emitEvent: emitEvent, ); @@ -1307,9 +1385,7 @@ class AddressOForm implements FormModel { final currentForm = this.currentForm; return const DeepCollectionEquality().equals( - currentForm is FormControlCollection - ? currentForm.rawValue - : currentForm.value, + currentForm.rawValue, AddressOForm.formElements(other).rawValue, ); } @@ -1330,8 +1406,16 @@ class AddressOForm implements FormModel { } @override - AbstractControl get currentForm { - return path == null ? form : form.control(path!); + bool get hasChanged { + return !const DeepCollectionEquality().equals( + currentForm.rawValue, + initial, + ); + } + + @override + FormGroup get currentForm { + return path == null ? form : form.control(path!) as FormGroup; } @override @@ -1339,51 +1423,118 @@ class AddressOForm implements FormModel { AddressO? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(AddressOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + AddressOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void upsertValue( + AddressO? value, { + bool updateParent = true, + bool emitEvent = true, + }) { + final formElements = AddressOForm.formElements(value); + + currentForm.addAll(formElements.controls); + } @override void reset({ AddressO? value, bool updateParent = true, bool emitEvent = true, - }) => - form.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void updateInitial(Map? value, String? path) { + if (_formModel != null) { + _formModel?.updateInitial(currentForm.rawValue, path); + return; + } + + if (value == null) return; + + if (path == null || path.isEmpty) { + initial.addAll(value); + return; + } + + final keys = path.split('.'); + Object? current = initial; + for (var i = 0; i < keys.length - 1; i++) { + final key = keys[i]; + + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current = current[index]; + continue; + } + } + + if (current is Map) { + if (!current.containsKey(key)) { + current[key] = {}; + } + current = current[key]; + continue; + } + + return; + } + + final key = keys.last; + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current[index] = value; + } + } else if (current is Map) { + current[key] = value; + } + } String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(AddressO? addressO) => FormGroup({ - streetControlName: FormControl( - value: addressO?.street, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - cityControlName: FormControl( - value: addressO?.city, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - zipControlName: FormControl( - value: addressO?.zip, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(AddressO? addressO) => FormGroup( + { + streetControlName: FormControl( + value: addressO?.street, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + cityControlName: FormControl( + value: addressO?.city, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + zipControlName: FormControl( + value: addressO?.zip, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } @Rf(output: true) @@ -1393,12 +1544,13 @@ class UserProfileOOutput { final String lastName; final AddressOOutput home; final AddressOOutput? office; - UserProfileOOutput( - {required this.id, - @RfControl(validators: [RequiredValidator()]) required this.firstName, - @RfControl(validators: [RequiredValidator()]) required this.lastName, - required this.home, - this.office}); + UserProfileOOutput({ + required this.id, + @RfControl(validators: [RequiredValidator()]) required this.firstName, + @RfControl(validators: [RequiredValidator()]) required this.lastName, + required this.home, + this.office, + }); } @RfGroup() @@ -1406,14 +1558,17 @@ class AddressOOutput { final String? street; final String city; final String? zip; - AddressOOutput( - {@RfControl() this.street, - @RfControl(validators: [RequiredValidator()]) required this.city, - @RfControl() this.zip}); + AddressOOutput({ + @RfControl() this.street, + @RfControl(validators: [RequiredValidator()]) required this.city, + @RfControl() this.zip, + }); } class ReactiveUserProfileOFormArrayBuilder< - ReactiveUserProfileOFormArrayBuilderT> extends StatelessWidget { + ReactiveUserProfileOFormArrayBuilderT +> + extends StatelessWidget { const ReactiveUserProfileOFormArrayBuilder({ Key? key, this.control, @@ -1422,30 +1577,41 @@ class ReactiveUserProfileOFormArrayBuilder< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - UserProfileOForm formModel)? control; + UserProfileOForm formModel, + )? + control; - final Widget Function(BuildContext context, List itemList, - UserProfileOForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + UserProfileOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveUserProfileOFormArrayBuilderT? item, - UserProfileOForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveUserProfileOFormArrayBuilderT? item, + UserProfileOForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -1461,18 +1627,9 @@ class ReactiveUserProfileOFormArrayBuilder< return ReactiveFormArrayItemBuilder( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1481,7 +1638,9 @@ class ReactiveUserProfileOFormArrayBuilder< } class ReactiveUserProfileOFormArrayBuilder2< - ReactiveUserProfileOFormArrayBuilderT> extends StatelessWidget { + ReactiveUserProfileOFormArrayBuilderT +> + extends StatelessWidget { const ReactiveUserProfileOFormArrayBuilder2({ Key? key, this.control, @@ -1490,36 +1649,43 @@ class ReactiveUserProfileOFormArrayBuilder2< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - UserProfileOForm formModel)? control; + UserProfileOForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - UserProfileOForm formModel - }) params)? builder; + ({BuildContext context, List itemList, UserProfileOForm formModel}) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveUserProfileOFormArrayBuilderT? item, - UserProfileOForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveUserProfileOFormArrayBuilderT? item, + UserProfileOForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -1536,23 +1702,17 @@ class ReactiveUserProfileOFormArrayBuilder2< formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1561,32 +1721,48 @@ class ReactiveUserProfileOFormArrayBuilder2< } class ReactiveUserProfileOFormFormGroupArrayBuilder< - ReactiveUserProfileOFormFormGroupArrayBuilderT> extends StatelessWidget { + ReactiveUserProfileOFormFormGroupArrayBuilderT +> + extends StatelessWidget { const ReactiveUserProfileOFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? extended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(UserProfileOForm formModel)? + getExtended; - final ExtendedControl?>, - List> - Function(UserProfileOForm formModel)? getExtended; - - final Widget Function(BuildContext context, List itemList, - UserProfileOForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + UserProfileOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveUserProfileOFormFormGroupArrayBuilderT? item, - UserProfileOForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveUserProfileOFormFormGroupArrayBuilderT? item, + UserProfileOForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -1601,26 +1777,18 @@ class ReactiveUserProfileOFormFormGroupArrayBuilder< return StreamBuilder?>?>( stream: value.control.valueChanges, builder: (context, snapshot) { - final itemList = (value.value() ?? - []) - .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) - .values - .toList(); - - return builder?.call( - context, - itemList, - formModel, - ) ?? + final itemList = + (value.value() ?? + []) + .asMap() + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) + .values + .toList(); + + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/generator_tests/test/doc/user_profile_test.dart b/packages/generator_tests/test/doc/user_profile_test.dart index ea556ce2..42a9a8e8 100644 --- a/packages/generator_tests/test/doc/user_profile_test.dart +++ b/packages/generator_tests/test/doc/user_profile_test.dart @@ -91,7 +91,11 @@ class ReactiveUserProfileFormConsumer extends StatelessWidget { final Widget? child; final Widget Function( - BuildContext context, UserProfileForm formModel, Widget? child) builder; + BuildContext context, + UserProfileForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -110,11 +114,7 @@ class UserProfileFormInheritedStreamer extends InheritedStreamer { required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final UserProfileForm form; } @@ -136,19 +136,19 @@ class ReactiveUserProfileForm extends StatelessWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - static UserProfileForm? of( - BuildContext context, { - bool listen = true, - }) { + static UserProfileForm? of(BuildContext context, {bool listen = true}) { if (listen) { return context .dependOnInheritedWidgetOfExactType< - UserProfileFormInheritedStreamer>() + UserProfileFormInheritedStreamer + >() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - UserProfileFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + UserProfileFormInheritedStreamer + >(); return element == null ? null : (element.widget as UserProfileFormInheritedStreamer).form; @@ -195,10 +195,14 @@ class UserProfileFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, UserProfileForm formModel, Widget? child) builder; + BuildContext context, + UserProfileForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, UserProfileForm formModel)? - initState; + initState; @override _UserProfileFormBuilderState createState() => _UserProfileFormBuilderState(); @@ -211,8 +215,11 @@ class _UserProfileFormBuilderState extends State { @override void initState() { - _formModel = - UserProfileForm(UserProfileForm.formElements(widget.model), null); + _formModel = UserProfileForm( + UserProfileForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -289,10 +296,8 @@ class _UserProfileFormBuilderState extends State { final _logUserProfileForm = Logger.detached('UserProfileForm'); class UserProfileForm implements FormModel { - UserProfileForm( - this.form, - this.path, - ); + UserProfileForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String idControlName = "id"; @@ -308,8 +313,14 @@ class UserProfileForm implements FormModel { final String? path; + // ignore: unused_field + final FormModel? _formModel; + final Map _disabled = {}; + @override + final Map initial; + String idControlPath() => pathBuilder(idControlName); String firstNameControlPath() => pathBuilder(firstNameControlName); @@ -328,7 +339,7 @@ class UserProfileForm implements FormModel { Address get _homeValue => homeForm.model; - Address? get _officeValue => officeForm.model; + Address? get _officeValue => containsOffice ? officeForm.model : null; String get _idRawValue => idControl.value as String; @@ -338,10 +349,8 @@ class UserProfileForm implements FormModel { Address get _homeRawValue => homeForm.rawModel; - Address? get _officeRawValue => officeForm.rawModel; + Address? get _officeRawValue => containsOffice ? officeForm.rawModel : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsId { try { form.control(idControlPath()); @@ -351,8 +360,6 @@ class UserProfileForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsFirstName { try { form.control(firstNameControlPath()); @@ -362,8 +369,6 @@ class UserProfileForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsLastName { try { form.control(lastNameControlPath()); @@ -373,8 +378,6 @@ class UserProfileForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsHome { try { form.control(homeControlPath()); @@ -384,8 +387,6 @@ class UserProfileForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsOffice { try { form.control(officeControlPath()); @@ -415,12 +416,7 @@ class UserProfileForm implements FormModel { void get officeFocus => form.focus(officeControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void officeRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void officeRemove({bool updateParent = true, bool emitEvent = true}) { if (containsOffice) { final controlPath = path; if (controlPath == null) { @@ -448,8 +444,11 @@ class UserProfileForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - idControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + idControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void firstNameValueUpdate( @@ -457,8 +456,11 @@ class UserProfileForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - firstNameControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + firstNameControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void lastNameValueUpdate( @@ -466,8 +468,11 @@ class UserProfileForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - lastNameControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + lastNameControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void homeValueUpdate( @@ -475,8 +480,11 @@ class UserProfileForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - homeControl.updateValue(AddressForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + homeControl.updateValue( + AddressForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void officeValueUpdate( @@ -484,8 +492,11 @@ class UserProfileForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - officeControl.updateValue(AddressForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + officeControl.updateValue( + AddressForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void idValuePatch( @@ -493,8 +504,11 @@ class UserProfileForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - idControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + idControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void firstNameValuePatch( @@ -502,8 +516,11 @@ class UserProfileForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - firstNameControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + firstNameControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void lastNameValuePatch( @@ -511,8 +528,11 @@ class UserProfileForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - lastNameControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + lastNameControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void homeValuePatch( @@ -520,8 +540,11 @@ class UserProfileForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - homeControl.updateValue(AddressForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + homeControl.updateValue( + AddressForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void officeValuePatch( @@ -529,8 +552,11 @@ class UserProfileForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - officeControl.updateValue(AddressForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + officeControl.updateValue( + AddressForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void idValueReset( @@ -539,14 +565,13 @@ class UserProfileForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - idControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => idControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void firstNameValueReset( String value, { @@ -554,14 +579,13 @@ class UserProfileForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - firstNameControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => firstNameControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void lastNameValueReset( String value, { @@ -569,14 +593,13 @@ class UserProfileForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - lastNameControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => lastNameControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void homeValueReset( Address value, { @@ -584,11 +607,11 @@ class UserProfileForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - homeControl.reset( - value: AddressForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => homeControl.reset( + value: AddressForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); void officeValueReset( Address? value, { @@ -596,11 +619,11 @@ class UserProfileForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - officeControl.reset( - value: AddressForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => officeControl.reset( + value: AddressForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); FormControl get idControl => form.control(idControlPath()) as FormControl; @@ -615,9 +638,11 @@ class UserProfileForm implements FormModel { FormGroup get officeControl => form.control(officeControlPath()) as FormGroup; - AddressForm get homeForm => AddressForm(form, pathBuilder('home')); + AddressForm get homeForm => + AddressForm(form, pathBuilder('home'), _formModel ?? this); - AddressForm get officeForm => AddressForm(form, pathBuilder('office')); + AddressForm get officeForm => + AddressForm(form, pathBuilder('office'), _formModel ?? this); void idSetDisabled( bool disabled, { @@ -630,10 +655,7 @@ class UserProfileForm implements FormModel { emitEvent: emitEvent, ); } else { - idControl.markAsEnabled( - updateParent: updateParent, - emitEvent: emitEvent, - ); + idControl.markAsEnabled(updateParent: updateParent, emitEvent: emitEvent); } } @@ -721,49 +743,44 @@ class UserProfileForm implements FormModel { ); } return UserProfile( - id: _idValue, - firstName: _firstNameValue, - lastName: _lastNameValue, - home: _homeValue, - office: _officeValue); + id: _idValue, + firstName: _firstNameValue, + lastName: _lastNameValue, + home: _homeValue, + office: _officeValue, + ); } @override UserProfile get rawModel { return UserProfile( - id: _idRawValue, - firstName: _firstNameRawValue, - lastName: _lastNameRawValue, - home: _homeRawValue, - office: _officeRawValue); + id: _idRawValue, + firstName: _firstNameRawValue, + lastName: _lastNameRawValue, + home: _homeRawValue, + office: _officeRawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { - final currentFormInstance = currentForm; - - if (currentFormInstance is! FormGroup) { - return; - } - + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); homeForm.toggleDisabled(); officeForm.toggleDisabled(); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { homeForm.toggleDisabled(); officeForm.toggleDisabled(); - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { - currentFormInstance.controls[key]?.markAsEnabled( + currentForm.controls[key]?.markAsEnabled( updateParent: updateParent, emitEvent: emitEvent, ); @@ -779,9 +796,7 @@ class UserProfileForm implements FormModel { final currentForm = this.currentForm; return const DeepCollectionEquality().equals( - currentForm is FormControlCollection - ? currentForm.rawValue - : currentForm.value, + currentForm.rawValue, UserProfileForm.formElements(other).rawValue, ); } @@ -802,8 +817,16 @@ class UserProfileForm implements FormModel { } @override - AbstractControl get currentForm { - return path == null ? form : form.control(path!); + bool get hasChanged { + return !const DeepCollectionEquality().equals( + currentForm.rawValue, + initial, + ); + } + + @override + FormGroup get currentForm { + return path == null ? form : form.control(path!) as FormGroup; } @override @@ -811,62 +834,126 @@ class UserProfileForm implements FormModel { UserProfile? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(UserProfileForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + UserProfileForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void upsertValue( + UserProfile? value, { + bool updateParent = true, + bool emitEvent = true, + }) { + final formElements = UserProfileForm.formElements(value); + + currentForm.addAll(formElements.controls); + } @override void reset({ UserProfile? value, bool updateParent = true, bool emitEvent = true, - }) => - form.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void updateInitial(Map? value, String? path) { + if (_formModel != null) { + _formModel?.updateInitial(currentForm.rawValue, path); + return; + } + + if (value == null) return; + + if (path == null || path.isEmpty) { + initial.addAll(value); + return; + } + + final keys = path.split('.'); + Object? current = initial; + for (var i = 0; i < keys.length - 1; i++) { + final key = keys[i]; + + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current = current[index]; + continue; + } + } + + if (current is Map) { + if (!current.containsKey(key)) { + current[key] = {}; + } + current = current[key]; + continue; + } + + return; + } + + final key = keys.last; + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current[index] = value; + } + } else if (current is Map) { + current[key] = value; + } + } String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(UserProfile? userProfile) => FormGroup({ - idControlName: FormControl( - value: userProfile?.id, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - firstNameControlName: FormControl( - value: userProfile?.firstName, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - lastNameControlName: FormControl( - value: userProfile?.lastName, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - homeControlName: AddressForm.formElements(userProfile?.home), - officeControlName: AddressForm.formElements(userProfile?.office) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(UserProfile? userProfile) => FormGroup( + { + idControlName: FormControl( + value: userProfile?.id, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + firstNameControlName: FormControl( + value: userProfile?.firstName, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + lastNameControlName: FormControl( + value: userProfile?.lastName, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + homeControlName: AddressForm.formElements(userProfile?.home), + officeControlName: AddressForm.formElements(userProfile?.office), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } final _logAddressForm = Logger.detached('AddressForm'); class AddressForm implements FormModel { - AddressForm( - this.form, - this.path, - ); + AddressForm(this.form, this.path, this._formModel) : initial = form.rawValue; static const String streetControlName = "street"; @@ -878,28 +965,32 @@ class AddressForm implements FormModel { final String? path; + // ignore: unused_field + final FormModel? _formModel; + final Map _disabled = {}; + @override + final Map initial; + String streetControlPath() => pathBuilder(streetControlName); String cityControlPath() => pathBuilder(cityControlName); String zipControlPath() => pathBuilder(zipControlName); - String? get _streetValue => streetControl.value; + String? get _streetValue => containsStreet ? streetControl.value : null; - String? get _cityValue => cityControl.value; + String? get _cityValue => containsCity ? cityControl.value : null; - String? get _zipValue => zipControl.value; + String? get _zipValue => containsZip ? zipControl.value : null; - String? get _streetRawValue => streetControl.value; + String? get _streetRawValue => containsStreet ? streetControl.value : null; - String? get _cityRawValue => cityControl.value; + String? get _cityRawValue => containsCity ? cityControl.value : null; - String? get _zipRawValue => zipControl.value; + String? get _zipRawValue => containsZip ? zipControl.value : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsStreet { try { form.control(streetControlPath()); @@ -909,8 +1000,6 @@ class AddressForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsCity { try { form.control(cityControlPath()); @@ -920,8 +1009,6 @@ class AddressForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsZip { try { form.control(zipControlPath()); @@ -943,12 +1030,7 @@ class AddressForm implements FormModel { void get zipFocus => form.focus(zipControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void streetRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void streetRemove({bool updateParent = true, bool emitEvent = true}) { if (containsStreet) { final controlPath = path; if (controlPath == null) { @@ -971,12 +1053,7 @@ class AddressForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void cityRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void cityRemove({bool updateParent = true, bool emitEvent = true}) { if (containsCity) { final controlPath = path; if (controlPath == null) { @@ -999,12 +1076,7 @@ class AddressForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void zipRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void zipRemove({bool updateParent = true, bool emitEvent = true}) { if (containsZip) { final controlPath = path; if (controlPath == null) { @@ -1032,8 +1104,11 @@ class AddressForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - streetControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + streetControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void cityValueUpdate( @@ -1041,8 +1116,11 @@ class AddressForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - cityControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + cityControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void zipValueUpdate( @@ -1050,8 +1128,11 @@ class AddressForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - zipControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + zipControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void streetValuePatch( @@ -1059,8 +1140,11 @@ class AddressForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - streetControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + streetControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void cityValuePatch( @@ -1068,8 +1152,11 @@ class AddressForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - cityControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + cityControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void zipValuePatch( @@ -1077,8 +1164,11 @@ class AddressForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - zipControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + zipControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void streetValueReset( @@ -1087,14 +1177,13 @@ class AddressForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - streetControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => streetControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void cityValueReset( String? value, { @@ -1102,14 +1191,13 @@ class AddressForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - cityControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => cityControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void zipValueReset( String? value, { @@ -1117,14 +1205,13 @@ class AddressForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - zipControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => zipControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get streetControl => form.control(streetControlPath()) as FormControl; @@ -1206,31 +1293,27 @@ class AddressForm implements FormModel { @override Address get rawModel { return Address( - street: _streetRawValue, city: _cityRawValue, zip: _zipRawValue); + street: _streetRawValue, + city: _cityRawValue, + zip: _zipRawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { - final currentFormInstance = currentForm; - - if (currentFormInstance is! FormGroup) { - return; - } - + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { - currentFormInstance.controls.forEach((key, control) { + currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { - currentFormInstance.controls[key]?.markAsEnabled( + currentForm.controls[key]?.markAsEnabled( updateParent: updateParent, emitEvent: emitEvent, ); @@ -1246,9 +1329,7 @@ class AddressForm implements FormModel { final currentForm = this.currentForm; return const DeepCollectionEquality().equals( - currentForm is FormControlCollection - ? currentForm.rawValue - : currentForm.value, + currentForm.rawValue, AddressForm.formElements(other).rawValue, ); } @@ -1269,8 +1350,16 @@ class AddressForm implements FormModel { } @override - AbstractControl get currentForm { - return path == null ? form : form.control(path!); + bool get hasChanged { + return !const DeepCollectionEquality().equals( + currentForm.rawValue, + initial, + ); + } + + @override + FormGroup get currentForm { + return path == null ? form : form.control(path!) as FormGroup; } @override @@ -1278,51 +1367,118 @@ class AddressForm implements FormModel { Address? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(AddressForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + AddressForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void upsertValue( + Address? value, { + bool updateParent = true, + bool emitEvent = true, + }) { + final formElements = AddressForm.formElements(value); + + currentForm.addAll(formElements.controls); + } @override void reset({ Address? value, bool updateParent = true, bool emitEvent = true, - }) => - form.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); + + @override + void updateInitial(Map? value, String? path) { + if (_formModel != null) { + _formModel?.updateInitial(currentForm.rawValue, path); + return; + } + + if (value == null) return; + + if (path == null || path.isEmpty) { + initial.addAll(value); + return; + } + + final keys = path.split('.'); + Object? current = initial; + for (var i = 0; i < keys.length - 1; i++) { + final key = keys[i]; + + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current = current[index]; + continue; + } + } + + if (current is Map) { + if (!current.containsKey(key)) { + current[key] = {}; + } + current = current[key]; + continue; + } + + return; + } + + final key = keys.last; + if (current is List) { + final index = int.tryParse(key); + if (index != null && index >= 0 && index < current.length) { + current[index] = value; + } + } else if (current is Map) { + current[key] = value; + } + } String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(Address? address) => FormGroup({ - streetControlName: FormControl( - value: address?.street, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - cityControlName: FormControl( - value: address?.city, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - zipControlName: FormControl( - value: address?.zip, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(Address? address) => FormGroup( + { + streetControlName: FormControl( + value: address?.street, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + cityControlName: FormControl( + value: address?.city, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + zipControlName: FormControl( + value: address?.zip, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } class ReactiveUserProfileFormArrayBuilder @@ -1335,29 +1491,41 @@ class ReactiveUserProfileFormArrayBuilder required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - UserProfileForm formModel)? control; + UserProfileForm formModel, + )? + control; - final Widget Function(BuildContext context, List itemList, - UserProfileForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + UserProfileForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveUserProfileFormArrayBuilderT? item, - UserProfileForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveUserProfileFormArrayBuilderT? item, + UserProfileForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -1373,18 +1541,9 @@ class ReactiveUserProfileFormArrayBuilder return ReactiveFormArrayItemBuilder( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1402,35 +1561,43 @@ class ReactiveUserProfileFormArrayBuilder2 required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - UserProfileForm formModel)? control; + UserProfileForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - UserProfileForm formModel - }) params)? builder; + ({BuildContext context, List itemList, UserProfileForm formModel}) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveUserProfileFormArrayBuilderT? item, - UserProfileForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveUserProfileFormArrayBuilderT? item, + UserProfileForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -1447,23 +1614,17 @@ class ReactiveUserProfileFormArrayBuilder2 formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1472,32 +1633,48 @@ class ReactiveUserProfileFormArrayBuilder2 } class ReactiveUserProfileFormFormGroupArrayBuilder< - ReactiveUserProfileFormFormGroupArrayBuilderT> extends StatelessWidget { + ReactiveUserProfileFormFormGroupArrayBuilderT +> + extends StatelessWidget { const ReactiveUserProfileFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(UserProfileForm formModel)? + getExtended; - final ExtendedControl?>, - List>? extended; - - final ExtendedControl?>, - List> - Function(UserProfileForm formModel)? getExtended; - - final Widget Function(BuildContext context, List itemList, - UserProfileForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + UserProfileForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveUserProfileFormFormGroupArrayBuilderT? item, - UserProfileForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveUserProfileFormFormGroupArrayBuilderT? item, + UserProfileForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -1515,23 +1692,14 @@ class ReactiveUserProfileFormFormGroupArrayBuilder< final itemList = (value.value() ?? []) .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) .values .toList(); - return builder?.call( - context, - itemList, - formModel, - ) ?? + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/reactive_forms_generator/CHANGELOG.md b/packages/reactive_forms_generator/CHANGELOG.md index 787733a8..44cd59eb 100644 --- a/packages/reactive_forms_generator/CHANGELOG.md +++ b/packages/reactive_forms_generator/CHANGELOG.md @@ -1,3 +1,7 @@ +## [7.5.0-beta1] + +* properly handle missing controls in form + ## [7.5.0-beta0] * Beta experiments diff --git a/packages/reactive_forms_generator/example/lib/docs/animated_url_list/animated_url_list.gform.dart b/packages/reactive_forms_generator/example/lib/docs/animated_url_list/animated_url_list.gform.dart index 98fc8874..ca81c12d 100644 --- a/packages/reactive_forms_generator/example/lib/docs/animated_url_list/animated_url_list.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/animated_url_list/animated_url_list.gform.dart @@ -19,8 +19,11 @@ class ReactiveAnimatedUrlListFormConsumer extends StatelessWidget { final Widget? child; final Widget Function( - BuildContext context, AnimatedUrlListForm formModel, Widget? child) - builder; + BuildContext context, + AnimatedUrlListForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -39,11 +42,7 @@ class AnimatedUrlListFormInheritedStreamer extends InheritedStreamer { required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final AnimatedUrlListForm form; } @@ -65,19 +64,19 @@ class ReactiveAnimatedUrlListForm extends StatelessWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - static AnimatedUrlListForm? of( - BuildContext context, { - bool listen = true, - }) { + static AnimatedUrlListForm? of(BuildContext context, {bool listen = true}) { if (listen) { return context .dependOnInheritedWidgetOfExactType< - AnimatedUrlListFormInheritedStreamer>() + AnimatedUrlListFormInheritedStreamer + >() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - AnimatedUrlListFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + AnimatedUrlListFormInheritedStreamer + >(); return element == null ? null : (element.widget as AnimatedUrlListFormInheritedStreamer).form; @@ -125,11 +124,14 @@ class AnimatedUrlListFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, AnimatedUrlListForm formModel, Widget? child) - builder; + BuildContext context, + AnimatedUrlListForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, AnimatedUrlListForm formModel)? - initState; + initState; @override _AnimatedUrlListFormBuilderState createState() => @@ -145,7 +147,10 @@ class _AnimatedUrlListFormBuilderState @override void initState() { _formModel = AnimatedUrlListForm( - AnimatedUrlListForm.formElements(widget.model), null, null); + AnimatedUrlListForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -223,11 +228,8 @@ final _logAnimatedUrlListForm = Logger.detached('AnimatedUrlListForm'); class AnimatedUrlListForm implements FormModel { - AnimatedUrlListForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + AnimatedUrlListForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String urlListControlName = "urlList"; @@ -235,7 +237,7 @@ class AnimatedUrlListForm final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -251,8 +253,6 @@ class AnimatedUrlListForm List get _urlListRawValue => urlListUrlEntityForm.map((e) => e.rawModel).toList(); - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsUrlList { try { form.control(urlListControlPath()); @@ -294,15 +294,19 @@ class AnimatedUrlListForm if (toUpdate.isNotEmpty) { urlListControl.updateValue( - toUpdate.map((e) => UrlEntityForm.formElements(e).rawValue).toList(), - updateParent: updateParent, - emitEvent: emitEvent); + toUpdate.map((e) => UrlEntityForm.formElements(e).rawValue).toList(), + updateParent: updateParent, + emitEvent: emitEvent, + ); } if (toAdd.isNotEmpty) { toAdd.forEach((e) { - urlListControl.add(UrlEntityForm.formElements(e), - updateParent: updateParent, emitEvent: emitEvent); + urlListControl.add( + UrlEntityForm.formElements(e), + updateParent: updateParent, + emitEvent: emitEvent, + ); }); } } @@ -327,10 +331,7 @@ class AnimatedUrlListForm ); } - void urlListClear({ - bool updateParent = true, - bool emitEvent = true, - }) { + void urlListClear({bool updateParent = true, bool emitEvent = true}) { urlListUrlEntityForm.clear(); urlListControl.clear(updateParent: updateParent, emitEvent: emitEvent); } @@ -343,18 +344,17 @@ class AnimatedUrlListForm final keys = urlListUrlEntityForm.asMap().keys; final toPatch = []; - (value).asMap().forEach( - (k, v) { - if (keys.contains(k)) { - toPatch.add(v); - } - }, - ); + (value).asMap().forEach((k, v) { + if (keys.contains(k)) { + toPatch.add(v); + } + }); urlListControl.patchValue( - toPatch.map((e) => UrlEntityForm.formElements(e).rawValue).toList(), - updateParent: updateParent, - emitEvent: emitEvent); + toPatch.map((e) => UrlEntityForm.formElements(e).rawValue).toList(), + updateParent: updateParent, + emitEvent: emitEvent, + ); } void urlListValueReset( @@ -363,12 +363,11 @@ class AnimatedUrlListForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - urlListControl.reset( - value: - value.map((e) => UrlEntityForm.formElements(e).rawValue).toList(), - updateParent: updateParent, - emitEvent: emitEvent); + }) => urlListControl.reset( + value: value.map((e) => UrlEntityForm.formElements(e).rawValue).toList(), + updateParent: updateParent, + emitEvent: emitEvent, + ); FormArray> get urlListControl => form.control(urlListControlPath()) as FormArray>; @@ -378,8 +377,12 @@ class AnimatedUrlListForm return values .asMap() - .map((k, v) => MapEntry(k, - UrlEntityForm(form, pathBuilder("urlList.$k"), _formModel ?? this))) + .map( + (k, v) => MapEntry( + k, + UrlEntityForm(form, pathBuilder("urlList.$k"), _formModel ?? this), + ), + ) .values .toList(); } @@ -403,11 +406,11 @@ class AnimatedUrlListForm } ExtendedControl?>, List> - get urlListExtendedControl => - ExtendedControl?>, List>( - form.control(urlListControlPath()) - as FormArray>, - () => urlListUrlEntityForm); + get urlListExtendedControl => + ExtendedControl?>, List>( + form.control(urlListControlPath()) as FormArray>, + () => urlListUrlEntityForm, + ); void addUrlListItem(UrlEntity value) { urlListControl.add(UrlEntityForm.formElements(value)); @@ -443,10 +446,7 @@ class AnimatedUrlListForm } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; @@ -455,7 +455,9 @@ class AnimatedUrlListForm urlListUrlEntityForm.forEach((e) => e.toggleDisabled()); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { urlListUrlEntityForm.forEach((e) => e.toggleDisabled()); @@ -515,9 +517,11 @@ class AnimatedUrlListForm AnimatedUrlList? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(AnimatedUrlListForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + AnimatedUrlListForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -535,17 +539,14 @@ class AnimatedUrlListForm AnimatedUrlList? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -596,30 +597,30 @@ class AnimatedUrlListForm String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(AnimatedUrlList? animatedUrlList) => FormGroup({ - urlListControlName: FormArray( - (animatedUrlList?.urlList ?? []) - .map((e) => UrlEntityForm.formElements(e)) - .toList(), - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(AnimatedUrlList? animatedUrlList) => FormGroup( + { + urlListControlName: FormArray( + (animatedUrlList?.urlList ?? []) + .map((e) => UrlEntityForm.formElements(e)) + .toList(), + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } final _logUrlEntityForm = Logger.detached('UrlEntityForm'); class UrlEntityForm implements FormModel { - UrlEntityForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + UrlEntityForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String labelControlName = "label"; @@ -629,7 +630,7 @@ class UrlEntityForm implements FormModel { final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -649,8 +650,6 @@ class UrlEntityForm implements FormModel { String get _urlRawValue => urlControl.value ?? ""; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsLabel { try { form.control(labelControlPath()); @@ -660,8 +659,6 @@ class UrlEntityForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsUrl { try { form.control(urlControlPath()); @@ -684,8 +681,11 @@ class UrlEntityForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - labelControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + labelControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void urlValueUpdate( @@ -693,8 +693,11 @@ class UrlEntityForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - urlControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + urlControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void labelValuePatch( @@ -702,8 +705,11 @@ class UrlEntityForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - labelControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + labelControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void urlValuePatch( @@ -711,8 +717,11 @@ class UrlEntityForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - urlControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + urlControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void labelValueReset( @@ -721,14 +730,13 @@ class UrlEntityForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - labelControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => labelControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void urlValueReset( String value, { @@ -736,14 +744,13 @@ class UrlEntityForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - urlControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => urlControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get labelControl => form.control(labelControlPath()) as FormControl; @@ -807,17 +814,16 @@ class UrlEntityForm implements FormModel { } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { @@ -875,9 +881,11 @@ class UrlEntityForm implements FormModel { UrlEntity? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(UrlEntityForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + UrlEntityForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -895,17 +903,14 @@ class UrlEntityForm implements FormModel { UrlEntity? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -956,30 +961,36 @@ class UrlEntityForm implements FormModel { String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(UrlEntity? urlEntity) => FormGroup({ - labelControlName: FormControl( - value: urlEntity?.label, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - urlControlName: FormControl( - value: urlEntity?.url, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(UrlEntity? urlEntity) => FormGroup( + { + labelControlName: FormControl( + value: urlEntity?.label, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + urlControlName: FormControl( + value: urlEntity?.url, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } class ReactiveAnimatedUrlListFormArrayBuilder< - ReactiveAnimatedUrlListFormArrayBuilderT> extends StatelessWidget { + ReactiveAnimatedUrlListFormArrayBuilderT +> + extends StatelessWidget { const ReactiveAnimatedUrlListFormArrayBuilder({ Key? key, this.control, @@ -988,30 +999,41 @@ class ReactiveAnimatedUrlListFormArrayBuilder< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - AnimatedUrlListForm formModel)? control; + AnimatedUrlListForm formModel, + )? + control; - final Widget Function(BuildContext context, List itemList, - AnimatedUrlListForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + AnimatedUrlListForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveAnimatedUrlListFormArrayBuilderT? item, - AnimatedUrlListForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveAnimatedUrlListFormArrayBuilderT? item, + AnimatedUrlListForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -1025,21 +1047,13 @@ class ReactiveAnimatedUrlListFormArrayBuilder< final itemBuilder = this.itemBuilder; return ReactiveFormArrayItemBuilder< - ReactiveAnimatedUrlListFormArrayBuilderT>( + ReactiveAnimatedUrlListFormArrayBuilderT + >( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1048,7 +1062,9 @@ class ReactiveAnimatedUrlListFormArrayBuilder< } class ReactiveAnimatedUrlListFormArrayBuilder2< - ReactiveAnimatedUrlListFormArrayBuilderT> extends StatelessWidget { + ReactiveAnimatedUrlListFormArrayBuilderT +> + extends StatelessWidget { const ReactiveAnimatedUrlListFormArrayBuilder2({ Key? key, this.control, @@ -1057,36 +1073,47 @@ class ReactiveAnimatedUrlListFormArrayBuilder2< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - AnimatedUrlListForm formModel)? control; + AnimatedUrlListForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - AnimatedUrlListForm formModel - }) params)? builder; + ({ + BuildContext context, + List itemList, + AnimatedUrlListForm formModel, + }) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveAnimatedUrlListFormArrayBuilderT? item, - AnimatedUrlListForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveAnimatedUrlListFormArrayBuilderT? item, + AnimatedUrlListForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -1100,27 +1127,22 @@ class ReactiveAnimatedUrlListFormArrayBuilder2< final itemBuilder = this.itemBuilder; return ReactiveFormArrayItemBuilder< - ReactiveAnimatedUrlListFormArrayBuilderT>( + ReactiveAnimatedUrlListFormArrayBuilderT + >( formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1129,32 +1151,48 @@ class ReactiveAnimatedUrlListFormArrayBuilder2< } class ReactiveAnimatedUrlListFormFormGroupArrayBuilder< - ReactiveAnimatedUrlListFormFormGroupArrayBuilderT> extends StatelessWidget { + ReactiveAnimatedUrlListFormFormGroupArrayBuilderT +> + extends StatelessWidget { const ReactiveAnimatedUrlListFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? extended; - - final ExtendedControl?>, - List> - Function(AnimatedUrlListForm formModel)? getExtended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(AnimatedUrlListForm formModel)? + getExtended; - final Widget Function(BuildContext context, List itemList, - AnimatedUrlListForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + AnimatedUrlListForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveAnimatedUrlListFormFormGroupArrayBuilderT? item, - AnimatedUrlListForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveAnimatedUrlListFormFormGroupArrayBuilderT? item, + AnimatedUrlListForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -1169,26 +1207,18 @@ class ReactiveAnimatedUrlListFormFormGroupArrayBuilder< return StreamBuilder?>?>( stream: value.control.valueChanges, builder: (context, snapshot) { - final itemList = (value.value() ?? - []) - .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) - .values - .toList(); - - return builder?.call( - context, - itemList, - formModel, - ) ?? + final itemList = + (value.value() ?? + []) + .asMap() + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) + .values + .toList(); + + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/reactive_forms_generator/example/lib/docs/animated_url_list/animated_url_list_output.gform.dart b/packages/reactive_forms_generator/example/lib/docs/animated_url_list/animated_url_list_output.gform.dart index ef337afa..200ffa9f 100644 --- a/packages/reactive_forms_generator/example/lib/docs/animated_url_list/animated_url_list_output.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/animated_url_list/animated_url_list_output.gform.dart @@ -19,8 +19,11 @@ class ReactiveAnimatedUrlLisOFormConsumer extends StatelessWidget { final Widget? child; final Widget Function( - BuildContext context, AnimatedUrlLisOForm formModel, Widget? child) - builder; + BuildContext context, + AnimatedUrlLisOForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -39,11 +42,7 @@ class AnimatedUrlLisOFormInheritedStreamer extends InheritedStreamer { required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final AnimatedUrlLisOForm form; } @@ -65,19 +64,19 @@ class ReactiveAnimatedUrlLisOForm extends StatelessWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - static AnimatedUrlLisOForm? of( - BuildContext context, { - bool listen = true, - }) { + static AnimatedUrlLisOForm? of(BuildContext context, {bool listen = true}) { if (listen) { return context .dependOnInheritedWidgetOfExactType< - AnimatedUrlLisOFormInheritedStreamer>() + AnimatedUrlLisOFormInheritedStreamer + >() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - AnimatedUrlLisOFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + AnimatedUrlLisOFormInheritedStreamer + >(); return element == null ? null : (element.widget as AnimatedUrlLisOFormInheritedStreamer).form; @@ -125,11 +124,14 @@ class AnimatedUrlLisOFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, AnimatedUrlLisOForm formModel, Widget? child) - builder; + BuildContext context, + AnimatedUrlLisOForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, AnimatedUrlLisOForm formModel)? - initState; + initState; @override _AnimatedUrlLisOFormBuilderState createState() => @@ -145,7 +147,10 @@ class _AnimatedUrlLisOFormBuilderState @override void initState() { _formModel = AnimatedUrlLisOForm( - AnimatedUrlLisOForm.formElements(widget.model), null, null); + AnimatedUrlLisOForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -223,11 +228,8 @@ final _logAnimatedUrlLisOForm = Logger.detached('AnimatedUrlLisOForm'); class AnimatedUrlLisOForm implements FormModel { - AnimatedUrlLisOForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + AnimatedUrlLisOForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String urlListControlName = "urlList"; @@ -235,7 +237,7 @@ class AnimatedUrlLisOForm final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -251,8 +253,6 @@ class AnimatedUrlLisOForm List get _urlListRawValue => urlListUrlEntityOForm.map((e) => e.rawModel).toList(); - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsUrlList { try { form.control(urlListControlPath()); @@ -294,15 +294,19 @@ class AnimatedUrlLisOForm if (toUpdate.isNotEmpty) { urlListControl.updateValue( - toUpdate.map((e) => UrlEntityOForm.formElements(e).rawValue).toList(), - updateParent: updateParent, - emitEvent: emitEvent); + toUpdate.map((e) => UrlEntityOForm.formElements(e).rawValue).toList(), + updateParent: updateParent, + emitEvent: emitEvent, + ); } if (toAdd.isNotEmpty) { toAdd.forEach((e) { - urlListControl.add(UrlEntityOForm.formElements(e), - updateParent: updateParent, emitEvent: emitEvent); + urlListControl.add( + UrlEntityOForm.formElements(e), + updateParent: updateParent, + emitEvent: emitEvent, + ); }); } } @@ -327,10 +331,7 @@ class AnimatedUrlLisOForm ); } - void urlListClear({ - bool updateParent = true, - bool emitEvent = true, - }) { + void urlListClear({bool updateParent = true, bool emitEvent = true}) { urlListUrlEntityOForm.clear(); urlListControl.clear(updateParent: updateParent, emitEvent: emitEvent); } @@ -343,18 +344,17 @@ class AnimatedUrlLisOForm final keys = urlListUrlEntityOForm.asMap().keys; final toPatch = []; - (value).asMap().forEach( - (k, v) { - if (keys.contains(k)) { - toPatch.add(v); - } - }, - ); + (value).asMap().forEach((k, v) { + if (keys.contains(k)) { + toPatch.add(v); + } + }); urlListControl.patchValue( - toPatch.map((e) => UrlEntityOForm.formElements(e).rawValue).toList(), - updateParent: updateParent, - emitEvent: emitEvent); + toPatch.map((e) => UrlEntityOForm.formElements(e).rawValue).toList(), + updateParent: updateParent, + emitEvent: emitEvent, + ); } void urlListValueReset( @@ -363,13 +363,11 @@ class AnimatedUrlLisOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - urlListControl.reset( - value: value - .map((e) => UrlEntityOForm.formElements(e).rawValue) - .toList(), - updateParent: updateParent, - emitEvent: emitEvent); + }) => urlListControl.reset( + value: value.map((e) => UrlEntityOForm.formElements(e).rawValue).toList(), + updateParent: updateParent, + emitEvent: emitEvent, + ); FormArray> get urlListControl => form.control(urlListControlPath()) as FormArray>; @@ -379,10 +377,12 @@ class AnimatedUrlLisOForm return values .asMap() - .map((k, v) => MapEntry( + .map( + (k, v) => MapEntry( k, - UrlEntityOForm( - form, pathBuilder("urlList.$k"), _formModel ?? this))) + UrlEntityOForm(form, pathBuilder("urlList.$k"), _formModel ?? this), + ), + ) .values .toList(); } @@ -406,11 +406,11 @@ class AnimatedUrlLisOForm } ExtendedControl?>, List> - get urlListExtendedControl => - ExtendedControl?>, List>( - form.control(urlListControlPath()) - as FormArray>, - () => urlListUrlEntityOForm); + get urlListExtendedControl => + ExtendedControl?>, List>( + form.control(urlListControlPath()) as FormArray>, + () => urlListUrlEntityOForm, + ); void addUrlListItem(UrlEntityO value) { urlListControl.add(UrlEntityOForm.formElements(value)); @@ -447,10 +447,7 @@ class AnimatedUrlLisOForm } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; @@ -459,7 +456,9 @@ class AnimatedUrlLisOForm urlListUrlEntityOForm.forEach((e) => e.toggleDisabled()); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { urlListUrlEntityOForm.forEach((e) => e.toggleDisabled()); @@ -519,9 +518,11 @@ class AnimatedUrlLisOForm AnimatedUrlLisO? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(AnimatedUrlLisOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + AnimatedUrlLisOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -539,17 +540,14 @@ class AnimatedUrlLisOForm AnimatedUrlLisO? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -600,30 +598,30 @@ class AnimatedUrlLisOForm String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(AnimatedUrlLisO? animatedUrlLisO) => FormGroup({ - urlListControlName: FormArray( - (animatedUrlLisO?.urlList ?? []) - .map((e) => UrlEntityOForm.formElements(e)) - .toList(), - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(AnimatedUrlLisO? animatedUrlLisO) => FormGroup( + { + urlListControlName: FormArray( + (animatedUrlLisO?.urlList ?? []) + .map((e) => UrlEntityOForm.formElements(e)) + .toList(), + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } final _logUrlEntityOForm = Logger.detached('UrlEntityOForm'); class UrlEntityOForm implements FormModel { - UrlEntityOForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + UrlEntityOForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String labelControlName = "label"; @@ -633,7 +631,7 @@ class UrlEntityOForm implements FormModel { final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -649,12 +647,10 @@ class UrlEntityOForm implements FormModel { String get _urlValue => urlControl.value as String; - String? get _labelRawValue => labelControl.value; + String? get _labelRawValue => containsLabel ? labelControl.value : null; - String? get _urlRawValue => urlControl.value; + String? get _urlRawValue => containsUrl ? urlControl.value : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsLabel { try { form.control(labelControlPath()); @@ -664,8 +660,6 @@ class UrlEntityOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsUrl { try { form.control(urlControlPath()); @@ -683,12 +677,7 @@ class UrlEntityOForm implements FormModel { void get urlFocus => form.focus(urlControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void labelRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void labelRemove({bool updateParent = true, bool emitEvent = true}) { if (containsLabel) { final controlPath = path; if (controlPath == null) { @@ -711,12 +700,7 @@ class UrlEntityOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void urlRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void urlRemove({bool updateParent = true, bool emitEvent = true}) { if (containsUrl) { final controlPath = path; if (controlPath == null) { @@ -744,8 +728,11 @@ class UrlEntityOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - labelControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + labelControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void urlValueUpdate( @@ -753,8 +740,11 @@ class UrlEntityOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - urlControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + urlControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void labelValuePatch( @@ -762,8 +752,11 @@ class UrlEntityOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - labelControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + labelControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void urlValuePatch( @@ -771,8 +764,11 @@ class UrlEntityOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - urlControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + urlControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void labelValueReset( @@ -781,14 +777,13 @@ class UrlEntityOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - labelControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => labelControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void urlValueReset( String? value, { @@ -796,14 +791,13 @@ class UrlEntityOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - urlControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => urlControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get labelControl => form.control(labelControlPath()) as FormControl; @@ -868,17 +862,16 @@ class UrlEntityOForm implements FormModel { } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { @@ -936,9 +929,11 @@ class UrlEntityOForm implements FormModel { UrlEntityO? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(UrlEntityOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + UrlEntityOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -956,17 +951,14 @@ class UrlEntityOForm implements FormModel { UrlEntityO? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -1017,31 +1009,36 @@ class UrlEntityOForm implements FormModel { String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(UrlEntityO? urlEntityO) => FormGroup({ - labelControlName: FormControl( - value: urlEntityO?.label, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - urlControlName: FormControl( - value: urlEntityO?.url, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(UrlEntityO? urlEntityO) => FormGroup( + { + labelControlName: FormControl( + value: urlEntityO?.label, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + urlControlName: FormControl( + value: urlEntityO?.url, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } @Rf(output: true) class AnimatedUrlLisOOutput { final List urlList; + AnimatedUrlLisOOutput({@RfArray() required this.urlList}); } @@ -1049,13 +1046,17 @@ class AnimatedUrlLisOOutput { class UrlEntityOOutput { final String label; final String url; - UrlEntityOOutput( - {@RfControl(validators: [RequiredValidator()]) required this.label, - @RfControl(validators: [RequiredValidator()]) required this.url}); + + UrlEntityOOutput({ + @RfControl(validators: [RequiredValidator()]) required this.label, + @RfControl(validators: [RequiredValidator()]) required this.url, + }); } class ReactiveAnimatedUrlLisOFormArrayBuilder< - ReactiveAnimatedUrlLisOFormArrayBuilderT> extends StatelessWidget { + ReactiveAnimatedUrlLisOFormArrayBuilderT +> + extends StatelessWidget { const ReactiveAnimatedUrlLisOFormArrayBuilder({ Key? key, this.control, @@ -1064,30 +1065,41 @@ class ReactiveAnimatedUrlLisOFormArrayBuilder< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - AnimatedUrlLisOForm formModel)? control; + AnimatedUrlLisOForm formModel, + )? + control; - final Widget Function(BuildContext context, List itemList, - AnimatedUrlLisOForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + AnimatedUrlLisOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveAnimatedUrlLisOFormArrayBuilderT? item, - AnimatedUrlLisOForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveAnimatedUrlLisOFormArrayBuilderT? item, + AnimatedUrlLisOForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -1101,21 +1113,13 @@ class ReactiveAnimatedUrlLisOFormArrayBuilder< final itemBuilder = this.itemBuilder; return ReactiveFormArrayItemBuilder< - ReactiveAnimatedUrlLisOFormArrayBuilderT>( + ReactiveAnimatedUrlLisOFormArrayBuilderT + >( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1124,7 +1128,9 @@ class ReactiveAnimatedUrlLisOFormArrayBuilder< } class ReactiveAnimatedUrlLisOFormArrayBuilder2< - ReactiveAnimatedUrlLisOFormArrayBuilderT> extends StatelessWidget { + ReactiveAnimatedUrlLisOFormArrayBuilderT +> + extends StatelessWidget { const ReactiveAnimatedUrlLisOFormArrayBuilder2({ Key? key, this.control, @@ -1133,36 +1139,47 @@ class ReactiveAnimatedUrlLisOFormArrayBuilder2< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - AnimatedUrlLisOForm formModel)? control; + AnimatedUrlLisOForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - AnimatedUrlLisOForm formModel - }) params)? builder; + ({ + BuildContext context, + List itemList, + AnimatedUrlLisOForm formModel, + }) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveAnimatedUrlLisOFormArrayBuilderT? item, - AnimatedUrlLisOForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveAnimatedUrlLisOFormArrayBuilderT? item, + AnimatedUrlLisOForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -1176,27 +1193,22 @@ class ReactiveAnimatedUrlLisOFormArrayBuilder2< final itemBuilder = this.itemBuilder; return ReactiveFormArrayItemBuilder< - ReactiveAnimatedUrlLisOFormArrayBuilderT>( + ReactiveAnimatedUrlLisOFormArrayBuilderT + >( formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1205,32 +1217,48 @@ class ReactiveAnimatedUrlLisOFormArrayBuilder2< } class ReactiveAnimatedUrlLisOFormFormGroupArrayBuilder< - ReactiveAnimatedUrlLisOFormFormGroupArrayBuilderT> extends StatelessWidget { + ReactiveAnimatedUrlLisOFormFormGroupArrayBuilderT +> + extends StatelessWidget { const ReactiveAnimatedUrlLisOFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? extended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(AnimatedUrlLisOForm formModel)? + getExtended; - final ExtendedControl?>, - List> - Function(AnimatedUrlLisOForm formModel)? getExtended; - - final Widget Function(BuildContext context, List itemList, - AnimatedUrlLisOForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + AnimatedUrlLisOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveAnimatedUrlLisOFormFormGroupArrayBuilderT? item, - AnimatedUrlLisOForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveAnimatedUrlLisOFormFormGroupArrayBuilderT? item, + AnimatedUrlLisOForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -1245,26 +1273,18 @@ class ReactiveAnimatedUrlLisOFormFormGroupArrayBuilder< return StreamBuilder?>?>( stream: value.control.valueChanges, builder: (context, snapshot) { - final itemList = (value.value() ?? - []) - .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) - .values - .toList(); - - return builder?.call( - context, - itemList, - formModel, - ) ?? + final itemList = + (value.value() ?? + []) + .asMap() + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) + .values + .toList(); + + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/reactive_forms_generator/example/lib/docs/annotateless/annotateless.gform.dart b/packages/reactive_forms_generator/example/lib/docs/annotateless/annotateless.gform.dart index 1cf5c852..6d3babe7 100644 --- a/packages/reactive_forms_generator/example/lib/docs/annotateless/annotateless.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/annotateless/annotateless.gform.dart @@ -19,7 +19,11 @@ class ReactiveAnnotatelessFormConsumer extends StatelessWidget { final Widget? child; final Widget Function( - BuildContext context, AnnotatelessForm formModel, Widget? child) builder; + BuildContext context, + AnnotatelessForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -38,11 +42,7 @@ class AnnotatelessFormInheritedStreamer extends InheritedStreamer { required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final AnnotatelessForm form; } @@ -64,19 +64,19 @@ class ReactiveAnnotatelessForm extends StatelessWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - static AnnotatelessForm? of( - BuildContext context, { - bool listen = true, - }) { + static AnnotatelessForm? of(BuildContext context, {bool listen = true}) { if (listen) { return context .dependOnInheritedWidgetOfExactType< - AnnotatelessFormInheritedStreamer>() + AnnotatelessFormInheritedStreamer + >() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - AnnotatelessFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + AnnotatelessFormInheritedStreamer + >(); return element == null ? null : (element.widget as AnnotatelessFormInheritedStreamer).form; @@ -124,10 +124,14 @@ class AnnotatelessFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, AnnotatelessForm formModel, Widget? child) builder; + BuildContext context, + AnnotatelessForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, AnnotatelessForm formModel)? - initState; + initState; @override _AnnotatelessFormBuilderState createState() => @@ -142,7 +146,10 @@ class _AnnotatelessFormBuilderState extends State { @override void initState() { _formModel = AnnotatelessForm( - AnnotatelessForm.formElements(widget.model), null, null); + AnnotatelessForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -219,11 +226,8 @@ class _AnnotatelessFormBuilderState extends State { final _logAnnotatelessForm = Logger.detached('AnnotatelessForm'); class AnnotatelessForm implements FormModel { - AnnotatelessForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + AnnotatelessForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String emailControlName = "email"; @@ -233,7 +237,7 @@ class AnnotatelessForm implements FormModel { final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -253,8 +257,6 @@ class AnnotatelessForm implements FormModel { String get _passwordRawValue => passwordControl.value ?? ""; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsEmail { try { form.control(emailControlPath()); @@ -264,8 +266,6 @@ class AnnotatelessForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsPassword { try { form.control(passwordControlPath()); @@ -288,8 +288,11 @@ class AnnotatelessForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - emailControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void passwordValueUpdate( @@ -297,8 +300,11 @@ class AnnotatelessForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - passwordControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + passwordControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailValuePatch( @@ -306,8 +312,11 @@ class AnnotatelessForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - emailControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void passwordValuePatch( @@ -315,8 +324,11 @@ class AnnotatelessForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - passwordControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + passwordControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailValueReset( @@ -325,14 +337,13 @@ class AnnotatelessForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - emailControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => emailControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void passwordValueReset( String value, { @@ -340,14 +351,13 @@ class AnnotatelessForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - passwordControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => passwordControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get emailControl => form.control(emailControlPath()) as FormControl; @@ -411,17 +421,16 @@ class AnnotatelessForm implements FormModel { } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { @@ -479,9 +488,11 @@ class AnnotatelessForm implements FormModel { Annotateless? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(AnnotatelessForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + AnnotatelessForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -499,17 +510,14 @@ class AnnotatelessForm implements FormModel { Annotateless? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -560,30 +568,36 @@ class AnnotatelessForm implements FormModel { String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(Annotateless? annotateless) => FormGroup({ - emailControlName: FormControl( - value: annotateless?.email, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - passwordControlName: FormControl( - value: annotateless?.password, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(Annotateless? annotateless) => FormGroup( + { + emailControlName: FormControl( + value: annotateless?.email, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + passwordControlName: FormControl( + value: annotateless?.password, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } class ReactiveAnnotatelessFormArrayBuilder< - ReactiveAnnotatelessFormArrayBuilderT> extends StatelessWidget { + ReactiveAnnotatelessFormArrayBuilderT +> + extends StatelessWidget { const ReactiveAnnotatelessFormArrayBuilder({ Key? key, this.control, @@ -592,30 +606,41 @@ class ReactiveAnnotatelessFormArrayBuilder< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - AnnotatelessForm formModel)? control; + AnnotatelessForm formModel, + )? + control; - final Widget Function(BuildContext context, List itemList, - AnnotatelessForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + AnnotatelessForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveAnnotatelessFormArrayBuilderT? item, - AnnotatelessForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveAnnotatelessFormArrayBuilderT? item, + AnnotatelessForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -631,18 +656,9 @@ class ReactiveAnnotatelessFormArrayBuilder< return ReactiveFormArrayItemBuilder( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -651,7 +667,9 @@ class ReactiveAnnotatelessFormArrayBuilder< } class ReactiveAnnotatelessFormArrayBuilder2< - ReactiveAnnotatelessFormArrayBuilderT> extends StatelessWidget { + ReactiveAnnotatelessFormArrayBuilderT +> + extends StatelessWidget { const ReactiveAnnotatelessFormArrayBuilder2({ Key? key, this.control, @@ -660,36 +678,43 @@ class ReactiveAnnotatelessFormArrayBuilder2< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - AnnotatelessForm formModel)? control; + AnnotatelessForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - AnnotatelessForm formModel - }) params)? builder; + ({BuildContext context, List itemList, AnnotatelessForm formModel}) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveAnnotatelessFormArrayBuilderT? item, - AnnotatelessForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveAnnotatelessFormArrayBuilderT? item, + AnnotatelessForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -706,23 +731,17 @@ class ReactiveAnnotatelessFormArrayBuilder2< formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -731,32 +750,48 @@ class ReactiveAnnotatelessFormArrayBuilder2< } class ReactiveAnnotatelessFormFormGroupArrayBuilder< - ReactiveAnnotatelessFormFormGroupArrayBuilderT> extends StatelessWidget { + ReactiveAnnotatelessFormFormGroupArrayBuilderT +> + extends StatelessWidget { const ReactiveAnnotatelessFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? extended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(AnnotatelessForm formModel)? + getExtended; - final ExtendedControl?>, - List> - Function(AnnotatelessForm formModel)? getExtended; - - final Widget Function(BuildContext context, List itemList, - AnnotatelessForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + AnnotatelessForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveAnnotatelessFormFormGroupArrayBuilderT? item, - AnnotatelessForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveAnnotatelessFormFormGroupArrayBuilderT? item, + AnnotatelessForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -771,26 +806,18 @@ class ReactiveAnnotatelessFormFormGroupArrayBuilder< return StreamBuilder?>?>( stream: value.control.valueChanges, builder: (context, snapshot) { - final itemList = (value.value() ?? - []) - .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) - .values - .toList(); - - return builder?.call( - context, - itemList, - formModel, - ) ?? + final itemList = + (value.value() ?? + []) + .asMap() + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) + .values + .toList(); + + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/reactive_forms_generator/example/lib/docs/annotateless/annotateless_output.gform.dart b/packages/reactive_forms_generator/example/lib/docs/annotateless/annotateless_output.gform.dart index 5297b65f..b7f6707a 100644 --- a/packages/reactive_forms_generator/example/lib/docs/annotateless/annotateless_output.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/annotateless/annotateless_output.gform.dart @@ -19,7 +19,11 @@ class ReactiveAnnotatelessOFormConsumer extends StatelessWidget { final Widget? child; final Widget Function( - BuildContext context, AnnotatelessOForm formModel, Widget? child) builder; + BuildContext context, + AnnotatelessOForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -38,11 +42,7 @@ class AnnotatelessOFormInheritedStreamer extends InheritedStreamer { required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final AnnotatelessOForm form; } @@ -64,19 +64,19 @@ class ReactiveAnnotatelessOForm extends StatelessWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - static AnnotatelessOForm? of( - BuildContext context, { - bool listen = true, - }) { + static AnnotatelessOForm? of(BuildContext context, {bool listen = true}) { if (listen) { return context .dependOnInheritedWidgetOfExactType< - AnnotatelessOFormInheritedStreamer>() + AnnotatelessOFormInheritedStreamer + >() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - AnnotatelessOFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + AnnotatelessOFormInheritedStreamer + >(); return element == null ? null : (element.widget as AnnotatelessOFormInheritedStreamer).form; @@ -124,10 +124,14 @@ class AnnotatelessOFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, AnnotatelessOForm formModel, Widget? child) builder; + BuildContext context, + AnnotatelessOForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, AnnotatelessOForm formModel)? - initState; + initState; @override _AnnotatelessOFormBuilderState createState() => @@ -142,7 +146,10 @@ class _AnnotatelessOFormBuilderState extends State { @override void initState() { _formModel = AnnotatelessOForm( - AnnotatelessOForm.formElements(widget.model), null, null); + AnnotatelessOForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -220,11 +227,8 @@ final _logAnnotatelessOForm = Logger.detached('AnnotatelessOForm'); class AnnotatelessOForm implements FormModel { - AnnotatelessOForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + AnnotatelessOForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String emailControlName = "email"; @@ -234,7 +238,7 @@ class AnnotatelessOForm final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -254,8 +258,6 @@ class AnnotatelessOForm String get _passwordRawValue => passwordControl.value ?? ""; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsEmail { try { form.control(emailControlPath()); @@ -265,8 +267,6 @@ class AnnotatelessOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsPassword { try { form.control(passwordControlPath()); @@ -289,8 +289,11 @@ class AnnotatelessOForm bool updateParent = true, bool emitEvent = true, }) { - emailControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void passwordValueUpdate( @@ -298,8 +301,11 @@ class AnnotatelessOForm bool updateParent = true, bool emitEvent = true, }) { - passwordControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + passwordControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailValuePatch( @@ -307,8 +313,11 @@ class AnnotatelessOForm bool updateParent = true, bool emitEvent = true, }) { - emailControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void passwordValuePatch( @@ -316,8 +325,11 @@ class AnnotatelessOForm bool updateParent = true, bool emitEvent = true, }) { - passwordControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + passwordControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailValueReset( @@ -326,14 +338,13 @@ class AnnotatelessOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - emailControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => emailControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void passwordValueReset( String value, { @@ -341,14 +352,13 @@ class AnnotatelessOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - passwordControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => passwordControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get emailControl => form.control(emailControlPath()) as FormControl; @@ -413,17 +423,16 @@ class AnnotatelessOForm } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { @@ -481,9 +490,11 @@ class AnnotatelessOForm AnnotatelessO? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(AnnotatelessOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + AnnotatelessOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -501,17 +512,14 @@ class AnnotatelessOForm AnnotatelessO? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -562,26 +570,30 @@ class AnnotatelessOForm String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(AnnotatelessO? annotatelessO) => FormGroup({ - emailControlName: FormControl( - value: annotatelessO?.email, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - passwordControlName: FormControl( - value: annotatelessO?.password, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(AnnotatelessO? annotatelessO) => FormGroup( + { + emailControlName: FormControl( + value: annotatelessO?.email, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + passwordControlName: FormControl( + value: annotatelessO?.password, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } @Rf(output: true) @@ -594,7 +606,9 @@ class AnnotatelessOOutput extends Equatable { } class ReactiveAnnotatelessOFormArrayBuilder< - ReactiveAnnotatelessOFormArrayBuilderT> extends StatelessWidget { + ReactiveAnnotatelessOFormArrayBuilderT +> + extends StatelessWidget { const ReactiveAnnotatelessOFormArrayBuilder({ Key? key, this.control, @@ -603,30 +617,41 @@ class ReactiveAnnotatelessOFormArrayBuilder< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - AnnotatelessOForm formModel)? control; + AnnotatelessOForm formModel, + )? + control; - final Widget Function(BuildContext context, List itemList, - AnnotatelessOForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + AnnotatelessOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveAnnotatelessOFormArrayBuilderT? item, - AnnotatelessOForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveAnnotatelessOFormArrayBuilderT? item, + AnnotatelessOForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -642,18 +667,9 @@ class ReactiveAnnotatelessOFormArrayBuilder< return ReactiveFormArrayItemBuilder( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -662,7 +678,9 @@ class ReactiveAnnotatelessOFormArrayBuilder< } class ReactiveAnnotatelessOFormArrayBuilder2< - ReactiveAnnotatelessOFormArrayBuilderT> extends StatelessWidget { + ReactiveAnnotatelessOFormArrayBuilderT +> + extends StatelessWidget { const ReactiveAnnotatelessOFormArrayBuilder2({ Key? key, this.control, @@ -671,36 +689,43 @@ class ReactiveAnnotatelessOFormArrayBuilder2< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - AnnotatelessOForm formModel)? control; + AnnotatelessOForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - AnnotatelessOForm formModel - }) params)? builder; + ({BuildContext context, List itemList, AnnotatelessOForm formModel}) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveAnnotatelessOFormArrayBuilderT? item, - AnnotatelessOForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveAnnotatelessOFormArrayBuilderT? item, + AnnotatelessOForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -717,23 +742,17 @@ class ReactiveAnnotatelessOFormArrayBuilder2< formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -742,32 +761,48 @@ class ReactiveAnnotatelessOFormArrayBuilder2< } class ReactiveAnnotatelessOFormFormGroupArrayBuilder< - ReactiveAnnotatelessOFormFormGroupArrayBuilderT> extends StatelessWidget { + ReactiveAnnotatelessOFormFormGroupArrayBuilderT +> + extends StatelessWidget { const ReactiveAnnotatelessOFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? extended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(AnnotatelessOForm formModel)? + getExtended; - final ExtendedControl?>, - List> - Function(AnnotatelessOForm formModel)? getExtended; - - final Widget Function(BuildContext context, List itemList, - AnnotatelessOForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + AnnotatelessOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveAnnotatelessOFormFormGroupArrayBuilderT? item, - AnnotatelessOForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveAnnotatelessOFormFormGroupArrayBuilderT? item, + AnnotatelessOForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -782,26 +817,18 @@ class ReactiveAnnotatelessOFormFormGroupArrayBuilder< return StreamBuilder?>?>( stream: value.control.valueChanges, builder: (context, snapshot) { - final itemList = (value.value() ?? - []) - .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) - .values - .toList(); - - return builder?.call( - context, - itemList, - formModel, - ) ?? + final itemList = + (value.value() ?? + []) + .asMap() + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) + .values + .toList(); + + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/reactive_forms_generator/example/lib/docs/array_nullable/array_nullable.gform.dart b/packages/reactive_forms_generator/example/lib/docs/array_nullable/array_nullable.gform.dart index 9e7cf02e..79c4d7fd 100644 --- a/packages/reactive_forms_generator/example/lib/docs/array_nullable/array_nullable.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/array_nullable/array_nullable.gform.dart @@ -19,7 +19,11 @@ class ReactiveArrayNullableFormConsumer extends StatelessWidget { final Widget? child; final Widget Function( - BuildContext context, ArrayNullableForm formModel, Widget? child) builder; + BuildContext context, + ArrayNullableForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -38,11 +42,7 @@ class ArrayNullableFormInheritedStreamer extends InheritedStreamer { required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final ArrayNullableForm form; } @@ -64,19 +64,19 @@ class ReactiveArrayNullableForm extends StatelessWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - static ArrayNullableForm? of( - BuildContext context, { - bool listen = true, - }) { + static ArrayNullableForm? of(BuildContext context, {bool listen = true}) { if (listen) { return context .dependOnInheritedWidgetOfExactType< - ArrayNullableFormInheritedStreamer>() + ArrayNullableFormInheritedStreamer + >() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - ArrayNullableFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + ArrayNullableFormInheritedStreamer + >(); return element == null ? null : (element.widget as ArrayNullableFormInheritedStreamer).form; @@ -124,10 +124,14 @@ class ArrayNullableFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, ArrayNullableForm formModel, Widget? child) builder; + BuildContext context, + ArrayNullableForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, ArrayNullableForm formModel)? - initState; + initState; @override _ArrayNullableFormBuilderState createState() => @@ -142,7 +146,10 @@ class _ArrayNullableFormBuilderState extends State { @override void initState() { _formModel = ArrayNullableForm( - ArrayNullableForm.formElements(widget.model), null, null); + ArrayNullableForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -219,11 +226,8 @@ class _ArrayNullableFormBuilderState extends State { final _logArrayNullableForm = Logger.detached('ArrayNullableForm'); class ArrayNullableForm implements FormModel { - ArrayNullableForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + ArrayNullableForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String emailListControlName = "emailList"; @@ -241,7 +245,7 @@ class ArrayNullableForm implements FormModel { final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -262,7 +266,8 @@ class ArrayNullableForm implements FormModel { String modeListControlPath() => pathBuilder(modeListControlName); - List? get _someListValue => someListControl.value; + List? get _someListValue => + containsSomeList ? someListControl.value : null; List get _someListRequiredValue => someListRequiredControl.value ?? const []; @@ -273,13 +278,16 @@ class ArrayNullableForm implements FormModel { List get _fruitListValue => fruitListControl.rawValue.whereType().toList(); - List? get _vegetablesListValue => - vegetablesListControl.rawValue.whereType().toList(); + List? get _vegetablesListValue => containsVegetablesList + ? vegetablesListControl.rawValue.whereType().toList() + : null; - List? get _modeListValue => - modeListControl.rawValue.whereType().toList(); + List? get _modeListValue => containsModeList + ? modeListControl.rawValue.whereType().toList() + : null; - List? get _someListRawValue => someListControl.value; + List? get _someListRawValue => + containsSomeList ? someListControl.value : null; List get _someListRequiredRawValue => someListRequiredControl.value ?? const []; @@ -290,14 +298,14 @@ class ArrayNullableForm implements FormModel { List get _fruitListRawValue => fruitListControl.rawValue.whereType().toList(); - List? get _vegetablesListRawValue => - vegetablesListControl.rawValue.whereType().toList(); + List? get _vegetablesListRawValue => containsVegetablesList + ? vegetablesListControl.rawValue.whereType().toList() + : null; - List? get _modeListRawValue => - modeListControl.rawValue.whereType().toList(); + List? get _modeListRawValue => containsModeList + ? modeListControl.rawValue.whereType().toList() + : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsSomeList { try { form.control(someListControlPath()); @@ -307,8 +315,6 @@ class ArrayNullableForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsSomeListRequired { try { form.control(someListRequiredControlPath()); @@ -318,8 +324,6 @@ class ArrayNullableForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsEmailList { try { form.control(emailListControlPath()); @@ -329,8 +333,6 @@ class ArrayNullableForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsFruitList { try { form.control(fruitListControlPath()); @@ -340,8 +342,6 @@ class ArrayNullableForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsVegetablesList { try { form.control(vegetablesListControlPath()); @@ -351,8 +351,6 @@ class ArrayNullableForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsModeList { try { form.control(modeListControlPath()); @@ -387,12 +385,7 @@ class ArrayNullableForm implements FormModel { void get modeListFocus => form.focus(modeListControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void someListRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void someListRemove({bool updateParent = true, bool emitEvent = true}) { if (containsSomeList) { final controlPath = path; if (controlPath == null) { @@ -415,12 +408,7 @@ class ArrayNullableForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void vegetablesListRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void vegetablesListRemove({bool updateParent = true, bool emitEvent = true}) { if (containsVegetablesList) { final controlPath = path; if (controlPath == null) { @@ -443,12 +431,7 @@ class ArrayNullableForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void modeListRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void modeListRemove({bool updateParent = true, bool emitEvent = true}) { if (containsModeList) { final controlPath = path; if (controlPath == null) { @@ -476,8 +459,11 @@ class ArrayNullableForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - someListControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + someListControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void someListRequiredValueUpdate( @@ -485,8 +471,11 @@ class ArrayNullableForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - someListRequiredControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + someListRequiredControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailListValueUpdate( @@ -494,8 +483,11 @@ class ArrayNullableForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - emailListControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailListControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void fruitListValueUpdate( @@ -503,8 +495,11 @@ class ArrayNullableForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - fruitListControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + fruitListControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void vegetablesListValueUpdate( @@ -512,8 +507,11 @@ class ArrayNullableForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - vegetablesListControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + vegetablesListControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void modeListValueUpdate( @@ -521,8 +519,11 @@ class ArrayNullableForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - modeListControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + modeListControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void someListValuePatch( @@ -530,8 +531,11 @@ class ArrayNullableForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - someListControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + someListControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void someListRequiredValuePatch( @@ -539,8 +543,11 @@ class ArrayNullableForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - someListRequiredControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + someListRequiredControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailListValuePatch( @@ -548,8 +555,11 @@ class ArrayNullableForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - emailListControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailListControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void fruitListValuePatch( @@ -557,8 +567,11 @@ class ArrayNullableForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - fruitListControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + fruitListControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void vegetablesListValuePatch( @@ -566,8 +579,11 @@ class ArrayNullableForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - vegetablesListControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + vegetablesListControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void modeListValuePatch( @@ -575,8 +591,11 @@ class ArrayNullableForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - modeListControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + modeListControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void someListValueReset( @@ -585,14 +604,13 @@ class ArrayNullableForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - someListControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => someListControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void someListRequiredValueReset( List value, { @@ -600,14 +618,13 @@ class ArrayNullableForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - someListRequiredControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => someListRequiredControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void emailListValueReset( List value, { @@ -615,14 +632,13 @@ class ArrayNullableForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - emailListControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => emailListControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void fruitListValueReset( List value, { @@ -630,14 +646,13 @@ class ArrayNullableForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - fruitListControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => fruitListControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void vegetablesListValueReset( List? value, { @@ -645,14 +660,13 @@ class ArrayNullableForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - vegetablesListControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => vegetablesListControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void modeListValueReset( List? value, { @@ -660,14 +674,13 @@ class ArrayNullableForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - modeListControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => modeListControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl> get someListControl => form.control(someListControlPath()) as FormControl>; @@ -838,13 +851,15 @@ class ArrayNullableForm implements FormModel { break; } - emailListControl.add(FormControl( - value: value, - validators: resultingValidators, - asyncValidators: resultingAsyncValidators, - asyncValidatorsDebounceTime: asyncValidatorsDebounceTime ?? 250, - disabled: disabled ?? false, - )); + emailListControl.add( + FormControl( + value: value, + validators: resultingValidators, + asyncValidators: resultingAsyncValidators, + asyncValidatorsDebounceTime: asyncValidatorsDebounceTime ?? 250, + disabled: disabled ?? false, + ), + ); } void addFruitListItem( @@ -878,13 +893,15 @@ class ArrayNullableForm implements FormModel { break; } - fruitListControl.add(FormControl( - value: value, - validators: resultingValidators, - asyncValidators: resultingAsyncValidators, - asyncValidatorsDebounceTime: asyncValidatorsDebounceTime ?? 250, - disabled: disabled ?? false, - )); + fruitListControl.add( + FormControl( + value: value, + validators: resultingValidators, + asyncValidators: resultingAsyncValidators, + asyncValidatorsDebounceTime: asyncValidatorsDebounceTime ?? 250, + disabled: disabled ?? false, + ), + ); } void addVegetablesListItem( @@ -918,13 +935,15 @@ class ArrayNullableForm implements FormModel { break; } - vegetablesListControl.add(FormControl( - value: value, - validators: resultingValidators, - asyncValidators: resultingAsyncValidators, - asyncValidatorsDebounceTime: asyncValidatorsDebounceTime ?? 250, - disabled: disabled ?? false, - )); + vegetablesListControl.add( + FormControl( + value: value, + validators: resultingValidators, + asyncValidators: resultingAsyncValidators, + asyncValidatorsDebounceTime: asyncValidatorsDebounceTime ?? 250, + disabled: disabled ?? false, + ), + ); } void addModeListItem( @@ -958,13 +977,15 @@ class ArrayNullableForm implements FormModel { break; } - modeListControl.add(FormControl( - value: value, - validators: resultingValidators, - asyncValidators: resultingAsyncValidators, - asyncValidatorsDebounceTime: asyncValidatorsDebounceTime ?? 250, - disabled: disabled ?? false, - )); + modeListControl.add( + FormControl( + value: value, + validators: resultingValidators, + asyncValidators: resultingAsyncValidators, + asyncValidatorsDebounceTime: asyncValidatorsDebounceTime ?? 250, + disabled: disabled ?? false, + ), + ); } @override @@ -979,37 +1000,38 @@ class ArrayNullableForm implements FormModel { ); } return ArrayNullable( - emailList: _emailListValue, - fruitList: _fruitListValue, - vegetablesList: _vegetablesListValue, - modeList: _modeListValue, - someList: _someListValue, - someListRequired: _someListRequiredValue); + emailList: _emailListValue, + fruitList: _fruitListValue, + vegetablesList: _vegetablesListValue, + modeList: _modeListValue, + someList: _someListValue, + someListRequired: _someListRequiredValue, + ); } @override ArrayNullable get rawModel { return ArrayNullable( - emailList: _emailListRawValue, - fruitList: _fruitListRawValue, - vegetablesList: _vegetablesListRawValue, - modeList: _modeListRawValue, - someList: _someListRawValue, - someListRequired: _someListRequiredRawValue); + emailList: _emailListRawValue, + fruitList: _fruitListRawValue, + vegetablesList: _vegetablesListRawValue, + modeList: _modeListRawValue, + someList: _someListRawValue, + someListRequired: _someListRequiredRawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { @@ -1067,9 +1089,11 @@ class ArrayNullableForm implements FormModel { ArrayNullable? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(ArrayNullableForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + ArrayNullableForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -1087,17 +1111,14 @@ class ArrayNullableForm implements FormModel { ArrayNullable? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -1148,86 +1169,104 @@ class ArrayNullableForm implements FormModel { String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(ArrayNullable? arrayNullable) => FormGroup({ - emailListControlName: FormArray( - (arrayNullable?.emailList ?? []) - .map((e) => FormControl( - value: e, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - )) - .toList(), - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false), - fruitListControlName: FormArray( - (arrayNullable?.fruitList ?? []) - .map((e) => FormControl( - value: e, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - )) - .toList(), - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false), - vegetablesListControlName: FormArray( - (arrayNullable?.vegetablesList ?? []) - .map((e) => FormControl( - value: e, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - )) - .toList(), - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false), - modeListControlName: FormArray( - (arrayNullable?.modeList ?? []) - .map((e) => FormControl( - value: e, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - )) - .toList(), - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false), - someListControlName: FormControl>( - value: arrayNullable?.someList, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - someListRequiredControlName: FormControl>( - value: arrayNullable?.someListRequired, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(ArrayNullable? arrayNullable) => FormGroup( + { + emailListControlName: FormArray( + (arrayNullable?.emailList ?? []) + .map( + (e) => FormControl( + value: e, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ), + ) + .toList(), + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ), + fruitListControlName: FormArray( + (arrayNullable?.fruitList ?? []) + .map( + (e) => FormControl( + value: e, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ), + ) + .toList(), + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ), + vegetablesListControlName: FormArray( + (arrayNullable?.vegetablesList ?? []) + .map( + (e) => FormControl( + value: e, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ), + ) + .toList(), + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ), + modeListControlName: FormArray( + (arrayNullable?.modeList ?? []) + .map( + (e) => FormControl( + value: e, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ), + ) + .toList(), + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ), + someListControlName: FormControl>( + value: arrayNullable?.someList, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + someListRequiredControlName: FormControl>( + value: arrayNullable?.someListRequired, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } class ReactiveArrayNullableFormArrayBuilder< - ReactiveArrayNullableFormArrayBuilderT> extends StatelessWidget { + ReactiveArrayNullableFormArrayBuilderT +> + extends StatelessWidget { const ReactiveArrayNullableFormArrayBuilder({ Key? key, this.control, @@ -1236,30 +1275,41 @@ class ReactiveArrayNullableFormArrayBuilder< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - ArrayNullableForm formModel)? control; + ArrayNullableForm formModel, + )? + control; - final Widget Function(BuildContext context, List itemList, - ArrayNullableForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + ArrayNullableForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveArrayNullableFormArrayBuilderT? item, - ArrayNullableForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveArrayNullableFormArrayBuilderT? item, + ArrayNullableForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -1275,18 +1325,9 @@ class ReactiveArrayNullableFormArrayBuilder< return ReactiveFormArrayItemBuilder( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1295,7 +1336,9 @@ class ReactiveArrayNullableFormArrayBuilder< } class ReactiveArrayNullableFormArrayBuilder2< - ReactiveArrayNullableFormArrayBuilderT> extends StatelessWidget { + ReactiveArrayNullableFormArrayBuilderT +> + extends StatelessWidget { const ReactiveArrayNullableFormArrayBuilder2({ Key? key, this.control, @@ -1304,36 +1347,43 @@ class ReactiveArrayNullableFormArrayBuilder2< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - ArrayNullableForm formModel)? control; + ArrayNullableForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - ArrayNullableForm formModel - }) params)? builder; + ({BuildContext context, List itemList, ArrayNullableForm formModel}) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveArrayNullableFormArrayBuilderT? item, - ArrayNullableForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveArrayNullableFormArrayBuilderT? item, + ArrayNullableForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -1350,23 +1400,17 @@ class ReactiveArrayNullableFormArrayBuilder2< formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1375,32 +1419,48 @@ class ReactiveArrayNullableFormArrayBuilder2< } class ReactiveArrayNullableFormFormGroupArrayBuilder< - ReactiveArrayNullableFormFormGroupArrayBuilderT> extends StatelessWidget { + ReactiveArrayNullableFormFormGroupArrayBuilderT +> + extends StatelessWidget { const ReactiveArrayNullableFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(ArrayNullableForm formModel)? + getExtended; - final ExtendedControl?>, - List>? extended; - - final ExtendedControl?>, - List> - Function(ArrayNullableForm formModel)? getExtended; - - final Widget Function(BuildContext context, List itemList, - ArrayNullableForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + ArrayNullableForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveArrayNullableFormFormGroupArrayBuilderT? item, - ArrayNullableForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveArrayNullableFormFormGroupArrayBuilderT? item, + ArrayNullableForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -1415,26 +1475,18 @@ class ReactiveArrayNullableFormFormGroupArrayBuilder< return StreamBuilder?>?>( stream: value.control.valueChanges, builder: (context, snapshot) { - final itemList = (value.value() ?? - []) - .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) - .values - .toList(); - - return builder?.call( - context, - itemList, - formModel, - ) ?? + final itemList = + (value.value() ?? + []) + .asMap() + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) + .values + .toList(); + + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/reactive_forms_generator/example/lib/docs/array_nullable/array_nullable_output.gform.dart b/packages/reactive_forms_generator/example/lib/docs/array_nullable/array_nullable_output.gform.dart index 1e4d9ffd..7abb7680 100644 --- a/packages/reactive_forms_generator/example/lib/docs/array_nullable/array_nullable_output.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/array_nullable/array_nullable_output.gform.dart @@ -19,8 +19,11 @@ class ReactiveArrayNullableOFormConsumer extends StatelessWidget { final Widget? child; final Widget Function( - BuildContext context, ArrayNullableOForm formModel, Widget? child) - builder; + BuildContext context, + ArrayNullableOForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -39,11 +42,7 @@ class ArrayNullableOFormInheritedStreamer extends InheritedStreamer { required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final ArrayNullableOForm form; } @@ -65,19 +64,19 @@ class ReactiveArrayNullableOForm extends StatelessWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - static ArrayNullableOForm? of( - BuildContext context, { - bool listen = true, - }) { + static ArrayNullableOForm? of(BuildContext context, {bool listen = true}) { if (listen) { return context .dependOnInheritedWidgetOfExactType< - ArrayNullableOFormInheritedStreamer>() + ArrayNullableOFormInheritedStreamer + >() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - ArrayNullableOFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + ArrayNullableOFormInheritedStreamer + >(); return element == null ? null : (element.widget as ArrayNullableOFormInheritedStreamer).form; @@ -125,11 +124,14 @@ class ArrayNullableOFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, ArrayNullableOForm formModel, Widget? child) - builder; + BuildContext context, + ArrayNullableOForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, ArrayNullableOForm formModel)? - initState; + initState; @override _ArrayNullableOFormBuilderState createState() => @@ -144,7 +146,10 @@ class _ArrayNullableOFormBuilderState extends State { @override void initState() { _formModel = ArrayNullableOForm( - ArrayNullableOForm.formElements(widget.model), null, null); + ArrayNullableOForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -222,11 +227,8 @@ final _logArrayNullableOForm = Logger.detached('ArrayNullableOForm'); class ArrayNullableOForm implements FormModel { - ArrayNullableOForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + ArrayNullableOForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String emailListControlName = "emailList"; @@ -242,7 +244,7 @@ class ArrayNullableOForm final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -260,7 +262,8 @@ class ArrayNullableOForm String modeListControlPath() => pathBuilder(modeListControlName); - List? get _someListValue => someListControl.value; + List? get _someListValue => + containsSomeList ? someListControl.value : null; List get _emailListValue => emailListControl.rawValue.whereType().toList(); @@ -268,13 +271,16 @@ class ArrayNullableOForm List get _fruitListValue => fruitListControl.rawValue.whereType().toList(); - List? get _vegetablesListValue => - vegetablesListControl.rawValue.whereType().toList(); + List? get _vegetablesListValue => containsVegetablesList + ? vegetablesListControl.rawValue.whereType().toList() + : null; - List? get _modeListValue => - modeListControl.rawValue.whereType().toList(); + List? get _modeListValue => containsModeList + ? modeListControl.rawValue.whereType().toList() + : null; - List? get _someListRawValue => someListControl.value; + List? get _someListRawValue => + containsSomeList ? someListControl.value : null; List get _emailListRawValue => emailListControl.rawValue.whereType().toList(); @@ -282,14 +288,14 @@ class ArrayNullableOForm List get _fruitListRawValue => fruitListControl.rawValue.whereType().toList(); - List? get _vegetablesListRawValue => - vegetablesListControl.rawValue.whereType().toList(); + List? get _vegetablesListRawValue => containsVegetablesList + ? vegetablesListControl.rawValue.whereType().toList() + : null; - List? get _modeListRawValue => - modeListControl.rawValue.whereType().toList(); + List? get _modeListRawValue => containsModeList + ? modeListControl.rawValue.whereType().toList() + : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsSomeList { try { form.control(someListControlPath()); @@ -299,8 +305,6 @@ class ArrayNullableOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsEmailList { try { form.control(emailListControlPath()); @@ -310,8 +314,6 @@ class ArrayNullableOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsFruitList { try { form.control(fruitListControlPath()); @@ -321,8 +323,6 @@ class ArrayNullableOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsVegetablesList { try { form.control(vegetablesListControlPath()); @@ -332,8 +332,6 @@ class ArrayNullableOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsModeList { try { form.control(modeListControlPath()); @@ -363,12 +361,7 @@ class ArrayNullableOForm void get modeListFocus => form.focus(modeListControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void someListRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void someListRemove({bool updateParent = true, bool emitEvent = true}) { if (containsSomeList) { final controlPath = path; if (controlPath == null) { @@ -391,12 +384,7 @@ class ArrayNullableOForm } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void vegetablesListRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void vegetablesListRemove({bool updateParent = true, bool emitEvent = true}) { if (containsVegetablesList) { final controlPath = path; if (controlPath == null) { @@ -419,12 +407,7 @@ class ArrayNullableOForm } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void modeListRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void modeListRemove({bool updateParent = true, bool emitEvent = true}) { if (containsModeList) { final controlPath = path; if (controlPath == null) { @@ -452,8 +435,11 @@ class ArrayNullableOForm bool updateParent = true, bool emitEvent = true, }) { - someListControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + someListControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailListValueUpdate( @@ -461,8 +447,11 @@ class ArrayNullableOForm bool updateParent = true, bool emitEvent = true, }) { - emailListControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailListControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void fruitListValueUpdate( @@ -470,8 +459,11 @@ class ArrayNullableOForm bool updateParent = true, bool emitEvent = true, }) { - fruitListControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + fruitListControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void vegetablesListValueUpdate( @@ -479,8 +471,11 @@ class ArrayNullableOForm bool updateParent = true, bool emitEvent = true, }) { - vegetablesListControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + vegetablesListControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void modeListValueUpdate( @@ -488,8 +483,11 @@ class ArrayNullableOForm bool updateParent = true, bool emitEvent = true, }) { - modeListControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + modeListControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void someListValuePatch( @@ -497,8 +495,11 @@ class ArrayNullableOForm bool updateParent = true, bool emitEvent = true, }) { - someListControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + someListControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailListValuePatch( @@ -506,8 +507,11 @@ class ArrayNullableOForm bool updateParent = true, bool emitEvent = true, }) { - emailListControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailListControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void fruitListValuePatch( @@ -515,8 +519,11 @@ class ArrayNullableOForm bool updateParent = true, bool emitEvent = true, }) { - fruitListControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + fruitListControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void vegetablesListValuePatch( @@ -524,8 +531,11 @@ class ArrayNullableOForm bool updateParent = true, bool emitEvent = true, }) { - vegetablesListControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + vegetablesListControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void modeListValuePatch( @@ -533,8 +543,11 @@ class ArrayNullableOForm bool updateParent = true, bool emitEvent = true, }) { - modeListControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + modeListControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void someListValueReset( @@ -543,14 +556,13 @@ class ArrayNullableOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - someListControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => someListControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void emailListValueReset( List value, { @@ -558,14 +570,13 @@ class ArrayNullableOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - emailListControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => emailListControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void fruitListValueReset( List value, { @@ -573,14 +584,13 @@ class ArrayNullableOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - fruitListControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => fruitListControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void vegetablesListValueReset( List? value, { @@ -588,14 +598,13 @@ class ArrayNullableOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - vegetablesListControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => vegetablesListControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void modeListValueReset( List? value, { @@ -603,14 +612,13 @@ class ArrayNullableOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - modeListControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => modeListControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl> get someListControl => form.control(someListControlPath()) as FormControl>; @@ -760,13 +768,15 @@ class ArrayNullableOForm break; } - emailListControl.add(FormControl( - value: value, - validators: resultingValidators, - asyncValidators: resultingAsyncValidators, - asyncValidatorsDebounceTime: asyncValidatorsDebounceTime ?? 250, - disabled: disabled ?? false, - )); + emailListControl.add( + FormControl( + value: value, + validators: resultingValidators, + asyncValidators: resultingAsyncValidators, + asyncValidatorsDebounceTime: asyncValidatorsDebounceTime ?? 250, + disabled: disabled ?? false, + ), + ); } void addFruitListItem( @@ -800,13 +810,15 @@ class ArrayNullableOForm break; } - fruitListControl.add(FormControl( - value: value, - validators: resultingValidators, - asyncValidators: resultingAsyncValidators, - asyncValidatorsDebounceTime: asyncValidatorsDebounceTime ?? 250, - disabled: disabled ?? false, - )); + fruitListControl.add( + FormControl( + value: value, + validators: resultingValidators, + asyncValidators: resultingAsyncValidators, + asyncValidatorsDebounceTime: asyncValidatorsDebounceTime ?? 250, + disabled: disabled ?? false, + ), + ); } void addVegetablesListItem( @@ -840,13 +852,15 @@ class ArrayNullableOForm break; } - vegetablesListControl.add(FormControl( - value: value, - validators: resultingValidators, - asyncValidators: resultingAsyncValidators, - asyncValidatorsDebounceTime: asyncValidatorsDebounceTime ?? 250, - disabled: disabled ?? false, - )); + vegetablesListControl.add( + FormControl( + value: value, + validators: resultingValidators, + asyncValidators: resultingAsyncValidators, + asyncValidatorsDebounceTime: asyncValidatorsDebounceTime ?? 250, + disabled: disabled ?? false, + ), + ); } void addModeListItem( @@ -880,13 +894,15 @@ class ArrayNullableOForm break; } - modeListControl.add(FormControl( - value: value, - validators: resultingValidators, - asyncValidators: resultingAsyncValidators, - asyncValidatorsDebounceTime: asyncValidatorsDebounceTime ?? 250, - disabled: disabled ?? false, - )); + modeListControl.add( + FormControl( + value: value, + validators: resultingValidators, + asyncValidators: resultingAsyncValidators, + asyncValidatorsDebounceTime: asyncValidatorsDebounceTime ?? 250, + disabled: disabled ?? false, + ), + ); } @override @@ -902,35 +918,36 @@ class ArrayNullableOForm ); } return ArrayNullableOOutput( - emailList: _emailListValue, - fruitList: _fruitListValue, - vegetablesList: _vegetablesListValue, - modeList: _modeListValue, - someList: _someListValue); + emailList: _emailListValue, + fruitList: _fruitListValue, + vegetablesList: _vegetablesListValue, + modeList: _modeListValue, + someList: _someListValue, + ); } @override ArrayNullableO get rawModel { return ArrayNullableO( - emailList: _emailListRawValue, - fruitList: _fruitListRawValue, - vegetablesList: _vegetablesListRawValue, - modeList: _modeListRawValue, - someList: _someListRawValue); + emailList: _emailListRawValue, + fruitList: _fruitListRawValue, + vegetablesList: _vegetablesListRawValue, + modeList: _modeListRawValue, + someList: _someListRawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { @@ -988,9 +1005,11 @@ class ArrayNullableOForm ArrayNullableO? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(ArrayNullableOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + ArrayNullableOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -1008,17 +1027,14 @@ class ArrayNullableOForm ArrayNullableO? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -1069,75 +1085,90 @@ class ArrayNullableOForm String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(ArrayNullableO? arrayNullableO) => FormGroup({ - emailListControlName: FormArray( - (arrayNullableO?.emailList ?? []) - .map((e) => FormControl( - value: e, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - )) - .toList(), - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false), - fruitListControlName: FormArray( - (arrayNullableO?.fruitList ?? []) - .map((e) => FormControl( - value: e, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - )) - .toList(), - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false), - vegetablesListControlName: FormArray( - (arrayNullableO?.vegetablesList ?? []) - .map((e) => FormControl( - value: e, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - )) - .toList(), - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false), - modeListControlName: FormArray( - (arrayNullableO?.modeList ?? []) - .map((e) => FormControl( - value: e, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - )) - .toList(), - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false), - someListControlName: FormControl>( - value: arrayNullableO?.someList, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(ArrayNullableO? arrayNullableO) => FormGroup( + { + emailListControlName: FormArray( + (arrayNullableO?.emailList ?? []) + .map( + (e) => FormControl( + value: e, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ), + ) + .toList(), + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ), + fruitListControlName: FormArray( + (arrayNullableO?.fruitList ?? []) + .map( + (e) => FormControl( + value: e, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ), + ) + .toList(), + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ), + vegetablesListControlName: FormArray( + (arrayNullableO?.vegetablesList ?? []) + .map( + (e) => FormControl( + value: e, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ), + ) + .toList(), + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ), + modeListControlName: FormArray( + (arrayNullableO?.modeList ?? []) + .map( + (e) => FormControl( + value: e, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ), + ) + .toList(), + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ), + someListControlName: FormControl>( + value: arrayNullableO?.someList, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } @Rf(output: true) @@ -1147,16 +1178,19 @@ class ArrayNullableOOutput { final List? vegetablesList; final List? someList; final List? modeList; - ArrayNullableOOutput( - {@RfArray(validators: [RequiredValidator()]) required this.emailList, - @RfArray() this.fruitList = const [], - @RfArray() this.vegetablesList, - @RfArray() this.modeList, - @RfControl>() this.someList}); + ArrayNullableOOutput({ + @RfArray(validators: [RequiredValidator()]) required this.emailList, + @RfArray() this.fruitList = const [], + @RfArray() this.vegetablesList, + @RfArray() this.modeList, + @RfControl>() this.someList, + }); } class ReactiveArrayNullableOFormArrayBuilder< - ReactiveArrayNullableOFormArrayBuilderT> extends StatelessWidget { + ReactiveArrayNullableOFormArrayBuilderT +> + extends StatelessWidget { const ReactiveArrayNullableOFormArrayBuilder({ Key? key, this.control, @@ -1165,30 +1199,41 @@ class ReactiveArrayNullableOFormArrayBuilder< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - ArrayNullableOForm formModel)? control; + ArrayNullableOForm formModel, + )? + control; - final Widget Function(BuildContext context, List itemList, - ArrayNullableOForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + ArrayNullableOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveArrayNullableOFormArrayBuilderT? item, - ArrayNullableOForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveArrayNullableOFormArrayBuilderT? item, + ArrayNullableOForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -1202,21 +1247,13 @@ class ReactiveArrayNullableOFormArrayBuilder< final itemBuilder = this.itemBuilder; return ReactiveFormArrayItemBuilder< - ReactiveArrayNullableOFormArrayBuilderT>( + ReactiveArrayNullableOFormArrayBuilderT + >( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1225,7 +1262,9 @@ class ReactiveArrayNullableOFormArrayBuilder< } class ReactiveArrayNullableOFormArrayBuilder2< - ReactiveArrayNullableOFormArrayBuilderT> extends StatelessWidget { + ReactiveArrayNullableOFormArrayBuilderT +> + extends StatelessWidget { const ReactiveArrayNullableOFormArrayBuilder2({ Key? key, this.control, @@ -1234,36 +1273,47 @@ class ReactiveArrayNullableOFormArrayBuilder2< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - ArrayNullableOForm formModel)? control; + ArrayNullableOForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - ArrayNullableOForm formModel - }) params)? builder; + ({ + BuildContext context, + List itemList, + ArrayNullableOForm formModel, + }) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveArrayNullableOFormArrayBuilderT? item, - ArrayNullableOForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveArrayNullableOFormArrayBuilderT? item, + ArrayNullableOForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -1277,27 +1327,22 @@ class ReactiveArrayNullableOFormArrayBuilder2< final itemBuilder = this.itemBuilder; return ReactiveFormArrayItemBuilder< - ReactiveArrayNullableOFormArrayBuilderT>( + ReactiveArrayNullableOFormArrayBuilderT + >( formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1306,32 +1351,48 @@ class ReactiveArrayNullableOFormArrayBuilder2< } class ReactiveArrayNullableOFormFormGroupArrayBuilder< - ReactiveArrayNullableOFormFormGroupArrayBuilderT> extends StatelessWidget { + ReactiveArrayNullableOFormFormGroupArrayBuilderT +> + extends StatelessWidget { const ReactiveArrayNullableOFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(ArrayNullableOForm formModel)? + getExtended; - final ExtendedControl?>, - List>? extended; - - final ExtendedControl?>, - List> - Function(ArrayNullableOForm formModel)? getExtended; - - final Widget Function(BuildContext context, List itemList, - ArrayNullableOForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + ArrayNullableOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveArrayNullableOFormFormGroupArrayBuilderT? item, - ArrayNullableOForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveArrayNullableOFormFormGroupArrayBuilderT? item, + ArrayNullableOForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -1346,26 +1407,18 @@ class ReactiveArrayNullableOFormFormGroupArrayBuilder< return StreamBuilder?>?>( stream: value.control.valueChanges, builder: (context, snapshot) { - final itemList = (value.value() ?? - []) - .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) - .values - .toList(); - - return builder?.call( - context, - itemList, - formModel, - ) ?? + final itemList = + (value.value() ?? + []) + .asMap() + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) + .values + .toList(); + + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/reactive_forms_generator/example/lib/docs/create/create_output.gform.dart b/packages/reactive_forms_generator/example/lib/docs/create/create_output.gform.dart index cfce6332..194141d9 100644 --- a/packages/reactive_forms_generator/example/lib/docs/create/create_output.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/create/create_output.gform.dart @@ -19,7 +19,11 @@ class ReactiveMSICreateFormConsumer extends StatelessWidget { final Widget? child; final Widget Function( - BuildContext context, MSICreateForm formModel, Widget? child) builder; + BuildContext context, + MSICreateForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -38,11 +42,7 @@ class MSICreateFormInheritedStreamer extends InheritedStreamer { required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final MSICreateForm form; } @@ -64,18 +64,17 @@ class ReactiveMSICreateForm extends StatelessWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - static MSICreateForm? of( - BuildContext context, { - bool listen = true, - }) { + static MSICreateForm? of(BuildContext context, {bool listen = true}) { if (listen) { return context .dependOnInheritedWidgetOfExactType() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - MSICreateFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + MSICreateFormInheritedStreamer + >(); return element == null ? null : (element.widget as MSICreateFormInheritedStreamer).form; @@ -122,7 +121,11 @@ class MSICreateFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, MSICreateForm formModel, Widget? child) builder; + BuildContext context, + MSICreateForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, MSICreateForm formModel)? initState; @@ -137,8 +140,11 @@ class _MSICreateFormBuilderState extends State { @override void initState() { - _formModel = - MSICreateForm(MSICreateForm.formElements(widget.model), null, null); + _formModel = MSICreateForm( + MSICreateForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -215,11 +221,8 @@ class _MSICreateFormBuilderState extends State { final _logMSICreateForm = Logger.detached('MSICreateForm'); class MSICreateForm implements FormModel { - MSICreateForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + MSICreateForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String idControlName = "id"; @@ -246,7 +249,7 @@ class MSICreateForm implements FormModel { final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -275,11 +278,13 @@ class MSICreateForm implements FormModel { String adminsControlPath() => pathBuilder(adminsControlName); - String? get _idValue => idControl.value; + String? get _idValue => containsId ? idControl.value : null; - String? get _businessNumberValue => businessNumberControl.value; + String? get _businessNumberValue => + containsBusinessNumber ? businessNumberControl.value : null; - List? get _fileIdsValue => fileIdsControl.value; + List? get _fileIdsValue => + containsFileIds ? fileIdsControl.value : null; String get _nameValue => nameControl.value as String; @@ -297,15 +302,17 @@ class MSICreateForm implements FormModel { List get _adminsValue => adminsAdminContactInformationForm.map((e) => e.model).toList(); - String? get _idRawValue => idControl.value; + String? get _idRawValue => containsId ? idControl.value : null; - String? get _businessNumberRawValue => businessNumberControl.value; + String? get _businessNumberRawValue => + containsBusinessNumber ? businessNumberControl.value : null; - List? get _fileIdsRawValue => fileIdsControl.value; + List? get _fileIdsRawValue => + containsFileIds ? fileIdsControl.value : null; - String? get _nameRawValue => nameControl.value; + String? get _nameRawValue => containsName ? nameControl.value : null; - String? get _emailRawValue => emailControl.value; + String? get _emailRawValue => containsEmail ? emailControl.value : null; bool get _sameMailingAddressAsCompanyRawValue => sameMailingAddressAsCompanyControl.value ?? false; @@ -319,8 +326,6 @@ class MSICreateForm implements FormModel { List get _adminsRawValue => adminsAdminContactInformationForm.map((e) => e.rawModel).toList(); - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsId { try { form.control(idControlPath()); @@ -330,8 +335,6 @@ class MSICreateForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsBusinessNumber { try { form.control(businessNumberControlPath()); @@ -341,8 +344,6 @@ class MSICreateForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsFileIds { try { form.control(fileIdsControlPath()); @@ -352,8 +353,6 @@ class MSICreateForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsName { try { form.control(nameControlPath()); @@ -363,8 +362,6 @@ class MSICreateForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsEmail { try { form.control(emailControlPath()); @@ -374,8 +371,6 @@ class MSICreateForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsSameMailingAddressAsCompany { try { form.control(sameMailingAddressAsCompanyControlPath()); @@ -385,8 +380,6 @@ class MSICreateForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsCompanyAddress { try { form.control(companyAddressControlPath()); @@ -396,8 +389,6 @@ class MSICreateForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsPrimaryContact { try { form.control(primaryContactControlPath()); @@ -407,8 +398,6 @@ class MSICreateForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsMailingAddress { try { form.control(mailingAddressControlPath()); @@ -418,8 +407,6 @@ class MSICreateForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsAdmins { try { form.control(adminsControlPath()); @@ -471,12 +458,7 @@ class MSICreateForm implements FormModel { void get adminsFocus => form.focus(adminsControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void idRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void idRemove({bool updateParent = true, bool emitEvent = true}) { if (containsId) { final controlPath = path; if (controlPath == null) { @@ -499,12 +481,7 @@ class MSICreateForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void businessNumberRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void businessNumberRemove({bool updateParent = true, bool emitEvent = true}) { if (containsBusinessNumber) { final controlPath = path; if (controlPath == null) { @@ -527,12 +504,7 @@ class MSICreateForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void fileIdsRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void fileIdsRemove({bool updateParent = true, bool emitEvent = true}) { if (containsFileIds) { final controlPath = path; if (controlPath == null) { @@ -555,12 +527,7 @@ class MSICreateForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void nameRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void nameRemove({bool updateParent = true, bool emitEvent = true}) { if (containsName) { final controlPath = path; if (controlPath == null) { @@ -583,12 +550,7 @@ class MSICreateForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void emailRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void emailRemove({bool updateParent = true, bool emitEvent = true}) { if (containsEmail) { final controlPath = path; if (controlPath == null) { @@ -616,8 +578,11 @@ class MSICreateForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - idControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + idControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void businessNumberValueUpdate( @@ -625,8 +590,11 @@ class MSICreateForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - businessNumberControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + businessNumberControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void fileIdsValueUpdate( @@ -634,8 +602,11 @@ class MSICreateForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - fileIdsControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + fileIdsControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void nameValueUpdate( @@ -643,8 +614,11 @@ class MSICreateForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - nameControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + nameControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailValueUpdate( @@ -652,8 +626,11 @@ class MSICreateForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - emailControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void sameMailingAddressAsCompanyValueUpdate( @@ -661,8 +638,11 @@ class MSICreateForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - sameMailingAddressAsCompanyControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + sameMailingAddressAsCompanyControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void companyAddressValueUpdate( @@ -670,8 +650,11 @@ class MSICreateForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - companyAddressControl.updateValue(AddressForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + companyAddressControl.updateValue( + AddressForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void primaryContactValueUpdate( @@ -680,9 +663,10 @@ class MSICreateForm implements FormModel { bool emitEvent = true, }) { primaryContactControl.updateValue( - PrimaryContactForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + PrimaryContactForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void mailingAddressValueUpdate( @@ -690,8 +674,11 @@ class MSICreateForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - mailingAddressControl.updateValue(AddressForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + mailingAddressControl.updateValue( + AddressForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void adminsValueUpdate( @@ -722,17 +709,21 @@ class MSICreateForm implements FormModel { if (toUpdate.isNotEmpty) { adminsControl.updateValue( - toUpdate - .map((e) => AdminContactInformationForm.formElements(e).rawValue) - .toList(), - updateParent: updateParent, - emitEvent: emitEvent); + toUpdate + .map((e) => AdminContactInformationForm.formElements(e).rawValue) + .toList(), + updateParent: updateParent, + emitEvent: emitEvent, + ); } if (toAdd.isNotEmpty) { toAdd.forEach((e) { - adminsControl.add(AdminContactInformationForm.formElements(e), - updateParent: updateParent, emitEvent: emitEvent); + adminsControl.add( + AdminContactInformationForm.formElements(e), + updateParent: updateParent, + emitEvent: emitEvent, + ); }); } } @@ -757,10 +748,7 @@ class MSICreateForm implements FormModel { ); } - void adminsClear({ - bool updateParent = true, - bool emitEvent = true, - }) { + void adminsClear({bool updateParent = true, bool emitEvent = true}) { adminsAdminContactInformationForm.clear(); adminsControl.clear(updateParent: updateParent, emitEvent: emitEvent); } @@ -770,8 +758,11 @@ class MSICreateForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - idControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + idControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void businessNumberValuePatch( @@ -779,8 +770,11 @@ class MSICreateForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - businessNumberControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + businessNumberControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void fileIdsValuePatch( @@ -788,8 +782,11 @@ class MSICreateForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - fileIdsControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + fileIdsControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void nameValuePatch( @@ -797,8 +794,11 @@ class MSICreateForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - nameControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + nameControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailValuePatch( @@ -806,8 +806,11 @@ class MSICreateForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - emailControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void sameMailingAddressAsCompanyValuePatch( @@ -815,8 +818,11 @@ class MSICreateForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - sameMailingAddressAsCompanyControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + sameMailingAddressAsCompanyControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void companyAddressValuePatch( @@ -824,8 +830,11 @@ class MSICreateForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - companyAddressControl.updateValue(AddressForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + companyAddressControl.updateValue( + AddressForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void primaryContactValuePatch( @@ -834,9 +843,10 @@ class MSICreateForm implements FormModel { bool emitEvent = true, }) { primaryContactControl.updateValue( - PrimaryContactForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + PrimaryContactForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void mailingAddressValuePatch( @@ -844,8 +854,11 @@ class MSICreateForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - mailingAddressControl.updateValue(AddressForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + mailingAddressControl.updateValue( + AddressForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void adminsValuePatch( @@ -856,20 +869,19 @@ class MSICreateForm implements FormModel { final keys = adminsAdminContactInformationForm.asMap().keys; final toPatch = []; - (value).asMap().forEach( - (k, v) { - if (keys.contains(k)) { - toPatch.add(v); - } - }, - ); + (value).asMap().forEach((k, v) { + if (keys.contains(k)) { + toPatch.add(v); + } + }); adminsControl.patchValue( - toPatch - .map((e) => AdminContactInformationForm.formElements(e).rawValue) - .toList(), - updateParent: updateParent, - emitEvent: emitEvent); + toPatch + .map((e) => AdminContactInformationForm.formElements(e).rawValue) + .toList(), + updateParent: updateParent, + emitEvent: emitEvent, + ); } void idValueReset( @@ -878,14 +890,13 @@ class MSICreateForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - idControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => idControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void businessNumberValueReset( String? value, { @@ -893,14 +904,13 @@ class MSICreateForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - businessNumberControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => businessNumberControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void fileIdsValueReset( List? value, { @@ -908,14 +918,13 @@ class MSICreateForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - fileIdsControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => fileIdsControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void nameValueReset( String? value, { @@ -923,14 +932,13 @@ class MSICreateForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - nameControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => nameControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void emailValueReset( String? value, { @@ -938,14 +946,13 @@ class MSICreateForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - emailControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => emailControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void sameMailingAddressAsCompanyValueReset( bool value, { @@ -953,14 +960,13 @@ class MSICreateForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - sameMailingAddressAsCompanyControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => sameMailingAddressAsCompanyControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void companyAddressValueReset( Address value, { @@ -968,11 +974,11 @@ class MSICreateForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - companyAddressControl.reset( - value: AddressForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => companyAddressControl.reset( + value: AddressForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); void primaryContactValueReset( PrimaryContact value, { @@ -980,11 +986,11 @@ class MSICreateForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - primaryContactControl.reset( - value: PrimaryContactForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => primaryContactControl.reset( + value: PrimaryContactForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); void mailingAddressValueReset( Address value, { @@ -992,11 +998,11 @@ class MSICreateForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - mailingAddressControl.reset( - value: AddressForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => mailingAddressControl.reset( + value: AddressForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); void adminsValueReset( List value, { @@ -1004,13 +1010,13 @@ class MSICreateForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - adminsControl.reset( - value: value - .map((e) => AdminContactInformationForm.formElements(e).rawValue) - .toList(), - updateParent: updateParent, - emitEvent: emitEvent); + }) => adminsControl.reset( + value: value + .map((e) => AdminContactInformationForm.formElements(e).rawValue) + .toList(), + updateParent: updateParent, + emitEvent: emitEvent, + ); FormControl get idControl => form.control(idControlPath()) as FormControl; @@ -1047,7 +1053,10 @@ class MSICreateForm implements FormModel { AddressForm(form, pathBuilder('companyAddress'), _formModel ?? this); PrimaryContactForm get primaryContactForm => PrimaryContactForm( - form, pathBuilder('primaryContact'), _formModel ?? this); + form, + pathBuilder('primaryContact'), + _formModel ?? this, + ); AddressForm get mailingAddressForm => AddressForm(form, pathBuilder('mailingAddress'), _formModel ?? this); @@ -1057,10 +1066,16 @@ class MSICreateForm implements FormModel { return values .asMap() - .map((k, v) => MapEntry( + .map( + (k, v) => MapEntry( k, AdminContactInformationForm( - form, pathBuilder("admins.$k"), _formModel ?? this))) + form, + pathBuilder("admins.$k"), + _formModel ?? this, + ), + ), + ) .values .toList(); } @@ -1076,10 +1091,7 @@ class MSICreateForm implements FormModel { emitEvent: emitEvent, ); } else { - idControl.markAsEnabled( - updateParent: updateParent, - emitEvent: emitEvent, - ); + idControl.markAsEnabled(updateParent: updateParent, emitEvent: emitEvent); } } @@ -1245,12 +1257,18 @@ class MSICreateForm implements FormModel { } } - ExtendedControl?>, - List> - get adminsExtendedControl => ExtendedControl?>, - List>( - form.control(adminsControlPath()) as FormArray>, - () => adminsAdminContactInformationForm); + ExtendedControl< + List?>, + List + > + get adminsExtendedControl => + ExtendedControl< + List?>, + List + >( + form.control(adminsControlPath()) as FormArray>, + () => adminsAdminContactInformationForm, + ); void addAdminsItem(AdminContactInformation value) { adminsControl.add(AdminContactInformationForm.formElements(value)); @@ -1279,38 +1297,37 @@ class MSICreateForm implements FormModel { ); } return MSICreateOutput( - id: _idValue, - businessNumber: _businessNumberValue, - fileIds: _fileIdsValue, - name: _nameValue, - companyAddress: _companyAddressValue, - email: _emailValue, - primaryContact: _primaryContactValue, - sameMailingAddressAsCompany: _sameMailingAddressAsCompanyValue, - mailingAddress: _mailingAddressValue, - admins: _adminsValue); + id: _idValue, + businessNumber: _businessNumberValue, + fileIds: _fileIdsValue, + name: _nameValue, + companyAddress: _companyAddressValue, + email: _emailValue, + primaryContact: _primaryContactValue, + sameMailingAddressAsCompany: _sameMailingAddressAsCompanyValue, + mailingAddress: _mailingAddressValue, + admins: _adminsValue, + ); } @override MSICreate get rawModel { return MSICreate( - id: _idRawValue, - businessNumber: _businessNumberRawValue, - fileIds: _fileIdsRawValue, - name: _nameRawValue, - companyAddress: _companyAddressRawValue, - email: _emailRawValue, - primaryContact: _primaryContactRawValue, - sameMailingAddressAsCompany: _sameMailingAddressAsCompanyRawValue, - mailingAddress: _mailingAddressRawValue, - admins: _adminsRawValue); + id: _idRawValue, + businessNumber: _businessNumberRawValue, + fileIds: _fileIdsRawValue, + name: _nameRawValue, + companyAddress: _companyAddressRawValue, + email: _emailRawValue, + primaryContact: _primaryContactRawValue, + sameMailingAddressAsCompany: _sameMailingAddressAsCompanyRawValue, + mailingAddress: _mailingAddressRawValue, + admins: _adminsRawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; @@ -1321,7 +1338,9 @@ class MSICreateForm implements FormModel { primaryContactForm.toggleDisabled(); mailingAddressForm.toggleDisabled(); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { adminsAdminContactInformationForm.forEach((e) => e.toggleDisabled()); companyAddressForm.toggleDisabled(); @@ -1383,9 +1402,11 @@ class MSICreateForm implements FormModel { MSICreate? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(MSICreateForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + MSICreateForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -1403,17 +1424,14 @@ class MSICreateForm implements FormModel { MSICreate? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -1464,78 +1482,86 @@ class MSICreateForm implements FormModel { String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(MSICreate? mSICreate) => FormGroup({ - idControlName: FormControl( - value: mSICreate?.id, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - businessNumberControlName: FormControl( - value: mSICreate?.businessNumber, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - fileIdsControlName: FormControl>( - value: mSICreate?.fileIds, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - nameControlName: FormControl( - value: mSICreate?.name, - validators: [RequiredValidator(), MaxLengthValidator(120)], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - emailControlName: FormControl( - value: mSICreate?.email, - validators: [RequiredValidator(), EmailValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - sameMailingAddressAsCompanyControlName: FormControl( - value: mSICreate?.sameMailingAddressAsCompany, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - adminsControlName: FormArray( - (mSICreate?.admins ?? []) - .map((e) => AdminContactInformationForm.formElements(e)) - .toList(), - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false), - companyAddressControlName: - AddressForm.formElements(mSICreate?.companyAddress), - primaryContactControlName: - PrimaryContactForm.formElements(mSICreate?.primaryContact), - mailingAddressControlName: - AddressForm.formElements(mSICreate?.mailingAddress) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(MSICreate? mSICreate) => FormGroup( + { + idControlName: FormControl( + value: mSICreate?.id, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + businessNumberControlName: FormControl( + value: mSICreate?.businessNumber, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + fileIdsControlName: FormControl>( + value: mSICreate?.fileIds, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + nameControlName: FormControl( + value: mSICreate?.name, + validators: [RequiredValidator(), MaxLengthValidator(120)], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + emailControlName: FormControl( + value: mSICreate?.email, + validators: [RequiredValidator(), EmailValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + sameMailingAddressAsCompanyControlName: FormControl( + value: mSICreate?.sameMailingAddressAsCompany, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + adminsControlName: FormArray( + (mSICreate?.admins ?? []) + .map((e) => AdminContactInformationForm.formElements(e)) + .toList(), + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ), + companyAddressControlName: AddressForm.formElements( + mSICreate?.companyAddress, + ), + primaryContactControlName: PrimaryContactForm.formElements( + mSICreate?.primaryContact, + ), + mailingAddressControlName: AddressForm.formElements( + mSICreate?.mailingAddress, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } final _logAddressForm = Logger.detached('AddressForm'); class AddressForm implements FormModel { - AddressForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + AddressForm(this.form, this.path, this._formModel) : initial = form.rawValue; static const String streetControlName = "street"; @@ -1549,7 +1575,7 @@ class AddressForm implements FormModel { final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -1574,16 +1600,15 @@ class AddressForm implements FormModel { String get _zipCodeValue => zipCodeControl.value as String; - String? get _streetRawValue => streetControl.value; + String? get _streetRawValue => containsStreet ? streetControl.value : null; - String? get _cityRawValue => cityControl.value; + String? get _cityRawValue => containsCity ? cityControl.value : null; - String? get _stateOrProvinceRawValue => stateOrProvinceControl.value; + String? get _stateOrProvinceRawValue => + containsStateOrProvince ? stateOrProvinceControl.value : null; - String? get _zipCodeRawValue => zipCodeControl.value; + String? get _zipCodeRawValue => containsZipCode ? zipCodeControl.value : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsStreet { try { form.control(streetControlPath()); @@ -1593,8 +1618,6 @@ class AddressForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsCity { try { form.control(cityControlPath()); @@ -1604,8 +1627,6 @@ class AddressForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsStateOrProvince { try { form.control(stateOrProvinceControlPath()); @@ -1615,8 +1636,6 @@ class AddressForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsZipCode { try { form.control(zipCodeControlPath()); @@ -1643,12 +1662,7 @@ class AddressForm implements FormModel { void get zipCodeFocus => form.focus(zipCodeControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void streetRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void streetRemove({bool updateParent = true, bool emitEvent = true}) { if (containsStreet) { final controlPath = path; if (controlPath == null) { @@ -1671,12 +1685,7 @@ class AddressForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void cityRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void cityRemove({bool updateParent = true, bool emitEvent = true}) { if (containsCity) { final controlPath = path; if (controlPath == null) { @@ -1699,8 +1708,6 @@ class AddressForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') void stateOrProvinceRemove({ bool updateParent = true, bool emitEvent = true, @@ -1727,12 +1734,7 @@ class AddressForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void zipCodeRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void zipCodeRemove({bool updateParent = true, bool emitEvent = true}) { if (containsZipCode) { final controlPath = path; if (controlPath == null) { @@ -1760,8 +1762,11 @@ class AddressForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - streetControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + streetControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void cityValueUpdate( @@ -1769,8 +1774,11 @@ class AddressForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - cityControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + cityControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void stateOrProvinceValueUpdate( @@ -1778,8 +1786,11 @@ class AddressForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - stateOrProvinceControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + stateOrProvinceControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void zipCodeValueUpdate( @@ -1787,8 +1798,11 @@ class AddressForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - zipCodeControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + zipCodeControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void streetValuePatch( @@ -1796,8 +1810,11 @@ class AddressForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - streetControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + streetControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void cityValuePatch( @@ -1805,8 +1822,11 @@ class AddressForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - cityControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + cityControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void stateOrProvinceValuePatch( @@ -1814,8 +1834,11 @@ class AddressForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - stateOrProvinceControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + stateOrProvinceControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void zipCodeValuePatch( @@ -1823,8 +1846,11 @@ class AddressForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - zipCodeControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + zipCodeControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void streetValueReset( @@ -1833,14 +1859,13 @@ class AddressForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - streetControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => streetControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void cityValueReset( String? value, { @@ -1848,14 +1873,13 @@ class AddressForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - cityControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => cityControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void stateOrProvinceValueReset( String? value, { @@ -1863,14 +1887,13 @@ class AddressForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - stateOrProvinceControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => stateOrProvinceControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void zipCodeValueReset( String? value, { @@ -1878,14 +1901,13 @@ class AddressForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - zipCodeControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => zipCodeControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get streetControl => form.control(streetControlPath()) as FormControl; @@ -1984,33 +2006,34 @@ class AddressForm implements FormModel { ); } return AddressOutput( - street: _streetValue, - city: _cityValue, - stateOrProvince: _stateOrProvinceValue, - zipCode: _zipCodeValue); + street: _streetValue, + city: _cityValue, + stateOrProvince: _stateOrProvinceValue, + zipCode: _zipCodeValue, + ); } @override Address get rawModel { return Address( - street: _streetRawValue, - city: _cityRawValue, - stateOrProvince: _stateOrProvinceRawValue, - zipCode: _zipCodeRawValue); + street: _streetRawValue, + city: _cityRawValue, + stateOrProvince: _stateOrProvinceRawValue, + zipCode: _zipCodeRawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { @@ -2068,9 +2091,11 @@ class AddressForm implements FormModel { Address? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(AddressForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + AddressForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -2088,17 +2113,14 @@ class AddressForm implements FormModel { Address? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -2149,51 +2171,54 @@ class AddressForm implements FormModel { String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(Address? address) => FormGroup({ - streetControlName: FormControl( - value: address?.street, - validators: [RequiredValidator(), MaxLengthValidator(120)], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - cityControlName: FormControl( - value: address?.city, - validators: [RequiredValidator(), MaxLengthValidator(120)], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - stateOrProvinceControlName: FormControl( - value: address?.stateOrProvince, - validators: [RequiredValidator(), MaxLengthValidator(120)], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - zipCodeControlName: FormControl( - value: address?.zipCode, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(Address? address) => FormGroup( + { + streetControlName: FormControl( + value: address?.street, + validators: [RequiredValidator(), MaxLengthValidator(120)], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + cityControlName: FormControl( + value: address?.city, + validators: [RequiredValidator(), MaxLengthValidator(120)], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + stateOrProvinceControlName: FormControl( + value: address?.stateOrProvince, + validators: [RequiredValidator(), MaxLengthValidator(120)], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + zipCodeControlName: FormControl( + value: address?.zipCode, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } final _logPrimaryContactForm = Logger.detached('PrimaryContactForm'); class PrimaryContactForm implements FormModel { - PrimaryContactForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + PrimaryContactForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String fullNameControlName = "fullName"; @@ -2205,7 +2230,7 @@ class PrimaryContactForm final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -2221,18 +2246,18 @@ class PrimaryContactForm String get _fullNameValue => fullNameControl.value as String; - String? get _jobTitleValue => jobTitleControl.value; + String? get _jobTitleValue => containsJobTitle ? jobTitleControl.value : null; String get _emailValue => emailControl.value as String; - String? get _fullNameRawValue => fullNameControl.value; + String? get _fullNameRawValue => + containsFullName ? fullNameControl.value : null; - String? get _jobTitleRawValue => jobTitleControl.value; + String? get _jobTitleRawValue => + containsJobTitle ? jobTitleControl.value : null; - String? get _emailRawValue => emailControl.value; + String? get _emailRawValue => containsEmail ? emailControl.value : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsFullName { try { form.control(fullNameControlPath()); @@ -2242,8 +2267,6 @@ class PrimaryContactForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsJobTitle { try { form.control(jobTitleControlPath()); @@ -2253,8 +2276,6 @@ class PrimaryContactForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsEmail { try { form.control(emailControlPath()); @@ -2276,12 +2297,7 @@ class PrimaryContactForm void get emailFocus => form.focus(emailControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void fullNameRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void fullNameRemove({bool updateParent = true, bool emitEvent = true}) { if (containsFullName) { final controlPath = path; if (controlPath == null) { @@ -2304,12 +2320,7 @@ class PrimaryContactForm } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void jobTitleRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void jobTitleRemove({bool updateParent = true, bool emitEvent = true}) { if (containsJobTitle) { final controlPath = path; if (controlPath == null) { @@ -2332,12 +2343,7 @@ class PrimaryContactForm } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void emailRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void emailRemove({bool updateParent = true, bool emitEvent = true}) { if (containsEmail) { final controlPath = path; if (controlPath == null) { @@ -2365,8 +2371,11 @@ class PrimaryContactForm bool updateParent = true, bool emitEvent = true, }) { - fullNameControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + fullNameControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void jobTitleValueUpdate( @@ -2374,8 +2383,11 @@ class PrimaryContactForm bool updateParent = true, bool emitEvent = true, }) { - jobTitleControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + jobTitleControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailValueUpdate( @@ -2383,8 +2395,11 @@ class PrimaryContactForm bool updateParent = true, bool emitEvent = true, }) { - emailControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void fullNameValuePatch( @@ -2392,8 +2407,11 @@ class PrimaryContactForm bool updateParent = true, bool emitEvent = true, }) { - fullNameControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + fullNameControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void jobTitleValuePatch( @@ -2401,8 +2419,11 @@ class PrimaryContactForm bool updateParent = true, bool emitEvent = true, }) { - jobTitleControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + jobTitleControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailValuePatch( @@ -2410,8 +2431,11 @@ class PrimaryContactForm bool updateParent = true, bool emitEvent = true, }) { - emailControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void fullNameValueReset( @@ -2420,14 +2444,13 @@ class PrimaryContactForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - fullNameControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => fullNameControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void jobTitleValueReset( String? value, { @@ -2435,14 +2458,13 @@ class PrimaryContactForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - jobTitleControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => jobTitleControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void emailValueReset( String? value, { @@ -2450,14 +2472,13 @@ class PrimaryContactForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - emailControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => emailControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get fullNameControl => form.control(fullNameControlPath()) as FormControl; @@ -2535,29 +2556,32 @@ class PrimaryContactForm ); } return PrimaryContactOutput( - fullName: _fullNameValue, jobTitle: _jobTitleValue, email: _emailValue); + fullName: _fullNameValue, + jobTitle: _jobTitleValue, + email: _emailValue, + ); } @override PrimaryContact get rawModel { return PrimaryContact( - fullName: _fullNameRawValue, - jobTitle: _jobTitleRawValue, - email: _emailRawValue); + fullName: _fullNameRawValue, + jobTitle: _jobTitleRawValue, + email: _emailRawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { @@ -2615,9 +2639,11 @@ class PrimaryContactForm PrimaryContact? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(PrimaryContactForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + PrimaryContactForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -2635,17 +2661,14 @@ class PrimaryContactForm PrimaryContact? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -2696,46 +2719,49 @@ class PrimaryContactForm String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(PrimaryContact? primaryContact) => FormGroup({ - fullNameControlName: FormControl( - value: primaryContact?.fullName, - validators: [RequiredValidator(), MaxLengthValidator(120)], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - jobTitleControlName: FormControl( - value: primaryContact?.jobTitle, - validators: [MaxLengthValidator(120)], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - emailControlName: FormControl( - value: primaryContact?.email, - validators: [RequiredValidator(), EmailValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(PrimaryContact? primaryContact) => FormGroup( + { + fullNameControlName: FormControl( + value: primaryContact?.fullName, + validators: [RequiredValidator(), MaxLengthValidator(120)], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + jobTitleControlName: FormControl( + value: primaryContact?.jobTitle, + validators: [MaxLengthValidator(120)], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + emailControlName: FormControl( + value: primaryContact?.email, + validators: [RequiredValidator(), EmailValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } -final _logAdminContactInformationForm = - Logger.detached('AdminContactInformationForm'); +final _logAdminContactInformationForm = Logger.detached( + 'AdminContactInformationForm', +); class AdminContactInformationForm implements FormModel { - AdminContactInformationForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + AdminContactInformationForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String firstNameControlName = "firstName"; @@ -2747,7 +2773,7 @@ class AdminContactInformationForm final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -2767,14 +2793,14 @@ class AdminContactInformationForm String get _emailValue => emailControl.value as String; - String? get _firstNameRawValue => firstNameControl.value; + String? get _firstNameRawValue => + containsFirstName ? firstNameControl.value : null; - String? get _lastNameRawValue => lastNameControl.value; + String? get _lastNameRawValue => + containsLastName ? lastNameControl.value : null; - String? get _emailRawValue => emailControl.value; + String? get _emailRawValue => containsEmail ? emailControl.value : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsFirstName { try { form.control(firstNameControlPath()); @@ -2784,8 +2810,6 @@ class AdminContactInformationForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsLastName { try { form.control(lastNameControlPath()); @@ -2795,8 +2819,6 @@ class AdminContactInformationForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsEmail { try { form.control(emailControlPath()); @@ -2818,12 +2840,7 @@ class AdminContactInformationForm void get emailFocus => form.focus(emailControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void firstNameRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void firstNameRemove({bool updateParent = true, bool emitEvent = true}) { if (containsFirstName) { final controlPath = path; if (controlPath == null) { @@ -2846,12 +2863,7 @@ class AdminContactInformationForm } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void lastNameRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void lastNameRemove({bool updateParent = true, bool emitEvent = true}) { if (containsLastName) { final controlPath = path; if (controlPath == null) { @@ -2874,12 +2886,7 @@ class AdminContactInformationForm } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void emailRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void emailRemove({bool updateParent = true, bool emitEvent = true}) { if (containsEmail) { final controlPath = path; if (controlPath == null) { @@ -2907,8 +2914,11 @@ class AdminContactInformationForm bool updateParent = true, bool emitEvent = true, }) { - firstNameControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + firstNameControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void lastNameValueUpdate( @@ -2916,8 +2926,11 @@ class AdminContactInformationForm bool updateParent = true, bool emitEvent = true, }) { - lastNameControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + lastNameControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailValueUpdate( @@ -2925,8 +2938,11 @@ class AdminContactInformationForm bool updateParent = true, bool emitEvent = true, }) { - emailControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void firstNameValuePatch( @@ -2934,8 +2950,11 @@ class AdminContactInformationForm bool updateParent = true, bool emitEvent = true, }) { - firstNameControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + firstNameControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void lastNameValuePatch( @@ -2943,8 +2962,11 @@ class AdminContactInformationForm bool updateParent = true, bool emitEvent = true, }) { - lastNameControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + lastNameControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailValuePatch( @@ -2952,8 +2974,11 @@ class AdminContactInformationForm bool updateParent = true, bool emitEvent = true, }) { - emailControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void firstNameValueReset( @@ -2962,14 +2987,13 @@ class AdminContactInformationForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - firstNameControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => firstNameControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void lastNameValueReset( String? value, { @@ -2977,14 +3001,13 @@ class AdminContactInformationForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - lastNameControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => lastNameControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void emailValueReset( String? value, { @@ -2992,14 +3015,13 @@ class AdminContactInformationForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - emailControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => emailControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get firstNameControl => form.control(firstNameControlPath()) as FormControl; @@ -3077,31 +3099,32 @@ class AdminContactInformationForm ); } return AdminContactInformationOutput( - firstName: _firstNameValue, - lastName: _lastNameValue, - email: _emailValue); + firstName: _firstNameValue, + lastName: _lastNameValue, + email: _emailValue, + ); } @override AdminContactInformation get rawModel { return AdminContactInformation( - firstName: _firstNameRawValue, - lastName: _lastNameRawValue, - email: _emailRawValue); + firstName: _firstNameRawValue, + lastName: _lastNameRawValue, + email: _emailRawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { @@ -3159,11 +3182,11 @@ class AdminContactInformationForm AdminContactInformation? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue( - AdminContactInformationForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.updateValue( + AdminContactInformationForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -3181,17 +3204,14 @@ class AdminContactInformationForm AdminContactInformation? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -3243,91 +3263,99 @@ class AdminContactInformationForm [path, pathItem].whereType().join("."); static FormGroup formElements( - AdminContactInformation? adminContactInformation) => - FormGroup({ - firstNameControlName: FormControl( - value: adminContactInformation?.firstName, - validators: [RequiredValidator(), MaxLengthValidator(120)], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - lastNameControlName: FormControl( - value: adminContactInformation?.lastName, - validators: [RequiredValidator(), MaxLengthValidator(120)], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - emailControlName: FormControl( - value: adminContactInformation?.email, - validators: [RequiredValidator(), EmailValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + AdminContactInformation? adminContactInformation, + ) => FormGroup( + { + firstNameControlName: FormControl( + value: adminContactInformation?.firstName, + validators: [RequiredValidator(), MaxLengthValidator(120)], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + lastNameControlName: FormControl( + value: adminContactInformation?.lastName, + validators: [RequiredValidator(), MaxLengthValidator(120)], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + emailControlName: FormControl( + value: adminContactInformation?.email, + validators: [RequiredValidator(), EmailValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } @Rf(output: true) @freezed abstract class MSICreateOutput with _$MSICreateOutput { - const factory MSICreateOutput( - {String? id, - String? businessNumber, - List? fileIds, - @RfControl(validators: [RequiredValidator(), MaxLengthValidator(120)]) - required String name, - required AddressOutput companyAddress, - @RfControl(validators: [RequiredValidator(), EmailValidator()]) - required String email, - required PrimaryContactOutput primaryContact, - @RfControl() @Default(false) bool sameMailingAddressAsCompany, - required AddressOutput mailingAddress, - @RfArray() - required List admins}) = _MSICreateOutput; + const factory MSICreateOutput({ + String? id, + String? businessNumber, + List? fileIds, + @RfControl(validators: [RequiredValidator(), MaxLengthValidator(120)]) + required String name, + required AddressOutput companyAddress, + @RfControl(validators: [RequiredValidator(), EmailValidator()]) + required String email, + required PrimaryContactOutput primaryContact, + @RfControl() @Default(false) bool sameMailingAddressAsCompany, + required AddressOutput mailingAddress, + @RfArray() + required List admins, + }) = _MSICreateOutput; } @RfGroup() @freezed abstract class AddressOutput with _$AddressOutput { - const factory AddressOutput( - {@RfControl(validators: [RequiredValidator(), MaxLengthValidator(120)]) - required String street, - @RfControl(validators: [RequiredValidator(), MaxLengthValidator(120)]) - required String city, - @RfControl(validators: [RequiredValidator(), MaxLengthValidator(120)]) - required String stateOrProvince, - @RfControl(validators: [RequiredValidator()]) - required String zipCode}) = _AddressOutput; + const factory AddressOutput({ + @RfControl(validators: [RequiredValidator(), MaxLengthValidator(120)]) + required String street, + @RfControl(validators: [RequiredValidator(), MaxLengthValidator(120)]) + required String city, + @RfControl(validators: [RequiredValidator(), MaxLengthValidator(120)]) + required String stateOrProvince, + @RfControl(validators: [RequiredValidator()]) required String zipCode, + }) = _AddressOutput; } @RfGroup() @freezed abstract class PrimaryContactOutput with _$PrimaryContactOutput { - const factory PrimaryContactOutput( - {@RfControl(validators: [RequiredValidator(), MaxLengthValidator(120)]) - required String fullName, - @RfControl(validators: [MaxLengthValidator(120)]) String? jobTitle, - @RfControl(validators: [RequiredValidator(), EmailValidator()]) - required String email}) = _PrimaryContactOutput; + const factory PrimaryContactOutput({ + @RfControl(validators: [RequiredValidator(), MaxLengthValidator(120)]) + required String fullName, + @RfControl(validators: [MaxLengthValidator(120)]) String? jobTitle, + @RfControl(validators: [RequiredValidator(), EmailValidator()]) + required String email, + }) = _PrimaryContactOutput; } @RfGroup() @freezed abstract class AdminContactInformationOutput with _$AdminContactInformationOutput { - const factory AdminContactInformationOutput( - {@RfControl(validators: [RequiredValidator(), MaxLengthValidator(120)]) - required String firstName, - @RfControl(validators: [RequiredValidator(), MaxLengthValidator(120)]) - required String lastName, - @RfControl(validators: [RequiredValidator(), EmailValidator()]) - required String email}) = _AdminContactInformationOutput; + const factory AdminContactInformationOutput({ + @RfControl(validators: [RequiredValidator(), MaxLengthValidator(120)]) + required String firstName, + @RfControl(validators: [RequiredValidator(), MaxLengthValidator(120)]) + required String lastName, + @RfControl(validators: [RequiredValidator(), EmailValidator()]) + required String email, + }) = _AdminContactInformationOutput; } class ReactiveMSICreateFormArrayBuilder @@ -3340,30 +3368,39 @@ class ReactiveMSICreateFormArrayBuilder required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - MSICreateForm formModel)? control; + MSICreateForm formModel, + )? + control; final Widget Function( - BuildContext context, List itemList, MSICreateForm formModel)? - builder; + BuildContext context, + List itemList, + MSICreateForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveMSICreateFormArrayBuilderT? item, - MSICreateForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveMSICreateFormArrayBuilderT? item, + MSICreateForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function(FormControl control)? - controlFilter; + controlFilter; @override Widget build(BuildContext context) { @@ -3379,18 +3416,9 @@ class ReactiveMSICreateFormArrayBuilder return ReactiveFormArrayItemBuilder( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -3408,35 +3436,41 @@ class ReactiveMSICreateFormArrayBuilder2 required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - MSICreateForm formModel)? control; + MSICreateForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - MSICreateForm formModel - }) params)? builder; + ({BuildContext context, List itemList, MSICreateForm formModel}) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveMSICreateFormArrayBuilderT? item, - MSICreateForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveMSICreateFormArrayBuilderT? item, + MSICreateForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function(FormControl control)? - controlFilter; + controlFilter; @override Widget build(BuildContext context) { @@ -3453,23 +3487,17 @@ class ReactiveMSICreateFormArrayBuilder2 formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -3478,33 +3506,48 @@ class ReactiveMSICreateFormArrayBuilder2 } class ReactiveMSICreateFormFormGroupArrayBuilder< - ReactiveMSICreateFormFormGroupArrayBuilderT> extends StatelessWidget { + ReactiveMSICreateFormFormGroupArrayBuilderT +> + extends StatelessWidget { const ReactiveMSICreateFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? extended; - - final ExtendedControl?>, - List> - Function(MSICreateForm formModel)? getExtended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(MSICreateForm formModel)? + getExtended; final Widget Function( - BuildContext context, List itemList, MSICreateForm formModel)? - builder; + BuildContext context, + List itemList, + MSICreateForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveMSICreateFormFormGroupArrayBuilderT? item, - MSICreateForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveMSICreateFormFormGroupArrayBuilderT? item, + MSICreateForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -3522,23 +3565,14 @@ class ReactiveMSICreateFormFormGroupArrayBuilder< final itemList = (value.value() ?? []) .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) .values .toList(); - return builder?.call( - context, - itemList, - formModel, - ) ?? + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/reactive_forms_generator/example/lib/docs/delivery_list/delivery_list.gform.dart b/packages/reactive_forms_generator/example/lib/docs/delivery_list/delivery_list.gform.dart index dbdad225..653ff1c9 100644 --- a/packages/reactive_forms_generator/example/lib/docs/delivery_list/delivery_list.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/delivery_list/delivery_list.gform.dart @@ -19,7 +19,11 @@ class ReactiveDeliveryListFormConsumer extends StatelessWidget { final Widget? child; final Widget Function( - BuildContext context, DeliveryListForm formModel, Widget? child) builder; + BuildContext context, + DeliveryListForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -38,11 +42,7 @@ class DeliveryListFormInheritedStreamer extends InheritedStreamer { required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final DeliveryListForm form; } @@ -64,19 +64,19 @@ class ReactiveDeliveryListForm extends StatelessWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - static DeliveryListForm? of( - BuildContext context, { - bool listen = true, - }) { + static DeliveryListForm? of(BuildContext context, {bool listen = true}) { if (listen) { return context .dependOnInheritedWidgetOfExactType< - DeliveryListFormInheritedStreamer>() + DeliveryListFormInheritedStreamer + >() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - DeliveryListFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + DeliveryListFormInheritedStreamer + >(); return element == null ? null : (element.widget as DeliveryListFormInheritedStreamer).form; @@ -124,10 +124,14 @@ class DeliveryListFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, DeliveryListForm formModel, Widget? child) builder; + BuildContext context, + DeliveryListForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, DeliveryListForm formModel)? - initState; + initState; @override _DeliveryListFormBuilderState createState() => @@ -142,7 +146,10 @@ class _DeliveryListFormBuilderState extends State { @override void initState() { _formModel = DeliveryListForm( - DeliveryListForm.formElements(widget.model), null, null); + DeliveryListForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -219,11 +226,8 @@ class _DeliveryListFormBuilderState extends State { final _logDeliveryListForm = Logger.detached('DeliveryListForm'); class DeliveryListForm implements FormModel { - DeliveryListForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + DeliveryListForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String deliveryListControlName = "deliveryList"; @@ -233,7 +237,7 @@ class DeliveryListForm implements FormModel { final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -248,17 +252,17 @@ class DeliveryListForm implements FormModel { List get _deliveryListValue => deliveryListDeliveryPointForm.map((e) => e.model).toList(); - List? get _clientListValue => - clientListClientForm.map((e) => e.model).toList(); + List? get _clientListValue => containsClientList + ? clientListClientForm.map((e) => e.model).toList() + : null; List get _deliveryListRawValue => deliveryListDeliveryPointForm.map((e) => e.rawModel).toList(); - List? get _clientListRawValue => - clientListClientForm.map((e) => e.rawModel).toList(); + List? get _clientListRawValue => containsClientList + ? clientListClientForm.map((e) => e.rawModel).toList() + : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsDeliveryList { try { form.control(deliveryListControlPath()); @@ -268,8 +272,6 @@ class DeliveryListForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsClientList { try { form.control(clientListControlPath()); @@ -287,12 +289,7 @@ class DeliveryListForm implements FormModel { void get clientListFocus => form.focus(clientListControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void clientListRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void clientListRemove({bool updateParent = true, bool emitEvent = true}) { if (containsClientList) { final controlPath = path; if (controlPath == null) { @@ -343,17 +340,21 @@ class DeliveryListForm implements FormModel { if (toUpdate.isNotEmpty) { deliveryListControl.updateValue( - toUpdate - .map((e) => DeliveryPointForm.formElements(e).rawValue) - .toList(), - updateParent: updateParent, - emitEvent: emitEvent); + toUpdate + .map((e) => DeliveryPointForm.formElements(e).rawValue) + .toList(), + updateParent: updateParent, + emitEvent: emitEvent, + ); } if (toAdd.isNotEmpty) { toAdd.forEach((e) { - deliveryListControl.add(DeliveryPointForm.formElements(e), - updateParent: updateParent, emitEvent: emitEvent); + deliveryListControl.add( + DeliveryPointForm.formElements(e), + updateParent: updateParent, + emitEvent: emitEvent, + ); }); } } @@ -386,15 +387,19 @@ class DeliveryListForm implements FormModel { if (toUpdate.isNotEmpty) { clientListControl.updateValue( - toUpdate.map((e) => ClientForm.formElements(e).rawValue).toList(), - updateParent: updateParent, - emitEvent: emitEvent); + toUpdate.map((e) => ClientForm.formElements(e).rawValue).toList(), + updateParent: updateParent, + emitEvent: emitEvent, + ); } if (toAdd.isNotEmpty) { toAdd.forEach((e) { - clientListControl.add(ClientForm.formElements(e), - updateParent: updateParent, emitEvent: emitEvent); + clientListControl.add( + ClientForm.formElements(e), + updateParent: updateParent, + emitEvent: emitEvent, + ); }); } } @@ -439,18 +444,12 @@ class DeliveryListForm implements FormModel { ); } - void deliveryListClear({ - bool updateParent = true, - bool emitEvent = true, - }) { + void deliveryListClear({bool updateParent = true, bool emitEvent = true}) { deliveryListDeliveryPointForm.clear(); deliveryListControl.clear(updateParent: updateParent, emitEvent: emitEvent); } - void clientListClear({ - bool updateParent = true, - bool emitEvent = true, - }) { + void clientListClear({bool updateParent = true, bool emitEvent = true}) { clientListClientForm.clear(); clientListControl.clear(updateParent: updateParent, emitEvent: emitEvent); } @@ -463,18 +462,17 @@ class DeliveryListForm implements FormModel { final keys = deliveryListDeliveryPointForm.asMap().keys; final toPatch = []; - (value).asMap().forEach( - (k, v) { - if (keys.contains(k)) { - toPatch.add(v); - } - }, - ); + (value).asMap().forEach((k, v) { + if (keys.contains(k)) { + toPatch.add(v); + } + }); deliveryListControl.patchValue( - toPatch.map((e) => DeliveryPointForm.formElements(e).rawValue).toList(), - updateParent: updateParent, - emitEvent: emitEvent); + toPatch.map((e) => DeliveryPointForm.formElements(e).rawValue).toList(), + updateParent: updateParent, + emitEvent: emitEvent, + ); } void clientListValuePatch( @@ -485,18 +483,17 @@ class DeliveryListForm implements FormModel { final keys = clientListClientForm.asMap().keys; final toPatch = []; - (value ?? []).asMap().forEach( - (k, v) { - if (keys.contains(k)) { - toPatch.add(v); - } - }, - ); + (value ?? []).asMap().forEach((k, v) { + if (keys.contains(k)) { + toPatch.add(v); + } + }); clientListControl.patchValue( - toPatch.map((e) => ClientForm.formElements(e).rawValue).toList(), - updateParent: updateParent, - emitEvent: emitEvent); + toPatch.map((e) => ClientForm.formElements(e).rawValue).toList(), + updateParent: updateParent, + emitEvent: emitEvent, + ); } void deliveryListValueReset( @@ -505,13 +502,13 @@ class DeliveryListForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - deliveryListControl.reset( - value: value - .map((e) => DeliveryPointForm.formElements(e).rawValue) - .toList(), - updateParent: updateParent, - emitEvent: emitEvent); + }) => deliveryListControl.reset( + value: value + .map((e) => DeliveryPointForm.formElements(e).rawValue) + .toList(), + updateParent: updateParent, + emitEvent: emitEvent, + ); void clientListValueReset( List? value, { @@ -519,12 +516,11 @@ class DeliveryListForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - clientListControl.reset( - value: - value?.map((e) => ClientForm.formElements(e).rawValue).toList(), - updateParent: updateParent, - emitEvent: emitEvent); + }) => clientListControl.reset( + value: value?.map((e) => ClientForm.formElements(e).rawValue).toList(), + updateParent: updateParent, + emitEvent: emitEvent, + ); FormArray> get deliveryListControl => form.control(deliveryListControlPath()) @@ -538,10 +534,16 @@ class DeliveryListForm implements FormModel { return values .asMap() - .map((k, v) => MapEntry( + .map( + (k, v) => MapEntry( k, DeliveryPointForm( - form, pathBuilder("deliveryList.$k"), _formModel ?? this))) + form, + pathBuilder("deliveryList.$k"), + _formModel ?? this, + ), + ), + ) .values .toList(); } @@ -551,8 +553,12 @@ class DeliveryListForm implements FormModel { return values .asMap() - .map((k, v) => MapEntry(k, - ClientForm(form, pathBuilder("clientList.$k"), _formModel ?? this))) + .map( + (k, v) => MapEntry( + k, + ClientForm(form, pathBuilder("clientList.$k"), _formModel ?? this), + ), + ) .values .toList(); } @@ -594,18 +600,20 @@ class DeliveryListForm implements FormModel { } ExtendedControl?>, List> - get deliveryListExtendedControl => - ExtendedControl?>, List>( - form.control(deliveryListControlPath()) - as FormArray>, - () => deliveryListDeliveryPointForm); + get deliveryListExtendedControl => + ExtendedControl?>, List>( + form.control(deliveryListControlPath()) + as FormArray>, + () => deliveryListDeliveryPointForm, + ); ExtendedControl?>, List> - get clientListExtendedControl => - ExtendedControl?>, List>( - form.control(clientListControlPath()) - as FormArray>, - () => clientListClientForm); + get clientListExtendedControl => + ExtendedControl?>, List>( + form.control(clientListControlPath()) + as FormArray>, + () => clientListClientForm, + ); void addDeliveryListItem(DeliveryPoint value) { deliveryListControl.add(DeliveryPointForm.formElements(value)); @@ -647,20 +655,21 @@ class DeliveryListForm implements FormModel { ); } return DeliveryList( - deliveryList: _deliveryListValue, clientList: _clientListValue); + deliveryList: _deliveryListValue, + clientList: _clientListValue, + ); } @override DeliveryList get rawModel { return DeliveryList( - deliveryList: _deliveryListRawValue, clientList: _clientListRawValue); + deliveryList: _deliveryListRawValue, + clientList: _clientListRawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; @@ -670,7 +679,9 @@ class DeliveryListForm implements FormModel { clientListClientForm.forEach((e) => e.toggleDisabled()); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { deliveryListDeliveryPointForm.forEach((e) => e.toggleDisabled()); clientListClientForm.forEach((e) => e.toggleDisabled()); @@ -731,9 +742,11 @@ class DeliveryListForm implements FormModel { DeliveryList? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(DeliveryListForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + DeliveryListForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -751,17 +764,14 @@ class DeliveryListForm implements FormModel { DeliveryList? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -812,38 +822,39 @@ class DeliveryListForm implements FormModel { String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(DeliveryList? deliveryList) => FormGroup({ - deliveryListControlName: FormArray( - (deliveryList?.deliveryList ?? []) - .map((e) => DeliveryPointForm.formElements(e)) - .toList(), - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false), - clientListControlName: FormArray( - (deliveryList?.clientList ?? []) - .map((e) => ClientForm.formElements(e)) - .toList(), - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(DeliveryList? deliveryList) => FormGroup( + { + deliveryListControlName: FormArray( + (deliveryList?.deliveryList ?? []) + .map((e) => DeliveryPointForm.formElements(e)) + .toList(), + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ), + clientListControlName: FormArray( + (deliveryList?.clientList ?? []) + .map((e) => ClientForm.formElements(e)) + .toList(), + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } final _logDeliveryPointForm = Logger.detached('DeliveryPointForm'); class DeliveryPointForm implements FormModel { - DeliveryPointForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + DeliveryPointForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String nameControlName = "name"; @@ -853,7 +864,7 @@ class DeliveryPointForm implements FormModel { final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -867,14 +878,13 @@ class DeliveryPointForm implements FormModel { String get _nameValue => nameControl.value ?? ""; - Address? get _addressValue => addressForm.model; + Address? get _addressValue => containsAddress ? addressForm.model : null; String get _nameRawValue => nameControl.value ?? ""; - Address? get _addressRawValue => addressForm.rawModel; + Address? get _addressRawValue => + containsAddress ? addressForm.rawModel : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsName { try { form.control(nameControlPath()); @@ -884,8 +894,6 @@ class DeliveryPointForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsAddress { try { form.control(addressControlPath()); @@ -903,12 +911,7 @@ class DeliveryPointForm implements FormModel { void get addressFocus => form.focus(addressControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void addressRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void addressRemove({bool updateParent = true, bool emitEvent = true}) { if (containsAddress) { final controlPath = path; if (controlPath == null) { @@ -936,8 +939,11 @@ class DeliveryPointForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - nameControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + nameControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void addressValueUpdate( @@ -945,8 +951,11 @@ class DeliveryPointForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - addressControl.updateValue(AddressForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + addressControl.updateValue( + AddressForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void nameValuePatch( @@ -954,8 +963,11 @@ class DeliveryPointForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - nameControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + nameControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void addressValuePatch( @@ -963,8 +975,11 @@ class DeliveryPointForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - addressControl.updateValue(AddressForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + addressControl.updateValue( + AddressForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void nameValueReset( @@ -973,14 +988,13 @@ class DeliveryPointForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - nameControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => nameControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void addressValueReset( Address? value, { @@ -988,11 +1002,11 @@ class DeliveryPointForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - addressControl.reset( - value: AddressForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => addressControl.reset( + value: AddressForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); FormControl get nameControl => form.control(nameControlPath()) as FormControl; @@ -1059,10 +1073,7 @@ class DeliveryPointForm implements FormModel { } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; @@ -1070,7 +1081,9 @@ class DeliveryPointForm implements FormModel { addressForm.toggleDisabled(); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { addressForm.toggleDisabled(); currentForm.controls.forEach((key, control) { @@ -1129,9 +1142,11 @@ class DeliveryPointForm implements FormModel { DeliveryPoint? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(DeliveryPointForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + DeliveryPointForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -1149,17 +1164,14 @@ class DeliveryPointForm implements FormModel { DeliveryPoint? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -1210,30 +1222,29 @@ class DeliveryPointForm implements FormModel { String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(DeliveryPoint? deliveryPoint) => FormGroup({ - nameControlName: FormControl( - value: deliveryPoint?.name, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - addressControlName: AddressForm.formElements(deliveryPoint?.address) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(DeliveryPoint? deliveryPoint) => FormGroup( + { + nameControlName: FormControl( + value: deliveryPoint?.name, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + addressControlName: AddressForm.formElements(deliveryPoint?.address), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } final _logAddressForm = Logger.detached('AddressForm'); class AddressForm implements FormModel { - AddressForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + AddressForm(this.form, this.path, this._formModel) : initial = form.rawValue; static const String streetControlName = "street"; @@ -1243,7 +1254,7 @@ class AddressForm implements FormModel { final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -1255,16 +1266,14 @@ class AddressForm implements FormModel { String cityControlPath() => pathBuilder(cityControlName); - String? get _streetValue => streetControl.value; + String? get _streetValue => containsStreet ? streetControl.value : null; - String? get _cityValue => cityControl.value; + String? get _cityValue => containsCity ? cityControl.value : null; - String? get _streetRawValue => streetControl.value; + String? get _streetRawValue => containsStreet ? streetControl.value : null; - String? get _cityRawValue => cityControl.value; + String? get _cityRawValue => containsCity ? cityControl.value : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsStreet { try { form.control(streetControlPath()); @@ -1274,8 +1283,6 @@ class AddressForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsCity { try { form.control(cityControlPath()); @@ -1293,12 +1300,7 @@ class AddressForm implements FormModel { void get cityFocus => form.focus(cityControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void streetRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void streetRemove({bool updateParent = true, bool emitEvent = true}) { if (containsStreet) { final controlPath = path; if (controlPath == null) { @@ -1321,12 +1323,7 @@ class AddressForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void cityRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void cityRemove({bool updateParent = true, bool emitEvent = true}) { if (containsCity) { final controlPath = path; if (controlPath == null) { @@ -1354,8 +1351,11 @@ class AddressForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - streetControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + streetControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void cityValueUpdate( @@ -1363,8 +1363,11 @@ class AddressForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - cityControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + cityControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void streetValuePatch( @@ -1372,8 +1375,11 @@ class AddressForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - streetControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + streetControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void cityValuePatch( @@ -1381,8 +1387,11 @@ class AddressForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - cityControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + cityControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void streetValueReset( @@ -1391,14 +1400,13 @@ class AddressForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - streetControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => streetControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void cityValueReset( String? value, { @@ -1406,14 +1414,13 @@ class AddressForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - cityControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => cityControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get streetControl => form.control(streetControlPath()) as FormControl; @@ -1477,17 +1484,16 @@ class AddressForm implements FormModel { } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { @@ -1545,9 +1551,11 @@ class AddressForm implements FormModel { Address? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(AddressForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + AddressForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -1565,17 +1573,14 @@ class AddressForm implements FormModel { Address? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -1626,36 +1631,36 @@ class AddressForm implements FormModel { String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(Address? address) => FormGroup({ - streetControlName: FormControl( - value: address?.street, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - cityControlName: FormControl( - value: address?.city, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(Address? address) => FormGroup( + { + streetControlName: FormControl( + value: address?.street, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + cityControlName: FormControl( + value: address?.city, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } final _logClientForm = Logger.detached('ClientForm'); class ClientForm implements FormModel { - ClientForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + ClientForm(this.form, this.path, this._formModel) : initial = form.rawValue; static const String clientTypeControlName = "clientType"; @@ -1667,7 +1672,7 @@ class ClientForm implements FormModel { final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -1683,18 +1688,16 @@ class ClientForm implements FormModel { ClientType get _clientTypeValue => clientTypeControl.value as ClientType; - String? get _nameValue => nameControl.value; + String? get _nameValue => containsName ? nameControl.value : null; - String? get _notesValue => notesControl.value; + String? get _notesValue => containsNotes ? notesControl.value : null; ClientType get _clientTypeRawValue => clientTypeControl.value as ClientType; - String? get _nameRawValue => nameControl.value; + String? get _nameRawValue => containsName ? nameControl.value : null; - String? get _notesRawValue => notesControl.value; + String? get _notesRawValue => containsNotes ? notesControl.value : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsClientType { try { form.control(clientTypeControlPath()); @@ -1704,8 +1707,6 @@ class ClientForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsName { try { form.control(nameControlPath()); @@ -1715,8 +1716,6 @@ class ClientForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsNotes { try { form.control(notesControlPath()); @@ -1738,12 +1737,7 @@ class ClientForm implements FormModel { void get notesFocus => form.focus(notesControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void nameRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void nameRemove({bool updateParent = true, bool emitEvent = true}) { if (containsName) { final controlPath = path; if (controlPath == null) { @@ -1766,12 +1760,7 @@ class ClientForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void notesRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void notesRemove({bool updateParent = true, bool emitEvent = true}) { if (containsNotes) { final controlPath = path; if (controlPath == null) { @@ -1799,8 +1788,11 @@ class ClientForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - clientTypeControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + clientTypeControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void nameValueUpdate( @@ -1808,8 +1800,11 @@ class ClientForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - nameControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + nameControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void notesValueUpdate( @@ -1817,8 +1812,11 @@ class ClientForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - notesControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + notesControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void clientTypeValuePatch( @@ -1826,8 +1824,11 @@ class ClientForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - clientTypeControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + clientTypeControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void nameValuePatch( @@ -1835,8 +1836,11 @@ class ClientForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - nameControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + nameControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void notesValuePatch( @@ -1844,8 +1848,11 @@ class ClientForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - notesControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + notesControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void clientTypeValueReset( @@ -1854,14 +1861,13 @@ class ClientForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - clientTypeControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => clientTypeControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void nameValueReset( String? value, { @@ -1869,14 +1875,13 @@ class ClientForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - nameControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => nameControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void notesValueReset( String? value, { @@ -1884,14 +1889,13 @@ class ClientForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - notesControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => notesControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get clientTypeControl => form.control(clientTypeControlPath()) as FormControl; @@ -1968,29 +1972,32 @@ class ClientForm implements FormModel { ); } return Client( - clientType: _clientTypeValue, name: _nameValue, notes: _notesValue); + clientType: _clientTypeValue, + name: _nameValue, + notes: _notesValue, + ); } @override Client get rawModel { return Client( - clientType: _clientTypeRawValue, - name: _nameRawValue, - notes: _notesRawValue); + clientType: _clientTypeRawValue, + name: _nameRawValue, + notes: _notesRawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { @@ -2048,9 +2055,11 @@ class ClientForm implements FormModel { Client? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(ClientForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + ClientForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -2068,17 +2077,14 @@ class ClientForm implements FormModel { Client? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -2129,37 +2135,44 @@ class ClientForm implements FormModel { String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(Client? client) => FormGroup({ - clientTypeControlName: FormControl( - value: client?.clientType, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - nameControlName: FormControl( - value: client?.name, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - notesControlName: FormControl( - value: client?.notes, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(Client? client) => FormGroup( + { + clientTypeControlName: FormControl( + value: client?.clientType, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + nameControlName: FormControl( + value: client?.name, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + notesControlName: FormControl( + value: client?.notes, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } class ReactiveDeliveryListFormArrayBuilder< - ReactiveDeliveryListFormArrayBuilderT> extends StatelessWidget { + ReactiveDeliveryListFormArrayBuilderT +> + extends StatelessWidget { const ReactiveDeliveryListFormArrayBuilder({ Key? key, this.control, @@ -2168,30 +2181,41 @@ class ReactiveDeliveryListFormArrayBuilder< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - DeliveryListForm formModel)? control; + DeliveryListForm formModel, + )? + control; - final Widget Function(BuildContext context, List itemList, - DeliveryListForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + DeliveryListForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveDeliveryListFormArrayBuilderT? item, - DeliveryListForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveDeliveryListFormArrayBuilderT? item, + DeliveryListForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -2207,18 +2231,9 @@ class ReactiveDeliveryListFormArrayBuilder< return ReactiveFormArrayItemBuilder( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -2227,7 +2242,9 @@ class ReactiveDeliveryListFormArrayBuilder< } class ReactiveDeliveryListFormArrayBuilder2< - ReactiveDeliveryListFormArrayBuilderT> extends StatelessWidget { + ReactiveDeliveryListFormArrayBuilderT +> + extends StatelessWidget { const ReactiveDeliveryListFormArrayBuilder2({ Key? key, this.control, @@ -2236,36 +2253,43 @@ class ReactiveDeliveryListFormArrayBuilder2< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - DeliveryListForm formModel)? control; + DeliveryListForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - DeliveryListForm formModel - }) params)? builder; + ({BuildContext context, List itemList, DeliveryListForm formModel}) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveDeliveryListFormArrayBuilderT? item, - DeliveryListForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveDeliveryListFormArrayBuilderT? item, + DeliveryListForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -2282,23 +2306,17 @@ class ReactiveDeliveryListFormArrayBuilder2< formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -2307,32 +2325,48 @@ class ReactiveDeliveryListFormArrayBuilder2< } class ReactiveDeliveryListFormFormGroupArrayBuilder< - ReactiveDeliveryListFormFormGroupArrayBuilderT> extends StatelessWidget { + ReactiveDeliveryListFormFormGroupArrayBuilderT +> + extends StatelessWidget { const ReactiveDeliveryListFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? extended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(DeliveryListForm formModel)? + getExtended; - final ExtendedControl?>, - List> - Function(DeliveryListForm formModel)? getExtended; - - final Widget Function(BuildContext context, List itemList, - DeliveryListForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + DeliveryListForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveDeliveryListFormFormGroupArrayBuilderT? item, - DeliveryListForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveDeliveryListFormFormGroupArrayBuilderT? item, + DeliveryListForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -2347,26 +2381,18 @@ class ReactiveDeliveryListFormFormGroupArrayBuilder< return StreamBuilder?>?>( stream: value.control.valueChanges, builder: (context, snapshot) { - final itemList = (value.value() ?? - []) - .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) - .values - .toList(); - - return builder?.call( - context, - itemList, - formModel, - ) ?? + final itemList = + (value.value() ?? + []) + .asMap() + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) + .values + .toList(); + + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); @@ -2382,8 +2408,12 @@ class ReactiveStandaloneDeliveryPointFormConsumer extends StatelessWidget { final Widget? child; - final Widget Function(BuildContext context, - StandaloneDeliveryPointForm formModel, Widget? child) builder; + final Widget Function( + BuildContext context, + StandaloneDeliveryPointForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -2403,11 +2433,7 @@ class StandaloneDeliveryPointFormInheritedStreamer required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final StandaloneDeliveryPointForm form; } @@ -2436,12 +2462,15 @@ class ReactiveStandaloneDeliveryPointForm extends StatelessWidget { if (listen) { return context .dependOnInheritedWidgetOfExactType< - StandaloneDeliveryPointFormInheritedStreamer>() + StandaloneDeliveryPointFormInheritedStreamer + >() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - StandaloneDeliveryPointFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + StandaloneDeliveryPointFormInheritedStreamer + >(); return element == null ? null : (element.widget as StandaloneDeliveryPointFormInheritedStreamer).form; @@ -2488,11 +2517,18 @@ class StandaloneDeliveryPointFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - final Widget Function(BuildContext context, - StandaloneDeliveryPointForm formModel, Widget? child) builder; + final Widget Function( + BuildContext context, + StandaloneDeliveryPointForm formModel, + Widget? child, + ) + builder; final void Function( - BuildContext context, StandaloneDeliveryPointForm formModel)? initState; + BuildContext context, + StandaloneDeliveryPointForm formModel, + )? + initState; @override _StandaloneDeliveryPointFormBuilderState createState() => @@ -2508,7 +2544,10 @@ class _StandaloneDeliveryPointFormBuilderState @override void initState() { _formModel = StandaloneDeliveryPointForm( - StandaloneDeliveryPointForm.formElements(widget.model), null, null); + StandaloneDeliveryPointForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -2516,8 +2555,9 @@ class _StandaloneDeliveryPointFormBuilderState widget.initState?.call(context, _formModel); - _logSubscription = - _logStandaloneDeliveryPointForm.onRecord.listen((LogRecord e) { + _logSubscription = _logStandaloneDeliveryPointForm.onRecord.listen(( + LogRecord e, + ) { // use `dumpErrorToConsole` for severe messages to ensure that severe // exceptions are formatted consistently with other Flutter examples and // avoids printing duplicate exceptions @@ -2583,16 +2623,14 @@ class _StandaloneDeliveryPointFormBuilderState } } -final _logStandaloneDeliveryPointForm = - Logger.detached('StandaloneDeliveryPointForm'); +final _logStandaloneDeliveryPointForm = Logger.detached( + 'StandaloneDeliveryPointForm', +); class StandaloneDeliveryPointForm implements FormModel { - StandaloneDeliveryPointForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + StandaloneDeliveryPointForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String nameControlName = "name"; @@ -2602,7 +2640,7 @@ class StandaloneDeliveryPointForm final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -2616,14 +2654,13 @@ class StandaloneDeliveryPointForm String get _nameValue => nameControl.value ?? ""; - Address? get _addressValue => addressForm.model; + Address? get _addressValue => containsAddress ? addressForm.model : null; String get _nameRawValue => nameControl.value ?? ""; - Address? get _addressRawValue => addressForm.rawModel; + Address? get _addressRawValue => + containsAddress ? addressForm.rawModel : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsName { try { form.control(nameControlPath()); @@ -2633,8 +2670,6 @@ class StandaloneDeliveryPointForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsAddress { try { form.control(addressControlPath()); @@ -2652,12 +2687,7 @@ class StandaloneDeliveryPointForm void get addressFocus => form.focus(addressControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void addressRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void addressRemove({bool updateParent = true, bool emitEvent = true}) { if (containsAddress) { final controlPath = path; if (controlPath == null) { @@ -2685,8 +2715,11 @@ class StandaloneDeliveryPointForm bool updateParent = true, bool emitEvent = true, }) { - nameControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + nameControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void addressValueUpdate( @@ -2694,8 +2727,11 @@ class StandaloneDeliveryPointForm bool updateParent = true, bool emitEvent = true, }) { - addressControl.updateValue(AddressForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + addressControl.updateValue( + AddressForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void nameValuePatch( @@ -2703,8 +2739,11 @@ class StandaloneDeliveryPointForm bool updateParent = true, bool emitEvent = true, }) { - nameControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + nameControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void addressValuePatch( @@ -2712,8 +2751,11 @@ class StandaloneDeliveryPointForm bool updateParent = true, bool emitEvent = true, }) { - addressControl.updateValue(AddressForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + addressControl.updateValue( + AddressForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void nameValueReset( @@ -2722,14 +2764,13 @@ class StandaloneDeliveryPointForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - nameControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => nameControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void addressValueReset( Address? value, { @@ -2737,11 +2778,11 @@ class StandaloneDeliveryPointForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - addressControl.reset( - value: AddressForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => addressControl.reset( + value: AddressForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); FormControl get nameControl => form.control(nameControlPath()) as FormControl; @@ -2808,10 +2849,7 @@ class StandaloneDeliveryPointForm } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; @@ -2819,7 +2857,9 @@ class StandaloneDeliveryPointForm addressForm.toggleDisabled(); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { addressForm.toggleDisabled(); currentForm.controls.forEach((key, control) { @@ -2878,11 +2918,11 @@ class StandaloneDeliveryPointForm DeliveryPoint? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue( - StandaloneDeliveryPointForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.updateValue( + StandaloneDeliveryPointForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -2900,17 +2940,14 @@ class StandaloneDeliveryPointForm DeliveryPoint? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -2961,24 +2998,29 @@ class StandaloneDeliveryPointForm String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(DeliveryPoint? deliveryPoint) => FormGroup({ - nameControlName: FormControl( - value: deliveryPoint?.name, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - addressControlName: AddressForm.formElements(deliveryPoint?.address) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(DeliveryPoint? deliveryPoint) => FormGroup( + { + nameControlName: FormControl( + value: deliveryPoint?.name, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + addressControlName: AddressForm.formElements(deliveryPoint?.address), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } class ReactiveStandaloneDeliveryPointFormArrayBuilder< - ReactiveStandaloneDeliveryPointFormArrayBuilderT> extends StatelessWidget { + ReactiveStandaloneDeliveryPointFormArrayBuilderT +> + extends StatelessWidget { const ReactiveStandaloneDeliveryPointFormArrayBuilder({ Key? key, this.control, @@ -2987,31 +3029,42 @@ class ReactiveStandaloneDeliveryPointFormArrayBuilder< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? - formControl; + formControl; final FormArray? Function( - StandaloneDeliveryPointForm formModel)? control; + StandaloneDeliveryPointForm formModel, + )? + control; - final Widget Function(BuildContext context, List itemList, - StandaloneDeliveryPointForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + StandaloneDeliveryPointForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveStandaloneDeliveryPointFormArrayBuilderT? item, - StandaloneDeliveryPointForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveStandaloneDeliveryPointFormArrayBuilderT? item, + StandaloneDeliveryPointForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl - control)? controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -3025,21 +3078,13 @@ class ReactiveStandaloneDeliveryPointFormArrayBuilder< final itemBuilder = this.itemBuilder; return ReactiveFormArrayItemBuilder< - ReactiveStandaloneDeliveryPointFormArrayBuilderT>( + ReactiveStandaloneDeliveryPointFormArrayBuilderT + >( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -3048,7 +3093,9 @@ class ReactiveStandaloneDeliveryPointFormArrayBuilder< } class ReactiveStandaloneDeliveryPointFormArrayBuilder2< - ReactiveStandaloneDeliveryPointFormArrayBuilderT> extends StatelessWidget { + ReactiveStandaloneDeliveryPointFormArrayBuilderT +> + extends StatelessWidget { const ReactiveStandaloneDeliveryPointFormArrayBuilder2({ Key? key, this.control, @@ -3057,37 +3104,48 @@ class ReactiveStandaloneDeliveryPointFormArrayBuilder2< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? - formControl; + formControl; final FormArray? Function( - StandaloneDeliveryPointForm formModel)? control; + StandaloneDeliveryPointForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - StandaloneDeliveryPointForm formModel - }) params)? builder; + ({ + BuildContext context, + List itemList, + StandaloneDeliveryPointForm formModel, + }) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveStandaloneDeliveryPointFormArrayBuilderT? item, - StandaloneDeliveryPointForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveStandaloneDeliveryPointFormArrayBuilderT? item, + StandaloneDeliveryPointForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl - control)? controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -3101,27 +3159,22 @@ class ReactiveStandaloneDeliveryPointFormArrayBuilder2< final itemBuilder = this.itemBuilder; return ReactiveFormArrayItemBuilder< - ReactiveStandaloneDeliveryPointFormArrayBuilderT>( + ReactiveStandaloneDeliveryPointFormArrayBuilderT + >( formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -3130,7 +3183,8 @@ class ReactiveStandaloneDeliveryPointFormArrayBuilder2< } class ReactiveStandaloneDeliveryPointFormFormGroupArrayBuilder< - ReactiveStandaloneDeliveryPointFormFormGroupArrayBuilderT> + ReactiveStandaloneDeliveryPointFormFormGroupArrayBuilderT +> extends StatelessWidget { const ReactiveStandaloneDeliveryPointFormFormGroupArrayBuilder({ Key? key, @@ -3138,26 +3192,39 @@ class ReactiveStandaloneDeliveryPointFormFormGroupArrayBuilder< this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? - extended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(StandaloneDeliveryPointForm formModel)? + getExtended; - final ExtendedControl?>, - List> - Function(StandaloneDeliveryPointForm formModel)? getExtended; - - final Widget Function(BuildContext context, List itemList, - StandaloneDeliveryPointForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + StandaloneDeliveryPointForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveStandaloneDeliveryPointFormFormGroupArrayBuilderT? item, - StandaloneDeliveryPointForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveStandaloneDeliveryPointFormFormGroupArrayBuilderT? item, + StandaloneDeliveryPointForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -3172,26 +3239,20 @@ class ReactiveStandaloneDeliveryPointFormFormGroupArrayBuilder< return StreamBuilder?>?>( stream: value.control.valueChanges, builder: (context, snapshot) { - final itemList = (value.value() ?? - []) - .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) - .values - .toList(); - - return builder?.call( - context, - itemList, - formModel, - ) ?? + final itemList = + (value.value() ?? + < + ReactiveStandaloneDeliveryPointFormFormGroupArrayBuilderT + >[]) + .asMap() + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) + .values + .toList(); + + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/reactive_forms_generator/example/lib/docs/delivery_list/delivery_list_output.gform.dart b/packages/reactive_forms_generator/example/lib/docs/delivery_list/delivery_list_output.gform.dart index 53db395d..81275f12 100644 --- a/packages/reactive_forms_generator/example/lib/docs/delivery_list/delivery_list_output.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/delivery_list/delivery_list_output.gform.dart @@ -19,7 +19,11 @@ class ReactiveDeliveryListOFormConsumer extends StatelessWidget { final Widget? child; final Widget Function( - BuildContext context, DeliveryListOForm formModel, Widget? child) builder; + BuildContext context, + DeliveryListOForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -38,11 +42,7 @@ class DeliveryListOFormInheritedStreamer extends InheritedStreamer { required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final DeliveryListOForm form; } @@ -64,19 +64,19 @@ class ReactiveDeliveryListOForm extends StatelessWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - static DeliveryListOForm? of( - BuildContext context, { - bool listen = true, - }) { + static DeliveryListOForm? of(BuildContext context, {bool listen = true}) { if (listen) { return context .dependOnInheritedWidgetOfExactType< - DeliveryListOFormInheritedStreamer>() + DeliveryListOFormInheritedStreamer + >() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - DeliveryListOFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + DeliveryListOFormInheritedStreamer + >(); return element == null ? null : (element.widget as DeliveryListOFormInheritedStreamer).form; @@ -124,10 +124,14 @@ class DeliveryListOFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, DeliveryListOForm formModel, Widget? child) builder; + BuildContext context, + DeliveryListOForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, DeliveryListOForm formModel)? - initState; + initState; @override _DeliveryListOFormBuilderState createState() => @@ -142,7 +146,10 @@ class _DeliveryListOFormBuilderState extends State { @override void initState() { _formModel = DeliveryListOForm( - DeliveryListOForm.formElements(widget.model), null, null); + DeliveryListOForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -220,11 +227,8 @@ final _logDeliveryListOForm = Logger.detached('DeliveryListOForm'); class DeliveryListOForm implements FormModel { - DeliveryListOForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + DeliveryListOForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String deliveryListControlName = "deliveryList"; @@ -234,7 +238,7 @@ class DeliveryListOForm final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -249,17 +253,17 @@ class DeliveryListOForm List get _deliveryListValue => deliveryListDeliveryPointOForm.map((e) => e.model).toList(); - List? get _clientListValue => - clientListClientOForm.map((e) => e.model).toList(); + List? get _clientListValue => containsClientList + ? clientListClientOForm.map((e) => e.model).toList() + : null; List get _deliveryListRawValue => deliveryListDeliveryPointOForm.map((e) => e.rawModel).toList(); - List? get _clientListRawValue => - clientListClientOForm.map((e) => e.rawModel).toList(); + List? get _clientListRawValue => containsClientList + ? clientListClientOForm.map((e) => e.rawModel).toList() + : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsDeliveryList { try { form.control(deliveryListControlPath()); @@ -269,8 +273,6 @@ class DeliveryListOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsClientList { try { form.control(clientListControlPath()); @@ -288,12 +290,7 @@ class DeliveryListOForm void get clientListFocus => form.focus(clientListControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void clientListRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void clientListRemove({bool updateParent = true, bool emitEvent = true}) { if (containsClientList) { final controlPath = path; if (controlPath == null) { @@ -344,17 +341,21 @@ class DeliveryListOForm if (toUpdate.isNotEmpty) { deliveryListControl.updateValue( - toUpdate - .map((e) => DeliveryPointOForm.formElements(e).rawValue) - .toList(), - updateParent: updateParent, - emitEvent: emitEvent); + toUpdate + .map((e) => DeliveryPointOForm.formElements(e).rawValue) + .toList(), + updateParent: updateParent, + emitEvent: emitEvent, + ); } if (toAdd.isNotEmpty) { toAdd.forEach((e) { - deliveryListControl.add(DeliveryPointOForm.formElements(e), - updateParent: updateParent, emitEvent: emitEvent); + deliveryListControl.add( + DeliveryPointOForm.formElements(e), + updateParent: updateParent, + emitEvent: emitEvent, + ); }); } } @@ -387,15 +388,19 @@ class DeliveryListOForm if (toUpdate.isNotEmpty) { clientListControl.updateValue( - toUpdate.map((e) => ClientOForm.formElements(e).rawValue).toList(), - updateParent: updateParent, - emitEvent: emitEvent); + toUpdate.map((e) => ClientOForm.formElements(e).rawValue).toList(), + updateParent: updateParent, + emitEvent: emitEvent, + ); } if (toAdd.isNotEmpty) { toAdd.forEach((e) { - clientListControl.add(ClientOForm.formElements(e), - updateParent: updateParent, emitEvent: emitEvent); + clientListControl.add( + ClientOForm.formElements(e), + updateParent: updateParent, + emitEvent: emitEvent, + ); }); } } @@ -440,18 +445,12 @@ class DeliveryListOForm ); } - void deliveryListClear({ - bool updateParent = true, - bool emitEvent = true, - }) { + void deliveryListClear({bool updateParent = true, bool emitEvent = true}) { deliveryListDeliveryPointOForm.clear(); deliveryListControl.clear(updateParent: updateParent, emitEvent: emitEvent); } - void clientListClear({ - bool updateParent = true, - bool emitEvent = true, - }) { + void clientListClear({bool updateParent = true, bool emitEvent = true}) { clientListClientOForm.clear(); clientListControl.clear(updateParent: updateParent, emitEvent: emitEvent); } @@ -464,20 +463,17 @@ class DeliveryListOForm final keys = deliveryListDeliveryPointOForm.asMap().keys; final toPatch = []; - (value).asMap().forEach( - (k, v) { - if (keys.contains(k)) { - toPatch.add(v); - } - }, - ); + (value).asMap().forEach((k, v) { + if (keys.contains(k)) { + toPatch.add(v); + } + }); deliveryListControl.patchValue( - toPatch - .map((e) => DeliveryPointOForm.formElements(e).rawValue) - .toList(), - updateParent: updateParent, - emitEvent: emitEvent); + toPatch.map((e) => DeliveryPointOForm.formElements(e).rawValue).toList(), + updateParent: updateParent, + emitEvent: emitEvent, + ); } void clientListValuePatch( @@ -488,18 +484,17 @@ class DeliveryListOForm final keys = clientListClientOForm.asMap().keys; final toPatch = []; - (value ?? []).asMap().forEach( - (k, v) { - if (keys.contains(k)) { - toPatch.add(v); - } - }, - ); + (value ?? []).asMap().forEach((k, v) { + if (keys.contains(k)) { + toPatch.add(v); + } + }); clientListControl.patchValue( - toPatch.map((e) => ClientOForm.formElements(e).rawValue).toList(), - updateParent: updateParent, - emitEvent: emitEvent); + toPatch.map((e) => ClientOForm.formElements(e).rawValue).toList(), + updateParent: updateParent, + emitEvent: emitEvent, + ); } void deliveryListValueReset( @@ -508,13 +503,13 @@ class DeliveryListOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - deliveryListControl.reset( - value: value - .map((e) => DeliveryPointOForm.formElements(e).rawValue) - .toList(), - updateParent: updateParent, - emitEvent: emitEvent); + }) => deliveryListControl.reset( + value: value + .map((e) => DeliveryPointOForm.formElements(e).rawValue) + .toList(), + updateParent: updateParent, + emitEvent: emitEvent, + ); void clientListValueReset( List? value, { @@ -522,12 +517,11 @@ class DeliveryListOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - clientListControl.reset( - value: - value?.map((e) => ClientOForm.formElements(e).rawValue).toList(), - updateParent: updateParent, - emitEvent: emitEvent); + }) => clientListControl.reset( + value: value?.map((e) => ClientOForm.formElements(e).rawValue).toList(), + updateParent: updateParent, + emitEvent: emitEvent, + ); FormArray> get deliveryListControl => form.control(deliveryListControlPath()) @@ -541,10 +535,16 @@ class DeliveryListOForm return values .asMap() - .map((k, v) => MapEntry( + .map( + (k, v) => MapEntry( k, DeliveryPointOForm( - form, pathBuilder("deliveryList.$k"), _formModel ?? this))) + form, + pathBuilder("deliveryList.$k"), + _formModel ?? this, + ), + ), + ) .values .toList(); } @@ -554,10 +554,12 @@ class DeliveryListOForm return values .asMap() - .map((k, v) => MapEntry( + .map( + (k, v) => MapEntry( k, - ClientOForm( - form, pathBuilder("clientList.$k"), _formModel ?? this))) + ClientOForm(form, pathBuilder("clientList.$k"), _formModel ?? this), + ), + ) .values .toList(); } @@ -599,18 +601,20 @@ class DeliveryListOForm } ExtendedControl?>, List> - get deliveryListExtendedControl => ExtendedControl< - List?>, List>( - form.control(deliveryListControlPath()) - as FormArray>, - () => deliveryListDeliveryPointOForm); + get deliveryListExtendedControl => + ExtendedControl?>, List>( + form.control(deliveryListControlPath()) + as FormArray>, + () => deliveryListDeliveryPointOForm, + ); ExtendedControl?>, List> - get clientListExtendedControl => - ExtendedControl?>, List>( - form.control(clientListControlPath()) - as FormArray>, - () => clientListClientOForm); + get clientListExtendedControl => + ExtendedControl?>, List>( + form.control(clientListControlPath()) + as FormArray>, + () => clientListClientOForm, + ); void addDeliveryListItem(DeliveryPointO value) { deliveryListControl.add(DeliveryPointOForm.formElements(value)); @@ -653,20 +657,21 @@ class DeliveryListOForm ); } return DeliveryListOOutput( - deliveryList: _deliveryListValue, clientList: _clientListValue); + deliveryList: _deliveryListValue, + clientList: _clientListValue, + ); } @override DeliveryListO get rawModel { return DeliveryListO( - deliveryList: _deliveryListRawValue, clientList: _clientListRawValue); + deliveryList: _deliveryListRawValue, + clientList: _clientListRawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; @@ -676,7 +681,9 @@ class DeliveryListOForm clientListClientOForm.forEach((e) => e.toggleDisabled()); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { deliveryListDeliveryPointOForm.forEach((e) => e.toggleDisabled()); clientListClientOForm.forEach((e) => e.toggleDisabled()); @@ -737,9 +744,11 @@ class DeliveryListOForm DeliveryListO? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(DeliveryListOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + DeliveryListOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -757,17 +766,14 @@ class DeliveryListOForm DeliveryListO? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -818,39 +824,40 @@ class DeliveryListOForm String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(DeliveryListO? deliveryListO) => FormGroup({ - deliveryListControlName: FormArray( - (deliveryListO?.deliveryList ?? []) - .map((e) => DeliveryPointOForm.formElements(e)) - .toList(), - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false), - clientListControlName: FormArray( - (deliveryListO?.clientList ?? []) - .map((e) => ClientOForm.formElements(e)) - .toList(), - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(DeliveryListO? deliveryListO) => FormGroup( + { + deliveryListControlName: FormArray( + (deliveryListO?.deliveryList ?? []) + .map((e) => DeliveryPointOForm.formElements(e)) + .toList(), + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ), + clientListControlName: FormArray( + (deliveryListO?.clientList ?? []) + .map((e) => ClientOForm.formElements(e)) + .toList(), + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } final _logDeliveryPointOForm = Logger.detached('DeliveryPointOForm'); class DeliveryPointOForm implements FormModel { - DeliveryPointOForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + DeliveryPointOForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String nameControlName = "name"; @@ -860,7 +867,7 @@ class DeliveryPointOForm final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -874,14 +881,14 @@ class DeliveryPointOForm String get _nameValue => nameControl.value ?? ""; - AddressOOutput? get _addressValue => addressForm.model; + AddressOOutput? get _addressValue => + containsAddress ? addressForm.model : null; String get _nameRawValue => nameControl.value ?? ""; - AddressO? get _addressRawValue => addressForm.rawModel; + AddressO? get _addressRawValue => + containsAddress ? addressForm.rawModel : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsName { try { form.control(nameControlPath()); @@ -891,8 +898,6 @@ class DeliveryPointOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsAddress { try { form.control(addressControlPath()); @@ -910,12 +915,7 @@ class DeliveryPointOForm void get addressFocus => form.focus(addressControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void addressRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void addressRemove({bool updateParent = true, bool emitEvent = true}) { if (containsAddress) { final controlPath = path; if (controlPath == null) { @@ -943,8 +943,11 @@ class DeliveryPointOForm bool updateParent = true, bool emitEvent = true, }) { - nameControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + nameControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void addressValueUpdate( @@ -952,8 +955,11 @@ class DeliveryPointOForm bool updateParent = true, bool emitEvent = true, }) { - addressControl.updateValue(AddressOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + addressControl.updateValue( + AddressOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void nameValuePatch( @@ -961,8 +967,11 @@ class DeliveryPointOForm bool updateParent = true, bool emitEvent = true, }) { - nameControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + nameControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void addressValuePatch( @@ -970,8 +979,11 @@ class DeliveryPointOForm bool updateParent = true, bool emitEvent = true, }) { - addressControl.updateValue(AddressOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + addressControl.updateValue( + AddressOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void nameValueReset( @@ -980,14 +992,13 @@ class DeliveryPointOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - nameControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => nameControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void addressValueReset( AddressO? value, { @@ -995,11 +1006,11 @@ class DeliveryPointOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - addressControl.reset( - value: AddressOForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => addressControl.reset( + value: AddressOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); FormControl get nameControl => form.control(nameControlPath()) as FormControl; @@ -1067,10 +1078,7 @@ class DeliveryPointOForm } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; @@ -1078,7 +1086,9 @@ class DeliveryPointOForm addressForm.toggleDisabled(); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { addressForm.toggleDisabled(); currentForm.controls.forEach((key, control) { @@ -1137,9 +1147,11 @@ class DeliveryPointOForm DeliveryPointO? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(DeliveryPointOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + DeliveryPointOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -1157,17 +1169,14 @@ class DeliveryPointOForm DeliveryPointO? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -1218,30 +1227,29 @@ class DeliveryPointOForm String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(DeliveryPointO? deliveryPointO) => FormGroup({ - nameControlName: FormControl( - value: deliveryPointO?.name, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - addressControlName: AddressOForm.formElements(deliveryPointO?.address) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(DeliveryPointO? deliveryPointO) => FormGroup( + { + nameControlName: FormControl( + value: deliveryPointO?.name, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + addressControlName: AddressOForm.formElements(deliveryPointO?.address), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } final _logAddressOForm = Logger.detached('AddressOForm'); class AddressOForm implements FormModel { - AddressOForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + AddressOForm(this.form, this.path, this._formModel) : initial = form.rawValue; static const String streetControlName = "street"; @@ -1251,7 +1259,7 @@ class AddressOForm implements FormModel { final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -1265,14 +1273,12 @@ class AddressOForm implements FormModel { String get _streetValue => streetControl.value as String; - String? get _cityValue => cityControl.value; + String? get _cityValue => containsCity ? cityControl.value : null; - String? get _streetRawValue => streetControl.value; + String? get _streetRawValue => containsStreet ? streetControl.value : null; - String? get _cityRawValue => cityControl.value; + String? get _cityRawValue => containsCity ? cityControl.value : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsStreet { try { form.control(streetControlPath()); @@ -1282,8 +1288,6 @@ class AddressOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsCity { try { form.control(cityControlPath()); @@ -1301,12 +1305,7 @@ class AddressOForm implements FormModel { void get cityFocus => form.focus(cityControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void streetRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void streetRemove({bool updateParent = true, bool emitEvent = true}) { if (containsStreet) { final controlPath = path; if (controlPath == null) { @@ -1329,12 +1328,7 @@ class AddressOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void cityRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void cityRemove({bool updateParent = true, bool emitEvent = true}) { if (containsCity) { final controlPath = path; if (controlPath == null) { @@ -1362,8 +1356,11 @@ class AddressOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - streetControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + streetControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void cityValueUpdate( @@ -1371,8 +1368,11 @@ class AddressOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - cityControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + cityControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void streetValuePatch( @@ -1380,8 +1380,11 @@ class AddressOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - streetControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + streetControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void cityValuePatch( @@ -1389,8 +1392,11 @@ class AddressOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - cityControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + cityControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void streetValueReset( @@ -1399,14 +1405,13 @@ class AddressOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - streetControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => streetControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void cityValueReset( String? value, { @@ -1414,14 +1419,13 @@ class AddressOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - cityControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => cityControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get streetControl => form.control(streetControlPath()) as FormControl; @@ -1486,17 +1490,16 @@ class AddressOForm implements FormModel { } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { @@ -1554,9 +1557,11 @@ class AddressOForm implements FormModel { AddressO? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(AddressOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + AddressOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -1574,17 +1579,14 @@ class AddressOForm implements FormModel { AddressO? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -1635,36 +1637,36 @@ class AddressOForm implements FormModel { String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(AddressO? addressO) => FormGroup({ - streetControlName: FormControl( - value: addressO?.street, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - cityControlName: FormControl( - value: addressO?.city, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(AddressO? addressO) => FormGroup( + { + streetControlName: FormControl( + value: addressO?.street, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + cityControlName: FormControl( + value: addressO?.city, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } final _logClientOForm = Logger.detached('ClientOForm'); class ClientOForm implements FormModel { - ClientOForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + ClientOForm(this.form, this.path, this._formModel) : initial = form.rawValue; static const String clientTypeControlName = "clientType"; @@ -1676,7 +1678,7 @@ class ClientOForm implements FormModel { final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -1692,18 +1694,16 @@ class ClientOForm implements FormModel { ClientType get _clientTypeValue => clientTypeControl.value as ClientType; - String? get _nameValue => nameControl.value; + String? get _nameValue => containsName ? nameControl.value : null; - String? get _notesValue => notesControl.value; + String? get _notesValue => containsNotes ? notesControl.value : null; ClientType get _clientTypeRawValue => clientTypeControl.value as ClientType; - String? get _nameRawValue => nameControl.value; + String? get _nameRawValue => containsName ? nameControl.value : null; - String? get _notesRawValue => notesControl.value; + String? get _notesRawValue => containsNotes ? notesControl.value : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsClientType { try { form.control(clientTypeControlPath()); @@ -1713,8 +1713,6 @@ class ClientOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsName { try { form.control(nameControlPath()); @@ -1724,8 +1722,6 @@ class ClientOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsNotes { try { form.control(notesControlPath()); @@ -1747,12 +1743,7 @@ class ClientOForm implements FormModel { void get notesFocus => form.focus(notesControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void nameRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void nameRemove({bool updateParent = true, bool emitEvent = true}) { if (containsName) { final controlPath = path; if (controlPath == null) { @@ -1775,12 +1766,7 @@ class ClientOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void notesRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void notesRemove({bool updateParent = true, bool emitEvent = true}) { if (containsNotes) { final controlPath = path; if (controlPath == null) { @@ -1808,8 +1794,11 @@ class ClientOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - clientTypeControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + clientTypeControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void nameValueUpdate( @@ -1817,8 +1806,11 @@ class ClientOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - nameControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + nameControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void notesValueUpdate( @@ -1826,8 +1818,11 @@ class ClientOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - notesControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + notesControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void clientTypeValuePatch( @@ -1835,8 +1830,11 @@ class ClientOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - clientTypeControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + clientTypeControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void nameValuePatch( @@ -1844,8 +1842,11 @@ class ClientOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - nameControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + nameControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void notesValuePatch( @@ -1853,8 +1854,11 @@ class ClientOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - notesControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + notesControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void clientTypeValueReset( @@ -1863,14 +1867,13 @@ class ClientOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - clientTypeControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => clientTypeControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void nameValueReset( String? value, { @@ -1878,14 +1881,13 @@ class ClientOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - nameControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => nameControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void notesValueReset( String? value, { @@ -1893,14 +1895,13 @@ class ClientOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - notesControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => notesControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get clientTypeControl => form.control(clientTypeControlPath()) as FormControl; @@ -1978,29 +1979,32 @@ class ClientOForm implements FormModel { ); } return ClientOOutput( - clientType: _clientTypeValue, name: _nameValue, notes: _notesValue); + clientType: _clientTypeValue, + name: _nameValue, + notes: _notesValue, + ); } @override ClientO get rawModel { return ClientO( - clientType: _clientTypeRawValue, - name: _nameRawValue, - notes: _notesRawValue); + clientType: _clientTypeRawValue, + name: _nameRawValue, + notes: _notesRawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { @@ -2058,9 +2062,11 @@ class ClientOForm implements FormModel { ClientO? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(ClientOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + ClientOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -2078,17 +2084,14 @@ class ClientOForm implements FormModel { ClientO? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -2139,41 +2142,48 @@ class ClientOForm implements FormModel { String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(ClientO? clientO) => FormGroup({ - clientTypeControlName: FormControl( - value: clientO?.clientType, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - nameControlName: FormControl( - value: clientO?.name, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - notesControlName: FormControl( - value: clientO?.notes, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(ClientO? clientO) => FormGroup( + { + clientTypeControlName: FormControl( + value: clientO?.clientType, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + nameControlName: FormControl( + value: clientO?.name, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + notesControlName: FormControl( + value: clientO?.notes, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } @Rf(output: true) class DeliveryListOOutput extends Equatable { final List deliveryList; final List? clientList; - const DeliveryListOOutput( - {@RfArray() required this.deliveryList, @RfArray() this.clientList}); + const DeliveryListOOutput({ + @RfArray() required this.deliveryList, + @RfArray() this.clientList, + }); @override List get props => [deliveryList, clientList]; } @@ -2183,9 +2193,10 @@ class DeliveryListOOutput extends Equatable { class DeliveryPointOOutput extends Equatable { final String name; final AddressOOutput? address; - const DeliveryPointOOutput( - {@RfControl(validators: [RequiredValidator()]) required this.name, - this.address}); + const DeliveryPointOOutput({ + @RfControl(validators: [RequiredValidator()]) required this.name, + this.address, + }); @override List get props => [name, address]; } @@ -2195,10 +2206,11 @@ class ClientOOutput extends Equatable { final ClientType clientType; final String? name; final String? notes; - const ClientOOutput( - {@RfControl() required this.clientType, - @RfControl() this.name, - @RfControl() this.notes}); + const ClientOOutput({ + @RfControl() required this.clientType, + @RfControl() this.name, + @RfControl() this.notes, + }); @override List get props => [name, notes]; } @@ -2207,15 +2219,18 @@ class ClientOOutput extends Equatable { class AddressOOutput extends Equatable { final String street; final String? city; - const AddressOOutput( - {@RfControl(validators: [RequiredValidator()]) required this.street, - @RfControl() this.city}); + const AddressOOutput({ + @RfControl(validators: [RequiredValidator()]) required this.street, + @RfControl() this.city, + }); @override List get props => [street, city]; } class ReactiveDeliveryListOFormArrayBuilder< - ReactiveDeliveryListOFormArrayBuilderT> extends StatelessWidget { + ReactiveDeliveryListOFormArrayBuilderT +> + extends StatelessWidget { const ReactiveDeliveryListOFormArrayBuilder({ Key? key, this.control, @@ -2224,30 +2239,41 @@ class ReactiveDeliveryListOFormArrayBuilder< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - DeliveryListOForm formModel)? control; + DeliveryListOForm formModel, + )? + control; - final Widget Function(BuildContext context, List itemList, - DeliveryListOForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + DeliveryListOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveDeliveryListOFormArrayBuilderT? item, - DeliveryListOForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveDeliveryListOFormArrayBuilderT? item, + DeliveryListOForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -2263,18 +2289,9 @@ class ReactiveDeliveryListOFormArrayBuilder< return ReactiveFormArrayItemBuilder( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -2283,7 +2300,9 @@ class ReactiveDeliveryListOFormArrayBuilder< } class ReactiveDeliveryListOFormArrayBuilder2< - ReactiveDeliveryListOFormArrayBuilderT> extends StatelessWidget { + ReactiveDeliveryListOFormArrayBuilderT +> + extends StatelessWidget { const ReactiveDeliveryListOFormArrayBuilder2({ Key? key, this.control, @@ -2292,36 +2311,43 @@ class ReactiveDeliveryListOFormArrayBuilder2< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - DeliveryListOForm formModel)? control; + DeliveryListOForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - DeliveryListOForm formModel - }) params)? builder; + ({BuildContext context, List itemList, DeliveryListOForm formModel}) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveDeliveryListOFormArrayBuilderT? item, - DeliveryListOForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveDeliveryListOFormArrayBuilderT? item, + DeliveryListOForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -2338,23 +2364,17 @@ class ReactiveDeliveryListOFormArrayBuilder2< formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -2363,32 +2383,48 @@ class ReactiveDeliveryListOFormArrayBuilder2< } class ReactiveDeliveryListOFormFormGroupArrayBuilder< - ReactiveDeliveryListOFormFormGroupArrayBuilderT> extends StatelessWidget { + ReactiveDeliveryListOFormFormGroupArrayBuilderT +> + extends StatelessWidget { const ReactiveDeliveryListOFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(DeliveryListOForm formModel)? + getExtended; - final ExtendedControl?>, - List>? extended; - - final ExtendedControl?>, - List> - Function(DeliveryListOForm formModel)? getExtended; - - final Widget Function(BuildContext context, List itemList, - DeliveryListOForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + DeliveryListOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveDeliveryListOFormFormGroupArrayBuilderT? item, - DeliveryListOForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveDeliveryListOFormFormGroupArrayBuilderT? item, + DeliveryListOForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -2403,26 +2439,18 @@ class ReactiveDeliveryListOFormFormGroupArrayBuilder< return StreamBuilder?>?>( stream: value.control.valueChanges, builder: (context, snapshot) { - final itemList = (value.value() ?? - []) - .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) - .values - .toList(); - - return builder?.call( - context, - itemList, - formModel, - ) ?? + final itemList = + (value.value() ?? + []) + .asMap() + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) + .values + .toList(); + + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); @@ -2438,8 +2466,12 @@ class ReactiveStandaloneDeliveryPointFormConsumer extends StatelessWidget { final Widget? child; - final Widget Function(BuildContext context, - StandaloneDeliveryPointForm formModel, Widget? child) builder; + final Widget Function( + BuildContext context, + StandaloneDeliveryPointForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -2459,11 +2491,7 @@ class StandaloneDeliveryPointFormInheritedStreamer required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final StandaloneDeliveryPointForm form; } @@ -2492,12 +2520,15 @@ class ReactiveStandaloneDeliveryPointForm extends StatelessWidget { if (listen) { return context .dependOnInheritedWidgetOfExactType< - StandaloneDeliveryPointFormInheritedStreamer>() + StandaloneDeliveryPointFormInheritedStreamer + >() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - StandaloneDeliveryPointFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + StandaloneDeliveryPointFormInheritedStreamer + >(); return element == null ? null : (element.widget as StandaloneDeliveryPointFormInheritedStreamer).form; @@ -2544,11 +2575,18 @@ class StandaloneDeliveryPointFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - final Widget Function(BuildContext context, - StandaloneDeliveryPointForm formModel, Widget? child) builder; + final Widget Function( + BuildContext context, + StandaloneDeliveryPointForm formModel, + Widget? child, + ) + builder; final void Function( - BuildContext context, StandaloneDeliveryPointForm formModel)? initState; + BuildContext context, + StandaloneDeliveryPointForm formModel, + )? + initState; @override _StandaloneDeliveryPointFormBuilderState createState() => @@ -2564,7 +2602,10 @@ class _StandaloneDeliveryPointFormBuilderState @override void initState() { _formModel = StandaloneDeliveryPointForm( - StandaloneDeliveryPointForm.formElements(widget.model), null, null); + StandaloneDeliveryPointForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -2572,8 +2613,9 @@ class _StandaloneDeliveryPointFormBuilderState widget.initState?.call(context, _formModel); - _logSubscription = - _logStandaloneDeliveryPointForm.onRecord.listen((LogRecord e) { + _logSubscription = _logStandaloneDeliveryPointForm.onRecord.listen(( + LogRecord e, + ) { // use `dumpErrorToConsole` for severe messages to ensure that severe // exceptions are formatted consistently with other Flutter examples and // avoids printing duplicate exceptions @@ -2639,16 +2681,14 @@ class _StandaloneDeliveryPointFormBuilderState } } -final _logStandaloneDeliveryPointForm = - Logger.detached('StandaloneDeliveryPointForm'); +final _logStandaloneDeliveryPointForm = Logger.detached( + 'StandaloneDeliveryPointForm', +); class StandaloneDeliveryPointForm implements FormModel { - StandaloneDeliveryPointForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + StandaloneDeliveryPointForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String nameControlName = "name"; @@ -2658,7 +2698,7 @@ class StandaloneDeliveryPointForm final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -2672,14 +2712,14 @@ class StandaloneDeliveryPointForm String get _nameValue => nameControl.value ?? ""; - AddressOOutput? get _addressValue => addressForm.model; + AddressOOutput? get _addressValue => + containsAddress ? addressForm.model : null; String get _nameRawValue => nameControl.value ?? ""; - AddressO? get _addressRawValue => addressForm.rawModel; + AddressO? get _addressRawValue => + containsAddress ? addressForm.rawModel : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsName { try { form.control(nameControlPath()); @@ -2689,8 +2729,6 @@ class StandaloneDeliveryPointForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsAddress { try { form.control(addressControlPath()); @@ -2708,12 +2746,7 @@ class StandaloneDeliveryPointForm void get addressFocus => form.focus(addressControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void addressRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void addressRemove({bool updateParent = true, bool emitEvent = true}) { if (containsAddress) { final controlPath = path; if (controlPath == null) { @@ -2741,8 +2774,11 @@ class StandaloneDeliveryPointForm bool updateParent = true, bool emitEvent = true, }) { - nameControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + nameControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void addressValueUpdate( @@ -2750,8 +2786,11 @@ class StandaloneDeliveryPointForm bool updateParent = true, bool emitEvent = true, }) { - addressControl.updateValue(AddressOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + addressControl.updateValue( + AddressOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void nameValuePatch( @@ -2759,8 +2798,11 @@ class StandaloneDeliveryPointForm bool updateParent = true, bool emitEvent = true, }) { - nameControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + nameControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void addressValuePatch( @@ -2768,8 +2810,11 @@ class StandaloneDeliveryPointForm bool updateParent = true, bool emitEvent = true, }) { - addressControl.updateValue(AddressOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + addressControl.updateValue( + AddressOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void nameValueReset( @@ -2778,14 +2823,13 @@ class StandaloneDeliveryPointForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - nameControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => nameControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void addressValueReset( AddressO? value, { @@ -2793,11 +2837,11 @@ class StandaloneDeliveryPointForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - addressControl.reset( - value: AddressOForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => addressControl.reset( + value: AddressOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); FormControl get nameControl => form.control(nameControlPath()) as FormControl; @@ -2865,10 +2909,7 @@ class StandaloneDeliveryPointForm } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; @@ -2876,7 +2917,9 @@ class StandaloneDeliveryPointForm addressForm.toggleDisabled(); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { addressForm.toggleDisabled(); currentForm.controls.forEach((key, control) { @@ -2935,11 +2978,11 @@ class StandaloneDeliveryPointForm DeliveryPointO? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue( - StandaloneDeliveryPointForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.updateValue( + StandaloneDeliveryPointForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -2957,17 +3000,14 @@ class StandaloneDeliveryPointForm DeliveryPointO? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -3018,24 +3058,29 @@ class StandaloneDeliveryPointForm String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(DeliveryPointO? deliveryPointO) => FormGroup({ - nameControlName: FormControl( - value: deliveryPointO?.name, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - addressControlName: AddressOForm.formElements(deliveryPointO?.address) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(DeliveryPointO? deliveryPointO) => FormGroup( + { + nameControlName: FormControl( + value: deliveryPointO?.name, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + addressControlName: AddressOForm.formElements(deliveryPointO?.address), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } class ReactiveStandaloneDeliveryPointFormArrayBuilder< - ReactiveStandaloneDeliveryPointFormArrayBuilderT> extends StatelessWidget { + ReactiveStandaloneDeliveryPointFormArrayBuilderT +> + extends StatelessWidget { const ReactiveStandaloneDeliveryPointFormArrayBuilder({ Key? key, this.control, @@ -3044,31 +3089,42 @@ class ReactiveStandaloneDeliveryPointFormArrayBuilder< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? - formControl; + formControl; final FormArray? Function( - StandaloneDeliveryPointForm formModel)? control; + StandaloneDeliveryPointForm formModel, + )? + control; - final Widget Function(BuildContext context, List itemList, - StandaloneDeliveryPointForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + StandaloneDeliveryPointForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveStandaloneDeliveryPointFormArrayBuilderT? item, - StandaloneDeliveryPointForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveStandaloneDeliveryPointFormArrayBuilderT? item, + StandaloneDeliveryPointForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl - control)? controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -3082,21 +3138,13 @@ class ReactiveStandaloneDeliveryPointFormArrayBuilder< final itemBuilder = this.itemBuilder; return ReactiveFormArrayItemBuilder< - ReactiveStandaloneDeliveryPointFormArrayBuilderT>( + ReactiveStandaloneDeliveryPointFormArrayBuilderT + >( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -3105,7 +3153,9 @@ class ReactiveStandaloneDeliveryPointFormArrayBuilder< } class ReactiveStandaloneDeliveryPointFormArrayBuilder2< - ReactiveStandaloneDeliveryPointFormArrayBuilderT> extends StatelessWidget { + ReactiveStandaloneDeliveryPointFormArrayBuilderT +> + extends StatelessWidget { const ReactiveStandaloneDeliveryPointFormArrayBuilder2({ Key? key, this.control, @@ -3114,37 +3164,48 @@ class ReactiveStandaloneDeliveryPointFormArrayBuilder2< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? - formControl; + formControl; final FormArray? Function( - StandaloneDeliveryPointForm formModel)? control; + StandaloneDeliveryPointForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - StandaloneDeliveryPointForm formModel - }) params)? builder; + ({ + BuildContext context, + List itemList, + StandaloneDeliveryPointForm formModel, + }) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveStandaloneDeliveryPointFormArrayBuilderT? item, - StandaloneDeliveryPointForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveStandaloneDeliveryPointFormArrayBuilderT? item, + StandaloneDeliveryPointForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl - control)? controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -3158,27 +3219,22 @@ class ReactiveStandaloneDeliveryPointFormArrayBuilder2< final itemBuilder = this.itemBuilder; return ReactiveFormArrayItemBuilder< - ReactiveStandaloneDeliveryPointFormArrayBuilderT>( + ReactiveStandaloneDeliveryPointFormArrayBuilderT + >( formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -3187,7 +3243,8 @@ class ReactiveStandaloneDeliveryPointFormArrayBuilder2< } class ReactiveStandaloneDeliveryPointFormFormGroupArrayBuilder< - ReactiveStandaloneDeliveryPointFormFormGroupArrayBuilderT> + ReactiveStandaloneDeliveryPointFormFormGroupArrayBuilderT +> extends StatelessWidget { const ReactiveStandaloneDeliveryPointFormFormGroupArrayBuilder({ Key? key, @@ -3195,26 +3252,39 @@ class ReactiveStandaloneDeliveryPointFormFormGroupArrayBuilder< this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? - extended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(StandaloneDeliveryPointForm formModel)? + getExtended; - final ExtendedControl?>, - List> - Function(StandaloneDeliveryPointForm formModel)? getExtended; - - final Widget Function(BuildContext context, List itemList, - StandaloneDeliveryPointForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + StandaloneDeliveryPointForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveStandaloneDeliveryPointFormFormGroupArrayBuilderT? item, - StandaloneDeliveryPointForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveStandaloneDeliveryPointFormFormGroupArrayBuilderT? item, + StandaloneDeliveryPointForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -3229,26 +3299,20 @@ class ReactiveStandaloneDeliveryPointFormFormGroupArrayBuilder< return StreamBuilder?>?>( stream: value.control.valueChanges, builder: (context, snapshot) { - final itemList = (value.value() ?? - []) - .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) - .values - .toList(); - - return builder?.call( - context, - itemList, - formModel, - ) ?? + final itemList = + (value.value() ?? + < + ReactiveStandaloneDeliveryPointFormFormGroupArrayBuilderT + >[]) + .asMap() + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) + .values + .toList(); + + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/reactive_forms_generator/example/lib/docs/freezed/freezed_class.gform.dart b/packages/reactive_forms_generator/example/lib/docs/freezed/freezed_class.gform.dart index e5cf1786..75b6174a 100644 --- a/packages/reactive_forms_generator/example/lib/docs/freezed/freezed_class.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/freezed/freezed_class.gform.dart @@ -19,7 +19,11 @@ class ReactiveFreezedClassFormConsumer extends StatelessWidget { final Widget? child; final Widget Function( - BuildContext context, FreezedClassForm formModel, Widget? child) builder; + BuildContext context, + FreezedClassForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -38,11 +42,7 @@ class FreezedClassFormInheritedStreamer extends InheritedStreamer { required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final FreezedClassForm form; } @@ -64,19 +64,19 @@ class ReactiveFreezedClassForm extends StatelessWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - static FreezedClassForm? of( - BuildContext context, { - bool listen = true, - }) { + static FreezedClassForm? of(BuildContext context, {bool listen = true}) { if (listen) { return context .dependOnInheritedWidgetOfExactType< - FreezedClassFormInheritedStreamer>() + FreezedClassFormInheritedStreamer + >() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - FreezedClassFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + FreezedClassFormInheritedStreamer + >(); return element == null ? null : (element.widget as FreezedClassFormInheritedStreamer).form; @@ -124,10 +124,14 @@ class FreezedClassFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, FreezedClassForm formModel, Widget? child) builder; + BuildContext context, + FreezedClassForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, FreezedClassForm formModel)? - initState; + initState; @override _FreezedClassFormBuilderState createState() => @@ -142,7 +146,10 @@ class _FreezedClassFormBuilderState extends State { @override void initState() { _formModel = FreezedClassForm( - FreezedClassForm.formElements(widget.model), null, null); + FreezedClassForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -219,11 +226,8 @@ class _FreezedClassFormBuilderState extends State { final _logFreezedClassForm = Logger.detached('FreezedClassForm'); class FreezedClassForm implements FormModel { - FreezedClassForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + FreezedClassForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String genderControlName = "gender"; @@ -241,7 +245,7 @@ class FreezedClassForm implements FormModel { final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -261,32 +265,32 @@ class FreezedClassForm implements FormModel { String selectedSpacesControlPath() => pathBuilder(selectedSpacesControlName); - String? get _genderValue => genderControl.value; + String? get _genderValue => containsGender ? genderControl.value : null; - String? get _idValue => idControl.value; + String? get _idValue => containsId ? idControl.value : null; - String? get _nameValue => nameControl.value; + String? get _nameValue => containsName ? nameControl.value : null; - String? get _logoImageValue => logoImageControl.value; + String? get _logoImageValue => + containsLogoImage ? logoImageControl.value : null; - double? get _yearValue => yearControl.value; + double? get _yearValue => containsYear ? yearControl.value : null; List get _selectedSpacesValue => selectedSpacesControl.value ?? []; - String? get _genderRawValue => genderControl.value; + String? get _genderRawValue => containsGender ? genderControl.value : null; - String? get _idRawValue => idControl.value; + String? get _idRawValue => containsId ? idControl.value : null; - String? get _nameRawValue => nameControl.value; + String? get _nameRawValue => containsName ? nameControl.value : null; - String? get _logoImageRawValue => logoImageControl.value; + String? get _logoImageRawValue => + containsLogoImage ? logoImageControl.value : null; - double? get _yearRawValue => yearControl.value; + double? get _yearRawValue => containsYear ? yearControl.value : null; List get _selectedSpacesRawValue => selectedSpacesControl.value ?? []; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsGender { try { form.control(genderControlPath()); @@ -296,8 +300,6 @@ class FreezedClassForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsId { try { form.control(idControlPath()); @@ -307,8 +309,6 @@ class FreezedClassForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsName { try { form.control(nameControlPath()); @@ -318,8 +318,6 @@ class FreezedClassForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsLogoImage { try { form.control(logoImageControlPath()); @@ -329,8 +327,6 @@ class FreezedClassForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsYear { try { form.control(yearControlPath()); @@ -340,8 +336,6 @@ class FreezedClassForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsSelectedSpaces { try { form.control(selectedSpacesControlPath()); @@ -375,12 +369,7 @@ class FreezedClassForm implements FormModel { void get selectedSpacesFocus => form.focus(selectedSpacesControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void genderRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void genderRemove({bool updateParent = true, bool emitEvent = true}) { if (containsGender) { final controlPath = path; if (controlPath == null) { @@ -403,12 +392,7 @@ class FreezedClassForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void idRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void idRemove({bool updateParent = true, bool emitEvent = true}) { if (containsId) { final controlPath = path; if (controlPath == null) { @@ -431,12 +415,7 @@ class FreezedClassForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void nameRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void nameRemove({bool updateParent = true, bool emitEvent = true}) { if (containsName) { final controlPath = path; if (controlPath == null) { @@ -459,12 +438,7 @@ class FreezedClassForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void logoImageRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void logoImageRemove({bool updateParent = true, bool emitEvent = true}) { if (containsLogoImage) { final controlPath = path; if (controlPath == null) { @@ -487,12 +461,7 @@ class FreezedClassForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void yearRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void yearRemove({bool updateParent = true, bool emitEvent = true}) { if (containsYear) { final controlPath = path; if (controlPath == null) { @@ -520,8 +489,11 @@ class FreezedClassForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - genderControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + genderControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void idValueUpdate( @@ -529,8 +501,11 @@ class FreezedClassForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - idControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + idControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void nameValueUpdate( @@ -538,8 +513,11 @@ class FreezedClassForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - nameControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + nameControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void logoImageValueUpdate( @@ -547,8 +525,11 @@ class FreezedClassForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - logoImageControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + logoImageControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void yearValueUpdate( @@ -556,8 +537,11 @@ class FreezedClassForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - yearControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + yearControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void selectedSpacesValueUpdate( @@ -565,8 +549,11 @@ class FreezedClassForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - selectedSpacesControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + selectedSpacesControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void genderValuePatch( @@ -574,8 +561,11 @@ class FreezedClassForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - genderControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + genderControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void idValuePatch( @@ -583,8 +573,11 @@ class FreezedClassForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - idControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + idControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void nameValuePatch( @@ -592,8 +585,11 @@ class FreezedClassForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - nameControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + nameControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void logoImageValuePatch( @@ -601,8 +597,11 @@ class FreezedClassForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - logoImageControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + logoImageControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void yearValuePatch( @@ -610,8 +609,11 @@ class FreezedClassForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - yearControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + yearControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void selectedSpacesValuePatch( @@ -619,8 +621,11 @@ class FreezedClassForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - selectedSpacesControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + selectedSpacesControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void genderValueReset( @@ -629,14 +634,13 @@ class FreezedClassForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - genderControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => genderControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void idValueReset( String? value, { @@ -644,14 +648,13 @@ class FreezedClassForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - idControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => idControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void nameValueReset( String? value, { @@ -659,14 +662,13 @@ class FreezedClassForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - nameControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => nameControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void logoImageValueReset( String? value, { @@ -674,14 +676,13 @@ class FreezedClassForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - logoImageControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => logoImageControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void yearValueReset( double? value, { @@ -689,14 +690,13 @@ class FreezedClassForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - yearControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => yearControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void selectedSpacesValueReset( List value, { @@ -704,14 +704,13 @@ class FreezedClassForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - selectedSpacesControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => selectedSpacesControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get genderControl => form.control(genderControlPath()) as FormControl; @@ -760,10 +759,7 @@ class FreezedClassForm implements FormModel { emitEvent: emitEvent, ); } else { - idControl.markAsEnabled( - updateParent: updateParent, - emitEvent: emitEvent, - ); + idControl.markAsEnabled(updateParent: updateParent, emitEvent: emitEvent); } } @@ -850,36 +846,39 @@ class FreezedClassForm implements FormModel { StackTrace.current, ); } - return FreezedClass(_genderValue, - id: _idValue, - name: _nameValue, - logoImage: _logoImageValue, - year: _yearValue, - selectedSpaces: _selectedSpacesValue); + return FreezedClass( + _genderValue, + id: _idValue, + name: _nameValue, + logoImage: _logoImageValue, + year: _yearValue, + selectedSpaces: _selectedSpacesValue, + ); } @override FreezedClass get rawModel { - return FreezedClass(_genderRawValue, - id: _idRawValue, - name: _nameRawValue, - logoImage: _logoImageRawValue, - year: _yearRawValue, - selectedSpaces: _selectedSpacesRawValue); + return FreezedClass( + _genderRawValue, + id: _idRawValue, + name: _nameRawValue, + logoImage: _logoImageRawValue, + year: _yearRawValue, + selectedSpaces: _selectedSpacesRawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { @@ -937,9 +936,11 @@ class FreezedClassForm implements FormModel { FreezedClass? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(FreezedClassForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + FreezedClassForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -957,17 +958,14 @@ class FreezedClassForm implements FormModel { FreezedClass? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -1018,58 +1016,68 @@ class FreezedClassForm implements FormModel { String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(FreezedClass? freezedClass) => FormGroup({ - genderControlName: FormControl( - value: freezedClass?.gender, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - idControlName: FormControl( - value: freezedClass?.id, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - nameControlName: FormControl( - value: freezedClass?.name, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - logoImageControlName: FormControl( - value: freezedClass?.logoImage, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - yearControlName: FormControl( - value: freezedClass?.year, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - selectedSpacesControlName: FormControl>( - value: freezedClass?.selectedSpaces, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(FreezedClass? freezedClass) => FormGroup( + { + genderControlName: FormControl( + value: freezedClass?.gender, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + idControlName: FormControl( + value: freezedClass?.id, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + nameControlName: FormControl( + value: freezedClass?.name, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + logoImageControlName: FormControl( + value: freezedClass?.logoImage, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + yearControlName: FormControl( + value: freezedClass?.year, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + selectedSpacesControlName: FormControl>( + value: freezedClass?.selectedSpaces, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } class ReactiveFreezedClassFormArrayBuilder< - ReactiveFreezedClassFormArrayBuilderT> extends StatelessWidget { + ReactiveFreezedClassFormArrayBuilderT +> + extends StatelessWidget { const ReactiveFreezedClassFormArrayBuilder({ Key? key, this.control, @@ -1078,30 +1086,41 @@ class ReactiveFreezedClassFormArrayBuilder< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - FreezedClassForm formModel)? control; + FreezedClassForm formModel, + )? + control; - final Widget Function(BuildContext context, List itemList, - FreezedClassForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + FreezedClassForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveFreezedClassFormArrayBuilderT? item, - FreezedClassForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveFreezedClassFormArrayBuilderT? item, + FreezedClassForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -1117,18 +1136,9 @@ class ReactiveFreezedClassFormArrayBuilder< return ReactiveFormArrayItemBuilder( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1137,7 +1147,9 @@ class ReactiveFreezedClassFormArrayBuilder< } class ReactiveFreezedClassFormArrayBuilder2< - ReactiveFreezedClassFormArrayBuilderT> extends StatelessWidget { + ReactiveFreezedClassFormArrayBuilderT +> + extends StatelessWidget { const ReactiveFreezedClassFormArrayBuilder2({ Key? key, this.control, @@ -1146,36 +1158,43 @@ class ReactiveFreezedClassFormArrayBuilder2< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - FreezedClassForm formModel)? control; + FreezedClassForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - FreezedClassForm formModel - }) params)? builder; + ({BuildContext context, List itemList, FreezedClassForm formModel}) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveFreezedClassFormArrayBuilderT? item, - FreezedClassForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveFreezedClassFormArrayBuilderT? item, + FreezedClassForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -1192,23 +1211,17 @@ class ReactiveFreezedClassFormArrayBuilder2< formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1217,32 +1230,48 @@ class ReactiveFreezedClassFormArrayBuilder2< } class ReactiveFreezedClassFormFormGroupArrayBuilder< - ReactiveFreezedClassFormFormGroupArrayBuilderT> extends StatelessWidget { + ReactiveFreezedClassFormFormGroupArrayBuilderT +> + extends StatelessWidget { const ReactiveFreezedClassFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(FreezedClassForm formModel)? + getExtended; - final ExtendedControl?>, - List>? extended; - - final ExtendedControl?>, - List> - Function(FreezedClassForm formModel)? getExtended; - - final Widget Function(BuildContext context, List itemList, - FreezedClassForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + FreezedClassForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveFreezedClassFormFormGroupArrayBuilderT? item, - FreezedClassForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveFreezedClassFormFormGroupArrayBuilderT? item, + FreezedClassForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -1257,26 +1286,18 @@ class ReactiveFreezedClassFormFormGroupArrayBuilder< return StreamBuilder?>?>( stream: value.control.valueChanges, builder: (context, snapshot) { - final itemList = (value.value() ?? - []) - .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) - .values - .toList(); - - return builder?.call( - context, - itemList, - formModel, - ) ?? + final itemList = + (value.value() ?? + []) + .asMap() + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) + .values + .toList(); + + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/reactive_forms_generator/example/lib/docs/freezed/freezed_class_output.gform.dart b/packages/reactive_forms_generator/example/lib/docs/freezed/freezed_class_output.gform.dart index 632df166..412dd485 100644 --- a/packages/reactive_forms_generator/example/lib/docs/freezed/freezed_class_output.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/freezed/freezed_class_output.gform.dart @@ -19,7 +19,11 @@ class ReactiveFreezedClassOFormConsumer extends StatelessWidget { final Widget? child; final Widget Function( - BuildContext context, FreezedClassOForm formModel, Widget? child) builder; + BuildContext context, + FreezedClassOForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -38,11 +42,7 @@ class FreezedClassOFormInheritedStreamer extends InheritedStreamer { required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final FreezedClassOForm form; } @@ -64,19 +64,19 @@ class ReactiveFreezedClassOForm extends StatelessWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - static FreezedClassOForm? of( - BuildContext context, { - bool listen = true, - }) { + static FreezedClassOForm? of(BuildContext context, {bool listen = true}) { if (listen) { return context .dependOnInheritedWidgetOfExactType< - FreezedClassOFormInheritedStreamer>() + FreezedClassOFormInheritedStreamer + >() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - FreezedClassOFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + FreezedClassOFormInheritedStreamer + >(); return element == null ? null : (element.widget as FreezedClassOFormInheritedStreamer).form; @@ -124,10 +124,14 @@ class FreezedClassOFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, FreezedClassOForm formModel, Widget? child) builder; + BuildContext context, + FreezedClassOForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, FreezedClassOForm formModel)? - initState; + initState; @override _FreezedClassOFormBuilderState createState() => @@ -142,7 +146,10 @@ class _FreezedClassOFormBuilderState extends State { @override void initState() { _formModel = FreezedClassOForm( - FreezedClassOForm.formElements(widget.model), null, null); + FreezedClassOForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -220,11 +227,8 @@ final _logFreezedClassOForm = Logger.detached('FreezedClassOForm'); class FreezedClassOForm implements FormModel { - FreezedClassOForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + FreezedClassOForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String genderControlName = "gender"; @@ -248,7 +252,7 @@ class FreezedClassOForm final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -274,44 +278,44 @@ class FreezedClassOForm String selectedSpacesControlPath() => pathBuilder(selectedSpacesControlName); - String? get _genderValue => genderControl.value; + String? get _genderValue => containsGender ? genderControl.value : null; String get _genderRValue => genderRControl.value as String; - String? get _idValue => idControl.value; + String? get _idValue => containsId ? idControl.value : null; String get _idRValue => idRControl.value as String; String get _idR2Value => idR2Control.value ?? ''; - String? get _nameValue => nameControl.value; + String? get _nameValue => containsName ? nameControl.value : null; - String? get _logoImageValue => logoImageControl.value; + String? get _logoImageValue => + containsLogoImage ? logoImageControl.value : null; - double? get _yearValue => yearControl.value; + double? get _yearValue => containsYear ? yearControl.value : null; List get _selectedSpacesValue => selectedSpacesControl.value ?? []; - String? get _genderRawValue => genderControl.value; + String? get _genderRawValue => containsGender ? genderControl.value : null; - String? get _genderRRawValue => genderRControl.value; + String? get _genderRRawValue => containsGenderR ? genderRControl.value : null; - String? get _idRawValue => idControl.value; + String? get _idRawValue => containsId ? idControl.value : null; - String? get _idRRawValue => idRControl.value; + String? get _idRRawValue => containsIdR ? idRControl.value : null; String get _idR2RawValue => idR2Control.value ?? ''; - String? get _nameRawValue => nameControl.value; + String? get _nameRawValue => containsName ? nameControl.value : null; - String? get _logoImageRawValue => logoImageControl.value; + String? get _logoImageRawValue => + containsLogoImage ? logoImageControl.value : null; - double? get _yearRawValue => yearControl.value; + double? get _yearRawValue => containsYear ? yearControl.value : null; List get _selectedSpacesRawValue => selectedSpacesControl.value ?? []; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsGender { try { form.control(genderControlPath()); @@ -321,8 +325,6 @@ class FreezedClassOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsGenderR { try { form.control(genderRControlPath()); @@ -332,8 +334,6 @@ class FreezedClassOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsId { try { form.control(idControlPath()); @@ -343,8 +343,6 @@ class FreezedClassOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsIdR { try { form.control(idRControlPath()); @@ -354,8 +352,6 @@ class FreezedClassOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsIdR2 { try { form.control(idR2ControlPath()); @@ -365,8 +361,6 @@ class FreezedClassOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsName { try { form.control(nameControlPath()); @@ -376,8 +370,6 @@ class FreezedClassOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsLogoImage { try { form.control(logoImageControlPath()); @@ -387,8 +379,6 @@ class FreezedClassOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsYear { try { form.control(yearControlPath()); @@ -398,8 +388,6 @@ class FreezedClassOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsSelectedSpaces { try { form.control(selectedSpacesControlPath()); @@ -445,12 +433,7 @@ class FreezedClassOForm void get selectedSpacesFocus => form.focus(selectedSpacesControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void genderRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void genderRemove({bool updateParent = true, bool emitEvent = true}) { if (containsGender) { final controlPath = path; if (controlPath == null) { @@ -473,12 +456,7 @@ class FreezedClassOForm } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void genderRRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void genderRRemove({bool updateParent = true, bool emitEvent = true}) { if (containsGenderR) { final controlPath = path; if (controlPath == null) { @@ -501,12 +479,7 @@ class FreezedClassOForm } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void idRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void idRemove({bool updateParent = true, bool emitEvent = true}) { if (containsId) { final controlPath = path; if (controlPath == null) { @@ -529,12 +502,7 @@ class FreezedClassOForm } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void idRRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void idRRemove({bool updateParent = true, bool emitEvent = true}) { if (containsIdR) { final controlPath = path; if (controlPath == null) { @@ -557,12 +525,7 @@ class FreezedClassOForm } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void nameRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void nameRemove({bool updateParent = true, bool emitEvent = true}) { if (containsName) { final controlPath = path; if (controlPath == null) { @@ -585,12 +548,7 @@ class FreezedClassOForm } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void logoImageRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void logoImageRemove({bool updateParent = true, bool emitEvent = true}) { if (containsLogoImage) { final controlPath = path; if (controlPath == null) { @@ -613,12 +571,7 @@ class FreezedClassOForm } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void yearRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void yearRemove({bool updateParent = true, bool emitEvent = true}) { if (containsYear) { final controlPath = path; if (controlPath == null) { @@ -646,8 +599,11 @@ class FreezedClassOForm bool updateParent = true, bool emitEvent = true, }) { - genderControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + genderControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void genderRValueUpdate( @@ -655,8 +611,11 @@ class FreezedClassOForm bool updateParent = true, bool emitEvent = true, }) { - genderRControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + genderRControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void idValueUpdate( @@ -664,8 +623,11 @@ class FreezedClassOForm bool updateParent = true, bool emitEvent = true, }) { - idControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + idControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void idRValueUpdate( @@ -673,8 +635,11 @@ class FreezedClassOForm bool updateParent = true, bool emitEvent = true, }) { - idRControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + idRControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void idR2ValueUpdate( @@ -682,8 +647,11 @@ class FreezedClassOForm bool updateParent = true, bool emitEvent = true, }) { - idR2Control.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + idR2Control.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void nameValueUpdate( @@ -691,8 +659,11 @@ class FreezedClassOForm bool updateParent = true, bool emitEvent = true, }) { - nameControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + nameControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void logoImageValueUpdate( @@ -700,8 +671,11 @@ class FreezedClassOForm bool updateParent = true, bool emitEvent = true, }) { - logoImageControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + logoImageControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void yearValueUpdate( @@ -709,8 +683,11 @@ class FreezedClassOForm bool updateParent = true, bool emitEvent = true, }) { - yearControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + yearControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void selectedSpacesValueUpdate( @@ -718,8 +695,11 @@ class FreezedClassOForm bool updateParent = true, bool emitEvent = true, }) { - selectedSpacesControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + selectedSpacesControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void genderValuePatch( @@ -727,8 +707,11 @@ class FreezedClassOForm bool updateParent = true, bool emitEvent = true, }) { - genderControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + genderControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void genderRValuePatch( @@ -736,8 +719,11 @@ class FreezedClassOForm bool updateParent = true, bool emitEvent = true, }) { - genderRControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + genderRControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void idValuePatch( @@ -745,8 +731,11 @@ class FreezedClassOForm bool updateParent = true, bool emitEvent = true, }) { - idControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + idControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void idRValuePatch( @@ -754,8 +743,11 @@ class FreezedClassOForm bool updateParent = true, bool emitEvent = true, }) { - idRControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + idRControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void idR2ValuePatch( @@ -763,8 +755,11 @@ class FreezedClassOForm bool updateParent = true, bool emitEvent = true, }) { - idR2Control.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + idR2Control.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void nameValuePatch( @@ -772,8 +767,11 @@ class FreezedClassOForm bool updateParent = true, bool emitEvent = true, }) { - nameControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + nameControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void logoImageValuePatch( @@ -781,8 +779,11 @@ class FreezedClassOForm bool updateParent = true, bool emitEvent = true, }) { - logoImageControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + logoImageControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void yearValuePatch( @@ -790,8 +791,11 @@ class FreezedClassOForm bool updateParent = true, bool emitEvent = true, }) { - yearControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + yearControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void selectedSpacesValuePatch( @@ -799,8 +803,11 @@ class FreezedClassOForm bool updateParent = true, bool emitEvent = true, }) { - selectedSpacesControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + selectedSpacesControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void genderValueReset( @@ -809,14 +816,13 @@ class FreezedClassOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - genderControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => genderControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void genderRValueReset( String? value, { @@ -824,14 +830,13 @@ class FreezedClassOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - genderRControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => genderRControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void idValueReset( String? value, { @@ -839,14 +844,13 @@ class FreezedClassOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - idControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => idControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void idRValueReset( String? value, { @@ -854,14 +858,13 @@ class FreezedClassOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - idRControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => idRControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void idR2ValueReset( String value, { @@ -869,14 +872,13 @@ class FreezedClassOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - idR2Control.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => idR2Control.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void nameValueReset( String? value, { @@ -884,14 +886,13 @@ class FreezedClassOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - nameControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => nameControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void logoImageValueReset( String? value, { @@ -899,14 +900,13 @@ class FreezedClassOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - logoImageControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => logoImageControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void yearValueReset( double? value, { @@ -914,14 +914,13 @@ class FreezedClassOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - yearControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => yearControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void selectedSpacesValueReset( List value, { @@ -929,14 +928,13 @@ class FreezedClassOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - selectedSpacesControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => selectedSpacesControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get genderControl => form.control(genderControlPath()) as FormControl; @@ -1012,10 +1010,7 @@ class FreezedClassOForm emitEvent: emitEvent, ); } else { - idControl.markAsEnabled( - updateParent: updateParent, - emitEvent: emitEvent, - ); + idControl.markAsEnabled(updateParent: updateParent, emitEvent: emitEvent); } } @@ -1139,40 +1134,45 @@ class FreezedClassOForm StackTrace.current, ); } - return FreezedClassOOutput(_genderValue, _genderRValue, - id: _idValue, - idR: _idRValue, - idR2: _idR2Value, - name: _nameValue, - logoImage: _logoImageValue, - year: _yearValue, - selectedSpaces: _selectedSpacesValue); + return FreezedClassOOutput( + _genderValue, + _genderRValue, + id: _idValue, + idR: _idRValue, + idR2: _idR2Value, + name: _nameValue, + logoImage: _logoImageValue, + year: _yearValue, + selectedSpaces: _selectedSpacesValue, + ); } @override FreezedClassO get rawModel { - return FreezedClassO(_genderRawValue, _genderRRawValue, - id: _idRawValue, - idR: _idRRawValue, - idR2: _idR2RawValue, - name: _nameRawValue, - logoImage: _logoImageRawValue, - year: _yearRawValue, - selectedSpaces: _selectedSpacesRawValue); + return FreezedClassO( + _genderRawValue, + _genderRRawValue, + id: _idRawValue, + idR: _idRRawValue, + idR2: _idR2RawValue, + name: _nameRawValue, + logoImage: _logoImageRawValue, + year: _yearRawValue, + selectedSpaces: _selectedSpacesRawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { @@ -1230,9 +1230,11 @@ class FreezedClassOForm FreezedClassO? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(FreezedClassOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + FreezedClassOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -1250,17 +1252,14 @@ class FreezedClassOForm FreezedClassO? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -1311,97 +1310,112 @@ class FreezedClassOForm String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(FreezedClassO? freezedClassO) => FormGroup({ - genderControlName: FormControl( - value: freezedClassO?.gender, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - genderRControlName: FormControl( - value: freezedClassO?.genderR, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - idControlName: FormControl( - value: freezedClassO?.id, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - idRControlName: FormControl( - value: freezedClassO?.idR, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - idR2ControlName: FormControl( - value: freezedClassO?.idR2, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - nameControlName: FormControl( - value: freezedClassO?.name, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - logoImageControlName: FormControl( - value: freezedClassO?.logoImage, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - yearControlName: FormControl( - value: freezedClassO?.year, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - selectedSpacesControlName: FormControl>( - value: freezedClassO?.selectedSpaces, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(FreezedClassO? freezedClassO) => FormGroup( + { + genderControlName: FormControl( + value: freezedClassO?.gender, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + genderRControlName: FormControl( + value: freezedClassO?.genderR, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + idControlName: FormControl( + value: freezedClassO?.id, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + idRControlName: FormControl( + value: freezedClassO?.idR, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + idR2ControlName: FormControl( + value: freezedClassO?.idR2, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + nameControlName: FormControl( + value: freezedClassO?.name, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + logoImageControlName: FormControl( + value: freezedClassO?.logoImage, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + yearControlName: FormControl( + value: freezedClassO?.year, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + selectedSpacesControlName: FormControl>( + value: freezedClassO?.selectedSpaces, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } @freezed @Rf(output: true) abstract class FreezedClassOOutput with _$FreezedClassOOutput { FreezedClassOOutput._(); - factory FreezedClassOOutput(@RfControl() String? gender, - @RfControl(validators: [RequiredValidator()]) String genderR, - {@RfControl() String? id, - @RfControl(validators: [RequiredValidator()]) required String idR, - @RfControl(validators: [RequiredValidator()]) required String idR2, - @RfControl() String? name, - @JsonKey(name: 'logo_image') @RfControl() String? logoImage, - @RfControl() double? year, - @Default([]) List selectedSpaces}) = _FreezedClassOOutput; + factory FreezedClassOOutput( + @RfControl() String? gender, + @RfControl(validators: [RequiredValidator()]) String genderR, { + @RfControl() String? id, + @RfControl(validators: [RequiredValidator()]) required String idR, + @RfControl(validators: [RequiredValidator()]) required String idR2, + @RfControl() String? name, + @JsonKey(name: 'logo_image') @RfControl() String? logoImage, + @RfControl() double? year, + @Default([]) List selectedSpaces, + }) = _FreezedClassOOutput; factory FreezedClassOOutput.fromJson(Map json) => _$FreezedClassOOutputFromJson(json); bool method() => false; } class ReactiveFreezedClassOFormArrayBuilder< - ReactiveFreezedClassOFormArrayBuilderT> extends StatelessWidget { + ReactiveFreezedClassOFormArrayBuilderT +> + extends StatelessWidget { const ReactiveFreezedClassOFormArrayBuilder({ Key? key, this.control, @@ -1410,30 +1424,41 @@ class ReactiveFreezedClassOFormArrayBuilder< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - FreezedClassOForm formModel)? control; + FreezedClassOForm formModel, + )? + control; - final Widget Function(BuildContext context, List itemList, - FreezedClassOForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + FreezedClassOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveFreezedClassOFormArrayBuilderT? item, - FreezedClassOForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveFreezedClassOFormArrayBuilderT? item, + FreezedClassOForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -1449,18 +1474,9 @@ class ReactiveFreezedClassOFormArrayBuilder< return ReactiveFormArrayItemBuilder( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1469,7 +1485,9 @@ class ReactiveFreezedClassOFormArrayBuilder< } class ReactiveFreezedClassOFormArrayBuilder2< - ReactiveFreezedClassOFormArrayBuilderT> extends StatelessWidget { + ReactiveFreezedClassOFormArrayBuilderT +> + extends StatelessWidget { const ReactiveFreezedClassOFormArrayBuilder2({ Key? key, this.control, @@ -1478,36 +1496,43 @@ class ReactiveFreezedClassOFormArrayBuilder2< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - FreezedClassOForm formModel)? control; + FreezedClassOForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - FreezedClassOForm formModel - }) params)? builder; + ({BuildContext context, List itemList, FreezedClassOForm formModel}) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveFreezedClassOFormArrayBuilderT? item, - FreezedClassOForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveFreezedClassOFormArrayBuilderT? item, + FreezedClassOForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -1524,23 +1549,17 @@ class ReactiveFreezedClassOFormArrayBuilder2< formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1549,32 +1568,48 @@ class ReactiveFreezedClassOFormArrayBuilder2< } class ReactiveFreezedClassOFormFormGroupArrayBuilder< - ReactiveFreezedClassOFormFormGroupArrayBuilderT> extends StatelessWidget { + ReactiveFreezedClassOFormFormGroupArrayBuilderT +> + extends StatelessWidget { const ReactiveFreezedClassOFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? extended; - - final ExtendedControl?>, - List> - Function(FreezedClassOForm formModel)? getExtended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(FreezedClassOForm formModel)? + getExtended; - final Widget Function(BuildContext context, List itemList, - FreezedClassOForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + FreezedClassOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveFreezedClassOFormFormGroupArrayBuilderT? item, - FreezedClassOForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveFreezedClassOFormFormGroupArrayBuilderT? item, + FreezedClassOForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -1589,26 +1624,18 @@ class ReactiveFreezedClassOFormFormGroupArrayBuilder< return StreamBuilder?>?>( stream: value.control.valueChanges, builder: (context, snapshot) { - final itemList = (value.value() ?? - []) - .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) - .values - .toList(); - - return builder?.call( - context, - itemList, - formModel, - ) ?? + final itemList = + (value.value() ?? + []) + .asMap() + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) + .values + .toList(); + + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/reactive_forms_generator/example/lib/docs/freezed2/test.gform.dart b/packages/reactive_forms_generator/example/lib/docs/freezed2/test.gform.dart index 115d7c29..72a1bb1f 100644 --- a/packages/reactive_forms_generator/example/lib/docs/freezed2/test.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/freezed2/test.gform.dart @@ -10,16 +10,13 @@ part of 'test.dart'; // ************************************************************************** class ReactiveTestFormConsumer extends StatelessWidget { - const ReactiveTestFormConsumer({ - Key? key, - required this.builder, - this.child, - }) : super(key: key); + const ReactiveTestFormConsumer({Key? key, required this.builder, this.child}) + : super(key: key); final Widget? child; final Widget Function(BuildContext context, TestForm formModel, Widget? child) - builder; + builder; @override Widget build(BuildContext context) { @@ -38,11 +35,7 @@ class TestFormInheritedStreamer extends InheritedStreamer { required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final TestForm form; } @@ -64,10 +57,7 @@ class ReactiveTestForm extends StatelessWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - static TestForm? of( - BuildContext context, { - bool listen = true, - }) { + static TestForm? of(BuildContext context, {bool listen = true}) { if (listen) { return context .dependOnInheritedWidgetOfExactType() @@ -121,7 +111,7 @@ class TestFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function(BuildContext context, TestForm formModel, Widget? child) - builder; + builder; final void Function(BuildContext context, TestForm formModel)? initState; @@ -213,11 +203,7 @@ class _TestFormBuilderState extends State { final _logTestForm = Logger.detached('TestForm'); class TestForm implements FormModel { - TestForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + TestForm(this.form, this.path, this._formModel) : initial = form.rawValue; static const String titleControlName = "title"; @@ -227,7 +213,7 @@ class TestForm implements FormModel { final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -241,14 +227,14 @@ class TestForm implements FormModel { String get _titleValue => titleControl.value as String; - String? get _descriptionValue => descriptionControl.value; + String? get _descriptionValue => + containsDescription ? descriptionControl.value : null; String get _titleRawValue => titleControl.value as String; - String? get _descriptionRawValue => descriptionControl.value; + String? get _descriptionRawValue => + containsDescription ? descriptionControl.value : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsTitle { try { form.control(titleControlPath()); @@ -258,8 +244,6 @@ class TestForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsDescription { try { form.control(descriptionControlPath()); @@ -277,12 +261,7 @@ class TestForm implements FormModel { void get descriptionFocus => form.focus(descriptionControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void descriptionRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void descriptionRemove({bool updateParent = true, bool emitEvent = true}) { if (containsDescription) { final controlPath = path; if (controlPath == null) { @@ -310,8 +289,11 @@ class TestForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - titleControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + titleControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void descriptionValueUpdate( @@ -319,8 +301,11 @@ class TestForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - descriptionControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + descriptionControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void titleValuePatch( @@ -328,8 +313,11 @@ class TestForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - titleControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + titleControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void descriptionValuePatch( @@ -337,8 +325,11 @@ class TestForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - descriptionControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + descriptionControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void titleValueReset( @@ -347,14 +338,13 @@ class TestForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - titleControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => titleControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void descriptionValueReset( String? value, { @@ -362,14 +352,13 @@ class TestForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - descriptionControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => descriptionControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get titleControl => form.control(titleControlPath()) as FormControl; @@ -433,17 +422,16 @@ class TestForm implements FormModel { } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { @@ -501,9 +489,11 @@ class TestForm implements FormModel { Test? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(TestForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + TestForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -517,21 +507,15 @@ class TestForm implements FormModel { } @override - void reset({ - Test? value, - bool updateParent = true, - bool emitEvent = true, - }) => + void reset({Test? value, bool updateParent = true, bool emitEvent = true}) => currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -582,26 +566,30 @@ class TestForm implements FormModel { String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(Test? test) => FormGroup({ - titleControlName: FormControl( - value: test?.title, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - descriptionControlName: FormControl( - value: test?.description, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(Test? test) => FormGroup( + { + titleControlName: FormControl( + value: test?.title, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + descriptionControlName: FormControl( + value: test?.description, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } class ReactiveTestFormArrayBuilder @@ -614,29 +602,37 @@ class ReactiveTestFormArrayBuilder required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function(TestForm formModel)? - control; + control; final Widget Function( - BuildContext context, List itemList, TestForm formModel)? builder; + BuildContext context, + List itemList, + TestForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveTestFormArrayBuilderT? item, - TestForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveTestFormArrayBuilderT? item, + TestForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function(FormControl control)? - controlFilter; + controlFilter; @override Widget build(BuildContext context) { @@ -652,18 +648,9 @@ class ReactiveTestFormArrayBuilder return ReactiveFormArrayItemBuilder( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -681,35 +668,38 @@ class ReactiveTestFormArrayBuilder2 required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function(TestForm formModel)? - control; + control; final Widget Function( - ({ - BuildContext context, - List itemList, - TestForm formModel - }) params)? builder; + ({BuildContext context, List itemList, TestForm formModel}) params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveTestFormArrayBuilderT? item, - TestForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveTestFormArrayBuilderT? item, + TestForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function(FormControl control)? - controlFilter; + controlFilter; @override Widget build(BuildContext context) { @@ -726,23 +716,17 @@ class ReactiveTestFormArrayBuilder2 formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -751,32 +735,48 @@ class ReactiveTestFormArrayBuilder2 } class ReactiveTestFormFormGroupArrayBuilder< - ReactiveTestFormFormGroupArrayBuilderT> extends StatelessWidget { + ReactiveTestFormFormGroupArrayBuilderT +> + extends StatelessWidget { const ReactiveTestFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? extended; - - final ExtendedControl?>, - List> - Function(TestForm formModel)? getExtended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(TestForm formModel)? + getExtended; final Widget Function( - BuildContext context, List itemList, TestForm formModel)? builder; + BuildContext context, + List itemList, + TestForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveTestFormFormGroupArrayBuilderT? item, - TestForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveTestFormFormGroupArrayBuilderT? item, + TestForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -794,23 +794,14 @@ class ReactiveTestFormFormGroupArrayBuilder< final itemList = (value.value() ?? []) .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) .values .toList(); - return builder?.call( - context, - itemList, - formModel, - ) ?? + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/reactive_forms_generator/example/lib/docs/generic/generic.gform.dart b/packages/reactive_forms_generator/example/lib/docs/generic/generic.gform.dart index e210592e..e24b0e61 100644 --- a/packages/reactive_forms_generator/example/lib/docs/generic/generic.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/generic/generic.gform.dart @@ -10,16 +10,17 @@ part of 'generic.dart'; // ************************************************************************** class ReactiveTagsFormConsumer extends StatelessWidget { - const ReactiveTagsFormConsumer({ - Key? key, - required this.builder, - this.child, - }) : super(key: key); + const ReactiveTagsFormConsumer({Key? key, required this.builder, this.child}) + : super(key: key); final Widget? child; final Widget Function( - BuildContext context, TagsForm formModel, Widget? child) builder; + BuildContext context, + TagsForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -38,11 +39,7 @@ class TagsFormInheritedStreamer extends InheritedStreamer { required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final TagsForm form; } @@ -64,18 +61,17 @@ class ReactiveTagsForm extends StatelessWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - static TagsForm? of( - BuildContext context, { - bool listen = true, - }) { + static TagsForm? of(BuildContext context, {bool listen = true}) { if (listen) { return context .dependOnInheritedWidgetOfExactType>() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - TagsFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + TagsFormInheritedStreamer + >(); return element == null ? null : (element.widget as TagsFormInheritedStreamer).form; @@ -121,7 +117,11 @@ class TagsFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, TagsForm formModel, Widget? child) builder; + BuildContext context, + TagsForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, TagsForm formModel)? initState; @@ -136,8 +136,11 @@ class _TagsFormBuilderState extends State> { @override void initState() { - _formModel = - TagsForm(TagsForm.formElements(widget.model), null, null); + _formModel = TagsForm( + TagsForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -214,11 +217,7 @@ class _TagsFormBuilderState extends State> { final _logTagsForm = Logger.detached('TagsForm'); class TagsForm implements FormModel, Tags> { - TagsForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + TagsForm(this.form, this.path, this._formModel) : initial = form.rawValue; static const String tagsControlName = "tags"; @@ -226,7 +225,7 @@ class TagsForm implements FormModel, Tags> { final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -236,12 +235,10 @@ class TagsForm implements FormModel, Tags> { String tagsControlPath() => pathBuilder(tagsControlName); - List? get _tagsValue => tagsControl.value; + List? get _tagsValue => containsTags ? tagsControl.value : null; - List? get _tagsRawValue => tagsControl.value; + List? get _tagsRawValue => containsTags ? tagsControl.value : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsTags { try { form.control(tagsControlPath()); @@ -255,12 +252,7 @@ class TagsForm implements FormModel, Tags> { void get tagsFocus => form.focus(tagsControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void tagsRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void tagsRemove({bool updateParent = true, bool emitEvent = true}) { if (containsTags) { final controlPath = path; if (controlPath == null) { @@ -288,8 +280,11 @@ class TagsForm implements FormModel, Tags> { bool updateParent = true, bool emitEvent = true, }) { - tagsControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + tagsControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void tagsValuePatch( @@ -297,8 +292,11 @@ class TagsForm implements FormModel, Tags> { bool updateParent = true, bool emitEvent = true, }) { - tagsControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + tagsControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void tagsValueReset( @@ -307,14 +305,13 @@ class TagsForm implements FormModel, Tags> { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - tagsControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => tagsControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl> get tagsControl => form.control(tagsControlPath()) as FormControl>; @@ -357,17 +354,16 @@ class TagsForm implements FormModel, Tags> { } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { @@ -425,9 +421,11 @@ class TagsForm implements FormModel, Tags> { Tags? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(TagsForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + TagsForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -445,17 +443,14 @@ class TagsForm implements FormModel, Tags> { Tags? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -506,19 +501,22 @@ class TagsForm implements FormModel, Tags> { String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(Tags? tags) => FormGroup({ - tagsControlName: FormControl>( - value: tags?.tags, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(Tags? tags) => FormGroup( + { + tagsControlName: FormControl>( + value: tags?.tags, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } class ReactiveTagsFormArrayBuilder @@ -531,30 +529,39 @@ class ReactiveTagsFormArrayBuilder required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - TagsForm formModel)? control; + TagsForm formModel, + )? + control; final Widget Function( - BuildContext context, List itemList, TagsForm formModel)? - builder; + BuildContext context, + List itemList, + TagsForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveTagsFormArrayBuilderT? item, - TagsForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveTagsFormArrayBuilderT? item, + TagsForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function(FormControl control)? - controlFilter; + controlFilter; @override Widget build(BuildContext context) { @@ -570,18 +577,9 @@ class ReactiveTagsFormArrayBuilder return ReactiveFormArrayItemBuilder( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -599,35 +597,41 @@ class ReactiveTagsFormArrayBuilder2 required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - TagsForm formModel)? control; + TagsForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - TagsForm formModel - }) params)? builder; + ({BuildContext context, List itemList, TagsForm formModel}) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveTagsFormArrayBuilderT? item, - TagsForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveTagsFormArrayBuilderT? item, + TagsForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function(FormControl control)? - controlFilter; + controlFilter; @override Widget build(BuildContext context) { @@ -644,23 +648,17 @@ class ReactiveTagsFormArrayBuilder2 formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -669,33 +667,49 @@ class ReactiveTagsFormArrayBuilder2 } class ReactiveTagsFormFormGroupArrayBuilder< - ReactiveTagsFormFormGroupArrayBuilderT, T> extends StatelessWidget { + ReactiveTagsFormFormGroupArrayBuilderT, + T +> + extends StatelessWidget { const ReactiveTagsFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? extended; - - final ExtendedControl?>, - List> - Function(TagsForm formModel)? getExtended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(TagsForm formModel)? + getExtended; final Widget Function( - BuildContext context, List itemList, TagsForm formModel)? - builder; + BuildContext context, + List itemList, + TagsForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveTagsFormFormGroupArrayBuilderT? item, - TagsForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveTagsFormFormGroupArrayBuilderT? item, + TagsForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -713,23 +727,14 @@ class ReactiveTagsFormFormGroupArrayBuilder< final itemList = (value.value() ?? []) .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) .values .toList(); - return builder?.call( - context, - itemList, - formModel, - ) ?? + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/reactive_forms_generator/example/lib/docs/generic/generic_output.gform.dart b/packages/reactive_forms_generator/example/lib/docs/generic/generic_output.gform.dart index 9ca97844..1704a8e8 100644 --- a/packages/reactive_forms_generator/example/lib/docs/generic/generic_output.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/generic/generic_output.gform.dart @@ -10,16 +10,17 @@ part of 'generic_output.dart'; // ************************************************************************** class ReactiveTagsOFormConsumer extends StatelessWidget { - const ReactiveTagsOFormConsumer({ - Key? key, - required this.builder, - this.child, - }) : super(key: key); + const ReactiveTagsOFormConsumer({Key? key, required this.builder, this.child}) + : super(key: key); final Widget? child; final Widget Function( - BuildContext context, TagsOForm formModel, Widget? child) builder; + BuildContext context, + TagsOForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -38,11 +39,7 @@ class TagsOFormInheritedStreamer extends InheritedStreamer { required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final TagsOForm form; } @@ -64,18 +61,17 @@ class ReactiveTagsOForm extends StatelessWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - static TagsOForm? of( - BuildContext context, { - bool listen = true, - }) { + static TagsOForm? of(BuildContext context, {bool listen = true}) { if (listen) { return context .dependOnInheritedWidgetOfExactType>() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - TagsOFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + TagsOFormInheritedStreamer + >(); return element == null ? null : (element.widget as TagsOFormInheritedStreamer).form; @@ -122,7 +118,11 @@ class TagsOFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, TagsOForm formModel, Widget? child) builder; + BuildContext context, + TagsOForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, TagsOForm formModel)? initState; @@ -137,8 +137,11 @@ class _TagsOFormBuilderState extends State> { @override void initState() { - _formModel = - TagsOForm(TagsOForm.formElements(widget.model), null, null); + _formModel = TagsOForm( + TagsOForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -215,11 +218,7 @@ class _TagsOFormBuilderState extends State> { final _logTagsOForm = Logger.detached('TagsOForm'); class TagsOForm implements FormModel, TagsOOutput> { - TagsOForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + TagsOForm(this.form, this.path, this._formModel) : initial = form.rawValue; static const String tagsControlName = "tags"; @@ -227,7 +226,7 @@ class TagsOForm implements FormModel, TagsOOutput> { final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -237,12 +236,10 @@ class TagsOForm implements FormModel, TagsOOutput> { String tagsControlPath() => pathBuilder(tagsControlName); - List? get _tagsValue => tagsControl.value; + List? get _tagsValue => containsTags ? tagsControl.value : null; - List? get _tagsRawValue => tagsControl.value; + List? get _tagsRawValue => containsTags ? tagsControl.value : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsTags { try { form.control(tagsControlPath()); @@ -256,12 +253,7 @@ class TagsOForm implements FormModel, TagsOOutput> { void get tagsFocus => form.focus(tagsControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void tagsRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void tagsRemove({bool updateParent = true, bool emitEvent = true}) { if (containsTags) { final controlPath = path; if (controlPath == null) { @@ -289,8 +281,11 @@ class TagsOForm implements FormModel, TagsOOutput> { bool updateParent = true, bool emitEvent = true, }) { - tagsControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + tagsControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void tagsValuePatch( @@ -298,8 +293,11 @@ class TagsOForm implements FormModel, TagsOOutput> { bool updateParent = true, bool emitEvent = true, }) { - tagsControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + tagsControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void tagsValueReset( @@ -308,14 +306,13 @@ class TagsOForm implements FormModel, TagsOOutput> { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - tagsControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => tagsControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl> get tagsControl => form.control(tagsControlPath()) as FormControl>; @@ -359,17 +356,16 @@ class TagsOForm implements FormModel, TagsOOutput> { } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { @@ -427,9 +423,11 @@ class TagsOForm implements FormModel, TagsOOutput> { TagsO? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(TagsOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + TagsOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -447,17 +445,14 @@ class TagsOForm implements FormModel, TagsOOutput> { TagsO? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -508,19 +503,22 @@ class TagsOForm implements FormModel, TagsOOutput> { String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(TagsO? tagsO) => FormGroup({ - tagsControlName: FormControl>( - value: tagsO?.tags, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(TagsO? tagsO) => FormGroup( + { + tagsControlName: FormControl>( + value: tagsO?.tags, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } @freezed @@ -540,30 +538,39 @@ class ReactiveTagsOFormArrayBuilder required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - TagsOForm formModel)? control; + TagsOForm formModel, + )? + control; final Widget Function( - BuildContext context, List itemList, TagsOForm formModel)? - builder; + BuildContext context, + List itemList, + TagsOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveTagsOFormArrayBuilderT? item, - TagsOForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveTagsOFormArrayBuilderT? item, + TagsOForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function(FormControl control)? - controlFilter; + controlFilter; @override Widget build(BuildContext context) { @@ -579,18 +586,9 @@ class ReactiveTagsOFormArrayBuilder return ReactiveFormArrayItemBuilder( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -608,35 +606,41 @@ class ReactiveTagsOFormArrayBuilder2 required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - TagsOForm formModel)? control; + TagsOForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - TagsOForm formModel - }) params)? builder; + ({BuildContext context, List itemList, TagsOForm formModel}) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveTagsOFormArrayBuilderT? item, - TagsOForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveTagsOFormArrayBuilderT? item, + TagsOForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function(FormControl control)? - controlFilter; + controlFilter; @override Widget build(BuildContext context) { @@ -653,23 +657,17 @@ class ReactiveTagsOFormArrayBuilder2 formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -678,33 +676,49 @@ class ReactiveTagsOFormArrayBuilder2 } class ReactiveTagsOFormFormGroupArrayBuilder< - ReactiveTagsOFormFormGroupArrayBuilderT, T> extends StatelessWidget { + ReactiveTagsOFormFormGroupArrayBuilderT, + T +> + extends StatelessWidget { const ReactiveTagsOFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? extended; - - final ExtendedControl?>, - List> - Function(TagsOForm formModel)? getExtended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(TagsOForm formModel)? + getExtended; final Widget Function( - BuildContext context, List itemList, TagsOForm formModel)? - builder; + BuildContext context, + List itemList, + TagsOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveTagsOFormFormGroupArrayBuilderT? item, - TagsOForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveTagsOFormFormGroupArrayBuilderT? item, + TagsOForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -722,23 +736,14 @@ class ReactiveTagsOFormFormGroupArrayBuilder< final itemList = (value.value() ?? []) .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) .values .toList(); - return builder?.call( - context, - itemList, - formModel, - ) ?? + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/reactive_forms_generator/example/lib/docs/generic_status_list/generic_status_list.gform.dart b/packages/reactive_forms_generator/example/lib/docs/generic_status_list/generic_status_list.gform.dart index 2353ee76..78c42dff 100644 --- a/packages/reactive_forms_generator/example/lib/docs/generic_status_list/generic_status_list.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/generic_status_list/generic_status_list.gform.dart @@ -19,7 +19,11 @@ class ReactiveStatusListFormConsumer extends StatelessWidget { final Widget? child; final Widget Function( - BuildContext context, StatusListForm formModel, Widget? child) builder; + BuildContext context, + StatusListForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -39,11 +43,7 @@ class StatusListFormInheritedStreamer required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final StatusListForm form; } @@ -72,12 +72,15 @@ class ReactiveStatusListForm extends StatelessWidget { if (listen) { return context .dependOnInheritedWidgetOfExactType< - StatusListFormInheritedStreamer>() + StatusListFormInheritedStreamer + >() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - StatusListFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + StatusListFormInheritedStreamer + >(); return element == null ? null : (element.widget as StatusListFormInheritedStreamer).form; @@ -125,10 +128,14 @@ class StatusListFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, StatusListForm formModel, Widget? child) builder; + BuildContext context, + StatusListForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, StatusListForm formModel)? - initState; + initState; @override _StatusListFormBuilderState createState() => @@ -144,7 +151,10 @@ class _StatusListFormBuilderState @override void initState() { _formModel = StatusListForm( - StatusListForm.formElements(widget.model), null, null); + StatusListForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -222,11 +232,8 @@ final _logStatusListForm = Logger.detached('StatusListForm'); class StatusListForm implements FormModel, StatusList> { - StatusListForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + StatusListForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String listControlName = "list"; @@ -234,7 +241,7 @@ class StatusListForm final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -248,8 +255,6 @@ class StatusListForm List get _listRawValue => listControl.rawValue.whereType().toList(); - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsList { try { form.control(listControlPath()); @@ -268,8 +273,11 @@ class StatusListForm bool updateParent = true, bool emitEvent = true, }) { - listControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + listControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void listValuePatch( @@ -277,8 +285,11 @@ class StatusListForm bool updateParent = true, bool emitEvent = true, }) { - listControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + listControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void listValueReset( @@ -287,14 +298,13 @@ class StatusListForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - listControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => listControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormArray get listControl => form.control(listControlPath()) as FormArray; @@ -351,13 +361,15 @@ class StatusListForm break; } - listControl.add(FormControl( - value: value, - validators: resultingValidators, - asyncValidators: resultingAsyncValidators, - asyncValidatorsDebounceTime: asyncValidatorsDebounceTime ?? 250, - disabled: disabled ?? false, - )); + listControl.add( + FormControl( + value: value, + validators: resultingValidators, + asyncValidators: resultingAsyncValidators, + asyncValidatorsDebounceTime: asyncValidatorsDebounceTime ?? 250, + disabled: disabled ?? false, + ), + ); } @override @@ -380,17 +392,16 @@ class StatusListForm } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { @@ -448,9 +459,11 @@ class StatusListForm StatusList? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(StatusListForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + StatusListForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -468,17 +481,14 @@ class StatusListForm StatusList? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -530,30 +540,38 @@ class StatusListForm [path, pathItem].whereType().join("."); static FormGroup formElements(StatusList? statusList) => - FormGroup({ - listControlName: FormArray( + FormGroup( + { + listControlName: FormArray( (statusList?.list ?? []) - .map((e) => FormControl( - value: e, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - )) + .map( + (e) => FormControl( + value: e, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ), + ) .toList(), validators: [], asyncValidators: [], asyncValidatorsDebounceTime: 250, - disabled: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + disabled: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } -class ReactiveStatusListFormArrayBuilder extends StatelessWidget { +class ReactiveStatusListFormArrayBuilder< + ReactiveStatusListFormArrayBuilderT, + T extends Enum +> + extends StatelessWidget { const ReactiveStatusListFormArrayBuilder({ Key? key, this.control, @@ -562,29 +580,39 @@ class ReactiveStatusListFormArrayBuilder? formControl; final FormArray? Function( - StatusListForm formModel)? control; + StatusListForm formModel, + )? + control; - final Widget Function(BuildContext context, List itemList, - StatusListForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + StatusListForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveStatusListFormArrayBuilderT? item, - StatusListForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveStatusListFormArrayBuilderT? item, + StatusListForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function(FormControl control)? - controlFilter; + controlFilter; @override Widget build(BuildContext context) { @@ -600,18 +628,9 @@ class ReactiveStatusListFormArrayBuilder( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -619,8 +638,11 @@ class ReactiveStatusListFormArrayBuilder extends StatelessWidget { +class ReactiveStatusListFormArrayBuilder2< + ReactiveStatusListFormArrayBuilderT, + T extends Enum +> + extends StatelessWidget { const ReactiveStatusListFormArrayBuilder2({ Key? key, this.control, @@ -629,35 +651,41 @@ class ReactiveStatusListFormArrayBuilder2? formControl; final FormArray? Function( - StatusListForm formModel)? control; + StatusListForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - StatusListForm formModel - }) params)? builder; + ({BuildContext context, List itemList, StatusListForm formModel}) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveStatusListFormArrayBuilderT? item, - StatusListForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveStatusListFormArrayBuilderT? item, + StatusListForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function(FormControl control)? - controlFilter; + controlFilter; @override Widget build(BuildContext context) { @@ -674,23 +702,17 @@ class ReactiveStatusListFormArrayBuilder2 builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -699,33 +721,49 @@ class ReactiveStatusListFormArrayBuilder2 extends StatelessWidget { + ReactiveStatusListFormFormGroupArrayBuilderT, + T extends Enum +> + extends StatelessWidget { const ReactiveStatusListFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? extended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(StatusListForm formModel)? + getExtended; - final ExtendedControl?>, - List> - Function(StatusListForm formModel)? getExtended; - - final Widget Function(BuildContext context, List itemList, - StatusListForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + StatusListForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveStatusListFormFormGroupArrayBuilderT? item, - StatusListForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveStatusListFormFormGroupArrayBuilderT? item, + StatusListForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -743,23 +781,14 @@ class ReactiveStatusListFormFormGroupArrayBuilder< final itemList = (value.value() ?? []) .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) .values .toList(); - return builder?.call( - context, - itemList, - formModel, - ) ?? + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/reactive_forms_generator/example/lib/docs/generic_status_list/generic_status_list_output.gform.dart b/packages/reactive_forms_generator/example/lib/docs/generic_status_list/generic_status_list_output.gform.dart index 68155111..d51f8812 100644 --- a/packages/reactive_forms_generator/example/lib/docs/generic_status_list/generic_status_list_output.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/generic_status_list/generic_status_list_output.gform.dart @@ -19,8 +19,11 @@ class ReactiveStatusListOFormConsumer extends StatelessWidget { final Widget? child; final Widget Function( - BuildContext context, StatusListOForm formModel, Widget? child) - builder; + BuildContext context, + StatusListOForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -40,11 +43,7 @@ class StatusListOFormInheritedStreamer required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final StatusListOForm form; } @@ -73,12 +72,15 @@ class ReactiveStatusListOForm extends StatelessWidget { if (listen) { return context .dependOnInheritedWidgetOfExactType< - StatusListOFormInheritedStreamer>() + StatusListOFormInheritedStreamer + >() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - StatusListOFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + StatusListOFormInheritedStreamer + >(); return element == null ? null : (element.widget as StatusListOFormInheritedStreamer).form; @@ -126,11 +128,14 @@ class StatusListOFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, StatusListOForm formModel, Widget? child) - builder; + BuildContext context, + StatusListOForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, StatusListOForm formModel)? - initState; + initState; @override _StatusListOFormBuilderState createState() => @@ -146,7 +151,10 @@ class _StatusListOFormBuilderState @override void initState() { _formModel = StatusListOForm( - StatusListOForm.formElements(widget.model), null, null); + StatusListOForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -224,11 +232,8 @@ final _logStatusListOForm = Logger.detached('StatusListOForm'); class StatusListOForm implements FormModel, StatusListOOutput> { - StatusListOForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + StatusListOForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String listControlName = "list"; @@ -236,7 +241,7 @@ class StatusListOForm final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -250,8 +255,6 @@ class StatusListOForm List get _listRawValue => listControl.rawValue.whereType().toList(); - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsList { try { form.control(listControlPath()); @@ -270,8 +273,11 @@ class StatusListOForm bool updateParent = true, bool emitEvent = true, }) { - listControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + listControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void listValuePatch( @@ -279,8 +285,11 @@ class StatusListOForm bool updateParent = true, bool emitEvent = true, }) { - listControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + listControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void listValueReset( @@ -289,14 +298,13 @@ class StatusListOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - listControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => listControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormArray get listControl => form.control(listControlPath()) as FormArray; @@ -353,13 +361,15 @@ class StatusListOForm break; } - listControl.add(FormControl( - value: value, - validators: resultingValidators, - asyncValidators: resultingAsyncValidators, - asyncValidatorsDebounceTime: asyncValidatorsDebounceTime ?? 250, - disabled: disabled ?? false, - )); + listControl.add( + FormControl( + value: value, + validators: resultingValidators, + asyncValidators: resultingAsyncValidators, + asyncValidatorsDebounceTime: asyncValidatorsDebounceTime ?? 250, + disabled: disabled ?? false, + ), + ); } @override @@ -383,17 +393,16 @@ class StatusListOForm } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { @@ -451,9 +460,11 @@ class StatusListOForm StatusListO? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(StatusListOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + StatusListOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -471,17 +482,14 @@ class StatusListOForm StatusListO? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -533,26 +541,31 @@ class StatusListOForm [path, pathItem].whereType().join("."); static FormGroup formElements(StatusListO? statusListO) => - FormGroup({ - listControlName: FormArray( + FormGroup( + { + listControlName: FormArray( (statusListO?.list ?? []) - .map((e) => FormControl( - value: e, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - )) + .map( + (e) => FormControl( + value: e, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ), + ) .toList(), validators: [], asyncValidators: [], asyncValidatorsDebounceTime: 250, - disabled: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + disabled: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } @Rf(output: true) @@ -561,8 +574,11 @@ class StatusListOOutput { StatusListOOutput({@RfArray() this.list = const []}); } -class ReactiveStatusListOFormArrayBuilder extends StatelessWidget { +class ReactiveStatusListOFormArrayBuilder< + ReactiveStatusListOFormArrayBuilderT, + T extends Enum +> + extends StatelessWidget { const ReactiveStatusListOFormArrayBuilder({ Key? key, this.control, @@ -571,29 +587,41 @@ class ReactiveStatusListOFormArrayBuilder? formControl; final FormArray? Function( - StatusListOForm formModel)? control; + StatusListOForm formModel, + )? + control; - final Widget Function(BuildContext context, List itemList, - StatusListOForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + StatusListOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveStatusListOFormArrayBuilderT? item, - StatusListOForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveStatusListOFormArrayBuilderT? item, + StatusListOForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -609,18 +637,9 @@ class ReactiveStatusListOFormArrayBuilder( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -628,8 +647,11 @@ class ReactiveStatusListOFormArrayBuilder extends StatelessWidget { +class ReactiveStatusListOFormArrayBuilder2< + ReactiveStatusListOFormArrayBuilderT, + T extends Enum +> + extends StatelessWidget { const ReactiveStatusListOFormArrayBuilder2({ Key? key, this.control, @@ -638,35 +660,47 @@ class ReactiveStatusListOFormArrayBuilder2? formControl; final FormArray? Function( - StatusListOForm formModel)? control; + StatusListOForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - StatusListOForm formModel - }) params)? builder; + ({ + BuildContext context, + List itemList, + StatusListOForm formModel, + }) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveStatusListOFormArrayBuilderT? item, - StatusListOForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveStatusListOFormArrayBuilderT? item, + StatusListOForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -683,23 +717,17 @@ class ReactiveStatusListOFormArrayBuilder2 builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -708,33 +736,49 @@ class ReactiveStatusListOFormArrayBuilder2 extends StatelessWidget { + ReactiveStatusListOFormFormGroupArrayBuilderT, + T extends Enum +> + extends StatelessWidget { const ReactiveStatusListOFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? extended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(StatusListOForm formModel)? + getExtended; - final ExtendedControl?>, - List> - Function(StatusListOForm formModel)? getExtended; - - final Widget Function(BuildContext context, List itemList, - StatusListOForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + StatusListOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveStatusListOFormFormGroupArrayBuilderT? item, - StatusListOForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveStatusListOFormFormGroupArrayBuilderT? item, + StatusListOForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -752,23 +796,14 @@ class ReactiveStatusListOFormFormGroupArrayBuilder< final itemList = (value.value() ?? []) .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) .values .toList(); - return builder?.call( - context, - itemList, - formModel, - ) ?? + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/reactive_forms_generator/example/lib/docs/group/group.gform.dart b/packages/reactive_forms_generator/example/lib/docs/group/group.gform.dart index c0c06bf8..d24cada0 100644 --- a/packages/reactive_forms_generator/example/lib/docs/group/group.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/group/group.gform.dart @@ -10,16 +10,17 @@ part of 'group.dart'; // ************************************************************************** class ReactiveGroupFormConsumer extends StatelessWidget { - const ReactiveGroupFormConsumer({ - Key? key, - required this.builder, - this.child, - }) : super(key: key); + const ReactiveGroupFormConsumer({Key? key, required this.builder, this.child}) + : super(key: key); final Widget? child; final Widget Function( - BuildContext context, GroupForm formModel, Widget? child) builder; + BuildContext context, + GroupForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -38,11 +39,7 @@ class GroupFormInheritedStreamer extends InheritedStreamer { required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final GroupForm form; } @@ -64,10 +61,7 @@ class ReactiveGroupForm extends StatelessWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - static GroupForm? of( - BuildContext context, { - bool listen = true, - }) { + static GroupForm? of(BuildContext context, {bool listen = true}) { if (listen) { return context .dependOnInheritedWidgetOfExactType() @@ -121,7 +115,11 @@ class GroupFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, GroupForm formModel, Widget? child) builder; + BuildContext context, + GroupForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, GroupForm formModel)? initState; @@ -213,11 +211,7 @@ class _GroupFormBuilderState extends State { final _logGroupForm = Logger.detached('GroupForm'); class GroupForm implements FormModel { - GroupForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + GroupForm(this.form, this.path, this._formModel) : initial = form.rawValue; static const String personalControlName = "personal"; @@ -231,7 +225,7 @@ class GroupForm implements FormModel { final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -247,24 +241,25 @@ class GroupForm implements FormModel { String address2ControlPath() => pathBuilder(address2ControlName); - Personal? get _personalValue => personalForm.model; + Personal? get _personalValue => containsPersonal ? personalForm.model : null; - Phone? get _phoneValue => phoneForm.model; + Phone? get _phoneValue => containsPhone ? phoneForm.model : null; - Address? get _addressValue => addressForm.model; + Address? get _addressValue => containsAddress ? addressForm.model : null; - Address? get _address2Value => address2Form.model; + Address? get _address2Value => containsAddress2 ? address2Form.model : null; - Personal? get _personalRawValue => personalForm.rawModel; + Personal? get _personalRawValue => + containsPersonal ? personalForm.rawModel : null; - Phone? get _phoneRawValue => phoneForm.rawModel; + Phone? get _phoneRawValue => containsPhone ? phoneForm.rawModel : null; - Address? get _addressRawValue => addressForm.rawModel; + Address? get _addressRawValue => + containsAddress ? addressForm.rawModel : null; - Address? get _address2RawValue => address2Form.rawModel; + Address? get _address2RawValue => + containsAddress2 ? address2Form.rawModel : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsPersonal { try { form.control(personalControlPath()); @@ -274,8 +269,6 @@ class GroupForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsPhone { try { form.control(phoneControlPath()); @@ -285,8 +278,6 @@ class GroupForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsAddress { try { form.control(addressControlPath()); @@ -296,8 +287,6 @@ class GroupForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsAddress2 { try { form.control(address2ControlPath()); @@ -323,12 +312,7 @@ class GroupForm implements FormModel { void get address2Focus => form.focus(address2ControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void personalRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void personalRemove({bool updateParent = true, bool emitEvent = true}) { if (containsPersonal) { final controlPath = path; if (controlPath == null) { @@ -351,12 +335,7 @@ class GroupForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void phoneRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void phoneRemove({bool updateParent = true, bool emitEvent = true}) { if (containsPhone) { final controlPath = path; if (controlPath == null) { @@ -379,12 +358,7 @@ class GroupForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void addressRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void addressRemove({bool updateParent = true, bool emitEvent = true}) { if (containsAddress) { final controlPath = path; if (controlPath == null) { @@ -407,12 +381,7 @@ class GroupForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void address2Remove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void address2Remove({bool updateParent = true, bool emitEvent = true}) { if (containsAddress2) { final controlPath = path; if (controlPath == null) { @@ -440,8 +409,11 @@ class GroupForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - personalControl.updateValue(PersonalForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + personalControl.updateValue( + PersonalForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void phoneValueUpdate( @@ -449,8 +421,11 @@ class GroupForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - phoneControl.updateValue(PhoneForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + phoneControl.updateValue( + PhoneForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void addressValueUpdate( @@ -458,8 +433,11 @@ class GroupForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - addressControl.updateValue(AddressForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + addressControl.updateValue( + AddressForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void address2ValueUpdate( @@ -467,8 +445,11 @@ class GroupForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - address2Control.updateValue(AddressForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + address2Control.updateValue( + AddressForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void personalValuePatch( @@ -476,8 +457,11 @@ class GroupForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - personalControl.updateValue(PersonalForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + personalControl.updateValue( + PersonalForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void phoneValuePatch( @@ -485,8 +469,11 @@ class GroupForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - phoneControl.updateValue(PhoneForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + phoneControl.updateValue( + PhoneForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void addressValuePatch( @@ -494,8 +481,11 @@ class GroupForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - addressControl.updateValue(AddressForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + addressControl.updateValue( + AddressForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void address2ValuePatch( @@ -503,8 +493,11 @@ class GroupForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - address2Control.updateValue(AddressForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + address2Control.updateValue( + AddressForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void personalValueReset( @@ -513,11 +506,11 @@ class GroupForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - personalControl.reset( - value: PersonalForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => personalControl.reset( + value: PersonalForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); void phoneValueReset( Phone? value, { @@ -525,11 +518,11 @@ class GroupForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - phoneControl.reset( - value: PhoneForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => phoneControl.reset( + value: PhoneForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); void addressValueReset( Address? value, { @@ -537,11 +530,11 @@ class GroupForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - addressControl.reset( - value: AddressForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => addressControl.reset( + value: AddressForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); void address2ValueReset( Address? value, { @@ -549,11 +542,11 @@ class GroupForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - address2Control.reset( - value: AddressForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => address2Control.reset( + value: AddressForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); FormGroup get personalControl => form.control(personalControlPath()) as FormGroup; @@ -662,26 +655,25 @@ class GroupForm implements FormModel { ); } return Group( - personal: _personalValue, - phone: _phoneValue, - address: _addressValue, - address2: _address2Value); + personal: _personalValue, + phone: _phoneValue, + address: _addressValue, + address2: _address2Value, + ); } @override Group get rawModel { return Group( - personal: _personalRawValue, - phone: _phoneRawValue, - address: _addressRawValue, - address2: _address2RawValue); + personal: _personalRawValue, + phone: _phoneRawValue, + address: _addressRawValue, + address2: _address2RawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; @@ -692,7 +684,9 @@ class GroupForm implements FormModel { addressForm.toggleDisabled(); address2Form.toggleDisabled(); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { personalForm.toggleDisabled(); phoneForm.toggleDisabled(); @@ -754,9 +748,11 @@ class GroupForm implements FormModel { Group? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(GroupForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + GroupForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -770,21 +766,15 @@ class GroupForm implements FormModel { } @override - void reset({ - Group? value, - bool updateParent = true, - bool emitEvent = true, - }) => + void reset({Group? value, bool updateParent = true, bool emitEvent = true}) => currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -835,26 +825,24 @@ class GroupForm implements FormModel { String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(Group? group) => FormGroup({ - personalControlName: PersonalForm.formElements(group?.personal), - phoneControlName: PhoneForm.formElements(group?.phone), - addressControlName: AddressForm.formElements(group?.address), - address2ControlName: AddressForm.formElements(group?.address2) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(Group? group) => FormGroup( + { + personalControlName: PersonalForm.formElements(group?.personal), + phoneControlName: PhoneForm.formElements(group?.phone), + addressControlName: AddressForm.formElements(group?.address), + address2ControlName: AddressForm.formElements(group?.address2), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } final _logPersonalForm = Logger.detached('PersonalForm'); class PersonalForm implements FormModel { - PersonalForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + PersonalForm(this.form, this.path, this._formModel) : initial = form.rawValue; static const String nameControlName = "name"; @@ -864,7 +852,7 @@ class PersonalForm implements FormModel { final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -876,16 +864,14 @@ class PersonalForm implements FormModel { String emailControlPath() => pathBuilder(emailControlName); - String? get _nameValue => nameControl.value; + String? get _nameValue => containsName ? nameControl.value : null; - String? get _emailValue => emailControl.value; + String? get _emailValue => containsEmail ? emailControl.value : null; - String? get _nameRawValue => nameControl.value; + String? get _nameRawValue => containsName ? nameControl.value : null; - String? get _emailRawValue => emailControl.value; + String? get _emailRawValue => containsEmail ? emailControl.value : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsName { try { form.control(nameControlPath()); @@ -895,8 +881,6 @@ class PersonalForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsEmail { try { form.control(emailControlPath()); @@ -914,12 +898,7 @@ class PersonalForm implements FormModel { void get emailFocus => form.focus(emailControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void nameRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void nameRemove({bool updateParent = true, bool emitEvent = true}) { if (containsName) { final controlPath = path; if (controlPath == null) { @@ -942,12 +921,7 @@ class PersonalForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void emailRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void emailRemove({bool updateParent = true, bool emitEvent = true}) { if (containsEmail) { final controlPath = path; if (controlPath == null) { @@ -975,8 +949,11 @@ class PersonalForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - nameControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + nameControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailValueUpdate( @@ -984,8 +961,11 @@ class PersonalForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - emailControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void nameValuePatch( @@ -993,8 +973,11 @@ class PersonalForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - nameControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + nameControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailValuePatch( @@ -1002,8 +985,11 @@ class PersonalForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - emailControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void nameValueReset( @@ -1012,14 +998,13 @@ class PersonalForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - nameControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => nameControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void emailValueReset( String? value, { @@ -1027,14 +1012,13 @@ class PersonalForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - emailControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => emailControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get nameControl => form.control(nameControlPath()) as FormControl; @@ -1098,17 +1082,16 @@ class PersonalForm implements FormModel { } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { @@ -1166,9 +1149,11 @@ class PersonalForm implements FormModel { Personal? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(PersonalForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + PersonalForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -1186,17 +1171,14 @@ class PersonalForm implements FormModel { Personal? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -1247,36 +1229,36 @@ class PersonalForm implements FormModel { String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(Personal? personal) => FormGroup({ - nameControlName: FormControl( - value: personal?.name, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - emailControlName: FormControl( - value: personal?.email, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(Personal? personal) => FormGroup( + { + nameControlName: FormControl( + value: personal?.name, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + emailControlName: FormControl( + value: personal?.email, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } final _logPhoneForm = Logger.detached('PhoneForm'); class PhoneForm implements FormModel { - PhoneForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + PhoneForm(this.form, this.path, this._formModel) : initial = form.rawValue; static const String phoneNumberControlName = "phoneNumber"; @@ -1286,7 +1268,7 @@ class PhoneForm implements FormModel { final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -1298,16 +1280,18 @@ class PhoneForm implements FormModel { String countryIsoControlPath() => pathBuilder(countryIsoControlName); - String? get _phoneNumberValue => phoneNumberControl.value; + String? get _phoneNumberValue => + containsPhoneNumber ? phoneNumberControl.value : null; - String? get _countryIsoValue => countryIsoControl.value; + String? get _countryIsoValue => + containsCountryIso ? countryIsoControl.value : null; - String? get _phoneNumberRawValue => phoneNumberControl.value; + String? get _phoneNumberRawValue => + containsPhoneNumber ? phoneNumberControl.value : null; - String? get _countryIsoRawValue => countryIsoControl.value; + String? get _countryIsoRawValue => + containsCountryIso ? countryIsoControl.value : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsPhoneNumber { try { form.control(phoneNumberControlPath()); @@ -1317,8 +1301,6 @@ class PhoneForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsCountryIso { try { form.control(countryIsoControlPath()); @@ -1336,12 +1318,7 @@ class PhoneForm implements FormModel { void get countryIsoFocus => form.focus(countryIsoControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void phoneNumberRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void phoneNumberRemove({bool updateParent = true, bool emitEvent = true}) { if (containsPhoneNumber) { final controlPath = path; if (controlPath == null) { @@ -1364,12 +1341,7 @@ class PhoneForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void countryIsoRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void countryIsoRemove({bool updateParent = true, bool emitEvent = true}) { if (containsCountryIso) { final controlPath = path; if (controlPath == null) { @@ -1397,8 +1369,11 @@ class PhoneForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - phoneNumberControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + phoneNumberControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void countryIsoValueUpdate( @@ -1406,8 +1381,11 @@ class PhoneForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - countryIsoControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + countryIsoControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void phoneNumberValuePatch( @@ -1415,8 +1393,11 @@ class PhoneForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - phoneNumberControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + phoneNumberControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void countryIsoValuePatch( @@ -1424,8 +1405,11 @@ class PhoneForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - countryIsoControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + countryIsoControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void phoneNumberValueReset( @@ -1434,14 +1418,13 @@ class PhoneForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - phoneNumberControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => phoneNumberControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void countryIsoValueReset( String? value, { @@ -1449,14 +1432,13 @@ class PhoneForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - countryIsoControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => countryIsoControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get phoneNumberControl => form.control(phoneNumberControlPath()) as FormControl; @@ -1517,21 +1499,22 @@ class PhoneForm implements FormModel { @override Phone get rawModel { return Phone( - phoneNumber: _phoneNumberRawValue, countryIso: _countryIsoRawValue); + phoneNumber: _phoneNumberRawValue, + countryIso: _countryIsoRawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { @@ -1589,9 +1572,11 @@ class PhoneForm implements FormModel { Phone? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(PhoneForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + PhoneForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -1605,21 +1590,15 @@ class PhoneForm implements FormModel { } @override - void reset({ - Phone? value, - bool updateParent = true, - bool emitEvent = true, - }) => + void reset({Phone? value, bool updateParent = true, bool emitEvent = true}) => currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -1670,36 +1649,36 @@ class PhoneForm implements FormModel { String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(Phone? phone) => FormGroup({ - phoneNumberControlName: FormControl( - value: phone?.phoneNumber, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - countryIsoControlName: FormControl( - value: phone?.countryIso, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(Phone? phone) => FormGroup( + { + phoneNumberControlName: FormControl( + value: phone?.phoneNumber, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + countryIsoControlName: FormControl( + value: phone?.countryIso, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } final _logAddressForm = Logger.detached('AddressForm'); class AddressForm implements FormModel { - AddressForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + AddressForm(this.form, this.path, this._formModel) : initial = form.rawValue; static const String streetControlName = "street"; @@ -1711,7 +1690,7 @@ class AddressForm implements FormModel { final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -1725,20 +1704,18 @@ class AddressForm implements FormModel { String zipControlPath() => pathBuilder(zipControlName); - String? get _streetValue => streetControl.value; + String? get _streetValue => containsStreet ? streetControl.value : null; - String? get _cityValue => cityControl.value; + String? get _cityValue => containsCity ? cityControl.value : null; - String? get _zipValue => zipControl.value; + String? get _zipValue => containsZip ? zipControl.value : null; - String? get _streetRawValue => streetControl.value; + String? get _streetRawValue => containsStreet ? streetControl.value : null; - String? get _cityRawValue => cityControl.value; + String? get _cityRawValue => containsCity ? cityControl.value : null; - String? get _zipRawValue => zipControl.value; + String? get _zipRawValue => containsZip ? zipControl.value : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsStreet { try { form.control(streetControlPath()); @@ -1748,8 +1725,6 @@ class AddressForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsCity { try { form.control(cityControlPath()); @@ -1759,8 +1734,6 @@ class AddressForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsZip { try { form.control(zipControlPath()); @@ -1782,12 +1755,7 @@ class AddressForm implements FormModel { void get zipFocus => form.focus(zipControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void streetRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void streetRemove({bool updateParent = true, bool emitEvent = true}) { if (containsStreet) { final controlPath = path; if (controlPath == null) { @@ -1810,12 +1778,7 @@ class AddressForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void cityRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void cityRemove({bool updateParent = true, bool emitEvent = true}) { if (containsCity) { final controlPath = path; if (controlPath == null) { @@ -1838,12 +1801,7 @@ class AddressForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void zipRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void zipRemove({bool updateParent = true, bool emitEvent = true}) { if (containsZip) { final controlPath = path; if (controlPath == null) { @@ -1871,8 +1829,11 @@ class AddressForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - streetControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + streetControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void cityValueUpdate( @@ -1880,8 +1841,11 @@ class AddressForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - cityControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + cityControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void zipValueUpdate( @@ -1889,8 +1853,11 @@ class AddressForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - zipControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + zipControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void streetValuePatch( @@ -1898,8 +1865,11 @@ class AddressForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - streetControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + streetControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void cityValuePatch( @@ -1907,8 +1877,11 @@ class AddressForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - cityControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + cityControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void zipValuePatch( @@ -1916,8 +1889,11 @@ class AddressForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - zipControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + zipControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void streetValueReset( @@ -1926,14 +1902,13 @@ class AddressForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - streetControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => streetControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void cityValueReset( String? value, { @@ -1941,14 +1916,13 @@ class AddressForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - cityControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => cityControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void zipValueReset( String? value, { @@ -1956,14 +1930,13 @@ class AddressForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - zipControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => zipControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get streetControl => form.control(streetControlPath()) as FormControl; @@ -2045,21 +2018,23 @@ class AddressForm implements FormModel { @override Address get rawModel { return Address( - street: _streetRawValue, city: _cityRawValue, zip: _zipRawValue); + street: _streetRawValue, + city: _cityRawValue, + zip: _zipRawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { @@ -2117,9 +2092,11 @@ class AddressForm implements FormModel { Address? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(AddressForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + AddressForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -2137,17 +2114,14 @@ class AddressForm implements FormModel { Address? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -2198,33 +2172,38 @@ class AddressForm implements FormModel { String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(Address? address) => FormGroup({ - streetControlName: FormControl( - value: address?.street, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - cityControlName: FormControl( - value: address?.city, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - zipControlName: FormControl( - value: address?.zip, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(Address? address) => FormGroup( + { + streetControlName: FormControl( + value: address?.street, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + cityControlName: FormControl( + value: address?.city, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + zipControlName: FormControl( + value: address?.zip, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } class ReactiveGroupFormArrayBuilder @@ -2237,30 +2216,39 @@ class ReactiveGroupFormArrayBuilder required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - GroupForm formModel)? control; + GroupForm formModel, + )? + control; final Widget Function( - BuildContext context, List itemList, GroupForm formModel)? - builder; + BuildContext context, + List itemList, + GroupForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveGroupFormArrayBuilderT? item, - GroupForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveGroupFormArrayBuilderT? item, + GroupForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function(FormControl control)? - controlFilter; + controlFilter; @override Widget build(BuildContext context) { @@ -2276,18 +2264,9 @@ class ReactiveGroupFormArrayBuilder return ReactiveFormArrayItemBuilder( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -2305,35 +2284,40 @@ class ReactiveGroupFormArrayBuilder2 required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - GroupForm formModel)? control; + GroupForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - GroupForm formModel - }) params)? builder; + ({BuildContext context, List itemList, GroupForm formModel}) params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveGroupFormArrayBuilderT? item, - GroupForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveGroupFormArrayBuilderT? item, + GroupForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function(FormControl control)? - controlFilter; + controlFilter; @override Widget build(BuildContext context) { @@ -2350,23 +2334,17 @@ class ReactiveGroupFormArrayBuilder2 formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -2375,33 +2353,48 @@ class ReactiveGroupFormArrayBuilder2 } class ReactiveGroupFormFormGroupArrayBuilder< - ReactiveGroupFormFormGroupArrayBuilderT> extends StatelessWidget { + ReactiveGroupFormFormGroupArrayBuilderT +> + extends StatelessWidget { const ReactiveGroupFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? extended; - - final ExtendedControl?>, - List> - Function(GroupForm formModel)? getExtended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(GroupForm formModel)? + getExtended; final Widget Function( - BuildContext context, List itemList, GroupForm formModel)? - builder; + BuildContext context, + List itemList, + GroupForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveGroupFormFormGroupArrayBuilderT? item, - GroupForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveGroupFormFormGroupArrayBuilderT? item, + GroupForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -2419,23 +2412,14 @@ class ReactiveGroupFormFormGroupArrayBuilder< final itemList = (value.value() ?? []) .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) .values .toList(); - return builder?.call( - context, - itemList, - formModel, - ) ?? + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/reactive_forms_generator/example/lib/docs/group/group_contains_form.dart b/packages/reactive_forms_generator/example/lib/docs/group/group_contains_form.dart new file mode 100644 index 00000000..b3a3815a --- /dev/null +++ b/packages/reactive_forms_generator/example/lib/docs/group/group_contains_form.dart @@ -0,0 +1,205 @@ +// ignore_for_file: avoid_print + +import 'package:example/docs/group/group.dart'; +import 'package:example/sample_screen.dart'; +import 'package:flutter/material.dart' hide ProgressIndicator; +import 'package:reactive_forms/reactive_forms.dart'; + +class GroupContainsFormWidget extends StatelessWidget { + const GroupContainsFormWidget({super.key}); + + @override + Widget build(BuildContext context) { + return SampleScreen( + title: const Text('Group'), + body: GroupFormBuilder( + model: Group( + personal: Personal(name: 'vasilich'), + phone: Phone(countryIso: 'UA'), + address: Address(city: 'Kiev'), + address2: Address(city: 'Odessa'), + ), + builder: (context, formModel, child) { + return Column( + children: [ + ReactiveTextField( + formControl: formModel.personalForm.nameControl, + decoration: const InputDecoration( + labelText: 'Name', + helperText: '', + helperStyle: TextStyle(height: 0.7), + errorStyle: TextStyle(height: 0.7), + ), + ), + const SizedBox(height: 16.0), + ReactiveTextField( + formControl: formModel.personalForm.emailControl, + decoration: const InputDecoration( + labelText: 'Email', + helperText: '', + helperStyle: TextStyle(height: 0.7), + errorStyle: TextStyle(height: 0.7), + ), + ), + const SizedBox(height: 16.0), + ReactiveTextField( + formControl: formModel.phoneForm.phoneNumberControl, + decoration: const InputDecoration( + labelText: 'Phone number', + helperText: '', + helperStyle: TextStyle(height: 0.7), + errorStyle: TextStyle(height: 0.7), + ), + ), + const SizedBox(height: 16.0), + ReactiveTextField( + formControl: formModel.phoneForm.countryIsoControl, + decoration: const InputDecoration( + labelText: 'Country iso', + helperText: '', + helperStyle: TextStyle(height: 0.7), + errorStyle: TextStyle(height: 0.7), + ), + ), + const SizedBox(height: 16.0), + ReactiveFormConsumer( + builder: (context, formGroup, child) { + if (!formModel.containsAddress) { + return FilledButton( + onPressed: () { + formModel.form.addAll({ + formModel.addressControlPath(): + AddressForm.formElements(Address()), + }); + }, + child: Text('Put back'), + ); + } + return Card( + margin: const EdgeInsets.all(0), + child: Padding( + padding: const EdgeInsets.all(8.0), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Row( + children: [ + const Text( + 'Address 1', + style: TextStyle(fontSize: 20), + ), + Spacer(), + IconButton( + onPressed: () { + formModel.addressRemove(); + }, + icon: Icon(Icons.delete), + ), + ], + ), + ReactiveTextField( + formControl: formModel.addressForm.cityControl, + decoration: const InputDecoration( + labelText: 'City', + helperText: '', + helperStyle: TextStyle(height: 0.7), + errorStyle: TextStyle(height: 0.7), + ), + ), + const SizedBox(height: 16.0), + ReactiveTextField( + formControl: formModel.addressForm.streetControl, + decoration: const InputDecoration( + labelText: 'Street', + helperText: '', + helperStyle: TextStyle(height: 0.7), + errorStyle: TextStyle(height: 0.7), + ), + ), + const SizedBox(height: 16.0), + ReactiveTextField( + formControl: formModel.addressForm.zipControl, + decoration: const InputDecoration( + labelText: 'Zip', + helperText: '', + helperStyle: TextStyle(height: 0.7), + errorStyle: TextStyle(height: 0.7), + ), + ), + ], + ), + ), + ); + }, + ), + const SizedBox(height: 16.0), + Card( + margin: const EdgeInsets.all(0), + child: Padding( + padding: const EdgeInsets.all(8.0), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + const Text('Address 2', style: TextStyle(fontSize: 20)), + ReactiveTextField( + formControl: formModel.address2Form.cityControl, + decoration: const InputDecoration( + labelText: 'City', + helperText: '', + helperStyle: TextStyle(height: 0.7), + errorStyle: TextStyle(height: 0.7), + ), + ), + const SizedBox(height: 16.0), + ReactiveTextField( + formControl: formModel.address2Form.streetControl, + decoration: const InputDecoration( + labelText: 'Street', + helperText: '', + helperStyle: TextStyle(height: 0.7), + errorStyle: TextStyle(height: 0.7), + ), + ), + const SizedBox(height: 16.0), + ReactiveTextField( + formControl: formModel.address2Form.zipControl, + decoration: const InputDecoration( + labelText: 'Zip', + helperText: '', + helperStyle: TextStyle(height: 0.7), + errorStyle: TextStyle(height: 0.7), + ), + ), + ], + ), + ), + ), + const SizedBox(height: 16.0), + ElevatedButton( + onPressed: () { + debugPrint(formModel.rawModel.address.toString()); + formModel.submit( + onValid: (model) { + debugPrint(model.toString()); + debugPrint(model.personal?.name); + debugPrint(model.personal?.email); + }, + ); + }, + child: const Text('Sign Up'), + ), + ReactiveGroupFormConsumer( + builder: (context, formModel, child) { + return ElevatedButton( + onPressed: formModel.form.valid ? () {} : null, + child: const Text('Submit'), + ); + }, + ), + ], + ); + }, + ), + ); + } +} diff --git a/packages/reactive_forms_generator/example/lib/docs/group/group_output.gform.dart b/packages/reactive_forms_generator/example/lib/docs/group/group_output.gform.dart index 5f91b24f..39b6120b 100644 --- a/packages/reactive_forms_generator/example/lib/docs/group/group_output.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/group/group_output.gform.dart @@ -19,7 +19,11 @@ class ReactiveGroupOFormConsumer extends StatelessWidget { final Widget? child; final Widget Function( - BuildContext context, GroupOForm formModel, Widget? child) builder; + BuildContext context, + GroupOForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -38,11 +42,7 @@ class GroupOFormInheritedStreamer extends InheritedStreamer { required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final GroupOForm form; } @@ -64,10 +64,7 @@ class ReactiveGroupOForm extends StatelessWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - static GroupOForm? of( - BuildContext context, { - bool listen = true, - }) { + static GroupOForm? of(BuildContext context, {bool listen = true}) { if (listen) { return context .dependOnInheritedWidgetOfExactType() @@ -121,7 +118,11 @@ class GroupOFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, GroupOForm formModel, Widget? child) builder; + BuildContext context, + GroupOForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, GroupOForm formModel)? initState; @@ -213,11 +214,7 @@ class _GroupOFormBuilderState extends State { final _logGroupOForm = Logger.detached('GroupOForm'); class GroupOForm implements FormModel { - GroupOForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + GroupOForm(this.form, this.path, this._formModel) : initial = form.rawValue; static const String personalControlName = "personal"; @@ -231,7 +228,7 @@ class GroupOForm implements FormModel { final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -247,24 +244,28 @@ class GroupOForm implements FormModel { String address2ControlPath() => pathBuilder(address2ControlName); - PersonalOOutput? get _personalValue => personalForm.model; + PersonalOOutput? get _personalValue => + containsPersonal ? personalForm.model : null; - PhoneOOutput? get _phoneValue => phoneForm.model; + PhoneOOutput? get _phoneValue => containsPhone ? phoneForm.model : null; - AddressOOutput? get _addressValue => addressForm.model; + AddressOOutput? get _addressValue => + containsAddress ? addressForm.model : null; - AddressOOutput? get _address2Value => address2Form.model; + AddressOOutput? get _address2Value => + containsAddress2 ? address2Form.model : null; - PersonalO? get _personalRawValue => personalForm.rawModel; + PersonalO? get _personalRawValue => + containsPersonal ? personalForm.rawModel : null; - PhoneO? get _phoneRawValue => phoneForm.rawModel; + PhoneO? get _phoneRawValue => containsPhone ? phoneForm.rawModel : null; - AddressO? get _addressRawValue => addressForm.rawModel; + AddressO? get _addressRawValue => + containsAddress ? addressForm.rawModel : null; - AddressO? get _address2RawValue => address2Form.rawModel; + AddressO? get _address2RawValue => + containsAddress2 ? address2Form.rawModel : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsPersonal { try { form.control(personalControlPath()); @@ -274,8 +275,6 @@ class GroupOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsPhone { try { form.control(phoneControlPath()); @@ -285,8 +284,6 @@ class GroupOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsAddress { try { form.control(addressControlPath()); @@ -296,8 +293,6 @@ class GroupOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsAddress2 { try { form.control(address2ControlPath()); @@ -323,12 +318,7 @@ class GroupOForm implements FormModel { void get address2Focus => form.focus(address2ControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void personalRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void personalRemove({bool updateParent = true, bool emitEvent = true}) { if (containsPersonal) { final controlPath = path; if (controlPath == null) { @@ -351,12 +341,7 @@ class GroupOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void phoneRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void phoneRemove({bool updateParent = true, bool emitEvent = true}) { if (containsPhone) { final controlPath = path; if (controlPath == null) { @@ -379,12 +364,7 @@ class GroupOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void addressRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void addressRemove({bool updateParent = true, bool emitEvent = true}) { if (containsAddress) { final controlPath = path; if (controlPath == null) { @@ -407,12 +387,7 @@ class GroupOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void address2Remove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void address2Remove({bool updateParent = true, bool emitEvent = true}) { if (containsAddress2) { final controlPath = path; if (controlPath == null) { @@ -440,8 +415,11 @@ class GroupOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - personalControl.updateValue(PersonalOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + personalControl.updateValue( + PersonalOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void phoneValueUpdate( @@ -449,8 +427,11 @@ class GroupOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - phoneControl.updateValue(PhoneOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + phoneControl.updateValue( + PhoneOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void addressValueUpdate( @@ -458,8 +439,11 @@ class GroupOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - addressControl.updateValue(AddressOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + addressControl.updateValue( + AddressOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void address2ValueUpdate( @@ -467,8 +451,11 @@ class GroupOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - address2Control.updateValue(AddressOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + address2Control.updateValue( + AddressOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void personalValuePatch( @@ -476,8 +463,11 @@ class GroupOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - personalControl.updateValue(PersonalOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + personalControl.updateValue( + PersonalOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void phoneValuePatch( @@ -485,8 +475,11 @@ class GroupOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - phoneControl.updateValue(PhoneOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + phoneControl.updateValue( + PhoneOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void addressValuePatch( @@ -494,8 +487,11 @@ class GroupOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - addressControl.updateValue(AddressOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + addressControl.updateValue( + AddressOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void address2ValuePatch( @@ -503,8 +499,11 @@ class GroupOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - address2Control.updateValue(AddressOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + address2Control.updateValue( + AddressOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void personalValueReset( @@ -513,11 +512,11 @@ class GroupOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - personalControl.reset( - value: PersonalOForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => personalControl.reset( + value: PersonalOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); void phoneValueReset( PhoneO? value, { @@ -525,11 +524,11 @@ class GroupOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - phoneControl.reset( - value: PhoneOForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => phoneControl.reset( + value: PhoneOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); void addressValueReset( AddressO? value, { @@ -537,11 +536,11 @@ class GroupOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - addressControl.reset( - value: AddressOForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => addressControl.reset( + value: AddressOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); void address2ValueReset( AddressO? value, { @@ -549,11 +548,11 @@ class GroupOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - address2Control.reset( - value: AddressOForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => address2Control.reset( + value: AddressOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); FormGroup get personalControl => form.control(personalControlPath()) as FormGroup; @@ -663,26 +662,25 @@ class GroupOForm implements FormModel { ); } return GroupOOutput( - personal: _personalValue, - phone: _phoneValue, - address: _addressValue, - address2: _address2Value); + personal: _personalValue, + phone: _phoneValue, + address: _addressValue, + address2: _address2Value, + ); } @override GroupO get rawModel { return GroupO( - personal: _personalRawValue, - phone: _phoneRawValue, - address: _addressRawValue, - address2: _address2RawValue); + personal: _personalRawValue, + phone: _phoneRawValue, + address: _addressRawValue, + address2: _address2RawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; @@ -693,7 +691,9 @@ class GroupOForm implements FormModel { addressForm.toggleDisabled(); address2Form.toggleDisabled(); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { personalForm.toggleDisabled(); phoneForm.toggleDisabled(); @@ -755,9 +755,11 @@ class GroupOForm implements FormModel { GroupO? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(GroupOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + GroupOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -775,17 +777,14 @@ class GroupOForm implements FormModel { GroupO? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -836,26 +835,25 @@ class GroupOForm implements FormModel { String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(GroupO? groupO) => FormGroup({ - personalControlName: PersonalOForm.formElements(groupO?.personal), - phoneControlName: PhoneOForm.formElements(groupO?.phone), - addressControlName: AddressOForm.formElements(groupO?.address), - address2ControlName: AddressOForm.formElements(groupO?.address2) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(GroupO? groupO) => FormGroup( + { + personalControlName: PersonalOForm.formElements(groupO?.personal), + phoneControlName: PhoneOForm.formElements(groupO?.phone), + addressControlName: AddressOForm.formElements(groupO?.address), + address2ControlName: AddressOForm.formElements(groupO?.address2), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } final _logPersonalOForm = Logger.detached('PersonalOForm'); class PersonalOForm implements FormModel { - PersonalOForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + PersonalOForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String nameControlName = "name"; @@ -865,7 +863,7 @@ class PersonalOForm implements FormModel { final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -877,16 +875,14 @@ class PersonalOForm implements FormModel { String emailControlPath() => pathBuilder(emailControlName); - String? get _nameValue => nameControl.value; + String? get _nameValue => containsName ? nameControl.value : null; - String? get _emailValue => emailControl.value; + String? get _emailValue => containsEmail ? emailControl.value : null; - String? get _nameRawValue => nameControl.value; + String? get _nameRawValue => containsName ? nameControl.value : null; - String? get _emailRawValue => emailControl.value; + String? get _emailRawValue => containsEmail ? emailControl.value : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsName { try { form.control(nameControlPath()); @@ -896,8 +892,6 @@ class PersonalOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsEmail { try { form.control(emailControlPath()); @@ -915,12 +909,7 @@ class PersonalOForm implements FormModel { void get emailFocus => form.focus(emailControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void nameRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void nameRemove({bool updateParent = true, bool emitEvent = true}) { if (containsName) { final controlPath = path; if (controlPath == null) { @@ -943,12 +932,7 @@ class PersonalOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void emailRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void emailRemove({bool updateParent = true, bool emitEvent = true}) { if (containsEmail) { final controlPath = path; if (controlPath == null) { @@ -976,8 +960,11 @@ class PersonalOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - nameControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + nameControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailValueUpdate( @@ -985,8 +972,11 @@ class PersonalOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - emailControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void nameValuePatch( @@ -994,8 +984,11 @@ class PersonalOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - nameControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + nameControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailValuePatch( @@ -1003,8 +996,11 @@ class PersonalOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - emailControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void nameValueReset( @@ -1013,14 +1009,13 @@ class PersonalOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - nameControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => nameControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void emailValueReset( String? value, { @@ -1028,14 +1023,13 @@ class PersonalOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - emailControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => emailControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get nameControl => form.control(nameControlPath()) as FormControl; @@ -1100,17 +1094,16 @@ class PersonalOForm implements FormModel { } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { @@ -1168,9 +1161,11 @@ class PersonalOForm implements FormModel { PersonalO? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(PersonalOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + PersonalOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -1188,17 +1183,14 @@ class PersonalOForm implements FormModel { PersonalO? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -1249,36 +1241,36 @@ class PersonalOForm implements FormModel { String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(PersonalO? personalO) => FormGroup({ - nameControlName: FormControl( - value: personalO?.name, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - emailControlName: FormControl( - value: personalO?.email, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(PersonalO? personalO) => FormGroup( + { + nameControlName: FormControl( + value: personalO?.name, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + emailControlName: FormControl( + value: personalO?.email, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } final _logPhoneOForm = Logger.detached('PhoneOForm'); class PhoneOForm implements FormModel { - PhoneOForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + PhoneOForm(this.form, this.path, this._formModel) : initial = form.rawValue; static const String phoneNumberControlName = "phoneNumber"; @@ -1288,7 +1280,7 @@ class PhoneOForm implements FormModel { final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -1300,16 +1292,18 @@ class PhoneOForm implements FormModel { String countryIsoControlPath() => pathBuilder(countryIsoControlName); - String? get _phoneNumberValue => phoneNumberControl.value; + String? get _phoneNumberValue => + containsPhoneNumber ? phoneNumberControl.value : null; - String? get _countryIsoValue => countryIsoControl.value; + String? get _countryIsoValue => + containsCountryIso ? countryIsoControl.value : null; - String? get _phoneNumberRawValue => phoneNumberControl.value; + String? get _phoneNumberRawValue => + containsPhoneNumber ? phoneNumberControl.value : null; - String? get _countryIsoRawValue => countryIsoControl.value; + String? get _countryIsoRawValue => + containsCountryIso ? countryIsoControl.value : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsPhoneNumber { try { form.control(phoneNumberControlPath()); @@ -1319,8 +1313,6 @@ class PhoneOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsCountryIso { try { form.control(countryIsoControlPath()); @@ -1338,12 +1330,7 @@ class PhoneOForm implements FormModel { void get countryIsoFocus => form.focus(countryIsoControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void phoneNumberRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void phoneNumberRemove({bool updateParent = true, bool emitEvent = true}) { if (containsPhoneNumber) { final controlPath = path; if (controlPath == null) { @@ -1366,12 +1353,7 @@ class PhoneOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void countryIsoRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void countryIsoRemove({bool updateParent = true, bool emitEvent = true}) { if (containsCountryIso) { final controlPath = path; if (controlPath == null) { @@ -1399,8 +1381,11 @@ class PhoneOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - phoneNumberControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + phoneNumberControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void countryIsoValueUpdate( @@ -1408,8 +1393,11 @@ class PhoneOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - countryIsoControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + countryIsoControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void phoneNumberValuePatch( @@ -1417,8 +1405,11 @@ class PhoneOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - phoneNumberControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + phoneNumberControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void countryIsoValuePatch( @@ -1426,8 +1417,11 @@ class PhoneOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - countryIsoControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + countryIsoControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void phoneNumberValueReset( @@ -1436,14 +1430,13 @@ class PhoneOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - phoneNumberControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => phoneNumberControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void countryIsoValueReset( String? value, { @@ -1451,14 +1444,13 @@ class PhoneOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - countryIsoControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => countryIsoControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get phoneNumberControl => form.control(phoneNumberControlPath()) as FormControl; @@ -1515,27 +1507,30 @@ class PhoneOForm implements FormModel { ); } return PhoneOOutput( - phoneNumber: _phoneNumberValue, countryIso: _countryIsoValue); + phoneNumber: _phoneNumberValue, + countryIso: _countryIsoValue, + ); } @override PhoneO get rawModel { return PhoneO( - phoneNumber: _phoneNumberRawValue, countryIso: _countryIsoRawValue); + phoneNumber: _phoneNumberRawValue, + countryIso: _countryIsoRawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { @@ -1593,9 +1588,11 @@ class PhoneOForm implements FormModel { PhoneO? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(PhoneOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + PhoneOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -1613,17 +1610,14 @@ class PhoneOForm implements FormModel { PhoneO? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -1674,36 +1668,36 @@ class PhoneOForm implements FormModel { String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(PhoneO? phoneO) => FormGroup({ - phoneNumberControlName: FormControl( - value: phoneO?.phoneNumber, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - countryIsoControlName: FormControl( - value: phoneO?.countryIso, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(PhoneO? phoneO) => FormGroup( + { + phoneNumberControlName: FormControl( + value: phoneO?.phoneNumber, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + countryIsoControlName: FormControl( + value: phoneO?.countryIso, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } final _logAddressOForm = Logger.detached('AddressOForm'); class AddressOForm implements FormModel { - AddressOForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + AddressOForm(this.form, this.path, this._formModel) : initial = form.rawValue; static const String streetControlName = "street"; @@ -1715,7 +1709,7 @@ class AddressOForm implements FormModel { final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -1729,20 +1723,18 @@ class AddressOForm implements FormModel { String zipControlPath() => pathBuilder(zipControlName); - String? get _streetValue => streetControl.value; + String? get _streetValue => containsStreet ? streetControl.value : null; - String? get _cityValue => cityControl.value; + String? get _cityValue => containsCity ? cityControl.value : null; - String? get _zipValue => zipControl.value; + String? get _zipValue => containsZip ? zipControl.value : null; - String? get _streetRawValue => streetControl.value; + String? get _streetRawValue => containsStreet ? streetControl.value : null; - String? get _cityRawValue => cityControl.value; + String? get _cityRawValue => containsCity ? cityControl.value : null; - String? get _zipRawValue => zipControl.value; + String? get _zipRawValue => containsZip ? zipControl.value : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsStreet { try { form.control(streetControlPath()); @@ -1752,8 +1744,6 @@ class AddressOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsCity { try { form.control(cityControlPath()); @@ -1763,8 +1753,6 @@ class AddressOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsZip { try { form.control(zipControlPath()); @@ -1786,12 +1774,7 @@ class AddressOForm implements FormModel { void get zipFocus => form.focus(zipControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void streetRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void streetRemove({bool updateParent = true, bool emitEvent = true}) { if (containsStreet) { final controlPath = path; if (controlPath == null) { @@ -1814,12 +1797,7 @@ class AddressOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void cityRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void cityRemove({bool updateParent = true, bool emitEvent = true}) { if (containsCity) { final controlPath = path; if (controlPath == null) { @@ -1842,12 +1820,7 @@ class AddressOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void zipRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void zipRemove({bool updateParent = true, bool emitEvent = true}) { if (containsZip) { final controlPath = path; if (controlPath == null) { @@ -1875,8 +1848,11 @@ class AddressOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - streetControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + streetControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void cityValueUpdate( @@ -1884,8 +1860,11 @@ class AddressOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - cityControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + cityControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void zipValueUpdate( @@ -1893,8 +1872,11 @@ class AddressOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - zipControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + zipControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void streetValuePatch( @@ -1902,8 +1884,11 @@ class AddressOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - streetControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + streetControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void cityValuePatch( @@ -1911,8 +1896,11 @@ class AddressOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - cityControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + cityControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void zipValuePatch( @@ -1920,8 +1908,11 @@ class AddressOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - zipControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + zipControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void streetValueReset( @@ -1930,14 +1921,13 @@ class AddressOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - streetControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => streetControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void cityValueReset( String? value, { @@ -1945,14 +1935,13 @@ class AddressOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - cityControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => cityControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void zipValueReset( String? value, { @@ -1960,14 +1949,13 @@ class AddressOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - zipControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => zipControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get streetControl => form.control(streetControlPath()) as FormControl; @@ -2045,27 +2033,32 @@ class AddressOForm implements FormModel { ); } return AddressOOutput( - street: _streetValue, city: _cityValue, zip: _zipValue); + street: _streetValue, + city: _cityValue, + zip: _zipValue, + ); } @override AddressO get rawModel { return AddressO( - street: _streetRawValue, city: _cityRawValue, zip: _zipRawValue); + street: _streetRawValue, + city: _cityRawValue, + zip: _zipRawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { @@ -2123,9 +2116,11 @@ class AddressOForm implements FormModel { AddressO? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(AddressOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + AddressOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -2143,17 +2138,14 @@ class AddressOForm implements FormModel { AddressO? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -2204,33 +2196,38 @@ class AddressOForm implements FormModel { String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(AddressO? addressO) => FormGroup({ - streetControlName: FormControl( - value: addressO?.street, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - cityControlName: FormControl( - value: addressO?.city, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - zipControlName: FormControl( - value: addressO?.zip, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(AddressO? addressO) => FormGroup( + { + streetControlName: FormControl( + value: addressO?.street, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + cityControlName: FormControl( + value: addressO?.city, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + zipControlName: FormControl( + value: addressO?.zip, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } @Rf(output: true) @@ -2246,17 +2243,20 @@ class GroupOOutput { class PersonalOOutput { final String? name; final String? email; - PersonalOOutput( - {@RfControl() this.name, @RfControl() this.email}); + PersonalOOutput({ + @RfControl() this.name, + @RfControl() this.email, + }); } @RfGroup() class PhoneOOutput { final String? phoneNumber; final String? countryIso; - PhoneOOutput( - {@RfControl() this.phoneNumber, - @RfControl() this.countryIso}); + PhoneOOutput({ + @RfControl() this.phoneNumber, + @RfControl() this.countryIso, + }); } @RfGroup() @@ -2264,10 +2264,11 @@ class AddressOOutput { final String? street; final String? city; final String? zip; - AddressOOutput( - {@RfControl() this.street, - @RfControl() this.city, - @RfControl() this.zip}); + AddressOOutput({ + @RfControl() this.street, + @RfControl() this.city, + @RfControl() this.zip, + }); } class ReactiveGroupOFormArrayBuilder @@ -2280,30 +2281,39 @@ class ReactiveGroupOFormArrayBuilder required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - GroupOForm formModel)? control; + GroupOForm formModel, + )? + control; final Widget Function( - BuildContext context, List itemList, GroupOForm formModel)? - builder; + BuildContext context, + List itemList, + GroupOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveGroupOFormArrayBuilderT? item, - GroupOForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveGroupOFormArrayBuilderT? item, + GroupOForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function(FormControl control)? - controlFilter; + controlFilter; @override Widget build(BuildContext context) { @@ -2319,18 +2329,9 @@ class ReactiveGroupOFormArrayBuilder return ReactiveFormArrayItemBuilder( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -2348,35 +2349,41 @@ class ReactiveGroupOFormArrayBuilder2 required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - GroupOForm formModel)? control; + GroupOForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - GroupOForm formModel - }) params)? builder; + ({BuildContext context, List itemList, GroupOForm formModel}) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveGroupOFormArrayBuilderT? item, - GroupOForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveGroupOFormArrayBuilderT? item, + GroupOForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function(FormControl control)? - controlFilter; + controlFilter; @override Widget build(BuildContext context) { @@ -2393,23 +2400,17 @@ class ReactiveGroupOFormArrayBuilder2 formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -2418,33 +2419,48 @@ class ReactiveGroupOFormArrayBuilder2 } class ReactiveGroupOFormFormGroupArrayBuilder< - ReactiveGroupOFormFormGroupArrayBuilderT> extends StatelessWidget { + ReactiveGroupOFormFormGroupArrayBuilderT +> + extends StatelessWidget { const ReactiveGroupOFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? extended; - - final ExtendedControl?>, - List> - Function(GroupOForm formModel)? getExtended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(GroupOForm formModel)? + getExtended; final Widget Function( - BuildContext context, List itemList, GroupOForm formModel)? - builder; + BuildContext context, + List itemList, + GroupOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveGroupOFormFormGroupArrayBuilderT? item, - GroupOForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveGroupOFormFormGroupArrayBuilderT? item, + GroupOForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -2462,23 +2478,14 @@ class ReactiveGroupOFormFormGroupArrayBuilder< final itemList = (value.value() ?? []) .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) .values .toList(); - return builder?.call( - context, - itemList, - formModel, - ) ?? + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/reactive_forms_generator/example/lib/docs/login/login.dart b/packages/reactive_forms_generator/example/lib/docs/login/login.dart index 9fa98f58..b14010d3 100644 --- a/packages/reactive_forms_generator/example/lib/docs/login/login.dart +++ b/packages/reactive_forms_generator/example/lib/docs/login/login.dart @@ -8,13 +8,13 @@ part 'login.gform.dart'; @Rf(output: false) @RfGroup(validators: [MustMatchValidator()]) class Login extends Equatable { - final String email; + final String? email; final String password; const Login({ @RfControl(validators: [RequiredValidator(), RequiredValidator()]) - this.email = "", + this.email, @RfControl(validators: [RequiredValidator()]) this.password = "", }); diff --git a/packages/reactive_forms_generator/example/lib/docs/login/login.gform.dart b/packages/reactive_forms_generator/example/lib/docs/login/login.gform.dart index 4eb3e3ec..5a7aa425 100644 --- a/packages/reactive_forms_generator/example/lib/docs/login/login.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/login/login.gform.dart @@ -10,16 +10,17 @@ part of 'login.dart'; // ************************************************************************** class ReactiveLoginFormConsumer extends StatelessWidget { - const ReactiveLoginFormConsumer({ - Key? key, - required this.builder, - this.child, - }) : super(key: key); + const ReactiveLoginFormConsumer({Key? key, required this.builder, this.child}) + : super(key: key); final Widget? child; final Widget Function( - BuildContext context, LoginForm formModel, Widget? child) builder; + BuildContext context, + LoginForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -38,11 +39,7 @@ class LoginFormInheritedStreamer extends InheritedStreamer { required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final LoginForm form; } @@ -64,10 +61,7 @@ class ReactiveLoginForm extends StatelessWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - static LoginForm? of( - BuildContext context, { - bool listen = true, - }) { + static LoginForm? of(BuildContext context, {bool listen = true}) { if (listen) { return context .dependOnInheritedWidgetOfExactType() @@ -121,7 +115,11 @@ class LoginFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, LoginForm formModel, Widget? child) builder; + BuildContext context, + LoginForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, LoginForm formModel)? initState; @@ -213,11 +211,7 @@ class _LoginFormBuilderState extends State { final _logLoginForm = Logger.detached('LoginForm'); class LoginForm implements FormModel { - LoginForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + LoginForm(this.form, this.path, this._formModel) : initial = form.rawValue; static const String emailControlName = "email"; @@ -227,7 +221,7 @@ class LoginForm implements FormModel { final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -239,16 +233,14 @@ class LoginForm implements FormModel { String passwordControlPath() => pathBuilder(passwordControlName); - String get _emailValue => emailControl.value ?? ""; + String? get _emailValue => containsEmail ? emailControl.value : null; String get _passwordValue => passwordControl.value ?? ""; - String get _emailRawValue => emailControl.value ?? ""; + String? get _emailRawValue => containsEmail ? emailControl.value : null; String get _passwordRawValue => passwordControl.value ?? ""; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsEmail { try { form.control(emailControlPath()); @@ -258,8 +250,6 @@ class LoginForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsPassword { try { form.control(passwordControlPath()); @@ -269,7 +259,7 @@ class LoginForm implements FormModel { } } - Map get emailErrors => emailControl.errors; + Map? get emailErrors => emailControl.errors; Map get passwordErrors => passwordControl.errors; @@ -277,13 +267,39 @@ class LoginForm implements FormModel { void get passwordFocus => form.focus(passwordControlPath()); + void emailRemove({bool updateParent = true, bool emitEvent = true}) { + if (containsEmail) { + final controlPath = path; + if (controlPath == null) { + form.removeControl( + emailControlName, + updateParent: updateParent, + emitEvent: emitEvent, + ); + } else { + final formGroup = form.control(controlPath); + + if (formGroup is FormGroup) { + formGroup.removeControl( + emailControlName, + updateParent: updateParent, + emitEvent: emitEvent, + ); + } + } + } + } + void emailValueUpdate( - String value, { + String? value, { bool updateParent = true, bool emitEvent = true, }) { - emailControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void passwordValueUpdate( @@ -291,17 +307,23 @@ class LoginForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - passwordControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + passwordControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailValuePatch( - String value, { + String? value, { bool updateParent = true, bool emitEvent = true, }) { - emailControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void passwordValuePatch( @@ -309,24 +331,26 @@ class LoginForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - passwordControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + passwordControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailValueReset( - String value, { + String? value, { bool updateParent = true, bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - emailControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => emailControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void passwordValueReset( String value, { @@ -334,14 +358,13 @@ class LoginForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - passwordControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => passwordControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get emailControl => form.control(emailControlPath()) as FormControl; @@ -405,17 +428,16 @@ class LoginForm implements FormModel { } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { @@ -473,9 +495,11 @@ class LoginForm implements FormModel { Login? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(LoginForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + LoginForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -489,21 +513,15 @@ class LoginForm implements FormModel { } @override - void reset({ - Login? value, - bool updateParent = true, - bool emitEvent = true, - }) => + void reset({Login? value, bool updateParent = true, bool emitEvent = true}) => currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -554,28 +572,30 @@ class LoginForm implements FormModel { String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(Login? login) => FormGroup({ - emailControlName: FormControl( - value: login?.email, - validators: [RequiredValidator(), RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - passwordControlName: FormControl( - value: login?.password, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [ - MustMatchValidator() - ], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(Login? login) => FormGroup( + { + emailControlName: FormControl( + value: login?.email, + validators: [RequiredValidator(), RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + passwordControlName: FormControl( + value: login?.password, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [MustMatchValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } class ReactiveLoginFormArrayBuilder @@ -588,30 +608,39 @@ class ReactiveLoginFormArrayBuilder required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - LoginForm formModel)? control; + LoginForm formModel, + )? + control; final Widget Function( - BuildContext context, List itemList, LoginForm formModel)? - builder; + BuildContext context, + List itemList, + LoginForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveLoginFormArrayBuilderT? item, - LoginForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveLoginFormArrayBuilderT? item, + LoginForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function(FormControl control)? - controlFilter; + controlFilter; @override Widget build(BuildContext context) { @@ -627,18 +656,9 @@ class ReactiveLoginFormArrayBuilder return ReactiveFormArrayItemBuilder( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -656,35 +676,40 @@ class ReactiveLoginFormArrayBuilder2 required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - LoginForm formModel)? control; + LoginForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - LoginForm formModel - }) params)? builder; + ({BuildContext context, List itemList, LoginForm formModel}) params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveLoginFormArrayBuilderT? item, - LoginForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveLoginFormArrayBuilderT? item, + LoginForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function(FormControl control)? - controlFilter; + controlFilter; @override Widget build(BuildContext context) { @@ -701,23 +726,17 @@ class ReactiveLoginFormArrayBuilder2 formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -726,33 +745,48 @@ class ReactiveLoginFormArrayBuilder2 } class ReactiveLoginFormFormGroupArrayBuilder< - ReactiveLoginFormFormGroupArrayBuilderT> extends StatelessWidget { + ReactiveLoginFormFormGroupArrayBuilderT +> + extends StatelessWidget { const ReactiveLoginFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? extended; - - final ExtendedControl?>, - List> - Function(LoginForm formModel)? getExtended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(LoginForm formModel)? + getExtended; final Widget Function( - BuildContext context, List itemList, LoginForm formModel)? - builder; + BuildContext context, + List itemList, + LoginForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveLoginFormFormGroupArrayBuilderT? item, - LoginForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveLoginFormFormGroupArrayBuilderT? item, + LoginForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -770,23 +804,14 @@ class ReactiveLoginFormFormGroupArrayBuilder< final itemList = (value.value() ?? []) .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) .values .toList(); - return builder?.call( - context, - itemList, - formModel, - ) ?? + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/reactive_forms_generator/example/lib/docs/login/login_output.gform.dart b/packages/reactive_forms_generator/example/lib/docs/login/login_output.gform.dart index 80faa5e4..9fde7b4f 100644 --- a/packages/reactive_forms_generator/example/lib/docs/login/login_output.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/login/login_output.gform.dart @@ -19,7 +19,11 @@ class ReactiveLoginOFormConsumer extends StatelessWidget { final Widget? child; final Widget Function( - BuildContext context, LoginOForm formModel, Widget? child) builder; + BuildContext context, + LoginOForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -38,11 +42,7 @@ class LoginOFormInheritedStreamer extends InheritedStreamer { required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final LoginOForm form; } @@ -64,10 +64,7 @@ class ReactiveLoginOForm extends StatelessWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - static LoginOForm? of( - BuildContext context, { - bool listen = true, - }) { + static LoginOForm? of(BuildContext context, {bool listen = true}) { if (listen) { return context .dependOnInheritedWidgetOfExactType() @@ -121,7 +118,11 @@ class LoginOFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, LoginOForm formModel, Widget? child) builder; + BuildContext context, + LoginOForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, LoginOForm formModel)? initState; @@ -213,11 +214,7 @@ class _LoginOFormBuilderState extends State { final _logLoginOForm = Logger.detached('LoginOForm'); class LoginOForm implements FormModel { - LoginOForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + LoginOForm(this.form, this.path, this._formModel) : initial = form.rawValue; static const String emailControlName = "email"; @@ -227,7 +224,7 @@ class LoginOForm implements FormModel { final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -243,12 +240,11 @@ class LoginOForm implements FormModel { String get _passwordValue => passwordControl.value as String; - String? get _emailRawValue => emailControl.value; + String? get _emailRawValue => containsEmail ? emailControl.value : null; - String? get _passwordRawValue => passwordControl.value; + String? get _passwordRawValue => + containsPassword ? passwordControl.value : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsEmail { try { form.control(emailControlPath()); @@ -258,8 +254,6 @@ class LoginOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsPassword { try { form.control(passwordControlPath()); @@ -277,12 +271,7 @@ class LoginOForm implements FormModel { void get passwordFocus => form.focus(passwordControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void emailRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void emailRemove({bool updateParent = true, bool emitEvent = true}) { if (containsEmail) { final controlPath = path; if (controlPath == null) { @@ -305,12 +294,7 @@ class LoginOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void passwordRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void passwordRemove({bool updateParent = true, bool emitEvent = true}) { if (containsPassword) { final controlPath = path; if (controlPath == null) { @@ -338,8 +322,11 @@ class LoginOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - emailControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void passwordValueUpdate( @@ -347,8 +334,11 @@ class LoginOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - passwordControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + passwordControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailValuePatch( @@ -356,8 +346,11 @@ class LoginOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - emailControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void passwordValuePatch( @@ -365,8 +358,11 @@ class LoginOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - passwordControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + passwordControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailValueReset( @@ -375,14 +371,13 @@ class LoginOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - emailControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => emailControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void passwordValueReset( String? value, { @@ -390,14 +385,13 @@ class LoginOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - passwordControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => passwordControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get emailControl => form.control(emailControlPath()) as FormControl; @@ -462,17 +456,16 @@ class LoginOForm implements FormModel { } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { @@ -530,9 +523,11 @@ class LoginOForm implements FormModel { LoginO? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(LoginOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + LoginOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -550,17 +545,14 @@ class LoginOForm implements FormModel { LoginO? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -611,41 +603,45 @@ class LoginOForm implements FormModel { String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(LoginO? loginO) => FormGroup({ - emailControlName: FormControl( - value: loginO?.email, - validators: [RequiredValidator(), RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - passwordControlName: FormControl( - value: loginO?.password, - validators: [RequiredValidator2()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [ - MustMatchValidator() - ], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(LoginO? loginO) => FormGroup( + { + emailControlName: FormControl( + value: loginO?.email, + validators: [RequiredValidator(), RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + passwordControlName: FormControl( + value: loginO?.password, + validators: [RequiredValidator2()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [MustMatchValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } @Rf( - output: true, - requiredValidators: [...defaultRequiredValidators, 'RequiredValidator2()']) + output: true, + requiredValidators: [...defaultRequiredValidators, 'RequiredValidator2()'], +) @RfGroup(validators: [MustMatchValidator()]) class LoginOOutput extends Equatable { final String email; final String password; - const LoginOOutput( - {@RfControl(validators: [RequiredValidator(), RequiredValidator()]) - required this.email, - @RfControl(validators: [RequiredValidator2()]) required this.password}); + const LoginOOutput({ + @RfControl(validators: [RequiredValidator(), RequiredValidator()]) + required this.email, + @RfControl(validators: [RequiredValidator2()]) required this.password, + }); @override List get props => [email, password]; } @@ -660,30 +656,39 @@ class ReactiveLoginOFormArrayBuilder required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - LoginOForm formModel)? control; + LoginOForm formModel, + )? + control; final Widget Function( - BuildContext context, List itemList, LoginOForm formModel)? - builder; + BuildContext context, + List itemList, + LoginOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveLoginOFormArrayBuilderT? item, - LoginOForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveLoginOFormArrayBuilderT? item, + LoginOForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function(FormControl control)? - controlFilter; + controlFilter; @override Widget build(BuildContext context) { @@ -699,18 +704,9 @@ class ReactiveLoginOFormArrayBuilder return ReactiveFormArrayItemBuilder( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -728,35 +724,41 @@ class ReactiveLoginOFormArrayBuilder2 required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - LoginOForm formModel)? control; + LoginOForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - LoginOForm formModel - }) params)? builder; + ({BuildContext context, List itemList, LoginOForm formModel}) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveLoginOFormArrayBuilderT? item, - LoginOForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveLoginOFormArrayBuilderT? item, + LoginOForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function(FormControl control)? - controlFilter; + controlFilter; @override Widget build(BuildContext context) { @@ -773,23 +775,17 @@ class ReactiveLoginOFormArrayBuilder2 formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -798,33 +794,48 @@ class ReactiveLoginOFormArrayBuilder2 } class ReactiveLoginOFormFormGroupArrayBuilder< - ReactiveLoginOFormFormGroupArrayBuilderT> extends StatelessWidget { + ReactiveLoginOFormFormGroupArrayBuilderT +> + extends StatelessWidget { const ReactiveLoginOFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? extended; - - final ExtendedControl?>, - List> - Function(LoginOForm formModel)? getExtended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(LoginOForm formModel)? + getExtended; final Widget Function( - BuildContext context, List itemList, LoginOForm formModel)? - builder; + BuildContext context, + List itemList, + LoginOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveLoginOFormFormGroupArrayBuilderT? item, - LoginOForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveLoginOFormFormGroupArrayBuilderT? item, + LoginOForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -842,23 +853,14 @@ class ReactiveLoginOFormFormGroupArrayBuilder< final itemList = (value.value() ?? []) .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) .values .toList(); - return builder?.call( - context, - itemList, - formModel, - ) ?? + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/reactive_forms_generator/example/lib/docs/login_extended/login_extended.gform.dart b/packages/reactive_forms_generator/example/lib/docs/login_extended/login_extended.gform.dart index 59144c45..8caf2c29 100644 --- a/packages/reactive_forms_generator/example/lib/docs/login_extended/login_extended.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/login_extended/login_extended.gform.dart @@ -19,7 +19,11 @@ class ReactiveLoginExtendedFormConsumer extends StatelessWidget { final Widget? child; final Widget Function( - BuildContext context, LoginExtendedForm formModel, Widget? child) builder; + BuildContext context, + LoginExtendedForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -38,11 +42,7 @@ class LoginExtendedFormInheritedStreamer extends InheritedStreamer { required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final LoginExtendedForm form; } @@ -64,19 +64,19 @@ class ReactiveLoginExtendedForm extends StatelessWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - static LoginExtendedForm? of( - BuildContext context, { - bool listen = true, - }) { + static LoginExtendedForm? of(BuildContext context, {bool listen = true}) { if (listen) { return context .dependOnInheritedWidgetOfExactType< - LoginExtendedFormInheritedStreamer>() + LoginExtendedFormInheritedStreamer + >() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - LoginExtendedFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + LoginExtendedFormInheritedStreamer + >(); return element == null ? null : (element.widget as LoginExtendedFormInheritedStreamer).form; @@ -124,10 +124,14 @@ class LoginExtendedFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, LoginExtendedForm formModel, Widget? child) builder; + BuildContext context, + LoginExtendedForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, LoginExtendedForm formModel)? - initState; + initState; @override _LoginExtendedFormBuilderState createState() => @@ -142,7 +146,10 @@ class _LoginExtendedFormBuilderState extends State { @override void initState() { _formModel = LoginExtendedForm( - LoginExtendedForm.formElements(widget.model), null, null); + LoginExtendedForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -219,11 +226,8 @@ class _LoginExtendedFormBuilderState extends State { final _logLoginExtendedForm = Logger.detached('LoginExtendedForm'); class LoginExtendedForm implements FormModel { - LoginExtendedForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + LoginExtendedForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String emailControlName = "email"; @@ -247,7 +251,7 @@ class LoginExtendedForm implements FormModel { final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -287,7 +291,8 @@ class LoginExtendedForm implements FormModel { double get _heightValue => heightControl.value as double; - String? get _unAnnotatedValue => unAnnotatedControl.value; + String? get _unAnnotatedValue => + containsUnAnnotated ? unAnnotatedControl.value : null; List get _someIntListValue => someIntListControl.value ?? const []; @@ -305,12 +310,11 @@ class LoginExtendedForm implements FormModel { double get _heightRawValue => heightControl.value as double; - String? get _unAnnotatedRawValue => unAnnotatedControl.value; + String? get _unAnnotatedRawValue => + containsUnAnnotated ? unAnnotatedControl.value : null; List get _someIntListRawValue => someIntListControl.value ?? const []; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsEmail { try { form.control(emailControlPath()); @@ -320,8 +324,6 @@ class LoginExtendedForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsPassword { try { form.control(passwordControlPath()); @@ -331,8 +333,6 @@ class LoginExtendedForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsRememberMe { try { form.control(rememberMeControlPath()); @@ -342,8 +342,6 @@ class LoginExtendedForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsTheme { try { form.control(themeControlPath()); @@ -353,8 +351,6 @@ class LoginExtendedForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsMode { try { form.control(modeControlPath()); @@ -364,8 +360,6 @@ class LoginExtendedForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsTimeout { try { form.control(timeoutControlPath()); @@ -375,8 +369,6 @@ class LoginExtendedForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsHeight { try { form.control(heightControlPath()); @@ -386,8 +378,6 @@ class LoginExtendedForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsUnAnnotated { try { form.control(unAnnotatedControlPath()); @@ -397,8 +387,6 @@ class LoginExtendedForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsSomeIntList { try { form.control(someIntListControlPath()); @@ -444,12 +432,7 @@ class LoginExtendedForm implements FormModel { void get someIntListFocus => form.focus(someIntListControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void unAnnotatedRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void unAnnotatedRemove({bool updateParent = true, bool emitEvent = true}) { if (containsUnAnnotated) { final controlPath = path; if (controlPath == null) { @@ -477,8 +460,11 @@ class LoginExtendedForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - emailControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void passwordValueUpdate( @@ -486,8 +472,11 @@ class LoginExtendedForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - passwordControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + passwordControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void rememberMeValueUpdate( @@ -495,8 +484,11 @@ class LoginExtendedForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - rememberMeControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + rememberMeControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void themeValueUpdate( @@ -504,8 +496,11 @@ class LoginExtendedForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - themeControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + themeControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void modeValueUpdate( @@ -513,8 +508,11 @@ class LoginExtendedForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - modeControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + modeControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void timeoutValueUpdate( @@ -522,8 +520,11 @@ class LoginExtendedForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - timeoutControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + timeoutControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void heightValueUpdate( @@ -531,8 +532,11 @@ class LoginExtendedForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - heightControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + heightControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void unAnnotatedValueUpdate( @@ -540,8 +544,11 @@ class LoginExtendedForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - unAnnotatedControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + unAnnotatedControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void someIntListValueUpdate( @@ -549,8 +556,11 @@ class LoginExtendedForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - someIntListControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + someIntListControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailValuePatch( @@ -558,8 +568,11 @@ class LoginExtendedForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - emailControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void passwordValuePatch( @@ -567,8 +580,11 @@ class LoginExtendedForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - passwordControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + passwordControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void rememberMeValuePatch( @@ -576,8 +592,11 @@ class LoginExtendedForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - rememberMeControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + rememberMeControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void themeValuePatch( @@ -585,8 +604,11 @@ class LoginExtendedForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - themeControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + themeControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void modeValuePatch( @@ -594,8 +616,11 @@ class LoginExtendedForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - modeControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + modeControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void timeoutValuePatch( @@ -603,8 +628,11 @@ class LoginExtendedForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - timeoutControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + timeoutControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void heightValuePatch( @@ -612,8 +640,11 @@ class LoginExtendedForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - heightControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + heightControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void unAnnotatedValuePatch( @@ -621,8 +652,11 @@ class LoginExtendedForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - unAnnotatedControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + unAnnotatedControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void someIntListValuePatch( @@ -630,8 +664,11 @@ class LoginExtendedForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - someIntListControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + someIntListControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailValueReset( @@ -640,14 +677,13 @@ class LoginExtendedForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - emailControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => emailControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void passwordValueReset( String value, { @@ -655,14 +691,13 @@ class LoginExtendedForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - passwordControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => passwordControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void rememberMeValueReset( bool value, { @@ -670,14 +705,13 @@ class LoginExtendedForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - rememberMeControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => rememberMeControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void themeValueReset( String value, { @@ -685,14 +719,13 @@ class LoginExtendedForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - themeControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => themeControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void modeValueReset( UserMode value, { @@ -700,14 +733,13 @@ class LoginExtendedForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - modeControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => modeControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void timeoutValueReset( int value, { @@ -715,14 +747,13 @@ class LoginExtendedForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - timeoutControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => timeoutControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void heightValueReset( double value, { @@ -730,14 +761,13 @@ class LoginExtendedForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - heightControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => heightControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void unAnnotatedValueReset( String? value, { @@ -745,14 +775,13 @@ class LoginExtendedForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - unAnnotatedControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => unAnnotatedControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void someIntListValueReset( List value, { @@ -760,14 +789,13 @@ class LoginExtendedForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - someIntListControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => someIntListControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get emailControl => form.control(emailControlPath()) as FormControl; @@ -970,43 +998,44 @@ class LoginExtendedForm implements FormModel { ); } return LoginExtended( - email: _emailValue, - password: _passwordValue, - rememberMe: _rememberMeValue, - theme: _themeValue, - mode: _modeValue, - timeout: _timeoutValue, - height: _heightValue, - unAnnotated: _unAnnotatedValue, - someIntList: _someIntListValue); + email: _emailValue, + password: _passwordValue, + rememberMe: _rememberMeValue, + theme: _themeValue, + mode: _modeValue, + timeout: _timeoutValue, + height: _heightValue, + unAnnotated: _unAnnotatedValue, + someIntList: _someIntListValue, + ); } @override LoginExtended get rawModel { return LoginExtended( - email: _emailRawValue, - password: _passwordRawValue, - rememberMe: _rememberMeRawValue, - theme: _themeRawValue, - mode: _modeRawValue, - timeout: _timeoutRawValue, - height: _heightRawValue, - unAnnotated: _unAnnotatedRawValue, - someIntList: _someIntListRawValue); + email: _emailRawValue, + password: _passwordRawValue, + rememberMe: _rememberMeRawValue, + theme: _themeRawValue, + mode: _modeRawValue, + timeout: _timeoutRawValue, + height: _heightRawValue, + unAnnotated: _unAnnotatedRawValue, + someIntList: _someIntListRawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { @@ -1064,9 +1093,11 @@ class LoginExtendedForm implements FormModel { LoginExtended? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(LoginExtendedForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + LoginExtendedForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -1084,17 +1115,14 @@ class LoginExtendedForm implements FormModel { LoginExtended? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -1145,81 +1173,92 @@ class LoginExtendedForm implements FormModel { String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(LoginExtended? loginExtended) => FormGroup({ - emailControlName: FormControl( - value: loginExtended?.email, - validators: [RequiredValidator()], - asyncValidators: [UniqueEmailAsyncValidator()], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - passwordControlName: FormControl( - value: loginExtended?.password, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - rememberMeControlName: FormControl( - value: loginExtended?.rememberMe, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - themeControlName: FormControl( - value: loginExtended?.theme, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - modeControlName: FormControl( - value: loginExtended?.mode, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - timeoutControlName: FormControl( - value: loginExtended?.timeout, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - heightControlName: FormControl( - value: loginExtended?.height, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - unAnnotatedControlName: FormControl( - value: loginExtended?.unAnnotated, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - someIntListControlName: FormControl>( - value: loginExtended?.someIntList, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [ - AllFieldsRequired() - ], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(LoginExtended? loginExtended) => FormGroup( + { + emailControlName: FormControl( + value: loginExtended?.email, + validators: [RequiredValidator()], + asyncValidators: [UniqueEmailAsyncValidator()], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + passwordControlName: FormControl( + value: loginExtended?.password, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + rememberMeControlName: FormControl( + value: loginExtended?.rememberMe, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + themeControlName: FormControl( + value: loginExtended?.theme, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + modeControlName: FormControl( + value: loginExtended?.mode, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + timeoutControlName: FormControl( + value: loginExtended?.timeout, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + heightControlName: FormControl( + value: loginExtended?.height, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + unAnnotatedControlName: FormControl( + value: loginExtended?.unAnnotated, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + someIntListControlName: FormControl>( + value: loginExtended?.someIntList, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [AllFieldsRequired()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } class ReactiveLoginExtendedFormArrayBuilder< - ReactiveLoginExtendedFormArrayBuilderT> extends StatelessWidget { + ReactiveLoginExtendedFormArrayBuilderT +> + extends StatelessWidget { const ReactiveLoginExtendedFormArrayBuilder({ Key? key, this.control, @@ -1228,30 +1267,41 @@ class ReactiveLoginExtendedFormArrayBuilder< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - LoginExtendedForm formModel)? control; + LoginExtendedForm formModel, + )? + control; - final Widget Function(BuildContext context, List itemList, - LoginExtendedForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + LoginExtendedForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveLoginExtendedFormArrayBuilderT? item, - LoginExtendedForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveLoginExtendedFormArrayBuilderT? item, + LoginExtendedForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -1267,18 +1317,9 @@ class ReactiveLoginExtendedFormArrayBuilder< return ReactiveFormArrayItemBuilder( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1287,7 +1328,9 @@ class ReactiveLoginExtendedFormArrayBuilder< } class ReactiveLoginExtendedFormArrayBuilder2< - ReactiveLoginExtendedFormArrayBuilderT> extends StatelessWidget { + ReactiveLoginExtendedFormArrayBuilderT +> + extends StatelessWidget { const ReactiveLoginExtendedFormArrayBuilder2({ Key? key, this.control, @@ -1296,36 +1339,43 @@ class ReactiveLoginExtendedFormArrayBuilder2< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - LoginExtendedForm formModel)? control; + LoginExtendedForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - LoginExtendedForm formModel - }) params)? builder; + ({BuildContext context, List itemList, LoginExtendedForm formModel}) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveLoginExtendedFormArrayBuilderT? item, - LoginExtendedForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveLoginExtendedFormArrayBuilderT? item, + LoginExtendedForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -1342,23 +1392,17 @@ class ReactiveLoginExtendedFormArrayBuilder2< formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1367,32 +1411,48 @@ class ReactiveLoginExtendedFormArrayBuilder2< } class ReactiveLoginExtendedFormFormGroupArrayBuilder< - ReactiveLoginExtendedFormFormGroupArrayBuilderT> extends StatelessWidget { + ReactiveLoginExtendedFormFormGroupArrayBuilderT +> + extends StatelessWidget { const ReactiveLoginExtendedFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(LoginExtendedForm formModel)? + getExtended; - final ExtendedControl?>, - List>? extended; - - final ExtendedControl?>, - List> - Function(LoginExtendedForm formModel)? getExtended; - - final Widget Function(BuildContext context, List itemList, - LoginExtendedForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + LoginExtendedForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveLoginExtendedFormFormGroupArrayBuilderT? item, - LoginExtendedForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveLoginExtendedFormFormGroupArrayBuilderT? item, + LoginExtendedForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -1407,26 +1467,18 @@ class ReactiveLoginExtendedFormFormGroupArrayBuilder< return StreamBuilder?>?>( stream: value.control.valueChanges, builder: (context, snapshot) { - final itemList = (value.value() ?? - []) - .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) - .values - .toList(); - - return builder?.call( - context, - itemList, - formModel, - ) ?? + final itemList = + (value.value() ?? + []) + .asMap() + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) + .values + .toList(); + + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/reactive_forms_generator/example/lib/docs/login_extended/login_extended_output.gform.dart b/packages/reactive_forms_generator/example/lib/docs/login_extended/login_extended_output.gform.dart index 38f9be0d..8bbf5502 100644 --- a/packages/reactive_forms_generator/example/lib/docs/login_extended/login_extended_output.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/login_extended/login_extended_output.gform.dart @@ -19,8 +19,11 @@ class ReactiveLoginExtendedOFormConsumer extends StatelessWidget { final Widget? child; final Widget Function( - BuildContext context, LoginExtendedOForm formModel, Widget? child) - builder; + BuildContext context, + LoginExtendedOForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -39,11 +42,7 @@ class LoginExtendedOFormInheritedStreamer extends InheritedStreamer { required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final LoginExtendedOForm form; } @@ -65,19 +64,19 @@ class ReactiveLoginExtendedOForm extends StatelessWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - static LoginExtendedOForm? of( - BuildContext context, { - bool listen = true, - }) { + static LoginExtendedOForm? of(BuildContext context, {bool listen = true}) { if (listen) { return context .dependOnInheritedWidgetOfExactType< - LoginExtendedOFormInheritedStreamer>() + LoginExtendedOFormInheritedStreamer + >() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - LoginExtendedOFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + LoginExtendedOFormInheritedStreamer + >(); return element == null ? null : (element.widget as LoginExtendedOFormInheritedStreamer).form; @@ -125,11 +124,14 @@ class LoginExtendedOFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, LoginExtendedOForm formModel, Widget? child) - builder; + BuildContext context, + LoginExtendedOForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, LoginExtendedOForm formModel)? - initState; + initState; @override _LoginExtendedOFormBuilderState createState() => @@ -144,7 +146,10 @@ class _LoginExtendedOFormBuilderState extends State { @override void initState() { _formModel = LoginExtendedOForm( - LoginExtendedOForm.formElements(widget.model), null, null); + LoginExtendedOForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -222,11 +227,8 @@ final _logLoginExtendedOForm = Logger.detached('LoginExtendedOForm'); class LoginExtendedOForm implements FormModel { - LoginExtendedOForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + LoginExtendedOForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String emailControlName = "email"; @@ -252,7 +254,7 @@ class LoginExtendedOForm final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -296,11 +298,12 @@ class LoginExtendedOForm double get _heightValue => heightControl.value as double; - String? get _unAnnotatedValue => unAnnotatedControl.value; + String? get _unAnnotatedValue => + containsUnAnnotated ? unAnnotatedControl.value : null; List get _someIntListValue => someIntListControl.value ?? const []; - String? get _emailRawValue => emailControl.value; + String? get _emailRawValue => containsEmail ? emailControl.value : null; String get _email2RawValue => email2Control.value ?? ""; @@ -316,12 +319,11 @@ class LoginExtendedOForm double get _heightRawValue => heightControl.value as double; - String? get _unAnnotatedRawValue => unAnnotatedControl.value; + String? get _unAnnotatedRawValue => + containsUnAnnotated ? unAnnotatedControl.value : null; List get _someIntListRawValue => someIntListControl.value ?? const []; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsEmail { try { form.control(emailControlPath()); @@ -331,8 +333,6 @@ class LoginExtendedOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsEmail2 { try { form.control(email2ControlPath()); @@ -342,8 +342,6 @@ class LoginExtendedOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsPassword { try { form.control(passwordControlPath()); @@ -353,8 +351,6 @@ class LoginExtendedOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsRememberMe { try { form.control(rememberMeControlPath()); @@ -364,8 +360,6 @@ class LoginExtendedOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsTheme { try { form.control(themeControlPath()); @@ -375,8 +369,6 @@ class LoginExtendedOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsMode { try { form.control(modeControlPath()); @@ -386,8 +378,6 @@ class LoginExtendedOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsTimeout { try { form.control(timeoutControlPath()); @@ -397,8 +387,6 @@ class LoginExtendedOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsHeight { try { form.control(heightControlPath()); @@ -408,8 +396,6 @@ class LoginExtendedOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsUnAnnotated { try { form.control(unAnnotatedControlPath()); @@ -419,8 +405,6 @@ class LoginExtendedOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsSomeIntList { try { form.control(someIntListControlPath()); @@ -470,12 +454,7 @@ class LoginExtendedOForm void get someIntListFocus => form.focus(someIntListControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void emailRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void emailRemove({bool updateParent = true, bool emitEvent = true}) { if (containsEmail) { final controlPath = path; if (controlPath == null) { @@ -498,12 +477,7 @@ class LoginExtendedOForm } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void unAnnotatedRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void unAnnotatedRemove({bool updateParent = true, bool emitEvent = true}) { if (containsUnAnnotated) { final controlPath = path; if (controlPath == null) { @@ -531,8 +505,11 @@ class LoginExtendedOForm bool updateParent = true, bool emitEvent = true, }) { - emailControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void email2ValueUpdate( @@ -540,8 +517,11 @@ class LoginExtendedOForm bool updateParent = true, bool emitEvent = true, }) { - email2Control.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + email2Control.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void passwordValueUpdate( @@ -549,8 +529,11 @@ class LoginExtendedOForm bool updateParent = true, bool emitEvent = true, }) { - passwordControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + passwordControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void rememberMeValueUpdate( @@ -558,8 +541,11 @@ class LoginExtendedOForm bool updateParent = true, bool emitEvent = true, }) { - rememberMeControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + rememberMeControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void themeValueUpdate( @@ -567,8 +553,11 @@ class LoginExtendedOForm bool updateParent = true, bool emitEvent = true, }) { - themeControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + themeControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void modeValueUpdate( @@ -576,8 +565,11 @@ class LoginExtendedOForm bool updateParent = true, bool emitEvent = true, }) { - modeControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + modeControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void timeoutValueUpdate( @@ -585,8 +577,11 @@ class LoginExtendedOForm bool updateParent = true, bool emitEvent = true, }) { - timeoutControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + timeoutControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void heightValueUpdate( @@ -594,8 +589,11 @@ class LoginExtendedOForm bool updateParent = true, bool emitEvent = true, }) { - heightControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + heightControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void unAnnotatedValueUpdate( @@ -603,8 +601,11 @@ class LoginExtendedOForm bool updateParent = true, bool emitEvent = true, }) { - unAnnotatedControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + unAnnotatedControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void someIntListValueUpdate( @@ -612,8 +613,11 @@ class LoginExtendedOForm bool updateParent = true, bool emitEvent = true, }) { - someIntListControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + someIntListControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailValuePatch( @@ -621,8 +625,11 @@ class LoginExtendedOForm bool updateParent = true, bool emitEvent = true, }) { - emailControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void email2ValuePatch( @@ -630,8 +637,11 @@ class LoginExtendedOForm bool updateParent = true, bool emitEvent = true, }) { - email2Control.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + email2Control.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void passwordValuePatch( @@ -639,8 +649,11 @@ class LoginExtendedOForm bool updateParent = true, bool emitEvent = true, }) { - passwordControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + passwordControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void rememberMeValuePatch( @@ -648,8 +661,11 @@ class LoginExtendedOForm bool updateParent = true, bool emitEvent = true, }) { - rememberMeControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + rememberMeControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void themeValuePatch( @@ -657,8 +673,11 @@ class LoginExtendedOForm bool updateParent = true, bool emitEvent = true, }) { - themeControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + themeControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void modeValuePatch( @@ -666,8 +685,11 @@ class LoginExtendedOForm bool updateParent = true, bool emitEvent = true, }) { - modeControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + modeControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void timeoutValuePatch( @@ -675,8 +697,11 @@ class LoginExtendedOForm bool updateParent = true, bool emitEvent = true, }) { - timeoutControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + timeoutControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void heightValuePatch( @@ -684,8 +709,11 @@ class LoginExtendedOForm bool updateParent = true, bool emitEvent = true, }) { - heightControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + heightControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void unAnnotatedValuePatch( @@ -693,8 +721,11 @@ class LoginExtendedOForm bool updateParent = true, bool emitEvent = true, }) { - unAnnotatedControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + unAnnotatedControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void someIntListValuePatch( @@ -702,8 +733,11 @@ class LoginExtendedOForm bool updateParent = true, bool emitEvent = true, }) { - someIntListControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + someIntListControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailValueReset( @@ -712,14 +746,13 @@ class LoginExtendedOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - emailControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => emailControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void email2ValueReset( String value, { @@ -727,14 +760,13 @@ class LoginExtendedOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - email2Control.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => email2Control.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void passwordValueReset( String value, { @@ -742,14 +774,13 @@ class LoginExtendedOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - passwordControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => passwordControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void rememberMeValueReset( bool value, { @@ -757,14 +788,13 @@ class LoginExtendedOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - rememberMeControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => rememberMeControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void themeValueReset( String value, { @@ -772,14 +802,13 @@ class LoginExtendedOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - themeControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => themeControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void modeValueReset( UserMode value, { @@ -787,14 +816,13 @@ class LoginExtendedOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - modeControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => modeControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void timeoutValueReset( int value, { @@ -802,14 +830,13 @@ class LoginExtendedOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - timeoutControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => timeoutControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void heightValueReset( double value, { @@ -817,14 +844,13 @@ class LoginExtendedOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - heightControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => heightControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void unAnnotatedValueReset( String? value, { @@ -832,14 +858,13 @@ class LoginExtendedOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - unAnnotatedControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => unAnnotatedControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void someIntListValueReset( List value, { @@ -847,14 +872,13 @@ class LoginExtendedOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - someIntListControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => someIntListControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get emailControl => form.control(emailControlPath()) as FormControl; @@ -1079,45 +1103,46 @@ class LoginExtendedOForm ); } return LoginExtendedOOutput( - email: _emailValue, - email2: _email2Value, - password: _passwordValue, - rememberMe: _rememberMeValue, - theme: _themeValue, - mode: _modeValue, - timeout: _timeoutValue, - height: _heightValue, - unAnnotated: _unAnnotatedValue, - someIntList: _someIntListValue); + email: _emailValue, + email2: _email2Value, + password: _passwordValue, + rememberMe: _rememberMeValue, + theme: _themeValue, + mode: _modeValue, + timeout: _timeoutValue, + height: _heightValue, + unAnnotated: _unAnnotatedValue, + someIntList: _someIntListValue, + ); } @override LoginExtendedO get rawModel { return LoginExtendedO( - email: _emailRawValue, - email2: _email2RawValue, - password: _passwordRawValue, - rememberMe: _rememberMeRawValue, - theme: _themeRawValue, - mode: _modeRawValue, - timeout: _timeoutRawValue, - height: _heightRawValue, - unAnnotated: _unAnnotatedRawValue, - someIntList: _someIntListRawValue); + email: _emailRawValue, + email2: _email2RawValue, + password: _passwordRawValue, + rememberMe: _rememberMeRawValue, + theme: _themeRawValue, + mode: _modeRawValue, + timeout: _timeoutRawValue, + height: _heightRawValue, + unAnnotated: _unAnnotatedRawValue, + someIntList: _someIntListRawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { @@ -1175,9 +1200,11 @@ class LoginExtendedOForm LoginExtendedO? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(LoginExtendedOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + LoginExtendedOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -1195,17 +1222,14 @@ class LoginExtendedOForm LoginExtendedO? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -1256,84 +1280,94 @@ class LoginExtendedOForm String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(LoginExtendedO? loginExtendedO) => FormGroup({ - emailControlName: FormControl( - value: loginExtendedO?.email, - validators: [RequiredValidator()], - asyncValidators: [UniqueEmailAsyncValidator()], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - email2ControlName: FormControl( - value: loginExtendedO?.email2, - validators: [RequiredValidator()], - asyncValidators: [UniqueEmailAsyncValidator()], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - passwordControlName: FormControl( - value: loginExtendedO?.password, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - rememberMeControlName: FormControl( - value: loginExtendedO?.rememberMe, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - themeControlName: FormControl( - value: loginExtendedO?.theme, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - modeControlName: FormControl( - value: loginExtendedO?.mode, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - timeoutControlName: FormControl( - value: loginExtendedO?.timeout, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - heightControlName: FormControl( - value: loginExtendedO?.height, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - unAnnotatedControlName: FormControl( - value: loginExtendedO?.unAnnotated, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - someIntListControlName: FormControl>( - value: loginExtendedO?.someIntList, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [ - AllFieldsRequired() - ], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(LoginExtendedO? loginExtendedO) => FormGroup( + { + emailControlName: FormControl( + value: loginExtendedO?.email, + validators: [RequiredValidator()], + asyncValidators: [UniqueEmailAsyncValidator()], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + email2ControlName: FormControl( + value: loginExtendedO?.email2, + validators: [RequiredValidator()], + asyncValidators: [UniqueEmailAsyncValidator()], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + passwordControlName: FormControl( + value: loginExtendedO?.password, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + rememberMeControlName: FormControl( + value: loginExtendedO?.rememberMe, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + themeControlName: FormControl( + value: loginExtendedO?.theme, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + modeControlName: FormControl( + value: loginExtendedO?.mode, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + timeoutControlName: FormControl( + value: loginExtendedO?.timeout, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + heightControlName: FormControl( + value: loginExtendedO?.height, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + unAnnotatedControlName: FormControl( + value: loginExtendedO?.unAnnotated, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + someIntListControlName: FormControl>( + value: loginExtendedO?.someIntList, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [AllFieldsRequired()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } @Rf(output: true) @@ -1349,27 +1383,32 @@ class LoginExtendedOOutput { final double height; final String? unAnnotated; final List someIntList; - LoginExtendedOOutput( - {@RfControl( - validators: [RequiredValidator()], - asyncValidators: [UniqueEmailAsyncValidator()]) - required this.email, - @RfControl( - validators: [RequiredValidator()], - asyncValidators: [UniqueEmailAsyncValidator()]) - required this.email2, - @RfControl(validators: [RequiredValidator()]) required this.password, - @RfControl(validators: [RequiredValidator()]) required this.rememberMe, - @RfControl(validators: [RequiredValidator()]) required this.theme, - @RfControl(validators: [RequiredValidator()]) required this.mode, - @RfControl(validators: [RequiredValidator()]) required this.timeout, - @RfControl(validators: [RequiredValidator()]) required this.height, - this.unAnnotated, - this.someIntList = const []}); + LoginExtendedOOutput({ + @RfControl( + validators: [RequiredValidator()], + asyncValidators: [UniqueEmailAsyncValidator()], + ) + required this.email, + @RfControl( + validators: [RequiredValidator()], + asyncValidators: [UniqueEmailAsyncValidator()], + ) + required this.email2, + @RfControl(validators: [RequiredValidator()]) required this.password, + @RfControl(validators: [RequiredValidator()]) required this.rememberMe, + @RfControl(validators: [RequiredValidator()]) required this.theme, + @RfControl(validators: [RequiredValidator()]) required this.mode, + @RfControl(validators: [RequiredValidator()]) required this.timeout, + @RfControl(validators: [RequiredValidator()]) required this.height, + this.unAnnotated, + this.someIntList = const [], + }); } class ReactiveLoginExtendedOFormArrayBuilder< - ReactiveLoginExtendedOFormArrayBuilderT> extends StatelessWidget { + ReactiveLoginExtendedOFormArrayBuilderT +> + extends StatelessWidget { const ReactiveLoginExtendedOFormArrayBuilder({ Key? key, this.control, @@ -1378,30 +1417,41 @@ class ReactiveLoginExtendedOFormArrayBuilder< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - LoginExtendedOForm formModel)? control; + LoginExtendedOForm formModel, + )? + control; - final Widget Function(BuildContext context, List itemList, - LoginExtendedOForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + LoginExtendedOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveLoginExtendedOFormArrayBuilderT? item, - LoginExtendedOForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveLoginExtendedOFormArrayBuilderT? item, + LoginExtendedOForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -1415,21 +1465,13 @@ class ReactiveLoginExtendedOFormArrayBuilder< final itemBuilder = this.itemBuilder; return ReactiveFormArrayItemBuilder< - ReactiveLoginExtendedOFormArrayBuilderT>( + ReactiveLoginExtendedOFormArrayBuilderT + >( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1438,7 +1480,9 @@ class ReactiveLoginExtendedOFormArrayBuilder< } class ReactiveLoginExtendedOFormArrayBuilder2< - ReactiveLoginExtendedOFormArrayBuilderT> extends StatelessWidget { + ReactiveLoginExtendedOFormArrayBuilderT +> + extends StatelessWidget { const ReactiveLoginExtendedOFormArrayBuilder2({ Key? key, this.control, @@ -1447,36 +1491,47 @@ class ReactiveLoginExtendedOFormArrayBuilder2< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - LoginExtendedOForm formModel)? control; + LoginExtendedOForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - LoginExtendedOForm formModel - }) params)? builder; + ({ + BuildContext context, + List itemList, + LoginExtendedOForm formModel, + }) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveLoginExtendedOFormArrayBuilderT? item, - LoginExtendedOForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveLoginExtendedOFormArrayBuilderT? item, + LoginExtendedOForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -1490,27 +1545,22 @@ class ReactiveLoginExtendedOFormArrayBuilder2< final itemBuilder = this.itemBuilder; return ReactiveFormArrayItemBuilder< - ReactiveLoginExtendedOFormArrayBuilderT>( + ReactiveLoginExtendedOFormArrayBuilderT + >( formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1519,32 +1569,48 @@ class ReactiveLoginExtendedOFormArrayBuilder2< } class ReactiveLoginExtendedOFormFormGroupArrayBuilder< - ReactiveLoginExtendedOFormFormGroupArrayBuilderT> extends StatelessWidget { + ReactiveLoginExtendedOFormFormGroupArrayBuilderT +> + extends StatelessWidget { const ReactiveLoginExtendedOFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(LoginExtendedOForm formModel)? + getExtended; - final ExtendedControl?>, - List>? extended; - - final ExtendedControl?>, - List> - Function(LoginExtendedOForm formModel)? getExtended; - - final Widget Function(BuildContext context, List itemList, - LoginExtendedOForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + LoginExtendedOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveLoginExtendedOFormFormGroupArrayBuilderT? item, - LoginExtendedOForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveLoginExtendedOFormFormGroupArrayBuilderT? item, + LoginExtendedOForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -1559,26 +1625,18 @@ class ReactiveLoginExtendedOFormFormGroupArrayBuilder< return StreamBuilder?>?>( stream: value.control.valueChanges, builder: (context, snapshot) { - final itemList = (value.value() ?? - []) - .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) - .values - .toList(); - - return builder?.call( - context, - itemList, - formModel, - ) ?? + final itemList = + (value.value() ?? + []) + .asMap() + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) + .values + .toList(); + + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/reactive_forms_generator/example/lib/docs/login_extended_nullable/login_extended_nullable.gform.dart b/packages/reactive_forms_generator/example/lib/docs/login_extended_nullable/login_extended_nullable.gform.dart index b3d422bf..b7609e1a 100644 --- a/packages/reactive_forms_generator/example/lib/docs/login_extended_nullable/login_extended_nullable.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/login_extended_nullable/login_extended_nullable.gform.dart @@ -18,8 +18,12 @@ class ReactiveLoginExtendedNullableFormConsumer extends StatelessWidget { final Widget? child; - final Widget Function(BuildContext context, - LoginExtendedNullableForm formModel, Widget? child) builder; + final Widget Function( + BuildContext context, + LoginExtendedNullableForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -39,11 +43,7 @@ class LoginExtendedNullableFormInheritedStreamer required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final LoginExtendedNullableForm form; } @@ -72,12 +72,15 @@ class ReactiveLoginExtendedNullableForm extends StatelessWidget { if (listen) { return context .dependOnInheritedWidgetOfExactType< - LoginExtendedNullableFormInheritedStreamer>() + LoginExtendedNullableFormInheritedStreamer + >() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - LoginExtendedNullableFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + LoginExtendedNullableFormInheritedStreamer + >(); return element == null ? null : (element.widget as LoginExtendedNullableFormInheritedStreamer).form; @@ -124,11 +127,18 @@ class LoginExtendedNullableFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - final Widget Function(BuildContext context, - LoginExtendedNullableForm formModel, Widget? child) builder; + final Widget Function( + BuildContext context, + LoginExtendedNullableForm formModel, + Widget? child, + ) + builder; final void Function( - BuildContext context, LoginExtendedNullableForm formModel)? initState; + BuildContext context, + LoginExtendedNullableForm formModel, + )? + initState; @override _LoginExtendedNullableFormBuilderState createState() => @@ -144,7 +154,10 @@ class _LoginExtendedNullableFormBuilderState @override void initState() { _formModel = LoginExtendedNullableForm( - LoginExtendedNullableForm.formElements(widget.model), null, null); + LoginExtendedNullableForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -152,8 +165,9 @@ class _LoginExtendedNullableFormBuilderState widget.initState?.call(context, _formModel); - _logSubscription = - _logLoginExtendedNullableForm.onRecord.listen((LogRecord e) { + _logSubscription = _logLoginExtendedNullableForm.onRecord.listen(( + LogRecord e, + ) { // use `dumpErrorToConsole` for severe messages to ensure that severe // exceptions are formatted consistently with other Flutter examples and // avoids printing duplicate exceptions @@ -219,16 +233,14 @@ class _LoginExtendedNullableFormBuilderState } } -final _logLoginExtendedNullableForm = - Logger.detached('LoginExtendedNullableForm'); +final _logLoginExtendedNullableForm = Logger.detached( + 'LoginExtendedNullableForm', +); class LoginExtendedNullableForm implements FormModel { - LoginExtendedNullableForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + LoginExtendedNullableForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String emailControlName = "email"; @@ -248,7 +260,7 @@ class LoginExtendedNullableForm final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -270,36 +282,37 @@ class LoginExtendedNullableForm String heightControlPath() => pathBuilder(heightControlName); - String? get _emailValue => emailControl.value; + String? get _emailValue => containsEmail ? emailControl.value : null; - String? get _passwordValue => passwordControl.value; + String? get _passwordValue => containsPassword ? passwordControl.value : null; - bool? get _rememberMeValue => rememberMeControl.value; + bool? get _rememberMeValue => + containsRememberMe ? rememberMeControl.value : null; - String? get _themeValue => themeControl.value; + String? get _themeValue => containsTheme ? themeControl.value : null; - UserMode? get _modeValue => modeControl.value; + UserMode? get _modeValue => containsMode ? modeControl.value : null; - int? get _timeoutValue => timeoutControl.value; + int? get _timeoutValue => containsTimeout ? timeoutControl.value : null; - double? get _heightValue => heightControl.value; + double? get _heightValue => containsHeight ? heightControl.value : null; - String? get _emailRawValue => emailControl.value; + String? get _emailRawValue => containsEmail ? emailControl.value : null; - String? get _passwordRawValue => passwordControl.value; + String? get _passwordRawValue => + containsPassword ? passwordControl.value : null; - bool? get _rememberMeRawValue => rememberMeControl.value; + bool? get _rememberMeRawValue => + containsRememberMe ? rememberMeControl.value : null; - String? get _themeRawValue => themeControl.value; + String? get _themeRawValue => containsTheme ? themeControl.value : null; - UserMode? get _modeRawValue => modeControl.value; + UserMode? get _modeRawValue => containsMode ? modeControl.value : null; - int? get _timeoutRawValue => timeoutControl.value; + int? get _timeoutRawValue => containsTimeout ? timeoutControl.value : null; - double? get _heightRawValue => heightControl.value; + double? get _heightRawValue => containsHeight ? heightControl.value : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsEmail { try { form.control(emailControlPath()); @@ -309,8 +322,6 @@ class LoginExtendedNullableForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsPassword { try { form.control(passwordControlPath()); @@ -320,8 +331,6 @@ class LoginExtendedNullableForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsRememberMe { try { form.control(rememberMeControlPath()); @@ -331,8 +340,6 @@ class LoginExtendedNullableForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsTheme { try { form.control(themeControlPath()); @@ -342,8 +349,6 @@ class LoginExtendedNullableForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsMode { try { form.control(modeControlPath()); @@ -353,8 +358,6 @@ class LoginExtendedNullableForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsTimeout { try { form.control(timeoutControlPath()); @@ -364,8 +367,6 @@ class LoginExtendedNullableForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsHeight { try { form.control(heightControlPath()); @@ -403,12 +404,7 @@ class LoginExtendedNullableForm void get heightFocus => form.focus(heightControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void emailRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void emailRemove({bool updateParent = true, bool emitEvent = true}) { if (containsEmail) { final controlPath = path; if (controlPath == null) { @@ -431,12 +427,7 @@ class LoginExtendedNullableForm } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void passwordRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void passwordRemove({bool updateParent = true, bool emitEvent = true}) { if (containsPassword) { final controlPath = path; if (controlPath == null) { @@ -459,12 +450,7 @@ class LoginExtendedNullableForm } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void rememberMeRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void rememberMeRemove({bool updateParent = true, bool emitEvent = true}) { if (containsRememberMe) { final controlPath = path; if (controlPath == null) { @@ -487,12 +473,7 @@ class LoginExtendedNullableForm } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void themeRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void themeRemove({bool updateParent = true, bool emitEvent = true}) { if (containsTheme) { final controlPath = path; if (controlPath == null) { @@ -515,12 +496,7 @@ class LoginExtendedNullableForm } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void modeRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void modeRemove({bool updateParent = true, bool emitEvent = true}) { if (containsMode) { final controlPath = path; if (controlPath == null) { @@ -543,12 +519,7 @@ class LoginExtendedNullableForm } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void timeoutRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void timeoutRemove({bool updateParent = true, bool emitEvent = true}) { if (containsTimeout) { final controlPath = path; if (controlPath == null) { @@ -571,12 +542,7 @@ class LoginExtendedNullableForm } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void heightRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void heightRemove({bool updateParent = true, bool emitEvent = true}) { if (containsHeight) { final controlPath = path; if (controlPath == null) { @@ -604,8 +570,11 @@ class LoginExtendedNullableForm bool updateParent = true, bool emitEvent = true, }) { - emailControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void passwordValueUpdate( @@ -613,8 +582,11 @@ class LoginExtendedNullableForm bool updateParent = true, bool emitEvent = true, }) { - passwordControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + passwordControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void rememberMeValueUpdate( @@ -622,8 +594,11 @@ class LoginExtendedNullableForm bool updateParent = true, bool emitEvent = true, }) { - rememberMeControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + rememberMeControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void themeValueUpdate( @@ -631,8 +606,11 @@ class LoginExtendedNullableForm bool updateParent = true, bool emitEvent = true, }) { - themeControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + themeControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void modeValueUpdate( @@ -640,8 +618,11 @@ class LoginExtendedNullableForm bool updateParent = true, bool emitEvent = true, }) { - modeControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + modeControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void timeoutValueUpdate( @@ -649,8 +630,11 @@ class LoginExtendedNullableForm bool updateParent = true, bool emitEvent = true, }) { - timeoutControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + timeoutControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void heightValueUpdate( @@ -658,8 +642,11 @@ class LoginExtendedNullableForm bool updateParent = true, bool emitEvent = true, }) { - heightControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + heightControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailValuePatch( @@ -667,8 +654,11 @@ class LoginExtendedNullableForm bool updateParent = true, bool emitEvent = true, }) { - emailControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void passwordValuePatch( @@ -676,8 +666,11 @@ class LoginExtendedNullableForm bool updateParent = true, bool emitEvent = true, }) { - passwordControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + passwordControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void rememberMeValuePatch( @@ -685,8 +678,11 @@ class LoginExtendedNullableForm bool updateParent = true, bool emitEvent = true, }) { - rememberMeControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + rememberMeControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void themeValuePatch( @@ -694,8 +690,11 @@ class LoginExtendedNullableForm bool updateParent = true, bool emitEvent = true, }) { - themeControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + themeControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void modeValuePatch( @@ -703,8 +702,11 @@ class LoginExtendedNullableForm bool updateParent = true, bool emitEvent = true, }) { - modeControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + modeControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void timeoutValuePatch( @@ -712,8 +714,11 @@ class LoginExtendedNullableForm bool updateParent = true, bool emitEvent = true, }) { - timeoutControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + timeoutControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void heightValuePatch( @@ -721,8 +726,11 @@ class LoginExtendedNullableForm bool updateParent = true, bool emitEvent = true, }) { - heightControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + heightControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailValueReset( @@ -731,14 +739,13 @@ class LoginExtendedNullableForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - emailControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => emailControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void passwordValueReset( String? value, { @@ -746,14 +753,13 @@ class LoginExtendedNullableForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - passwordControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => passwordControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void rememberMeValueReset( bool? value, { @@ -761,14 +767,13 @@ class LoginExtendedNullableForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - rememberMeControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => rememberMeControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void themeValueReset( String? value, { @@ -776,14 +781,13 @@ class LoginExtendedNullableForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - themeControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => themeControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void modeValueReset( UserMode? value, { @@ -791,14 +795,13 @@ class LoginExtendedNullableForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - modeControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => modeControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void timeoutValueReset( int? value, { @@ -806,14 +809,13 @@ class LoginExtendedNullableForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - timeoutControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => timeoutControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void heightValueReset( double? value, { @@ -821,14 +823,13 @@ class LoginExtendedNullableForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - heightControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => heightControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get emailControl => form.control(emailControlPath()) as FormControl; @@ -989,39 +990,40 @@ class LoginExtendedNullableForm ); } return LoginExtendedNullable( - email: _emailValue, - password: _passwordValue, - rememberMe: _rememberMeValue, - theme: _themeValue, - mode: _modeValue, - timeout: _timeoutValue, - height: _heightValue); + email: _emailValue, + password: _passwordValue, + rememberMe: _rememberMeValue, + theme: _themeValue, + mode: _modeValue, + timeout: _timeoutValue, + height: _heightValue, + ); } @override LoginExtendedNullable get rawModel { return LoginExtendedNullable( - email: _emailRawValue, - password: _passwordRawValue, - rememberMe: _rememberMeRawValue, - theme: _themeRawValue, - mode: _modeRawValue, - timeout: _timeoutRawValue, - height: _heightRawValue); + email: _emailRawValue, + password: _passwordRawValue, + rememberMe: _rememberMeRawValue, + theme: _themeRawValue, + mode: _modeRawValue, + timeout: _timeoutRawValue, + height: _heightRawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { @@ -1079,11 +1081,11 @@ class LoginExtendedNullableForm LoginExtendedNullable? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue( - LoginExtendedNullableForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.updateValue( + LoginExtendedNullableForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -1101,17 +1103,14 @@ class LoginExtendedNullableForm LoginExtendedNullable? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -1163,65 +1162,76 @@ class LoginExtendedNullableForm [path, pathItem].whereType().join("."); static FormGroup formElements(LoginExtendedNullable? loginExtendedNullable) => - FormGroup({ - emailControlName: FormControl( + FormGroup( + { + emailControlName: FormControl( value: loginExtendedNullable?.email, validators: [], asyncValidators: [], asyncValidatorsDebounceTime: 250, disabled: false, - touched: false), - passwordControlName: FormControl( + touched: false, + ), + passwordControlName: FormControl( value: loginExtendedNullable?.password, validators: [], asyncValidators: [], asyncValidatorsDebounceTime: 250, disabled: false, - touched: false), - rememberMeControlName: FormControl( + touched: false, + ), + rememberMeControlName: FormControl( value: loginExtendedNullable?.rememberMe, validators: [], asyncValidators: [], asyncValidatorsDebounceTime: 250, disabled: false, - touched: false), - themeControlName: FormControl( + touched: false, + ), + themeControlName: FormControl( value: loginExtendedNullable?.theme, validators: [], asyncValidators: [], asyncValidatorsDebounceTime: 250, disabled: false, - touched: false), - modeControlName: FormControl( + touched: false, + ), + modeControlName: FormControl( value: loginExtendedNullable?.mode, validators: [], asyncValidators: [], asyncValidatorsDebounceTime: 250, disabled: false, - touched: false), - timeoutControlName: FormControl( + touched: false, + ), + timeoutControlName: FormControl( value: loginExtendedNullable?.timeout, validators: [], asyncValidators: [], asyncValidatorsDebounceTime: 250, disabled: false, - touched: false), - heightControlName: FormControl( + touched: false, + ), + heightControlName: FormControl( value: loginExtendedNullable?.height, validators: [], asyncValidators: [], asyncValidatorsDebounceTime: 250, disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } class ReactiveLoginExtendedNullableFormArrayBuilder< - ReactiveLoginExtendedNullableFormArrayBuilderT> extends StatelessWidget { + ReactiveLoginExtendedNullableFormArrayBuilderT +> + extends StatelessWidget { const ReactiveLoginExtendedNullableFormArrayBuilder({ Key? key, this.control, @@ -1230,30 +1240,41 @@ class ReactiveLoginExtendedNullableFormArrayBuilder< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - LoginExtendedNullableForm formModel)? control; + LoginExtendedNullableForm formModel, + )? + control; - final Widget Function(BuildContext context, List itemList, - LoginExtendedNullableForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + LoginExtendedNullableForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveLoginExtendedNullableFormArrayBuilderT? item, - LoginExtendedNullableForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveLoginExtendedNullableFormArrayBuilderT? item, + LoginExtendedNullableForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -1267,21 +1288,13 @@ class ReactiveLoginExtendedNullableFormArrayBuilder< final itemBuilder = this.itemBuilder; return ReactiveFormArrayItemBuilder< - ReactiveLoginExtendedNullableFormArrayBuilderT>( + ReactiveLoginExtendedNullableFormArrayBuilderT + >( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1290,7 +1303,9 @@ class ReactiveLoginExtendedNullableFormArrayBuilder< } class ReactiveLoginExtendedNullableFormArrayBuilder2< - ReactiveLoginExtendedNullableFormArrayBuilderT> extends StatelessWidget { + ReactiveLoginExtendedNullableFormArrayBuilderT +> + extends StatelessWidget { const ReactiveLoginExtendedNullableFormArrayBuilder2({ Key? key, this.control, @@ -1299,36 +1314,47 @@ class ReactiveLoginExtendedNullableFormArrayBuilder2< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - LoginExtendedNullableForm formModel)? control; + LoginExtendedNullableForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - LoginExtendedNullableForm formModel - }) params)? builder; + ({ + BuildContext context, + List itemList, + LoginExtendedNullableForm formModel, + }) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveLoginExtendedNullableFormArrayBuilderT? item, - LoginExtendedNullableForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveLoginExtendedNullableFormArrayBuilderT? item, + LoginExtendedNullableForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -1342,27 +1368,22 @@ class ReactiveLoginExtendedNullableFormArrayBuilder2< final itemBuilder = this.itemBuilder; return ReactiveFormArrayItemBuilder< - ReactiveLoginExtendedNullableFormArrayBuilderT>( + ReactiveLoginExtendedNullableFormArrayBuilderT + >( formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1371,7 +1392,8 @@ class ReactiveLoginExtendedNullableFormArrayBuilder2< } class ReactiveLoginExtendedNullableFormFormGroupArrayBuilder< - ReactiveLoginExtendedNullableFormFormGroupArrayBuilderT> + ReactiveLoginExtendedNullableFormFormGroupArrayBuilderT +> extends StatelessWidget { const ReactiveLoginExtendedNullableFormFormGroupArrayBuilder({ Key? key, @@ -1379,25 +1401,39 @@ class ReactiveLoginExtendedNullableFormFormGroupArrayBuilder< this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? extended; - - final ExtendedControl?>, - List> - Function(LoginExtendedNullableForm formModel)? getExtended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(LoginExtendedNullableForm formModel)? + getExtended; - final Widget Function(BuildContext context, List itemList, - LoginExtendedNullableForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + LoginExtendedNullableForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveLoginExtendedNullableFormFormGroupArrayBuilderT? item, - LoginExtendedNullableForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveLoginExtendedNullableFormFormGroupArrayBuilderT? item, + LoginExtendedNullableForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -1412,26 +1448,18 @@ class ReactiveLoginExtendedNullableFormFormGroupArrayBuilder< return StreamBuilder?>?>( stream: value.control.valueChanges, builder: (context, snapshot) { - final itemList = (value.value() ?? - []) - .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) - .values - .toList(); - - return builder?.call( - context, - itemList, - formModel, - ) ?? + final itemList = + (value.value() ?? + []) + .asMap() + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) + .values + .toList(); + + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/reactive_forms_generator/example/lib/docs/login_extended_nullable/login_extended_nullable_output.gform.dart b/packages/reactive_forms_generator/example/lib/docs/login_extended_nullable/login_extended_nullable_output.gform.dart index 9eeea3a1..f65bedf4 100644 --- a/packages/reactive_forms_generator/example/lib/docs/login_extended_nullable/login_extended_nullable_output.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/login_extended_nullable/login_extended_nullable_output.gform.dart @@ -18,8 +18,12 @@ class ReactiveLoginExtendedNullableOFormConsumer extends StatelessWidget { final Widget? child; - final Widget Function(BuildContext context, - LoginExtendedNullableOForm formModel, Widget? child) builder; + final Widget Function( + BuildContext context, + LoginExtendedNullableOForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -39,11 +43,7 @@ class LoginExtendedNullableOFormInheritedStreamer required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final LoginExtendedNullableOForm form; } @@ -72,12 +72,15 @@ class ReactiveLoginExtendedNullableOForm extends StatelessWidget { if (listen) { return context .dependOnInheritedWidgetOfExactType< - LoginExtendedNullableOFormInheritedStreamer>() + LoginExtendedNullableOFormInheritedStreamer + >() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - LoginExtendedNullableOFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + LoginExtendedNullableOFormInheritedStreamer + >(); return element == null ? null : (element.widget as LoginExtendedNullableOFormInheritedStreamer).form; @@ -124,11 +127,18 @@ class LoginExtendedNullableOFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - final Widget Function(BuildContext context, - LoginExtendedNullableOForm formModel, Widget? child) builder; + final Widget Function( + BuildContext context, + LoginExtendedNullableOForm formModel, + Widget? child, + ) + builder; final void Function( - BuildContext context, LoginExtendedNullableOForm formModel)? initState; + BuildContext context, + LoginExtendedNullableOForm formModel, + )? + initState; @override _LoginExtendedNullableOFormBuilderState createState() => @@ -144,7 +154,10 @@ class _LoginExtendedNullableOFormBuilderState @override void initState() { _formModel = LoginExtendedNullableOForm( - LoginExtendedNullableOForm.formElements(widget.model), null, null); + LoginExtendedNullableOForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -152,8 +165,9 @@ class _LoginExtendedNullableOFormBuilderState widget.initState?.call(context, _formModel); - _logSubscription = - _logLoginExtendedNullableOForm.onRecord.listen((LogRecord e) { + _logSubscription = _logLoginExtendedNullableOForm.onRecord.listen(( + LogRecord e, + ) { // use `dumpErrorToConsole` for severe messages to ensure that severe // exceptions are formatted consistently with other Flutter examples and // avoids printing duplicate exceptions @@ -219,16 +233,14 @@ class _LoginExtendedNullableOFormBuilderState } } -final _logLoginExtendedNullableOForm = - Logger.detached('LoginExtendedNullableOForm'); +final _logLoginExtendedNullableOForm = Logger.detached( + 'LoginExtendedNullableOForm', +); class LoginExtendedNullableOForm implements FormModel { - LoginExtendedNullableOForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + LoginExtendedNullableOForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String emailControlName = "email"; @@ -248,7 +260,7 @@ class LoginExtendedNullableOForm final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -270,36 +282,37 @@ class LoginExtendedNullableOForm String heightControlPath() => pathBuilder(heightControlName); - String? get _emailValue => emailControl.value; + String? get _emailValue => containsEmail ? emailControl.value : null; - String? get _passwordValue => passwordControl.value; + String? get _passwordValue => containsPassword ? passwordControl.value : null; - bool? get _rememberMeValue => rememberMeControl.value; + bool? get _rememberMeValue => + containsRememberMe ? rememberMeControl.value : null; - String? get _themeValue => themeControl.value; + String? get _themeValue => containsTheme ? themeControl.value : null; - UserMode? get _modeValue => modeControl.value; + UserMode? get _modeValue => containsMode ? modeControl.value : null; - int? get _timeoutValue => timeoutControl.value; + int? get _timeoutValue => containsTimeout ? timeoutControl.value : null; - double? get _heightValue => heightControl.value; + double? get _heightValue => containsHeight ? heightControl.value : null; - String? get _emailRawValue => emailControl.value; + String? get _emailRawValue => containsEmail ? emailControl.value : null; - String? get _passwordRawValue => passwordControl.value; + String? get _passwordRawValue => + containsPassword ? passwordControl.value : null; - bool? get _rememberMeRawValue => rememberMeControl.value; + bool? get _rememberMeRawValue => + containsRememberMe ? rememberMeControl.value : null; - String? get _themeRawValue => themeControl.value; + String? get _themeRawValue => containsTheme ? themeControl.value : null; - UserMode? get _modeRawValue => modeControl.value; + UserMode? get _modeRawValue => containsMode ? modeControl.value : null; - int? get _timeoutRawValue => timeoutControl.value; + int? get _timeoutRawValue => containsTimeout ? timeoutControl.value : null; - double? get _heightRawValue => heightControl.value; + double? get _heightRawValue => containsHeight ? heightControl.value : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsEmail { try { form.control(emailControlPath()); @@ -309,8 +322,6 @@ class LoginExtendedNullableOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsPassword { try { form.control(passwordControlPath()); @@ -320,8 +331,6 @@ class LoginExtendedNullableOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsRememberMe { try { form.control(rememberMeControlPath()); @@ -331,8 +340,6 @@ class LoginExtendedNullableOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsTheme { try { form.control(themeControlPath()); @@ -342,8 +349,6 @@ class LoginExtendedNullableOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsMode { try { form.control(modeControlPath()); @@ -353,8 +358,6 @@ class LoginExtendedNullableOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsTimeout { try { form.control(timeoutControlPath()); @@ -364,8 +367,6 @@ class LoginExtendedNullableOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsHeight { try { form.control(heightControlPath()); @@ -403,12 +404,7 @@ class LoginExtendedNullableOForm void get heightFocus => form.focus(heightControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void emailRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void emailRemove({bool updateParent = true, bool emitEvent = true}) { if (containsEmail) { final controlPath = path; if (controlPath == null) { @@ -431,12 +427,7 @@ class LoginExtendedNullableOForm } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void passwordRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void passwordRemove({bool updateParent = true, bool emitEvent = true}) { if (containsPassword) { final controlPath = path; if (controlPath == null) { @@ -459,12 +450,7 @@ class LoginExtendedNullableOForm } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void rememberMeRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void rememberMeRemove({bool updateParent = true, bool emitEvent = true}) { if (containsRememberMe) { final controlPath = path; if (controlPath == null) { @@ -487,12 +473,7 @@ class LoginExtendedNullableOForm } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void themeRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void themeRemove({bool updateParent = true, bool emitEvent = true}) { if (containsTheme) { final controlPath = path; if (controlPath == null) { @@ -515,12 +496,7 @@ class LoginExtendedNullableOForm } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void modeRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void modeRemove({bool updateParent = true, bool emitEvent = true}) { if (containsMode) { final controlPath = path; if (controlPath == null) { @@ -543,12 +519,7 @@ class LoginExtendedNullableOForm } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void timeoutRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void timeoutRemove({bool updateParent = true, bool emitEvent = true}) { if (containsTimeout) { final controlPath = path; if (controlPath == null) { @@ -571,12 +542,7 @@ class LoginExtendedNullableOForm } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void heightRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void heightRemove({bool updateParent = true, bool emitEvent = true}) { if (containsHeight) { final controlPath = path; if (controlPath == null) { @@ -604,8 +570,11 @@ class LoginExtendedNullableOForm bool updateParent = true, bool emitEvent = true, }) { - emailControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void passwordValueUpdate( @@ -613,8 +582,11 @@ class LoginExtendedNullableOForm bool updateParent = true, bool emitEvent = true, }) { - passwordControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + passwordControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void rememberMeValueUpdate( @@ -622,8 +594,11 @@ class LoginExtendedNullableOForm bool updateParent = true, bool emitEvent = true, }) { - rememberMeControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + rememberMeControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void themeValueUpdate( @@ -631,8 +606,11 @@ class LoginExtendedNullableOForm bool updateParent = true, bool emitEvent = true, }) { - themeControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + themeControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void modeValueUpdate( @@ -640,8 +618,11 @@ class LoginExtendedNullableOForm bool updateParent = true, bool emitEvent = true, }) { - modeControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + modeControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void timeoutValueUpdate( @@ -649,8 +630,11 @@ class LoginExtendedNullableOForm bool updateParent = true, bool emitEvent = true, }) { - timeoutControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + timeoutControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void heightValueUpdate( @@ -658,8 +642,11 @@ class LoginExtendedNullableOForm bool updateParent = true, bool emitEvent = true, }) { - heightControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + heightControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailValuePatch( @@ -667,8 +654,11 @@ class LoginExtendedNullableOForm bool updateParent = true, bool emitEvent = true, }) { - emailControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void passwordValuePatch( @@ -676,8 +666,11 @@ class LoginExtendedNullableOForm bool updateParent = true, bool emitEvent = true, }) { - passwordControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + passwordControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void rememberMeValuePatch( @@ -685,8 +678,11 @@ class LoginExtendedNullableOForm bool updateParent = true, bool emitEvent = true, }) { - rememberMeControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + rememberMeControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void themeValuePatch( @@ -694,8 +690,11 @@ class LoginExtendedNullableOForm bool updateParent = true, bool emitEvent = true, }) { - themeControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + themeControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void modeValuePatch( @@ -703,8 +702,11 @@ class LoginExtendedNullableOForm bool updateParent = true, bool emitEvent = true, }) { - modeControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + modeControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void timeoutValuePatch( @@ -712,8 +714,11 @@ class LoginExtendedNullableOForm bool updateParent = true, bool emitEvent = true, }) { - timeoutControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + timeoutControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void heightValuePatch( @@ -721,8 +726,11 @@ class LoginExtendedNullableOForm bool updateParent = true, bool emitEvent = true, }) { - heightControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + heightControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailValueReset( @@ -731,14 +739,13 @@ class LoginExtendedNullableOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - emailControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => emailControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void passwordValueReset( String? value, { @@ -746,14 +753,13 @@ class LoginExtendedNullableOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - passwordControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => passwordControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void rememberMeValueReset( bool? value, { @@ -761,14 +767,13 @@ class LoginExtendedNullableOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - rememberMeControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => rememberMeControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void themeValueReset( String? value, { @@ -776,14 +781,13 @@ class LoginExtendedNullableOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - themeControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => themeControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void modeValueReset( UserMode? value, { @@ -791,14 +795,13 @@ class LoginExtendedNullableOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - modeControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => modeControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void timeoutValueReset( int? value, { @@ -806,14 +809,13 @@ class LoginExtendedNullableOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - timeoutControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => timeoutControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void heightValueReset( double? value, { @@ -821,14 +823,13 @@ class LoginExtendedNullableOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - heightControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => heightControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get emailControl => form.control(emailControlPath()) as FormControl; @@ -990,39 +991,40 @@ class LoginExtendedNullableOForm ); } return LoginExtendedNullableOOutput( - email: _emailValue, - password: _passwordValue, - rememberMe: _rememberMeValue, - theme: _themeValue, - mode: _modeValue, - timeout: _timeoutValue, - height: _heightValue); + email: _emailValue, + password: _passwordValue, + rememberMe: _rememberMeValue, + theme: _themeValue, + mode: _modeValue, + timeout: _timeoutValue, + height: _heightValue, + ); } @override LoginExtendedNullableO get rawModel { return LoginExtendedNullableO( - email: _emailRawValue, - password: _passwordRawValue, - rememberMe: _rememberMeRawValue, - theme: _themeRawValue, - mode: _modeRawValue, - timeout: _timeoutRawValue, - height: _heightRawValue); + email: _emailRawValue, + password: _passwordRawValue, + rememberMe: _rememberMeRawValue, + theme: _themeRawValue, + mode: _modeRawValue, + timeout: _timeoutRawValue, + height: _heightRawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { @@ -1080,11 +1082,11 @@ class LoginExtendedNullableOForm LoginExtendedNullableO? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue( - LoginExtendedNullableOForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.updateValue( + LoginExtendedNullableOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -1102,17 +1104,14 @@ class LoginExtendedNullableOForm LoginExtendedNullableO? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -1164,62 +1163,71 @@ class LoginExtendedNullableOForm [path, pathItem].whereType().join("."); static FormGroup formElements( - LoginExtendedNullableO? loginExtendedNullableO) => - FormGroup({ - emailControlName: FormControl( - value: loginExtendedNullableO?.email, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - passwordControlName: FormControl( - value: loginExtendedNullableO?.password, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - rememberMeControlName: FormControl( - value: loginExtendedNullableO?.rememberMe, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - themeControlName: FormControl( - value: loginExtendedNullableO?.theme, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - modeControlName: FormControl( - value: loginExtendedNullableO?.mode, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - timeoutControlName: FormControl( - value: loginExtendedNullableO?.timeout, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - heightControlName: FormControl( - value: loginExtendedNullableO?.height, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + LoginExtendedNullableO? loginExtendedNullableO, + ) => FormGroup( + { + emailControlName: FormControl( + value: loginExtendedNullableO?.email, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + passwordControlName: FormControl( + value: loginExtendedNullableO?.password, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + rememberMeControlName: FormControl( + value: loginExtendedNullableO?.rememberMe, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + themeControlName: FormControl( + value: loginExtendedNullableO?.theme, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + modeControlName: FormControl( + value: loginExtendedNullableO?.mode, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + timeoutControlName: FormControl( + value: loginExtendedNullableO?.timeout, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + heightControlName: FormControl( + value: loginExtendedNullableO?.height, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } @Rf(output: true) @@ -1231,18 +1239,21 @@ class LoginExtendedNullableOOutput { final UserMode? mode; final int? timeout; final double? height; - LoginExtendedNullableOOutput( - {@RfControl(validators: []) this.email, - @RfControl() this.password, - @RfControl() this.rememberMe, - @RfControl() this.theme, - @RfControl() this.mode, - @RfControl() this.timeout, - @RfControl() this.height}); + LoginExtendedNullableOOutput({ + @RfControl(validators: []) this.email, + @RfControl() this.password, + @RfControl() this.rememberMe, + @RfControl() this.theme, + @RfControl() this.mode, + @RfControl() this.timeout, + @RfControl() this.height, + }); } class ReactiveLoginExtendedNullableOFormArrayBuilder< - ReactiveLoginExtendedNullableOFormArrayBuilderT> extends StatelessWidget { + ReactiveLoginExtendedNullableOFormArrayBuilderT +> + extends StatelessWidget { const ReactiveLoginExtendedNullableOFormArrayBuilder({ Key? key, this.control, @@ -1251,30 +1262,41 @@ class ReactiveLoginExtendedNullableOFormArrayBuilder< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - LoginExtendedNullableOForm formModel)? control; + LoginExtendedNullableOForm formModel, + )? + control; - final Widget Function(BuildContext context, List itemList, - LoginExtendedNullableOForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + LoginExtendedNullableOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveLoginExtendedNullableOFormArrayBuilderT? item, - LoginExtendedNullableOForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveLoginExtendedNullableOFormArrayBuilderT? item, + LoginExtendedNullableOForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -1288,21 +1310,13 @@ class ReactiveLoginExtendedNullableOFormArrayBuilder< final itemBuilder = this.itemBuilder; return ReactiveFormArrayItemBuilder< - ReactiveLoginExtendedNullableOFormArrayBuilderT>( + ReactiveLoginExtendedNullableOFormArrayBuilderT + >( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1311,7 +1325,9 @@ class ReactiveLoginExtendedNullableOFormArrayBuilder< } class ReactiveLoginExtendedNullableOFormArrayBuilder2< - ReactiveLoginExtendedNullableOFormArrayBuilderT> extends StatelessWidget { + ReactiveLoginExtendedNullableOFormArrayBuilderT +> + extends StatelessWidget { const ReactiveLoginExtendedNullableOFormArrayBuilder2({ Key? key, this.control, @@ -1320,36 +1336,47 @@ class ReactiveLoginExtendedNullableOFormArrayBuilder2< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - LoginExtendedNullableOForm formModel)? control; + LoginExtendedNullableOForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - LoginExtendedNullableOForm formModel - }) params)? builder; + ({ + BuildContext context, + List itemList, + LoginExtendedNullableOForm formModel, + }) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveLoginExtendedNullableOFormArrayBuilderT? item, - LoginExtendedNullableOForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveLoginExtendedNullableOFormArrayBuilderT? item, + LoginExtendedNullableOForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -1363,27 +1390,22 @@ class ReactiveLoginExtendedNullableOFormArrayBuilder2< final itemBuilder = this.itemBuilder; return ReactiveFormArrayItemBuilder< - ReactiveLoginExtendedNullableOFormArrayBuilderT>( + ReactiveLoginExtendedNullableOFormArrayBuilderT + >( formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1392,7 +1414,8 @@ class ReactiveLoginExtendedNullableOFormArrayBuilder2< } class ReactiveLoginExtendedNullableOFormFormGroupArrayBuilder< - ReactiveLoginExtendedNullableOFormFormGroupArrayBuilderT> + ReactiveLoginExtendedNullableOFormFormGroupArrayBuilderT +> extends StatelessWidget { const ReactiveLoginExtendedNullableOFormFormGroupArrayBuilder({ Key? key, @@ -1400,25 +1423,39 @@ class ReactiveLoginExtendedNullableOFormFormGroupArrayBuilder< this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(LoginExtendedNullableOForm formModel)? + getExtended; - final ExtendedControl?>, - List>? extended; - - final ExtendedControl?>, - List> - Function(LoginExtendedNullableOForm formModel)? getExtended; - - final Widget Function(BuildContext context, List itemList, - LoginExtendedNullableOForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + LoginExtendedNullableOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveLoginExtendedNullableOFormFormGroupArrayBuilderT? item, - LoginExtendedNullableOForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveLoginExtendedNullableOFormFormGroupArrayBuilderT? item, + LoginExtendedNullableOForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -1433,26 +1470,20 @@ class ReactiveLoginExtendedNullableOFormFormGroupArrayBuilder< return StreamBuilder?>?>( stream: value.control.valueChanges, builder: (context, snapshot) { - final itemList = (value.value() ?? - []) - .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) - .values - .toList(); - - return builder?.call( - context, - itemList, - formModel, - ) ?? + final itemList = + (value.value() ?? + < + ReactiveLoginExtendedNullableOFormFormGroupArrayBuilderT + >[]) + .asMap() + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) + .values + .toList(); + + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/reactive_forms_generator/example/lib/docs/mailing_list/mailing_list.gform.dart b/packages/reactive_forms_generator/example/lib/docs/mailing_list/mailing_list.gform.dart index dad1c912..ad61f703 100644 --- a/packages/reactive_forms_generator/example/lib/docs/mailing_list/mailing_list.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/mailing_list/mailing_list.gform.dart @@ -19,7 +19,11 @@ class ReactiveMailingListFormConsumer extends StatelessWidget { final Widget? child; final Widget Function( - BuildContext context, MailingListForm formModel, Widget? child) builder; + BuildContext context, + MailingListForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -38,11 +42,7 @@ class MailingListFormInheritedStreamer extends InheritedStreamer { required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final MailingListForm form; } @@ -64,19 +64,19 @@ class ReactiveMailingListForm extends StatelessWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - static MailingListForm? of( - BuildContext context, { - bool listen = true, - }) { + static MailingListForm? of(BuildContext context, {bool listen = true}) { if (listen) { return context .dependOnInheritedWidgetOfExactType< - MailingListFormInheritedStreamer>() + MailingListFormInheritedStreamer + >() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - MailingListFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + MailingListFormInheritedStreamer + >(); return element == null ? null : (element.widget as MailingListFormInheritedStreamer).form; @@ -123,10 +123,14 @@ class MailingListFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, MailingListForm formModel, Widget? child) builder; + BuildContext context, + MailingListForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, MailingListForm formModel)? - initState; + initState; @override _MailingListFormBuilderState createState() => _MailingListFormBuilderState(); @@ -139,8 +143,11 @@ class _MailingListFormBuilderState extends State { @override void initState() { - _formModel = - MailingListForm(MailingListForm.formElements(widget.model), null, null); + _formModel = MailingListForm( + MailingListForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -217,11 +224,8 @@ class _MailingListFormBuilderState extends State { final _logMailingListForm = Logger.detached('MailingListForm'); class MailingListForm implements FormModel { - MailingListForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + MailingListForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String emailListControlName = "emailList"; @@ -229,7 +233,7 @@ class MailingListForm implements FormModel { final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -245,8 +249,6 @@ class MailingListForm implements FormModel { List get _emailListRawValue => emailListControl.rawValue.whereType().toList(); - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsEmailList { try { form.control(emailListControlPath()); @@ -265,8 +267,11 @@ class MailingListForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - emailListControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailListControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailListValuePatch( @@ -274,8 +279,11 @@ class MailingListForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - emailListControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailListControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailListValueReset( @@ -284,14 +292,13 @@ class MailingListForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - emailListControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => emailListControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormArray get emailListControl => form.control(emailListControlPath()) as FormArray; @@ -348,13 +355,15 @@ class MailingListForm implements FormModel { break; } - emailListControl.add(FormControl( - value: value, - validators: resultingValidators, - asyncValidators: resultingAsyncValidators, - asyncValidatorsDebounceTime: asyncValidatorsDebounceTime ?? 250, - disabled: disabled ?? false, - )); + emailListControl.add( + FormControl( + value: value, + validators: resultingValidators, + asyncValidators: resultingAsyncValidators, + asyncValidatorsDebounceTime: asyncValidatorsDebounceTime ?? 250, + disabled: disabled ?? false, + ), + ); } @override @@ -377,17 +386,16 @@ class MailingListForm implements FormModel { } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { @@ -445,9 +453,11 @@ class MailingListForm implements FormModel { MailingList? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(MailingListForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + MailingListForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -465,17 +475,14 @@ class MailingListForm implements FormModel { MailingList? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -526,26 +533,31 @@ class MailingListForm implements FormModel { String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(MailingList? mailingList) => FormGroup({ - emailListControlName: FormArray( - (mailingList?.emailList ?? []) - .map((e) => FormControl( - value: e, - validators: [EmailValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - )) - .toList(), - validators: [MailingListValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(MailingList? mailingList) => FormGroup( + { + emailListControlName: FormArray( + (mailingList?.emailList ?? []) + .map( + (e) => FormControl( + value: e, + validators: [EmailValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ), + ) + .toList(), + validators: [MailingListValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } class ReactiveMailingListFormArrayBuilder @@ -558,29 +570,41 @@ class ReactiveMailingListFormArrayBuilder required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - MailingListForm formModel)? control; + MailingListForm formModel, + )? + control; - final Widget Function(BuildContext context, List itemList, - MailingListForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + MailingListForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveMailingListFormArrayBuilderT? item, - MailingListForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveMailingListFormArrayBuilderT? item, + MailingListForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -596,18 +620,9 @@ class ReactiveMailingListFormArrayBuilder return ReactiveFormArrayItemBuilder( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -625,35 +640,43 @@ class ReactiveMailingListFormArrayBuilder2 required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - MailingListForm formModel)? control; + MailingListForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - MailingListForm formModel - }) params)? builder; + ({BuildContext context, List itemList, MailingListForm formModel}) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveMailingListFormArrayBuilderT? item, - MailingListForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveMailingListFormArrayBuilderT? item, + MailingListForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -670,23 +693,17 @@ class ReactiveMailingListFormArrayBuilder2 formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -695,32 +712,48 @@ class ReactiveMailingListFormArrayBuilder2 } class ReactiveMailingListFormFormGroupArrayBuilder< - ReactiveMailingListFormFormGroupArrayBuilderT> extends StatelessWidget { + ReactiveMailingListFormFormGroupArrayBuilderT +> + extends StatelessWidget { const ReactiveMailingListFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? extended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(MailingListForm formModel)? + getExtended; - final ExtendedControl?>, - List> - Function(MailingListForm formModel)? getExtended; - - final Widget Function(BuildContext context, List itemList, - MailingListForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + MailingListForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveMailingListFormFormGroupArrayBuilderT? item, - MailingListForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveMailingListFormFormGroupArrayBuilderT? item, + MailingListForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -738,23 +771,14 @@ class ReactiveMailingListFormFormGroupArrayBuilder< final itemList = (value.value() ?? []) .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) .values .toList(); - return builder?.call( - context, - itemList, - formModel, - ) ?? + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/reactive_forms_generator/example/lib/docs/mailing_list/mailing_list_output.gform.dart b/packages/reactive_forms_generator/example/lib/docs/mailing_list/mailing_list_output.gform.dart index 6ec47a3f..31ba0b4e 100644 --- a/packages/reactive_forms_generator/example/lib/docs/mailing_list/mailing_list_output.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/mailing_list/mailing_list_output.gform.dart @@ -19,7 +19,11 @@ class ReactiveMailingListOFormConsumer extends StatelessWidget { final Widget? child; final Widget Function( - BuildContext context, MailingListOForm formModel, Widget? child) builder; + BuildContext context, + MailingListOForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -38,11 +42,7 @@ class MailingListOFormInheritedStreamer extends InheritedStreamer { required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final MailingListOForm form; } @@ -64,19 +64,19 @@ class ReactiveMailingListOForm extends StatelessWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - static MailingListOForm? of( - BuildContext context, { - bool listen = true, - }) { + static MailingListOForm? of(BuildContext context, {bool listen = true}) { if (listen) { return context .dependOnInheritedWidgetOfExactType< - MailingListOFormInheritedStreamer>() + MailingListOFormInheritedStreamer + >() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - MailingListOFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + MailingListOFormInheritedStreamer + >(); return element == null ? null : (element.widget as MailingListOFormInheritedStreamer).form; @@ -124,10 +124,14 @@ class MailingListOFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, MailingListOForm formModel, Widget? child) builder; + BuildContext context, + MailingListOForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, MailingListOForm formModel)? - initState; + initState; @override _MailingListOFormBuilderState createState() => @@ -142,7 +146,10 @@ class _MailingListOFormBuilderState extends State { @override void initState() { _formModel = MailingListOForm( - MailingListOForm.formElements(widget.model), null, null); + MailingListOForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -219,11 +226,8 @@ class _MailingListOFormBuilderState extends State { final _logMailingListOForm = Logger.detached('MailingListOForm'); class MailingListOForm implements FormModel { - MailingListOForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + MailingListOForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String emailListControlName = "emailList"; @@ -231,7 +235,7 @@ class MailingListOForm implements FormModel { final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -247,8 +251,6 @@ class MailingListOForm implements FormModel { List get _emailListRawValue => emailListControl.rawValue.whereType().toList(); - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsEmailList { try { form.control(emailListControlPath()); @@ -267,8 +269,11 @@ class MailingListOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - emailListControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailListControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailListValuePatch( @@ -276,8 +281,11 @@ class MailingListOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - emailListControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailListControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailListValueReset( @@ -286,14 +294,13 @@ class MailingListOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - emailListControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => emailListControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormArray get emailListControl => form.control(emailListControlPath()) as FormArray; @@ -350,13 +357,15 @@ class MailingListOForm implements FormModel { break; } - emailListControl.add(FormControl( - value: value, - validators: resultingValidators, - asyncValidators: resultingAsyncValidators, - asyncValidatorsDebounceTime: asyncValidatorsDebounceTime ?? 250, - disabled: disabled ?? false, - )); + emailListControl.add( + FormControl( + value: value, + validators: resultingValidators, + asyncValidators: resultingAsyncValidators, + asyncValidatorsDebounceTime: asyncValidatorsDebounceTime ?? 250, + disabled: disabled ?? false, + ), + ); } @override @@ -380,17 +389,16 @@ class MailingListOForm implements FormModel { } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { @@ -448,9 +456,11 @@ class MailingListOForm implements FormModel { MailingListO? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(MailingListOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + MailingListOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -468,17 +478,14 @@ class MailingListOForm implements FormModel { MailingListO? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -529,40 +536,49 @@ class MailingListOForm implements FormModel { String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(MailingListO? mailingListO) => FormGroup({ - emailListControlName: FormArray( - (mailingListO?.emailList ?? []) - .map((e) => FormControl( - value: e, - validators: [EmailValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - )) - .toList(), - validators: [MailingListValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(MailingListO? mailingListO) => FormGroup( + { + emailListControlName: FormArray( + (mailingListO?.emailList ?? []) + .map( + (e) => FormControl( + value: e, + validators: [EmailValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ), + ) + .toList(), + validators: [MailingListValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } @Rf(output: true) class MailingListOOutput { final List emailList; - MailingListOOutput( - {@RfArray( - validators: [MailingListValidator()], - itemValidators: [EmailValidator()]) - this.emailList = const []}); + MailingListOOutput({ + @RfArray( + validators: [MailingListValidator()], + itemValidators: [EmailValidator()], + ) + this.emailList = const [], + }); } class ReactiveMailingListOFormArrayBuilder< - ReactiveMailingListOFormArrayBuilderT> extends StatelessWidget { + ReactiveMailingListOFormArrayBuilderT +> + extends StatelessWidget { const ReactiveMailingListOFormArrayBuilder({ Key? key, this.control, @@ -571,30 +587,41 @@ class ReactiveMailingListOFormArrayBuilder< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - MailingListOForm formModel)? control; + MailingListOForm formModel, + )? + control; - final Widget Function(BuildContext context, List itemList, - MailingListOForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + MailingListOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveMailingListOFormArrayBuilderT? item, - MailingListOForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveMailingListOFormArrayBuilderT? item, + MailingListOForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -610,18 +637,9 @@ class ReactiveMailingListOFormArrayBuilder< return ReactiveFormArrayItemBuilder( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -630,7 +648,9 @@ class ReactiveMailingListOFormArrayBuilder< } class ReactiveMailingListOFormArrayBuilder2< - ReactiveMailingListOFormArrayBuilderT> extends StatelessWidget { + ReactiveMailingListOFormArrayBuilderT +> + extends StatelessWidget { const ReactiveMailingListOFormArrayBuilder2({ Key? key, this.control, @@ -639,36 +659,43 @@ class ReactiveMailingListOFormArrayBuilder2< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - MailingListOForm formModel)? control; + MailingListOForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - MailingListOForm formModel - }) params)? builder; + ({BuildContext context, List itemList, MailingListOForm formModel}) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveMailingListOFormArrayBuilderT? item, - MailingListOForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveMailingListOFormArrayBuilderT? item, + MailingListOForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -685,23 +712,17 @@ class ReactiveMailingListOFormArrayBuilder2< formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -710,32 +731,48 @@ class ReactiveMailingListOFormArrayBuilder2< } class ReactiveMailingListOFormFormGroupArrayBuilder< - ReactiveMailingListOFormFormGroupArrayBuilderT> extends StatelessWidget { + ReactiveMailingListOFormFormGroupArrayBuilderT +> + extends StatelessWidget { const ReactiveMailingListOFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? extended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(MailingListOForm formModel)? + getExtended; - final ExtendedControl?>, - List> - Function(MailingListOForm formModel)? getExtended; - - final Widget Function(BuildContext context, List itemList, - MailingListOForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + MailingListOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveMailingListOFormFormGroupArrayBuilderT? item, - MailingListOForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveMailingListOFormFormGroupArrayBuilderT? item, + MailingListOForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -750,26 +787,18 @@ class ReactiveMailingListOFormFormGroupArrayBuilder< return StreamBuilder?>?>( stream: value.control.valueChanges, builder: (context, snapshot) { - final itemList = (value.value() ?? - []) - .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) - .values - .toList(); - - return builder?.call( - context, - itemList, - formModel, - ) ?? + final itemList = + (value.value() ?? + []) + .asMap() + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) + .values + .toList(); + + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/reactive_forms_generator/example/lib/docs/model_extends/model_extends.gform.dart b/packages/reactive_forms_generator/example/lib/docs/model_extends/model_extends.gform.dart index 0789c685..31a712f7 100644 --- a/packages/reactive_forms_generator/example/lib/docs/model_extends/model_extends.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/model_extends/model_extends.gform.dart @@ -19,7 +19,11 @@ class ReactiveModelExtendsFormConsumer extends StatelessWidget { final Widget? child; final Widget Function( - BuildContext context, ModelExtendsForm formModel, Widget? child) builder; + BuildContext context, + ModelExtendsForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -38,11 +42,7 @@ class ModelExtendsFormInheritedStreamer extends InheritedStreamer { required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final ModelExtendsForm form; } @@ -64,19 +64,19 @@ class ReactiveModelExtendsForm extends StatelessWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - static ModelExtendsForm? of( - BuildContext context, { - bool listen = true, - }) { + static ModelExtendsForm? of(BuildContext context, {bool listen = true}) { if (listen) { return context .dependOnInheritedWidgetOfExactType< - ModelExtendsFormInheritedStreamer>() + ModelExtendsFormInheritedStreamer + >() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - ModelExtendsFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + ModelExtendsFormInheritedStreamer + >(); return element == null ? null : (element.widget as ModelExtendsFormInheritedStreamer).form; @@ -124,10 +124,14 @@ class ModelExtendsFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, ModelExtendsForm formModel, Widget? child) builder; + BuildContext context, + ModelExtendsForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, ModelExtendsForm formModel)? - initState; + initState; @override _ModelExtendsFormBuilderState createState() => @@ -142,7 +146,10 @@ class _ModelExtendsFormBuilderState extends State { @override void initState() { _formModel = ModelExtendsForm( - ModelExtendsForm.formElements(widget.model), null, null); + ModelExtendsForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -219,11 +226,8 @@ class _ModelExtendsFormBuilderState extends State { final _logModelExtendsForm = Logger.detached('ModelExtendsForm'); class ModelExtendsForm implements FormModel { - ModelExtendsForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + ModelExtendsForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String emailControlName = "email"; @@ -233,7 +237,7 @@ class ModelExtendsForm implements FormModel { final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -253,8 +257,6 @@ class ModelExtendsForm implements FormModel { String get _passwordRawValue => passwordControl.value ?? ""; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsEmail { try { form.control(emailControlPath()); @@ -264,8 +266,6 @@ class ModelExtendsForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsPassword { try { form.control(passwordControlPath()); @@ -288,8 +288,11 @@ class ModelExtendsForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - emailControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void passwordValueUpdate( @@ -297,8 +300,11 @@ class ModelExtendsForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - passwordControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + passwordControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailValuePatch( @@ -306,8 +312,11 @@ class ModelExtendsForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - emailControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void passwordValuePatch( @@ -315,8 +324,11 @@ class ModelExtendsForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - passwordControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + passwordControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailValueReset( @@ -325,14 +337,13 @@ class ModelExtendsForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - emailControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => emailControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void passwordValueReset( String value, { @@ -340,14 +351,13 @@ class ModelExtendsForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - passwordControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => passwordControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get emailControl => form.control(emailControlPath()) as FormControl; @@ -411,17 +421,16 @@ class ModelExtendsForm implements FormModel { } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { @@ -479,9 +488,11 @@ class ModelExtendsForm implements FormModel { ModelExtends? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(ModelExtendsForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + ModelExtendsForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -499,17 +510,14 @@ class ModelExtendsForm implements FormModel { ModelExtends? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -560,30 +568,36 @@ class ModelExtendsForm implements FormModel { String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(ModelExtends? modelExtends) => FormGroup({ - emailControlName: FormControl( - value: modelExtends?.email, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - passwordControlName: FormControl( - value: modelExtends?.password, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(ModelExtends? modelExtends) => FormGroup( + { + emailControlName: FormControl( + value: modelExtends?.email, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + passwordControlName: FormControl( + value: modelExtends?.password, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } class ReactiveModelExtendsFormArrayBuilder< - ReactiveModelExtendsFormArrayBuilderT> extends StatelessWidget { + ReactiveModelExtendsFormArrayBuilderT +> + extends StatelessWidget { const ReactiveModelExtendsFormArrayBuilder({ Key? key, this.control, @@ -592,30 +606,41 @@ class ReactiveModelExtendsFormArrayBuilder< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - ModelExtendsForm formModel)? control; + ModelExtendsForm formModel, + )? + control; - final Widget Function(BuildContext context, List itemList, - ModelExtendsForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + ModelExtendsForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveModelExtendsFormArrayBuilderT? item, - ModelExtendsForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveModelExtendsFormArrayBuilderT? item, + ModelExtendsForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -631,18 +656,9 @@ class ReactiveModelExtendsFormArrayBuilder< return ReactiveFormArrayItemBuilder( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -651,7 +667,9 @@ class ReactiveModelExtendsFormArrayBuilder< } class ReactiveModelExtendsFormArrayBuilder2< - ReactiveModelExtendsFormArrayBuilderT> extends StatelessWidget { + ReactiveModelExtendsFormArrayBuilderT +> + extends StatelessWidget { const ReactiveModelExtendsFormArrayBuilder2({ Key? key, this.control, @@ -660,36 +678,43 @@ class ReactiveModelExtendsFormArrayBuilder2< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - ModelExtendsForm formModel)? control; + ModelExtendsForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - ModelExtendsForm formModel - }) params)? builder; + ({BuildContext context, List itemList, ModelExtendsForm formModel}) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveModelExtendsFormArrayBuilderT? item, - ModelExtendsForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveModelExtendsFormArrayBuilderT? item, + ModelExtendsForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -706,23 +731,17 @@ class ReactiveModelExtendsFormArrayBuilder2< formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -731,32 +750,48 @@ class ReactiveModelExtendsFormArrayBuilder2< } class ReactiveModelExtendsFormFormGroupArrayBuilder< - ReactiveModelExtendsFormFormGroupArrayBuilderT> extends StatelessWidget { + ReactiveModelExtendsFormFormGroupArrayBuilderT +> + extends StatelessWidget { const ReactiveModelExtendsFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? extended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(ModelExtendsForm formModel)? + getExtended; - final ExtendedControl?>, - List> - Function(ModelExtendsForm formModel)? getExtended; - - final Widget Function(BuildContext context, List itemList, - ModelExtendsForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + ModelExtendsForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveModelExtendsFormFormGroupArrayBuilderT? item, - ModelExtendsForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveModelExtendsFormFormGroupArrayBuilderT? item, + ModelExtendsForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -771,26 +806,18 @@ class ReactiveModelExtendsFormFormGroupArrayBuilder< return StreamBuilder?>?>( stream: value.control.valueChanges, builder: (context, snapshot) { - final itemList = (value.value() ?? - []) - .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) - .values - .toList(); - - return builder?.call( - context, - itemList, - formModel, - ) ?? + final itemList = + (value.value() ?? + []) + .asMap() + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) + .values + .toList(); + + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/reactive_forms_generator/example/lib/docs/model_implements/model_implements.gform.dart b/packages/reactive_forms_generator/example/lib/docs/model_implements/model_implements.gform.dart index 56d934aa..a42d02d0 100644 --- a/packages/reactive_forms_generator/example/lib/docs/model_implements/model_implements.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/model_implements/model_implements.gform.dart @@ -19,8 +19,11 @@ class ReactiveModelImplementsFormConsumer extends StatelessWidget { final Widget? child; final Widget Function( - BuildContext context, ModelImplementsForm formModel, Widget? child) - builder; + BuildContext context, + ModelImplementsForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -39,11 +42,7 @@ class ModelImplementsFormInheritedStreamer extends InheritedStreamer { required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final ModelImplementsForm form; } @@ -65,19 +64,19 @@ class ReactiveModelImplementsForm extends StatelessWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - static ModelImplementsForm? of( - BuildContext context, { - bool listen = true, - }) { + static ModelImplementsForm? of(BuildContext context, {bool listen = true}) { if (listen) { return context .dependOnInheritedWidgetOfExactType< - ModelImplementsFormInheritedStreamer>() + ModelImplementsFormInheritedStreamer + >() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - ModelImplementsFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + ModelImplementsFormInheritedStreamer + >(); return element == null ? null : (element.widget as ModelImplementsFormInheritedStreamer).form; @@ -125,11 +124,14 @@ class ModelImplementsFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, ModelImplementsForm formModel, Widget? child) - builder; + BuildContext context, + ModelImplementsForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, ModelImplementsForm formModel)? - initState; + initState; @override _ModelImplementsFormBuilderState createState() => @@ -145,7 +147,10 @@ class _ModelImplementsFormBuilderState @override void initState() { _formModel = ModelImplementsForm( - ModelImplementsForm.formElements(widget.model), null, null); + ModelImplementsForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -223,11 +228,8 @@ final _logModelImplementsForm = Logger.detached('ModelImplementsForm'); class ModelImplementsForm implements FormModel { - ModelImplementsForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + ModelImplementsForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String emailControlName = "email"; @@ -237,7 +239,7 @@ class ModelImplementsForm final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -257,8 +259,6 @@ class ModelImplementsForm String get _passwordRawValue => passwordControl.value ?? ""; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsEmail { try { form.control(emailControlPath()); @@ -268,8 +268,6 @@ class ModelImplementsForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsPassword { try { form.control(passwordControlPath()); @@ -292,8 +290,11 @@ class ModelImplementsForm bool updateParent = true, bool emitEvent = true, }) { - emailControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void passwordValueUpdate( @@ -301,8 +302,11 @@ class ModelImplementsForm bool updateParent = true, bool emitEvent = true, }) { - passwordControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + passwordControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailValuePatch( @@ -310,8 +314,11 @@ class ModelImplementsForm bool updateParent = true, bool emitEvent = true, }) { - emailControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void passwordValuePatch( @@ -319,8 +326,11 @@ class ModelImplementsForm bool updateParent = true, bool emitEvent = true, }) { - passwordControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + passwordControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailValueReset( @@ -329,14 +339,13 @@ class ModelImplementsForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - emailControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => emailControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void passwordValueReset( String value, { @@ -344,14 +353,13 @@ class ModelImplementsForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - passwordControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => passwordControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get emailControl => form.control(emailControlPath()) as FormControl; @@ -415,17 +423,16 @@ class ModelImplementsForm } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { @@ -483,9 +490,11 @@ class ModelImplementsForm ModelImplements? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(ModelImplementsForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + ModelImplementsForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -503,17 +512,14 @@ class ModelImplementsForm ModelImplements? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -564,30 +570,36 @@ class ModelImplementsForm String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(ModelImplements? modelImplements) => FormGroup({ - emailControlName: FormControl( - value: modelImplements?.email, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - passwordControlName: FormControl( - value: modelImplements?.password, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(ModelImplements? modelImplements) => FormGroup( + { + emailControlName: FormControl( + value: modelImplements?.email, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + passwordControlName: FormControl( + value: modelImplements?.password, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } class ReactiveModelImplementsFormArrayBuilder< - ReactiveModelImplementsFormArrayBuilderT> extends StatelessWidget { + ReactiveModelImplementsFormArrayBuilderT +> + extends StatelessWidget { const ReactiveModelImplementsFormArrayBuilder({ Key? key, this.control, @@ -596,30 +608,41 @@ class ReactiveModelImplementsFormArrayBuilder< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - ModelImplementsForm formModel)? control; + ModelImplementsForm formModel, + )? + control; - final Widget Function(BuildContext context, List itemList, - ModelImplementsForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + ModelImplementsForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveModelImplementsFormArrayBuilderT? item, - ModelImplementsForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveModelImplementsFormArrayBuilderT? item, + ModelImplementsForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -633,21 +656,13 @@ class ReactiveModelImplementsFormArrayBuilder< final itemBuilder = this.itemBuilder; return ReactiveFormArrayItemBuilder< - ReactiveModelImplementsFormArrayBuilderT>( + ReactiveModelImplementsFormArrayBuilderT + >( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -656,7 +671,9 @@ class ReactiveModelImplementsFormArrayBuilder< } class ReactiveModelImplementsFormArrayBuilder2< - ReactiveModelImplementsFormArrayBuilderT> extends StatelessWidget { + ReactiveModelImplementsFormArrayBuilderT +> + extends StatelessWidget { const ReactiveModelImplementsFormArrayBuilder2({ Key? key, this.control, @@ -665,36 +682,47 @@ class ReactiveModelImplementsFormArrayBuilder2< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - ModelImplementsForm formModel)? control; + ModelImplementsForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - ModelImplementsForm formModel - }) params)? builder; + ({ + BuildContext context, + List itemList, + ModelImplementsForm formModel, + }) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveModelImplementsFormArrayBuilderT? item, - ModelImplementsForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveModelImplementsFormArrayBuilderT? item, + ModelImplementsForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -708,27 +736,22 @@ class ReactiveModelImplementsFormArrayBuilder2< final itemBuilder = this.itemBuilder; return ReactiveFormArrayItemBuilder< - ReactiveModelImplementsFormArrayBuilderT>( + ReactiveModelImplementsFormArrayBuilderT + >( formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -737,32 +760,48 @@ class ReactiveModelImplementsFormArrayBuilder2< } class ReactiveModelImplementsFormFormGroupArrayBuilder< - ReactiveModelImplementsFormFormGroupArrayBuilderT> extends StatelessWidget { + ReactiveModelImplementsFormFormGroupArrayBuilderT +> + extends StatelessWidget { const ReactiveModelImplementsFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? extended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(ModelImplementsForm formModel)? + getExtended; - final ExtendedControl?>, - List> - Function(ModelImplementsForm formModel)? getExtended; - - final Widget Function(BuildContext context, List itemList, - ModelImplementsForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + ModelImplementsForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveModelImplementsFormFormGroupArrayBuilderT? item, - ModelImplementsForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveModelImplementsFormFormGroupArrayBuilderT? item, + ModelImplementsForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -777,26 +816,18 @@ class ReactiveModelImplementsFormFormGroupArrayBuilder< return StreamBuilder?>?>( stream: value.control.valueChanges, builder: (context, snapshot) { - final itemList = (value.value() ?? - []) - .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) - .values - .toList(); - - return builder?.call( - context, - itemList, - formModel, - ) ?? + final itemList = + (value.value() ?? + []) + .asMap() + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) + .values + .toList(); + + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/reactive_forms_generator/example/lib/docs/nested/nested.gform.dart b/packages/reactive_forms_generator/example/lib/docs/nested/nested.gform.dart index 45386401..cc25cab8 100644 --- a/packages/reactive_forms_generator/example/lib/docs/nested/nested.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/nested/nested.gform.dart @@ -19,7 +19,11 @@ class ReactiveSubGroupFormConsumer extends StatelessWidget { final Widget? child; final Widget Function( - BuildContext context, SubGroupForm formModel, Widget? child) builder; + BuildContext context, + SubGroupForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -38,11 +42,7 @@ class SubGroupFormInheritedStreamer extends InheritedStreamer { required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final SubGroupForm form; } @@ -64,18 +64,17 @@ class ReactiveSubGroupForm extends StatelessWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - static SubGroupForm? of( - BuildContext context, { - bool listen = true, - }) { + static SubGroupForm? of(BuildContext context, {bool listen = true}) { if (listen) { return context .dependOnInheritedWidgetOfExactType() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - SubGroupFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + SubGroupFormInheritedStreamer + >(); return element == null ? null : (element.widget as SubGroupFormInheritedStreamer).form; @@ -122,7 +121,11 @@ class SubGroupFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, SubGroupForm formModel, Widget? child) builder; + BuildContext context, + SubGroupForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, SubGroupForm formModel)? initState; @@ -137,8 +140,11 @@ class _SubGroupFormBuilderState extends State { @override void initState() { - _formModel = - SubGroupForm(SubGroupForm.formElements(widget.model), null, null); + _formModel = SubGroupForm( + SubGroupForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -215,11 +221,7 @@ class _SubGroupFormBuilderState extends State { final _logSubGroupForm = Logger.detached('SubGroupForm'); class SubGroupForm implements FormModel { - SubGroupForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + SubGroupForm(this.form, this.path, this._formModel) : initial = form.rawValue; static const String idControlName = "id"; @@ -227,7 +229,7 @@ class SubGroupForm implements FormModel { final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -241,8 +243,6 @@ class SubGroupForm implements FormModel { String get _idRawValue => idControl.value as String; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsId { try { form.control(idControlPath()); @@ -261,8 +261,11 @@ class SubGroupForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - idControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + idControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void idValuePatch( @@ -270,8 +273,11 @@ class SubGroupForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - idControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + idControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void idValueReset( @@ -280,14 +286,13 @@ class SubGroupForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - idControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => idControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get idControl => form.control(idControlPath()) as FormControl; @@ -303,10 +308,7 @@ class SubGroupForm implements FormModel { emitEvent: emitEvent, ); } else { - idControl.markAsEnabled( - updateParent: updateParent, - emitEvent: emitEvent, - ); + idControl.markAsEnabled(updateParent: updateParent, emitEvent: emitEvent); } } @@ -330,17 +332,16 @@ class SubGroupForm implements FormModel { } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { @@ -398,9 +399,11 @@ class SubGroupForm implements FormModel { SubGroup? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(SubGroupForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + SubGroupForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -418,17 +421,14 @@ class SubGroupForm implements FormModel { SubGroup? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -479,19 +479,22 @@ class SubGroupForm implements FormModel { String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(SubGroup? subGroup) => FormGroup({ - idControlName: FormControl( - value: subGroup?.id, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(SubGroup? subGroup) => FormGroup( + { + idControlName: FormControl( + value: subGroup?.id, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } class ReactiveSubGroupFormArrayBuilder @@ -504,30 +507,39 @@ class ReactiveSubGroupFormArrayBuilder required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - SubGroupForm formModel)? control; + SubGroupForm formModel, + )? + control; final Widget Function( - BuildContext context, List itemList, SubGroupForm formModel)? - builder; + BuildContext context, + List itemList, + SubGroupForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveSubGroupFormArrayBuilderT? item, - SubGroupForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveSubGroupFormArrayBuilderT? item, + SubGroupForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function(FormControl control)? - controlFilter; + controlFilter; @override Widget build(BuildContext context) { @@ -543,18 +555,9 @@ class ReactiveSubGroupFormArrayBuilder return ReactiveFormArrayItemBuilder( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -572,35 +575,41 @@ class ReactiveSubGroupFormArrayBuilder2 required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - SubGroupForm formModel)? control; + SubGroupForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - SubGroupForm formModel - }) params)? builder; + ({BuildContext context, List itemList, SubGroupForm formModel}) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveSubGroupFormArrayBuilderT? item, - SubGroupForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveSubGroupFormArrayBuilderT? item, + SubGroupForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function(FormControl control)? - controlFilter; + controlFilter; @override Widget build(BuildContext context) { @@ -617,23 +626,17 @@ class ReactiveSubGroupFormArrayBuilder2 formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -642,33 +645,48 @@ class ReactiveSubGroupFormArrayBuilder2 } class ReactiveSubGroupFormFormGroupArrayBuilder< - ReactiveSubGroupFormFormGroupArrayBuilderT> extends StatelessWidget { + ReactiveSubGroupFormFormGroupArrayBuilderT +> + extends StatelessWidget { const ReactiveSubGroupFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? extended; - - final ExtendedControl?>, - List> - Function(SubGroupForm formModel)? getExtended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(SubGroupForm formModel)? + getExtended; final Widget Function( - BuildContext context, List itemList, SubGroupForm formModel)? - builder; + BuildContext context, + List itemList, + SubGroupForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveSubGroupFormFormGroupArrayBuilderT? item, - SubGroupForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveSubGroupFormFormGroupArrayBuilderT? item, + SubGroupForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -686,23 +704,14 @@ class ReactiveSubGroupFormFormGroupArrayBuilder< final itemList = (value.value() ?? []) .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) .values .toList(); - return builder?.call( - context, - itemList, - formModel, - ) ?? + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); @@ -710,16 +719,17 @@ class ReactiveSubGroupFormFormGroupArrayBuilder< } class ReactiveGroupFormConsumer extends StatelessWidget { - const ReactiveGroupFormConsumer({ - Key? key, - required this.builder, - this.child, - }) : super(key: key); + const ReactiveGroupFormConsumer({Key? key, required this.builder, this.child}) + : super(key: key); final Widget? child; final Widget Function( - BuildContext context, GroupForm formModel, Widget? child) builder; + BuildContext context, + GroupForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -738,11 +748,7 @@ class GroupFormInheritedStreamer extends InheritedStreamer { required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final GroupForm form; } @@ -764,10 +770,7 @@ class ReactiveGroupForm extends StatelessWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - static GroupForm? of( - BuildContext context, { - bool listen = true, - }) { + static GroupForm? of(BuildContext context, {bool listen = true}) { if (listen) { return context .dependOnInheritedWidgetOfExactType() @@ -821,7 +824,11 @@ class GroupFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, GroupForm formModel, Widget? child) builder; + BuildContext context, + GroupForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, GroupForm formModel)? initState; @@ -913,11 +920,7 @@ class _GroupFormBuilderState extends State { final _logGroupForm = Logger.detached('GroupForm'); class GroupForm implements FormModel { - GroupForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + GroupForm(this.form, this.path, this._formModel) : initial = form.rawValue; static const String idControlName = "id"; @@ -927,7 +930,7 @@ class GroupForm implements FormModel { final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -949,8 +952,6 @@ class GroupForm implements FormModel { List get _subGroupListRawValue => subGroupListSubGroupForm.map((e) => e.rawModel).toList(); - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsId { try { form.control(idControlPath()); @@ -960,8 +961,6 @@ class GroupForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsSubGroupList { try { form.control(subGroupListControlPath()); @@ -984,8 +983,11 @@ class GroupForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - idControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + idControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void subGroupListValueUpdate( @@ -1016,15 +1018,19 @@ class GroupForm implements FormModel { if (toUpdate.isNotEmpty) { subGroupListControl.updateValue( - toUpdate.map((e) => SubGroupForm.formElements(e).rawValue).toList(), - updateParent: updateParent, - emitEvent: emitEvent); + toUpdate.map((e) => SubGroupForm.formElements(e).rawValue).toList(), + updateParent: updateParent, + emitEvent: emitEvent, + ); } if (toAdd.isNotEmpty) { toAdd.forEach((e) { - subGroupListControl.add(SubGroupForm.formElements(e), - updateParent: updateParent, emitEvent: emitEvent); + subGroupListControl.add( + SubGroupForm.formElements(e), + updateParent: updateParent, + emitEvent: emitEvent, + ); }); } } @@ -1049,10 +1055,7 @@ class GroupForm implements FormModel { ); } - void subGroupListClear({ - bool updateParent = true, - bool emitEvent = true, - }) { + void subGroupListClear({bool updateParent = true, bool emitEvent = true}) { subGroupListSubGroupForm.clear(); subGroupListControl.clear(updateParent: updateParent, emitEvent: emitEvent); } @@ -1062,8 +1065,11 @@ class GroupForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - idControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + idControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void subGroupListValuePatch( @@ -1074,18 +1080,17 @@ class GroupForm implements FormModel { final keys = subGroupListSubGroupForm.asMap().keys; final toPatch = []; - (value).asMap().forEach( - (k, v) { - if (keys.contains(k)) { - toPatch.add(v); - } - }, - ); + (value).asMap().forEach((k, v) { + if (keys.contains(k)) { + toPatch.add(v); + } + }); subGroupListControl.patchValue( - toPatch.map((e) => SubGroupForm.formElements(e).rawValue).toList(), - updateParent: updateParent, - emitEvent: emitEvent); + toPatch.map((e) => SubGroupForm.formElements(e).rawValue).toList(), + updateParent: updateParent, + emitEvent: emitEvent, + ); } void idValueReset( @@ -1094,14 +1099,13 @@ class GroupForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - idControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => idControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void subGroupListValueReset( List value, { @@ -1109,12 +1113,11 @@ class GroupForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - subGroupListControl.reset( - value: - value.map((e) => SubGroupForm.formElements(e).rawValue).toList(), - updateParent: updateParent, - emitEvent: emitEvent); + }) => subGroupListControl.reset( + value: value.map((e) => SubGroupForm.formElements(e).rawValue).toList(), + updateParent: updateParent, + emitEvent: emitEvent, + ); FormControl get idControl => form.control(idControlPath()) as FormControl; @@ -1128,10 +1131,16 @@ class GroupForm implements FormModel { return values .asMap() - .map((k, v) => MapEntry( + .map( + (k, v) => MapEntry( k, SubGroupForm( - form, pathBuilder("subGroupList.$k"), _formModel ?? this))) + form, + pathBuilder("subGroupList.$k"), + _formModel ?? this, + ), + ), + ) .values .toList(); } @@ -1147,10 +1156,7 @@ class GroupForm implements FormModel { emitEvent: emitEvent, ); } else { - idControl.markAsEnabled( - updateParent: updateParent, - emitEvent: emitEvent, - ); + idControl.markAsEnabled(updateParent: updateParent, emitEvent: emitEvent); } } @@ -1173,11 +1179,12 @@ class GroupForm implements FormModel { } ExtendedControl?>, List> - get subGroupListExtendedControl => - ExtendedControl?>, List>( - form.control(subGroupListControlPath()) - as FormArray>, - () => subGroupListSubGroupForm); + get subGroupListExtendedControl => + ExtendedControl?>, List>( + form.control(subGroupListControlPath()) + as FormArray>, + () => subGroupListSubGroupForm, + ); void addSubGroupListItem(SubGroup value) { subGroupListControl.add(SubGroupForm.formElements(value)); @@ -1213,10 +1220,7 @@ class GroupForm implements FormModel { } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; @@ -1225,7 +1229,9 @@ class GroupForm implements FormModel { subGroupListSubGroupForm.forEach((e) => e.toggleDisabled()); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { subGroupListSubGroupForm.forEach((e) => e.toggleDisabled()); @@ -1285,9 +1291,11 @@ class GroupForm implements FormModel { Group? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(GroupForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + GroupForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -1301,21 +1309,15 @@ class GroupForm implements FormModel { } @override - void reset({ - Group? value, - bool updateParent = true, - bool emitEvent = true, - }) => + void reset({Group? value, bool updateParent = true, bool emitEvent = true}) => currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -1366,27 +1368,31 @@ class GroupForm implements FormModel { String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(Group? group) => FormGroup({ - idControlName: FormControl( - value: group?.id, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - subGroupListControlName: FormArray( - (group?.subGroupList ?? []) - .map((e) => SubGroupForm.formElements(e)) - .toList(), - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(Group? group) => FormGroup( + { + idControlName: FormControl( + value: group?.id, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + subGroupListControlName: FormArray( + (group?.subGroupList ?? []) + .map((e) => SubGroupForm.formElements(e)) + .toList(), + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } class ReactiveGroupFormArrayBuilder @@ -1399,30 +1405,39 @@ class ReactiveGroupFormArrayBuilder required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - GroupForm formModel)? control; + GroupForm formModel, + )? + control; final Widget Function( - BuildContext context, List itemList, GroupForm formModel)? - builder; + BuildContext context, + List itemList, + GroupForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveGroupFormArrayBuilderT? item, - GroupForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveGroupFormArrayBuilderT? item, + GroupForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function(FormControl control)? - controlFilter; + controlFilter; @override Widget build(BuildContext context) { @@ -1438,18 +1453,9 @@ class ReactiveGroupFormArrayBuilder return ReactiveFormArrayItemBuilder( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1467,35 +1473,40 @@ class ReactiveGroupFormArrayBuilder2 required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - GroupForm formModel)? control; + GroupForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - GroupForm formModel - }) params)? builder; + ({BuildContext context, List itemList, GroupForm formModel}) params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveGroupFormArrayBuilderT? item, - GroupForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveGroupFormArrayBuilderT? item, + GroupForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function(FormControl control)? - controlFilter; + controlFilter; @override Widget build(BuildContext context) { @@ -1512,23 +1523,17 @@ class ReactiveGroupFormArrayBuilder2 formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1537,33 +1542,48 @@ class ReactiveGroupFormArrayBuilder2 } class ReactiveGroupFormFormGroupArrayBuilder< - ReactiveGroupFormFormGroupArrayBuilderT> extends StatelessWidget { + ReactiveGroupFormFormGroupArrayBuilderT +> + extends StatelessWidget { const ReactiveGroupFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? extended; - - final ExtendedControl?>, - List> - Function(GroupForm formModel)? getExtended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(GroupForm formModel)? + getExtended; final Widget Function( - BuildContext context, List itemList, GroupForm formModel)? - builder; + BuildContext context, + List itemList, + GroupForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveGroupFormFormGroupArrayBuilderT? item, - GroupForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveGroupFormFormGroupArrayBuilderT? item, + GroupForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -1581,23 +1601,14 @@ class ReactiveGroupFormFormGroupArrayBuilder< final itemList = (value.value() ?? []) .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) .values .toList(); - return builder?.call( - context, - itemList, - formModel, - ) ?? + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); @@ -1614,7 +1625,11 @@ class ReactiveNestedFormConsumer extends StatelessWidget { final Widget? child; final Widget Function( - BuildContext context, NestedForm formModel, Widget? child) builder; + BuildContext context, + NestedForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -1633,11 +1648,7 @@ class NestedFormInheritedStreamer extends InheritedStreamer { required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final NestedForm form; } @@ -1659,10 +1670,7 @@ class ReactiveNestedForm extends StatelessWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - static NestedForm? of( - BuildContext context, { - bool listen = true, - }) { + static NestedForm? of(BuildContext context, {bool listen = true}) { if (listen) { return context .dependOnInheritedWidgetOfExactType() @@ -1716,7 +1724,11 @@ class NestedFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, NestedForm formModel, Widget? child) builder; + BuildContext context, + NestedForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, NestedForm formModel)? initState; @@ -1808,11 +1820,7 @@ class _NestedFormBuilderState extends State { final _logNestedForm = Logger.detached('NestedForm'); class NestedForm implements FormModel { - NestedForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + NestedForm(this.form, this.path, this._formModel) : initial = form.rawValue; static const String groupListControlName = "groupList"; @@ -1820,7 +1828,7 @@ class NestedForm implements FormModel { final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -1836,8 +1844,6 @@ class NestedForm implements FormModel { List get _groupListRawValue => groupListGroupForm.map((e) => e.rawModel).toList(); - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsGroupList { try { form.control(groupListControlPath()); @@ -1879,15 +1885,19 @@ class NestedForm implements FormModel { if (toUpdate.isNotEmpty) { groupListControl.updateValue( - toUpdate.map((e) => GroupForm.formElements(e).rawValue).toList(), - updateParent: updateParent, - emitEvent: emitEvent); + toUpdate.map((e) => GroupForm.formElements(e).rawValue).toList(), + updateParent: updateParent, + emitEvent: emitEvent, + ); } if (toAdd.isNotEmpty) { toAdd.forEach((e) { - groupListControl.add(GroupForm.formElements(e), - updateParent: updateParent, emitEvent: emitEvent); + groupListControl.add( + GroupForm.formElements(e), + updateParent: updateParent, + emitEvent: emitEvent, + ); }); } } @@ -1912,10 +1922,7 @@ class NestedForm implements FormModel { ); } - void groupListClear({ - bool updateParent = true, - bool emitEvent = true, - }) { + void groupListClear({bool updateParent = true, bool emitEvent = true}) { groupListGroupForm.clear(); groupListControl.clear(updateParent: updateParent, emitEvent: emitEvent); } @@ -1928,18 +1935,17 @@ class NestedForm implements FormModel { final keys = groupListGroupForm.asMap().keys; final toPatch = []; - (value).asMap().forEach( - (k, v) { - if (keys.contains(k)) { - toPatch.add(v); - } - }, - ); + (value).asMap().forEach((k, v) { + if (keys.contains(k)) { + toPatch.add(v); + } + }); groupListControl.patchValue( - toPatch.map((e) => GroupForm.formElements(e).rawValue).toList(), - updateParent: updateParent, - emitEvent: emitEvent); + toPatch.map((e) => GroupForm.formElements(e).rawValue).toList(), + updateParent: updateParent, + emitEvent: emitEvent, + ); } void groupListValueReset( @@ -1948,11 +1954,11 @@ class NestedForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - groupListControl.reset( - value: value.map((e) => GroupForm.formElements(e).rawValue).toList(), - updateParent: updateParent, - emitEvent: emitEvent); + }) => groupListControl.reset( + value: value.map((e) => GroupForm.formElements(e).rawValue).toList(), + updateParent: updateParent, + emitEvent: emitEvent, + ); FormArray> get groupListControl => form.control(groupListControlPath()) as FormArray>; @@ -1962,8 +1968,12 @@ class NestedForm implements FormModel { return values .asMap() - .map((k, v) => MapEntry(k, - GroupForm(form, pathBuilder("groupList.$k"), _formModel ?? this))) + .map( + (k, v) => MapEntry( + k, + GroupForm(form, pathBuilder("groupList.$k"), _formModel ?? this), + ), + ) .values .toList(); } @@ -1987,11 +1997,11 @@ class NestedForm implements FormModel { } ExtendedControl?>, List> - get groupListExtendedControl => - ExtendedControl?>, List>( - form.control(groupListControlPath()) - as FormArray>, - () => groupListGroupForm); + get groupListExtendedControl => + ExtendedControl?>, List>( + form.control(groupListControlPath()) as FormArray>, + () => groupListGroupForm, + ); void addGroupListItem(Group value) { groupListControl.add(GroupForm.formElements(value)); @@ -2027,10 +2037,7 @@ class NestedForm implements FormModel { } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; @@ -2039,7 +2046,9 @@ class NestedForm implements FormModel { groupListGroupForm.forEach((e) => e.toggleDisabled()); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { groupListGroupForm.forEach((e) => e.toggleDisabled()); @@ -2099,9 +2108,11 @@ class NestedForm implements FormModel { Nested? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(NestedForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + NestedForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -2119,17 +2130,14 @@ class NestedForm implements FormModel { Nested? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -2180,20 +2188,23 @@ class NestedForm implements FormModel { String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(Nested? nested) => FormGroup({ - groupListControlName: FormArray( - (nested?.groupList ?? []) - .map((e) => GroupForm.formElements(e)) - .toList(), - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(Nested? nested) => FormGroup( + { + groupListControlName: FormArray( + (nested?.groupList ?? []) + .map((e) => GroupForm.formElements(e)) + .toList(), + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } class ReactiveNestedFormArrayBuilder @@ -2206,30 +2217,39 @@ class ReactiveNestedFormArrayBuilder required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - NestedForm formModel)? control; + NestedForm formModel, + )? + control; final Widget Function( - BuildContext context, List itemList, NestedForm formModel)? - builder; + BuildContext context, + List itemList, + NestedForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveNestedFormArrayBuilderT? item, - NestedForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveNestedFormArrayBuilderT? item, + NestedForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function(FormControl control)? - controlFilter; + controlFilter; @override Widget build(BuildContext context) { @@ -2245,18 +2265,9 @@ class ReactiveNestedFormArrayBuilder return ReactiveFormArrayItemBuilder( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -2274,35 +2285,41 @@ class ReactiveNestedFormArrayBuilder2 required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - NestedForm formModel)? control; + NestedForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - NestedForm formModel - }) params)? builder; + ({BuildContext context, List itemList, NestedForm formModel}) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveNestedFormArrayBuilderT? item, - NestedForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveNestedFormArrayBuilderT? item, + NestedForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function(FormControl control)? - controlFilter; + controlFilter; @override Widget build(BuildContext context) { @@ -2319,23 +2336,17 @@ class ReactiveNestedFormArrayBuilder2 formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -2344,33 +2355,48 @@ class ReactiveNestedFormArrayBuilder2 } class ReactiveNestedFormFormGroupArrayBuilder< - ReactiveNestedFormFormGroupArrayBuilderT> extends StatelessWidget { + ReactiveNestedFormFormGroupArrayBuilderT +> + extends StatelessWidget { const ReactiveNestedFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? extended; - - final ExtendedControl?>, - List> - Function(NestedForm formModel)? getExtended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(NestedForm formModel)? + getExtended; final Widget Function( - BuildContext context, List itemList, NestedForm formModel)? - builder; + BuildContext context, + List itemList, + NestedForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveNestedFormFormGroupArrayBuilderT? item, - NestedForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveNestedFormFormGroupArrayBuilderT? item, + NestedForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -2388,23 +2414,14 @@ class ReactiveNestedFormFormGroupArrayBuilder< final itemList = (value.value() ?? []) .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) .values .toList(); - return builder?.call( - context, - itemList, - formModel, - ) ?? + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/reactive_forms_generator/example/lib/docs/nested/nested_output.gform.dart b/packages/reactive_forms_generator/example/lib/docs/nested/nested_output.gform.dart index f300df1e..fa6215bc 100644 --- a/packages/reactive_forms_generator/example/lib/docs/nested/nested_output.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/nested/nested_output.gform.dart @@ -19,7 +19,11 @@ class ReactiveSubGroupOFormConsumer extends StatelessWidget { final Widget? child; final Widget Function( - BuildContext context, SubGroupOForm formModel, Widget? child) builder; + BuildContext context, + SubGroupOForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -38,11 +42,7 @@ class SubGroupOFormInheritedStreamer extends InheritedStreamer { required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final SubGroupOForm form; } @@ -64,18 +64,17 @@ class ReactiveSubGroupOForm extends StatelessWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - static SubGroupOForm? of( - BuildContext context, { - bool listen = true, - }) { + static SubGroupOForm? of(BuildContext context, {bool listen = true}) { if (listen) { return context .dependOnInheritedWidgetOfExactType() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - SubGroupOFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + SubGroupOFormInheritedStreamer + >(); return element == null ? null : (element.widget as SubGroupOFormInheritedStreamer).form; @@ -122,7 +121,11 @@ class SubGroupOFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, SubGroupOForm formModel, Widget? child) builder; + BuildContext context, + SubGroupOForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, SubGroupOForm formModel)? initState; @@ -137,8 +140,11 @@ class _SubGroupOFormBuilderState extends State { @override void initState() { - _formModel = - SubGroupOForm(SubGroupOForm.formElements(widget.model), null, null); + _formModel = SubGroupOForm( + SubGroupOForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -215,11 +221,8 @@ class _SubGroupOFormBuilderState extends State { final _logSubGroupOForm = Logger.detached('SubGroupOForm'); class SubGroupOForm implements FormModel { - SubGroupOForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + SubGroupOForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String idControlName = "id"; @@ -227,7 +230,7 @@ class SubGroupOForm implements FormModel { final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -241,8 +244,6 @@ class SubGroupOForm implements FormModel { String get _idRawValue => idControl.value as String; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsId { try { form.control(idControlPath()); @@ -261,8 +262,11 @@ class SubGroupOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - idControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + idControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void idValuePatch( @@ -270,8 +274,11 @@ class SubGroupOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - idControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + idControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void idValueReset( @@ -280,14 +287,13 @@ class SubGroupOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - idControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => idControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get idControl => form.control(idControlPath()) as FormControl; @@ -303,10 +309,7 @@ class SubGroupOForm implements FormModel { emitEvent: emitEvent, ); } else { - idControl.markAsEnabled( - updateParent: updateParent, - emitEvent: emitEvent, - ); + idControl.markAsEnabled(updateParent: updateParent, emitEvent: emitEvent); } } @@ -331,17 +334,16 @@ class SubGroupOForm implements FormModel { } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { @@ -399,9 +401,11 @@ class SubGroupOForm implements FormModel { SubGroupO? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(SubGroupOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + SubGroupOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -419,17 +423,14 @@ class SubGroupOForm implements FormModel { SubGroupO? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -480,19 +481,22 @@ class SubGroupOForm implements FormModel { String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(SubGroupO? subGroupO) => FormGroup({ - idControlName: FormControl( - value: subGroupO?.id, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(SubGroupO? subGroupO) => FormGroup( + { + idControlName: FormControl( + value: subGroupO?.id, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } @Rf(output: true) @@ -509,10 +513,10 @@ abstract class SubGroupOOutput with _$SubGroupOOutput { @RfGroup() @freezed abstract class GroupOOutput with _$GroupOOutput { - const factory GroupOOutput( - {@RfControl() required String id, - @RfArray() required List subGroupList}) = - _GroupOOutput; + const factory GroupOOutput({ + @RfControl() required String id, + @RfArray() required List subGroupList, + }) = _GroupOOutput; factory GroupOOutput.fromJson(Map json) => _$GroupOOutputFromJson(json); } @@ -521,9 +525,9 @@ abstract class GroupOOutput with _$GroupOOutput { @RfGroup() @freezed abstract class NestedOOutput with _$NestedOOutput { - const factory NestedOOutput( - {@RfArray() required List groupList}) = - _NestedOOutput; + const factory NestedOOutput({ + @RfArray() required List groupList, + }) = _NestedOOutput; factory NestedOOutput.fromJson(Map json) => _$NestedOOutputFromJson(json); } @@ -538,30 +542,39 @@ class ReactiveSubGroupOFormArrayBuilder required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - SubGroupOForm formModel)? control; + SubGroupOForm formModel, + )? + control; final Widget Function( - BuildContext context, List itemList, SubGroupOForm formModel)? - builder; + BuildContext context, + List itemList, + SubGroupOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveSubGroupOFormArrayBuilderT? item, - SubGroupOForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveSubGroupOFormArrayBuilderT? item, + SubGroupOForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function(FormControl control)? - controlFilter; + controlFilter; @override Widget build(BuildContext context) { @@ -577,18 +590,9 @@ class ReactiveSubGroupOFormArrayBuilder return ReactiveFormArrayItemBuilder( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -606,35 +610,41 @@ class ReactiveSubGroupOFormArrayBuilder2 required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - SubGroupOForm formModel)? control; + SubGroupOForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - SubGroupOForm formModel - }) params)? builder; + ({BuildContext context, List itemList, SubGroupOForm formModel}) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveSubGroupOFormArrayBuilderT? item, - SubGroupOForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveSubGroupOFormArrayBuilderT? item, + SubGroupOForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function(FormControl control)? - controlFilter; + controlFilter; @override Widget build(BuildContext context) { @@ -651,23 +661,17 @@ class ReactiveSubGroupOFormArrayBuilder2 formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -676,33 +680,48 @@ class ReactiveSubGroupOFormArrayBuilder2 } class ReactiveSubGroupOFormFormGroupArrayBuilder< - ReactiveSubGroupOFormFormGroupArrayBuilderT> extends StatelessWidget { + ReactiveSubGroupOFormFormGroupArrayBuilderT +> + extends StatelessWidget { const ReactiveSubGroupOFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? extended; - - final ExtendedControl?>, - List> - Function(SubGroupOForm formModel)? getExtended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(SubGroupOForm formModel)? + getExtended; final Widget Function( - BuildContext context, List itemList, SubGroupOForm formModel)? - builder; + BuildContext context, + List itemList, + SubGroupOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveSubGroupOFormFormGroupArrayBuilderT? item, - SubGroupOForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveSubGroupOFormFormGroupArrayBuilderT? item, + SubGroupOForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -720,23 +739,14 @@ class ReactiveSubGroupOFormFormGroupArrayBuilder< final itemList = (value.value() ?? []) .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) .values .toList(); - return builder?.call( - context, - itemList, - formModel, - ) ?? + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); @@ -753,7 +763,11 @@ class ReactiveGroupOFormConsumer extends StatelessWidget { final Widget? child; final Widget Function( - BuildContext context, GroupOForm formModel, Widget? child) builder; + BuildContext context, + GroupOForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -772,11 +786,7 @@ class GroupOFormInheritedStreamer extends InheritedStreamer { required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final GroupOForm form; } @@ -798,10 +808,7 @@ class ReactiveGroupOForm extends StatelessWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - static GroupOForm? of( - BuildContext context, { - bool listen = true, - }) { + static GroupOForm? of(BuildContext context, {bool listen = true}) { if (listen) { return context .dependOnInheritedWidgetOfExactType() @@ -855,7 +862,11 @@ class GroupOFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, GroupOForm formModel, Widget? child) builder; + BuildContext context, + GroupOForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, GroupOForm formModel)? initState; @@ -947,11 +958,7 @@ class _GroupOFormBuilderState extends State { final _logGroupOForm = Logger.detached('GroupOForm'); class GroupOForm implements FormModel { - GroupOForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + GroupOForm(this.form, this.path, this._formModel) : initial = form.rawValue; static const String idControlName = "id"; @@ -961,7 +968,7 @@ class GroupOForm implements FormModel { final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -983,8 +990,6 @@ class GroupOForm implements FormModel { List get _subGroupListRawValue => subGroupListSubGroupOForm.map((e) => e.rawModel).toList(); - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsId { try { form.control(idControlPath()); @@ -994,8 +999,6 @@ class GroupOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsSubGroupList { try { form.control(subGroupListControlPath()); @@ -1018,8 +1021,11 @@ class GroupOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - idControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + idControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void subGroupListValueUpdate( @@ -1050,15 +1056,19 @@ class GroupOForm implements FormModel { if (toUpdate.isNotEmpty) { subGroupListControl.updateValue( - toUpdate.map((e) => SubGroupOForm.formElements(e).rawValue).toList(), - updateParent: updateParent, - emitEvent: emitEvent); + toUpdate.map((e) => SubGroupOForm.formElements(e).rawValue).toList(), + updateParent: updateParent, + emitEvent: emitEvent, + ); } if (toAdd.isNotEmpty) { toAdd.forEach((e) { - subGroupListControl.add(SubGroupOForm.formElements(e), - updateParent: updateParent, emitEvent: emitEvent); + subGroupListControl.add( + SubGroupOForm.formElements(e), + updateParent: updateParent, + emitEvent: emitEvent, + ); }); } } @@ -1083,10 +1093,7 @@ class GroupOForm implements FormModel { ); } - void subGroupListClear({ - bool updateParent = true, - bool emitEvent = true, - }) { + void subGroupListClear({bool updateParent = true, bool emitEvent = true}) { subGroupListSubGroupOForm.clear(); subGroupListControl.clear(updateParent: updateParent, emitEvent: emitEvent); } @@ -1096,8 +1103,11 @@ class GroupOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - idControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + idControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void subGroupListValuePatch( @@ -1108,18 +1118,17 @@ class GroupOForm implements FormModel { final keys = subGroupListSubGroupOForm.asMap().keys; final toPatch = []; - (value).asMap().forEach( - (k, v) { - if (keys.contains(k)) { - toPatch.add(v); - } - }, - ); + (value).asMap().forEach((k, v) { + if (keys.contains(k)) { + toPatch.add(v); + } + }); subGroupListControl.patchValue( - toPatch.map((e) => SubGroupOForm.formElements(e).rawValue).toList(), - updateParent: updateParent, - emitEvent: emitEvent); + toPatch.map((e) => SubGroupOForm.formElements(e).rawValue).toList(), + updateParent: updateParent, + emitEvent: emitEvent, + ); } void idValueReset( @@ -1128,14 +1137,13 @@ class GroupOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - idControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => idControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void subGroupListValueReset( List value, { @@ -1143,12 +1151,11 @@ class GroupOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - subGroupListControl.reset( - value: - value.map((e) => SubGroupOForm.formElements(e).rawValue).toList(), - updateParent: updateParent, - emitEvent: emitEvent); + }) => subGroupListControl.reset( + value: value.map((e) => SubGroupOForm.formElements(e).rawValue).toList(), + updateParent: updateParent, + emitEvent: emitEvent, + ); FormControl get idControl => form.control(idControlPath()) as FormControl; @@ -1162,10 +1169,16 @@ class GroupOForm implements FormModel { return values .asMap() - .map((k, v) => MapEntry( + .map( + (k, v) => MapEntry( k, SubGroupOForm( - form, pathBuilder("subGroupList.$k"), _formModel ?? this))) + form, + pathBuilder("subGroupList.$k"), + _formModel ?? this, + ), + ), + ) .values .toList(); } @@ -1181,10 +1194,7 @@ class GroupOForm implements FormModel { emitEvent: emitEvent, ); } else { - idControl.markAsEnabled( - updateParent: updateParent, - emitEvent: emitEvent, - ); + idControl.markAsEnabled(updateParent: updateParent, emitEvent: emitEvent); } } @@ -1207,11 +1217,12 @@ class GroupOForm implements FormModel { } ExtendedControl?>, List> - get subGroupListExtendedControl => - ExtendedControl?>, List>( - form.control(subGroupListControlPath()) - as FormArray>, - () => subGroupListSubGroupOForm); + get subGroupListExtendedControl => + ExtendedControl?>, List>( + form.control(subGroupListControlPath()) + as FormArray>, + () => subGroupListSubGroupOForm, + ); void addSubGroupListItem(SubGroupO value) { subGroupListControl.add(SubGroupOForm.formElements(value)); @@ -1248,10 +1259,7 @@ class GroupOForm implements FormModel { } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; @@ -1260,7 +1268,9 @@ class GroupOForm implements FormModel { subGroupListSubGroupOForm.forEach((e) => e.toggleDisabled()); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { subGroupListSubGroupOForm.forEach((e) => e.toggleDisabled()); @@ -1320,9 +1330,11 @@ class GroupOForm implements FormModel { GroupO? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(GroupOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + GroupOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -1340,17 +1352,14 @@ class GroupOForm implements FormModel { GroupO? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -1401,27 +1410,31 @@ class GroupOForm implements FormModel { String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(GroupO? groupO) => FormGroup({ - idControlName: FormControl( - value: groupO?.id, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - subGroupListControlName: FormArray( - (groupO?.subGroupList ?? []) - .map((e) => SubGroupOForm.formElements(e)) - .toList(), - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(GroupO? groupO) => FormGroup( + { + idControlName: FormControl( + value: groupO?.id, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + subGroupListControlName: FormArray( + (groupO?.subGroupList ?? []) + .map((e) => SubGroupOForm.formElements(e)) + .toList(), + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } class ReactiveGroupOFormArrayBuilder @@ -1434,30 +1447,39 @@ class ReactiveGroupOFormArrayBuilder required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - GroupOForm formModel)? control; + GroupOForm formModel, + )? + control; final Widget Function( - BuildContext context, List itemList, GroupOForm formModel)? - builder; + BuildContext context, + List itemList, + GroupOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveGroupOFormArrayBuilderT? item, - GroupOForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveGroupOFormArrayBuilderT? item, + GroupOForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function(FormControl control)? - controlFilter; + controlFilter; @override Widget build(BuildContext context) { @@ -1473,18 +1495,9 @@ class ReactiveGroupOFormArrayBuilder return ReactiveFormArrayItemBuilder( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1502,35 +1515,41 @@ class ReactiveGroupOFormArrayBuilder2 required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - GroupOForm formModel)? control; + GroupOForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - GroupOForm formModel - }) params)? builder; + ({BuildContext context, List itemList, GroupOForm formModel}) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveGroupOFormArrayBuilderT? item, - GroupOForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveGroupOFormArrayBuilderT? item, + GroupOForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function(FormControl control)? - controlFilter; + controlFilter; @override Widget build(BuildContext context) { @@ -1547,23 +1566,17 @@ class ReactiveGroupOFormArrayBuilder2 formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1572,33 +1585,48 @@ class ReactiveGroupOFormArrayBuilder2 } class ReactiveGroupOFormFormGroupArrayBuilder< - ReactiveGroupOFormFormGroupArrayBuilderT> extends StatelessWidget { + ReactiveGroupOFormFormGroupArrayBuilderT +> + extends StatelessWidget { const ReactiveGroupOFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? extended; - - final ExtendedControl?>, - List> - Function(GroupOForm formModel)? getExtended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(GroupOForm formModel)? + getExtended; final Widget Function( - BuildContext context, List itemList, GroupOForm formModel)? - builder; + BuildContext context, + List itemList, + GroupOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveGroupOFormFormGroupArrayBuilderT? item, - GroupOForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveGroupOFormFormGroupArrayBuilderT? item, + GroupOForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -1616,23 +1644,14 @@ class ReactiveGroupOFormFormGroupArrayBuilder< final itemList = (value.value() ?? []) .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) .values .toList(); - return builder?.call( - context, - itemList, - formModel, - ) ?? + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); @@ -1649,7 +1668,11 @@ class ReactiveNestedOFormConsumer extends StatelessWidget { final Widget? child; final Widget Function( - BuildContext context, NestedOForm formModel, Widget? child) builder; + BuildContext context, + NestedOForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -1668,11 +1691,7 @@ class NestedOFormInheritedStreamer extends InheritedStreamer { required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final NestedOForm form; } @@ -1694,18 +1713,17 @@ class ReactiveNestedOForm extends StatelessWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - static NestedOForm? of( - BuildContext context, { - bool listen = true, - }) { + static NestedOForm? of(BuildContext context, {bool listen = true}) { if (listen) { return context .dependOnInheritedWidgetOfExactType() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - NestedOFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + NestedOFormInheritedStreamer + >(); return element == null ? null : (element.widget as NestedOFormInheritedStreamer).form; @@ -1751,7 +1769,11 @@ class NestedOFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, NestedOForm formModel, Widget? child) builder; + BuildContext context, + NestedOForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, NestedOForm formModel)? initState; @@ -1766,8 +1788,11 @@ class _NestedOFormBuilderState extends State { @override void initState() { - _formModel = - NestedOForm(NestedOForm.formElements(widget.model), null, null); + _formModel = NestedOForm( + NestedOForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -1844,11 +1869,7 @@ class _NestedOFormBuilderState extends State { final _logNestedOForm = Logger.detached('NestedOForm'); class NestedOForm implements FormModel { - NestedOForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + NestedOForm(this.form, this.path, this._formModel) : initial = form.rawValue; static const String groupListControlName = "groupList"; @@ -1856,7 +1877,7 @@ class NestedOForm implements FormModel { final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -1872,8 +1893,6 @@ class NestedOForm implements FormModel { List get _groupListRawValue => groupListGroupOForm.map((e) => e.rawModel).toList(); - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsGroupList { try { form.control(groupListControlPath()); @@ -1915,15 +1934,19 @@ class NestedOForm implements FormModel { if (toUpdate.isNotEmpty) { groupListControl.updateValue( - toUpdate.map((e) => GroupOForm.formElements(e).rawValue).toList(), - updateParent: updateParent, - emitEvent: emitEvent); + toUpdate.map((e) => GroupOForm.formElements(e).rawValue).toList(), + updateParent: updateParent, + emitEvent: emitEvent, + ); } if (toAdd.isNotEmpty) { toAdd.forEach((e) { - groupListControl.add(GroupOForm.formElements(e), - updateParent: updateParent, emitEvent: emitEvent); + groupListControl.add( + GroupOForm.formElements(e), + updateParent: updateParent, + emitEvent: emitEvent, + ); }); } } @@ -1948,10 +1971,7 @@ class NestedOForm implements FormModel { ); } - void groupListClear({ - bool updateParent = true, - bool emitEvent = true, - }) { + void groupListClear({bool updateParent = true, bool emitEvent = true}) { groupListGroupOForm.clear(); groupListControl.clear(updateParent: updateParent, emitEvent: emitEvent); } @@ -1964,18 +1984,17 @@ class NestedOForm implements FormModel { final keys = groupListGroupOForm.asMap().keys; final toPatch = []; - (value).asMap().forEach( - (k, v) { - if (keys.contains(k)) { - toPatch.add(v); - } - }, - ); + (value).asMap().forEach((k, v) { + if (keys.contains(k)) { + toPatch.add(v); + } + }); groupListControl.patchValue( - toPatch.map((e) => GroupOForm.formElements(e).rawValue).toList(), - updateParent: updateParent, - emitEvent: emitEvent); + toPatch.map((e) => GroupOForm.formElements(e).rawValue).toList(), + updateParent: updateParent, + emitEvent: emitEvent, + ); } void groupListValueReset( @@ -1984,11 +2003,11 @@ class NestedOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - groupListControl.reset( - value: value.map((e) => GroupOForm.formElements(e).rawValue).toList(), - updateParent: updateParent, - emitEvent: emitEvent); + }) => groupListControl.reset( + value: value.map((e) => GroupOForm.formElements(e).rawValue).toList(), + updateParent: updateParent, + emitEvent: emitEvent, + ); FormArray> get groupListControl => form.control(groupListControlPath()) as FormArray>; @@ -1998,8 +2017,12 @@ class NestedOForm implements FormModel { return values .asMap() - .map((k, v) => MapEntry(k, - GroupOForm(form, pathBuilder("groupList.$k"), _formModel ?? this))) + .map( + (k, v) => MapEntry( + k, + GroupOForm(form, pathBuilder("groupList.$k"), _formModel ?? this), + ), + ) .values .toList(); } @@ -2023,11 +2046,11 @@ class NestedOForm implements FormModel { } ExtendedControl?>, List> - get groupListExtendedControl => - ExtendedControl?>, List>( - form.control(groupListControlPath()) - as FormArray>, - () => groupListGroupOForm); + get groupListExtendedControl => + ExtendedControl?>, List>( + form.control(groupListControlPath()) as FormArray>, + () => groupListGroupOForm, + ); void addGroupListItem(GroupO value) { groupListControl.add(GroupOForm.formElements(value)); @@ -2064,10 +2087,7 @@ class NestedOForm implements FormModel { } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; @@ -2076,7 +2096,9 @@ class NestedOForm implements FormModel { groupListGroupOForm.forEach((e) => e.toggleDisabled()); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { groupListGroupOForm.forEach((e) => e.toggleDisabled()); @@ -2136,9 +2158,11 @@ class NestedOForm implements FormModel { NestedO? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(NestedOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + NestedOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -2156,17 +2180,14 @@ class NestedOForm implements FormModel { NestedO? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -2217,20 +2238,23 @@ class NestedOForm implements FormModel { String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(NestedO? nestedO) => FormGroup({ - groupListControlName: FormArray( - (nestedO?.groupList ?? []) - .map((e) => GroupOForm.formElements(e)) - .toList(), - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(NestedO? nestedO) => FormGroup( + { + groupListControlName: FormArray( + (nestedO?.groupList ?? []) + .map((e) => GroupOForm.formElements(e)) + .toList(), + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } class ReactiveNestedOFormArrayBuilder @@ -2243,30 +2267,39 @@ class ReactiveNestedOFormArrayBuilder required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - NestedOForm formModel)? control; + NestedOForm formModel, + )? + control; final Widget Function( - BuildContext context, List itemList, NestedOForm formModel)? - builder; + BuildContext context, + List itemList, + NestedOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveNestedOFormArrayBuilderT? item, - NestedOForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveNestedOFormArrayBuilderT? item, + NestedOForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function(FormControl control)? - controlFilter; + controlFilter; @override Widget build(BuildContext context) { @@ -2282,18 +2315,9 @@ class ReactiveNestedOFormArrayBuilder return ReactiveFormArrayItemBuilder( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -2311,35 +2335,41 @@ class ReactiveNestedOFormArrayBuilder2 required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - NestedOForm formModel)? control; + NestedOForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - NestedOForm formModel - }) params)? builder; + ({BuildContext context, List itemList, NestedOForm formModel}) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveNestedOFormArrayBuilderT? item, - NestedOForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveNestedOFormArrayBuilderT? item, + NestedOForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function(FormControl control)? - controlFilter; + controlFilter; @override Widget build(BuildContext context) { @@ -2356,23 +2386,17 @@ class ReactiveNestedOFormArrayBuilder2 formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -2381,33 +2405,48 @@ class ReactiveNestedOFormArrayBuilder2 } class ReactiveNestedOFormFormGroupArrayBuilder< - ReactiveNestedOFormFormGroupArrayBuilderT> extends StatelessWidget { + ReactiveNestedOFormFormGroupArrayBuilderT +> + extends StatelessWidget { const ReactiveNestedOFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? extended; - - final ExtendedControl?>, - List> - Function(NestedOForm formModel)? getExtended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(NestedOForm formModel)? + getExtended; final Widget Function( - BuildContext context, List itemList, NestedOForm formModel)? - builder; + BuildContext context, + List itemList, + NestedOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveNestedOFormFormGroupArrayBuilderT? item, - NestedOForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveNestedOFormFormGroupArrayBuilderT? item, + NestedOForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -2425,23 +2464,14 @@ class ReactiveNestedOFormFormGroupArrayBuilder< final itemList = (value.value() ?? []) .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) .values .toList(); - return builder?.call( - context, - itemList, - formModel, - ) ?? + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/reactive_forms_generator/example/lib/docs/nested_generics/nested_generics.gform.dart b/packages/reactive_forms_generator/example/lib/docs/nested_generics/nested_generics.gform.dart index bbdf7e57..d027d22d 100644 --- a/packages/reactive_forms_generator/example/lib/docs/nested_generics/nested_generics.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/nested_generics/nested_generics.gform.dart @@ -19,8 +19,12 @@ class ReactiveProductDetailsFormConsumer

final Widget? child; - final Widget Function(BuildContext context, - ProductDetailsForm formModel, Widget? child) builder; + final Widget Function( + BuildContext context, + ProductDetailsForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -40,11 +44,7 @@ class ProductDetailsFormInheritedStreamer

required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final ProductDetailsForm form; } @@ -74,12 +74,15 @@ class ReactiveProductDetailsForm

if (listen) { return context .dependOnInheritedWidgetOfExactType< - ProductDetailsFormInheritedStreamer>() + ProductDetailsFormInheritedStreamer + >() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - ProductDetailsFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + ProductDetailsFormInheritedStreamer + >(); return element == null ? null : (element.widget as ProductDetailsFormInheritedStreamer).form; @@ -101,12 +104,12 @@ class ReactiveProductDetailsForm

extension ReactiveReactiveProductDetailsFormExt on BuildContext { ProductDetailsForm? - productDetailsFormWatch

() => - ReactiveProductDetailsForm.of(this); + productDetailsFormWatch

() => + ReactiveProductDetailsForm.of(this); ProductDetailsForm? - productDetailsFormRead

() => - ReactiveProductDetailsForm.of(this, listen: false); + productDetailsFormRead

() => + ReactiveProductDetailsForm.of(this, listen: false); } class ProductDetailsFormBuilder

@@ -129,11 +132,15 @@ class ProductDetailsFormBuilder

final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - final Widget Function(BuildContext context, - ProductDetailsForm formModel, Widget? child) builder; + final Widget Function( + BuildContext context, + ProductDetailsForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, ProductDetailsForm formModel)? - initState; + initState; @override _ProductDetailsFormBuilderState createState() => @@ -149,7 +156,10 @@ class _ProductDetailsFormBuilderState

@override void initState() { _formModel = ProductDetailsForm( - ProductDetailsForm.formElements(widget.model), null, null); + ProductDetailsForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -227,11 +237,8 @@ final _logProductDetailsForm = Logger.detached('ProductDetailsForm'); class ProductDetailsForm

implements FormModel, ProductDetails> { - ProductDetailsForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + ProductDetailsForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String descriptionControlName = "description"; @@ -241,7 +248,7 @@ class ProductDetailsForm

final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -253,16 +260,16 @@ class ProductDetailsForm

String idControlPath() => pathBuilder(idControlName); - String? get _descriptionValue => descriptionControl.value; + String? get _descriptionValue => + containsDescription ? descriptionControl.value : null; - Id? get _idValue => idForm.model; + Id? get _idValue => containsId ? idForm.model : null; - String? get _descriptionRawValue => descriptionControl.value; + String? get _descriptionRawValue => + containsDescription ? descriptionControl.value : null; - Id? get _idRawValue => idForm.rawModel; + Id? get _idRawValue => containsId ? idForm.rawModel : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsDescription { try { form.control(descriptionControlPath()); @@ -272,8 +279,6 @@ class ProductDetailsForm

} } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsId { try { form.control(idControlPath()); @@ -291,12 +296,7 @@ class ProductDetailsForm

void get idFocus => form.focus(idControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void descriptionRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void descriptionRemove({bool updateParent = true, bool emitEvent = true}) { if (containsDescription) { final controlPath = path; if (controlPath == null) { @@ -319,12 +319,7 @@ class ProductDetailsForm

} } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void idRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void idRemove({bool updateParent = true, bool emitEvent = true}) { if (containsId) { final controlPath = path; if (controlPath == null) { @@ -352,8 +347,11 @@ class ProductDetailsForm

bool updateParent = true, bool emitEvent = true, }) { - descriptionControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + descriptionControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void idValueUpdate( @@ -361,8 +359,11 @@ class ProductDetailsForm

bool updateParent = true, bool emitEvent = true, }) { - idControl.updateValue(IdForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + idControl.updateValue( + IdForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void descriptionValuePatch( @@ -370,8 +371,11 @@ class ProductDetailsForm

bool updateParent = true, bool emitEvent = true, }) { - descriptionControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + descriptionControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void idValuePatch( @@ -379,8 +383,11 @@ class ProductDetailsForm

bool updateParent = true, bool emitEvent = true, }) { - idControl.updateValue(IdForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + idControl.updateValue( + IdForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void descriptionValueReset( @@ -389,14 +396,13 @@ class ProductDetailsForm

bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - descriptionControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => descriptionControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void idValueReset( Id? value, { @@ -404,11 +410,11 @@ class ProductDetailsForm

bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - idControl.reset( - value: IdForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => idControl.reset( + value: IdForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); FormControl get descriptionControl => form.control(descriptionControlPath()) as FormControl; @@ -447,10 +453,7 @@ class ProductDetailsForm

emitEvent: emitEvent, ); } else { - idControl.markAsEnabled( - updateParent: updateParent, - emitEvent: emitEvent, - ); + idControl.markAsEnabled(updateParent: updateParent, emitEvent: emitEvent); } } @@ -471,14 +474,13 @@ class ProductDetailsForm

@override ProductDetails get rawModel { return ProductDetails( - description: _descriptionRawValue, id: _idRawValue); + description: _descriptionRawValue, + id: _idRawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; @@ -486,7 +488,9 @@ class ProductDetailsForm

idForm.toggleDisabled(); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { idForm.toggleDisabled(); currentForm.controls.forEach((key, control) { @@ -545,9 +549,11 @@ class ProductDetailsForm

ProductDetails? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(ProductDetailsForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + ProductDetailsForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -565,17 +571,14 @@ class ProductDetailsForm

ProductDetails? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -627,32 +630,31 @@ class ProductDetailsForm

[path, pathItem].whereType().join("."); static FormGroup formElements

( - ProductDetails? productDetails) => - FormGroup({ - descriptionControlName: FormControl( - value: productDetails?.description, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - idControlName: IdForm.formElements(productDetails?.id) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + ProductDetails? productDetails, + ) => FormGroup( + { + descriptionControlName: FormControl( + value: productDetails?.description, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + idControlName: IdForm.formElements(productDetails?.id), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } final _logIdForm = Logger.detached('IdForm'); class IdForm

implements FormModel, Id> { - IdForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + IdForm(this.form, this.path, this._formModel) : initial = form.rawValue; static const String companyNameControlName = "companyName"; @@ -662,7 +664,7 @@ class IdForm

final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -674,16 +676,16 @@ class IdForm

String nameControlPath() => pathBuilder(nameControlName); - String? get _companyNameValue => companyNameControl.value; + String? get _companyNameValue => + containsCompanyName ? companyNameControl.value : null; - String? get _nameValue => nameControl.value; + String? get _nameValue => containsName ? nameControl.value : null; - String? get _companyNameRawValue => companyNameControl.value; + String? get _companyNameRawValue => + containsCompanyName ? companyNameControl.value : null; - String? get _nameRawValue => nameControl.value; + String? get _nameRawValue => containsName ? nameControl.value : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsCompanyName { try { form.control(companyNameControlPath()); @@ -693,8 +695,6 @@ class IdForm

} } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsName { try { form.control(nameControlPath()); @@ -712,12 +712,7 @@ class IdForm

void get nameFocus => form.focus(nameControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void companyNameRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void companyNameRemove({bool updateParent = true, bool emitEvent = true}) { if (containsCompanyName) { final controlPath = path; if (controlPath == null) { @@ -740,12 +735,7 @@ class IdForm

} } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void nameRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void nameRemove({bool updateParent = true, bool emitEvent = true}) { if (containsName) { final controlPath = path; if (controlPath == null) { @@ -773,8 +763,11 @@ class IdForm

bool updateParent = true, bool emitEvent = true, }) { - companyNameControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + companyNameControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void nameValueUpdate( @@ -782,8 +775,11 @@ class IdForm

bool updateParent = true, bool emitEvent = true, }) { - nameControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + nameControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void companyNameValuePatch( @@ -791,8 +787,11 @@ class IdForm

bool updateParent = true, bool emitEvent = true, }) { - companyNameControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + companyNameControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void nameValuePatch( @@ -800,8 +799,11 @@ class IdForm

bool updateParent = true, bool emitEvent = true, }) { - nameControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + nameControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void companyNameValueReset( @@ -810,14 +812,13 @@ class IdForm

bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - companyNameControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => companyNameControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void nameValueReset( String? value, { @@ -825,14 +826,13 @@ class IdForm

bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - nameControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => nameControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get companyNameControl => form.control(companyNameControlPath()) as FormControl; @@ -896,17 +896,16 @@ class IdForm

} @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { @@ -964,9 +963,11 @@ class IdForm

Id? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(IdForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + IdForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -984,17 +985,14 @@ class IdForm

Id? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -1046,33 +1044,39 @@ class IdForm

[path, pathItem].whereType().join("."); static FormGroup formElements

( - Id? id) => - FormGroup({ - companyNameControlName: FormControl( - value: id?.companyName, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - nameControlName: FormControl( - value: id?.name, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + Id? id, + ) => FormGroup( + { + companyNameControlName: FormControl( + value: id?.companyName, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + nameControlName: FormControl( + value: id?.name, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } class ReactiveProductDetailsFormArrayBuilder< - ReactiveProductDetailsFormArrayBuilderT, - P extends Product, - C extends Cart> extends StatelessWidget { + ReactiveProductDetailsFormArrayBuilderT, + P extends Product, + C extends Cart +> + extends StatelessWidget { const ReactiveProductDetailsFormArrayBuilder({ Key? key, this.control, @@ -1081,30 +1085,41 @@ class ReactiveProductDetailsFormArrayBuilder< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - ProductDetailsForm formModel)? control; + ProductDetailsForm formModel, + )? + control; - final Widget Function(BuildContext context, List itemList, - ProductDetailsForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + ProductDetailsForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveProductDetailsFormArrayBuilderT? item, - ProductDetailsForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveProductDetailsFormArrayBuilderT? item, + ProductDetailsForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -1118,21 +1133,13 @@ class ReactiveProductDetailsFormArrayBuilder< final itemBuilder = this.itemBuilder; return ReactiveFormArrayItemBuilder< - ReactiveProductDetailsFormArrayBuilderT>( + ReactiveProductDetailsFormArrayBuilderT + >( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1141,9 +1148,11 @@ class ReactiveProductDetailsFormArrayBuilder< } class ReactiveProductDetailsFormArrayBuilder2< - ReactiveProductDetailsFormArrayBuilderT, - P extends Product, - C extends Cart> extends StatelessWidget { + ReactiveProductDetailsFormArrayBuilderT, + P extends Product, + C extends Cart +> + extends StatelessWidget { const ReactiveProductDetailsFormArrayBuilder2({ Key? key, this.control, @@ -1152,36 +1161,47 @@ class ReactiveProductDetailsFormArrayBuilder2< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - ProductDetailsForm formModel)? control; + ProductDetailsForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - ProductDetailsForm formModel - }) params)? builder; + ({ + BuildContext context, + List itemList, + ProductDetailsForm formModel, + }) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveProductDetailsFormArrayBuilderT? item, - ProductDetailsForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveProductDetailsFormArrayBuilderT? item, + ProductDetailsForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -1195,27 +1215,22 @@ class ReactiveProductDetailsFormArrayBuilder2< final itemBuilder = this.itemBuilder; return ReactiveFormArrayItemBuilder< - ReactiveProductDetailsFormArrayBuilderT>( + ReactiveProductDetailsFormArrayBuilderT + >( formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1224,34 +1239,50 @@ class ReactiveProductDetailsFormArrayBuilder2< } class ReactiveProductDetailsFormFormGroupArrayBuilder< - ReactiveProductDetailsFormFormGroupArrayBuilderT, - P extends Product, - C extends Cart> extends StatelessWidget { + ReactiveProductDetailsFormFormGroupArrayBuilderT, + P extends Product, + C extends Cart +> + extends StatelessWidget { const ReactiveProductDetailsFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(ProductDetailsForm formModel)? + getExtended; - final ExtendedControl?>, - List>? extended; - - final ExtendedControl?>, - List> - Function(ProductDetailsForm formModel)? getExtended; - - final Widget Function(BuildContext context, List itemList, - ProductDetailsForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + ProductDetailsForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveProductDetailsFormFormGroupArrayBuilderT? item, - ProductDetailsForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveProductDetailsFormFormGroupArrayBuilderT? item, + ProductDetailsForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -1266,26 +1297,18 @@ class ReactiveProductDetailsFormFormGroupArrayBuilder< return StreamBuilder?>?>( stream: value.control.valueChanges, builder: (context, snapshot) { - final itemList = (value.value() ?? - []) - .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) - .values - .toList(); - - return builder?.call( - context, - itemList, - formModel, - ) ?? + final itemList = + (value.value() ?? + []) + .asMap() + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) + .values + .toList(); + + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); @@ -1294,16 +1317,17 @@ class ReactiveProductDetailsFormFormGroupArrayBuilder< class ReactiveIdFormConsumer

extends StatelessWidget { - const ReactiveIdFormConsumer({ - Key? key, - required this.builder, - this.child, - }) : super(key: key); + const ReactiveIdFormConsumer({Key? key, required this.builder, this.child}) + : super(key: key); final Widget? child; final Widget Function( - BuildContext context, IdForm formModel, Widget? child) builder; + BuildContext context, + IdForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -1323,11 +1347,7 @@ class IdFormInheritedStreamer

required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final IdForm form; } @@ -1360,8 +1380,10 @@ class ReactiveIdForm

?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - IdFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + IdFormInheritedStreamer + >(); return element == null ? null : (element.widget as IdFormInheritedStreamer).form; @@ -1409,7 +1431,11 @@ class IdFormBuilder

extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, IdForm formModel, Widget? child) builder; + BuildContext context, + IdForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, IdForm formModel)? initState; @@ -1425,8 +1451,11 @@ class _IdFormBuilderState

@override void initState() { - _formModel = - IdForm(IdForm.formElements(widget.model), null, null); + _formModel = IdForm( + IdForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -1500,8 +1529,12 @@ class _IdFormBuilderState

} } -class ReactiveIdFormArrayBuilder extends StatelessWidget { +class ReactiveIdFormArrayBuilder< + ReactiveIdFormArrayBuilderT, + P extends Product, + C extends Cart +> + extends StatelessWidget { const ReactiveIdFormArrayBuilder({ Key? key, this.control, @@ -1510,30 +1543,39 @@ class ReactiveIdFormArrayBuilder? formControl; final FormArray? Function( - IdForm formModel)? control; + IdForm formModel, + )? + control; final Widget Function( - BuildContext context, List itemList, IdForm formModel)? - builder; + BuildContext context, + List itemList, + IdForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveIdFormArrayBuilderT? item, - IdForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveIdFormArrayBuilderT? item, + IdForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function(FormControl control)? - controlFilter; + controlFilter; @override Widget build(BuildContext context) { @@ -1549,18 +1591,9 @@ class ReactiveIdFormArrayBuilder( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1568,8 +1601,12 @@ class ReactiveIdFormArrayBuilder extends StatelessWidget { +class ReactiveIdFormArrayBuilder2< + ReactiveIdFormArrayBuilderT, + P extends Product, + C extends Cart +> + extends StatelessWidget { const ReactiveIdFormArrayBuilder2({ Key? key, this.control, @@ -1578,35 +1615,41 @@ class ReactiveIdFormArrayBuilder2? formControl; final FormArray? Function( - IdForm formModel)? control; + IdForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - IdForm formModel - }) params)? builder; + ({BuildContext context, List itemList, IdForm formModel}) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveIdFormArrayBuilderT? item, - IdForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveIdFormArrayBuilderT? item, + IdForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function(FormControl control)? - controlFilter; + controlFilter; @override Widget build(BuildContext context) { @@ -1623,23 +1666,17 @@ class ReactiveIdFormArrayBuilder2 builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1647,34 +1684,51 @@ class ReactiveIdFormArrayBuilder2 extends StatelessWidget { +class ReactiveIdFormFormGroupArrayBuilder< + ReactiveIdFormFormGroupArrayBuilderT, + P extends Product, + C extends Cart +> + extends StatelessWidget { const ReactiveIdFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? extended; - - final ExtendedControl?>, - List> - Function(IdForm formModel)? getExtended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(IdForm formModel)? + getExtended; final Widget Function( - BuildContext context, List itemList, IdForm formModel)? - builder; + BuildContext context, + List itemList, + IdForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveIdFormFormGroupArrayBuilderT? item, - IdForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveIdFormFormGroupArrayBuilderT? item, + IdForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -1692,23 +1746,14 @@ class ReactiveIdFormFormGroupArrayBuilder[]) .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) .values .toList(); - return builder?.call( - context, - itemList, - formModel, - ) ?? + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/reactive_forms_generator/example/lib/docs/nested_generics/nested_generics_output.gform.dart b/packages/reactive_forms_generator/example/lib/docs/nested_generics/nested_generics_output.gform.dart index 7f3fa9c1..0fef5d7f 100644 --- a/packages/reactive_forms_generator/example/lib/docs/nested_generics/nested_generics_output.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/nested_generics/nested_generics_output.gform.dart @@ -19,8 +19,12 @@ class ReactiveProductDetailsOFormConsumer

final Widget? child; - final Widget Function(BuildContext context, - ProductDetailsOForm formModel, Widget? child) builder; + final Widget Function( + BuildContext context, + ProductDetailsOForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -40,11 +44,7 @@ class ProductDetailsOFormInheritedStreamer

required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final ProductDetailsOForm form; } @@ -74,12 +74,15 @@ class ReactiveProductDetailsOForm

if (listen) { return context .dependOnInheritedWidgetOfExactType< - ProductDetailsOFormInheritedStreamer>() + ProductDetailsOFormInheritedStreamer + >() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - ProductDetailsOFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + ProductDetailsOFormInheritedStreamer + >(); return element == null ? null : (element.widget as ProductDetailsOFormInheritedStreamer).form; @@ -101,12 +104,12 @@ class ReactiveProductDetailsOForm

extension ReactiveReactiveProductDetailsOFormExt on BuildContext { ProductDetailsOForm? - productDetailsOFormWatch

() => - ReactiveProductDetailsOForm.of(this); + productDetailsOFormWatch

() => + ReactiveProductDetailsOForm.of(this); ProductDetailsOForm? - productDetailsOFormRead

() => - ReactiveProductDetailsOForm.of(this, listen: false); + productDetailsOFormRead

() => + ReactiveProductDetailsOForm.of(this, listen: false); } class ProductDetailsOFormBuilder

@@ -129,11 +132,18 @@ class ProductDetailsOFormBuilder

final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - final Widget Function(BuildContext context, - ProductDetailsOForm formModel, Widget? child) builder; + final Widget Function( + BuildContext context, + ProductDetailsOForm formModel, + Widget? child, + ) + builder; final void Function( - BuildContext context, ProductDetailsOForm formModel)? initState; + BuildContext context, + ProductDetailsOForm formModel, + )? + initState; @override _ProductDetailsOFormBuilderState createState() => @@ -149,7 +159,10 @@ class _ProductDetailsOFormBuilderState

@override void initState() { _formModel = ProductDetailsOForm( - ProductDetailsOForm.formElements(widget.model), null, null); + ProductDetailsOForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -227,11 +240,8 @@ final _logProductDetailsOForm = Logger.detached('ProductDetailsOForm'); class ProductDetailsOForm

implements FormModel, ProductDetailsOOutput> { - ProductDetailsOForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + ProductDetailsOForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String descriptionControlName = "description"; @@ -241,7 +251,7 @@ class ProductDetailsOForm

final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -253,16 +263,16 @@ class ProductDetailsOForm

String idControlPath() => pathBuilder(idControlName); - String? get _descriptionValue => descriptionControl.value; + String? get _descriptionValue => + containsDescription ? descriptionControl.value : null; - IdOOutput? get _idValue => idForm.model; + IdOOutput? get _idValue => containsId ? idForm.model : null; - String? get _descriptionRawValue => descriptionControl.value; + String? get _descriptionRawValue => + containsDescription ? descriptionControl.value : null; - IdO? get _idRawValue => idForm.rawModel; + IdO? get _idRawValue => containsId ? idForm.rawModel : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsDescription { try { form.control(descriptionControlPath()); @@ -272,8 +282,6 @@ class ProductDetailsOForm

} } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsId { try { form.control(idControlPath()); @@ -291,12 +299,7 @@ class ProductDetailsOForm

void get idFocus => form.focus(idControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void descriptionRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void descriptionRemove({bool updateParent = true, bool emitEvent = true}) { if (containsDescription) { final controlPath = path; if (controlPath == null) { @@ -319,12 +322,7 @@ class ProductDetailsOForm

} } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void idRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void idRemove({bool updateParent = true, bool emitEvent = true}) { if (containsId) { final controlPath = path; if (controlPath == null) { @@ -352,8 +350,11 @@ class ProductDetailsOForm

bool updateParent = true, bool emitEvent = true, }) { - descriptionControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + descriptionControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void idValueUpdate( @@ -361,8 +362,11 @@ class ProductDetailsOForm

bool updateParent = true, bool emitEvent = true, }) { - idControl.updateValue(IdOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + idControl.updateValue( + IdOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void descriptionValuePatch( @@ -370,8 +374,11 @@ class ProductDetailsOForm

bool updateParent = true, bool emitEvent = true, }) { - descriptionControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + descriptionControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void idValuePatch( @@ -379,8 +386,11 @@ class ProductDetailsOForm

bool updateParent = true, bool emitEvent = true, }) { - idControl.updateValue(IdOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + idControl.updateValue( + IdOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void descriptionValueReset( @@ -389,14 +399,13 @@ class ProductDetailsOForm

bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - descriptionControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => descriptionControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void idValueReset( IdO? value, { @@ -404,11 +413,11 @@ class ProductDetailsOForm

bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - idControl.reset( - value: IdOForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => idControl.reset( + value: IdOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); FormControl get descriptionControl => form.control(descriptionControlPath()) as FormControl; @@ -447,10 +456,7 @@ class ProductDetailsOForm

emitEvent: emitEvent, ); } else { - idControl.markAsEnabled( - updateParent: updateParent, - emitEvent: emitEvent, - ); + idControl.markAsEnabled(updateParent: updateParent, emitEvent: emitEvent); } } @@ -467,20 +473,21 @@ class ProductDetailsOForm

); } return ProductDetailsOOutput( - description: _descriptionValue, id: _idValue); + description: _descriptionValue, + id: _idValue, + ); } @override ProductDetailsO get rawModel { return ProductDetailsO( - description: _descriptionRawValue, id: _idRawValue); + description: _descriptionRawValue, + id: _idRawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; @@ -488,7 +495,9 @@ class ProductDetailsOForm

idForm.toggleDisabled(); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { idForm.toggleDisabled(); currentForm.controls.forEach((key, control) { @@ -547,9 +556,11 @@ class ProductDetailsOForm

ProductDetailsO? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(ProductDetailsOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + ProductDetailsOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -567,17 +578,14 @@ class ProductDetailsOForm

ProductDetailsO? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -629,32 +637,31 @@ class ProductDetailsOForm

[path, pathItem].whereType().join("."); static FormGroup formElements

( - ProductDetailsO? productDetailsO) => - FormGroup({ - descriptionControlName: FormControl( - value: productDetailsO?.description, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - idControlName: IdOForm.formElements(productDetailsO?.id) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + ProductDetailsO? productDetailsO, + ) => FormGroup( + { + descriptionControlName: FormControl( + value: productDetailsO?.description, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + idControlName: IdOForm.formElements(productDetailsO?.id), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } final _logIdOForm = Logger.detached('IdOForm'); class IdOForm

implements FormModel, IdOOutput> { - IdOForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + IdOForm(this.form, this.path, this._formModel) : initial = form.rawValue; static const String companyNameControlName = "companyName"; @@ -664,7 +671,7 @@ class IdOForm

final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -676,16 +683,16 @@ class IdOForm

String nameControlPath() => pathBuilder(nameControlName); - String? get _companyNameValue => companyNameControl.value; + String? get _companyNameValue => + containsCompanyName ? companyNameControl.value : null; - String? get _nameValue => nameControl.value; + String? get _nameValue => containsName ? nameControl.value : null; - String? get _companyNameRawValue => companyNameControl.value; + String? get _companyNameRawValue => + containsCompanyName ? companyNameControl.value : null; - String? get _nameRawValue => nameControl.value; + String? get _nameRawValue => containsName ? nameControl.value : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsCompanyName { try { form.control(companyNameControlPath()); @@ -695,8 +702,6 @@ class IdOForm

} } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsName { try { form.control(nameControlPath()); @@ -714,12 +719,7 @@ class IdOForm

void get nameFocus => form.focus(nameControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void companyNameRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void companyNameRemove({bool updateParent = true, bool emitEvent = true}) { if (containsCompanyName) { final controlPath = path; if (controlPath == null) { @@ -742,12 +742,7 @@ class IdOForm

} } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void nameRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void nameRemove({bool updateParent = true, bool emitEvent = true}) { if (containsName) { final controlPath = path; if (controlPath == null) { @@ -775,8 +770,11 @@ class IdOForm

bool updateParent = true, bool emitEvent = true, }) { - companyNameControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + companyNameControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void nameValueUpdate( @@ -784,8 +782,11 @@ class IdOForm

bool updateParent = true, bool emitEvent = true, }) { - nameControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + nameControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void companyNameValuePatch( @@ -793,8 +794,11 @@ class IdOForm

bool updateParent = true, bool emitEvent = true, }) { - companyNameControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + companyNameControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void nameValuePatch( @@ -802,8 +806,11 @@ class IdOForm

bool updateParent = true, bool emitEvent = true, }) { - nameControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + nameControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void companyNameValueReset( @@ -812,14 +819,13 @@ class IdOForm

bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - companyNameControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => companyNameControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void nameValueReset( String? value, { @@ -827,14 +833,13 @@ class IdOForm

bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - nameControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => nameControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get companyNameControl => form.control(companyNameControlPath()) as FormControl; @@ -899,17 +904,16 @@ class IdOForm

} @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { @@ -967,9 +971,11 @@ class IdOForm

IdO? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(IdOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + IdOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -987,17 +993,14 @@ class IdOForm

IdO? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -1049,36 +1052,41 @@ class IdOForm

[path, pathItem].whereType().join("."); static FormGroup formElements

( - IdO? idO) => - FormGroup({ - companyNameControlName: FormControl( - value: idO?.companyName, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - nameControlName: FormControl( - value: idO?.name, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + IdO? idO, + ) => FormGroup( + { + companyNameControlName: FormControl( + value: idO?.companyName, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + nameControlName: FormControl( + value: idO?.name, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } @freezed @Rf(output: true) abstract class ProductDetailsOOutput

with _$ProductDetailsOOutput { - factory ProductDetailsOOutput( - {@RfControl() String? description, - @Rf(output: false) IdOOutput? id}) = _ProductDetailsOOutput; + factory ProductDetailsOOutput({ + @RfControl() String? description, + @Rf(output: false) IdOOutput? id, + }) = _ProductDetailsOOutput; ProductDetailsOOutput._(); } @@ -1087,16 +1095,19 @@ abstract class ProductDetailsOOutput

@RfGroup() abstract class IdOOutput

with _$IdOOutput { - factory IdOOutput( - {@RfControl() String? companyName, - @RfControl() String? name}) = _IdOOutput; + factory IdOOutput({ + @RfControl() String? companyName, + @RfControl() String? name, + }) = _IdOOutput; IdOOutput._(); } class ReactiveProductDetailsOFormArrayBuilder< - ReactiveProductDetailsOFormArrayBuilderT, - P extends Product, - C extends Cart> extends StatelessWidget { + ReactiveProductDetailsOFormArrayBuilderT, + P extends Product, + C extends Cart +> + extends StatelessWidget { const ReactiveProductDetailsOFormArrayBuilder({ Key? key, this.control, @@ -1105,30 +1116,41 @@ class ReactiveProductDetailsOFormArrayBuilder< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - ProductDetailsOForm formModel)? control; + ProductDetailsOForm formModel, + )? + control; - final Widget Function(BuildContext context, List itemList, - ProductDetailsOForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + ProductDetailsOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveProductDetailsOFormArrayBuilderT? item, - ProductDetailsOForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveProductDetailsOFormArrayBuilderT? item, + ProductDetailsOForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -1142,21 +1164,13 @@ class ReactiveProductDetailsOFormArrayBuilder< final itemBuilder = this.itemBuilder; return ReactiveFormArrayItemBuilder< - ReactiveProductDetailsOFormArrayBuilderT>( + ReactiveProductDetailsOFormArrayBuilderT + >( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1165,9 +1179,11 @@ class ReactiveProductDetailsOFormArrayBuilder< } class ReactiveProductDetailsOFormArrayBuilder2< - ReactiveProductDetailsOFormArrayBuilderT, - P extends Product, - C extends Cart> extends StatelessWidget { + ReactiveProductDetailsOFormArrayBuilderT, + P extends Product, + C extends Cart +> + extends StatelessWidget { const ReactiveProductDetailsOFormArrayBuilder2({ Key? key, this.control, @@ -1176,36 +1192,47 @@ class ReactiveProductDetailsOFormArrayBuilder2< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - ProductDetailsOForm formModel)? control; + ProductDetailsOForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - ProductDetailsOForm formModel - }) params)? builder; + ({ + BuildContext context, + List itemList, + ProductDetailsOForm formModel, + }) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveProductDetailsOFormArrayBuilderT? item, - ProductDetailsOForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveProductDetailsOFormArrayBuilderT? item, + ProductDetailsOForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -1219,27 +1246,22 @@ class ReactiveProductDetailsOFormArrayBuilder2< final itemBuilder = this.itemBuilder; return ReactiveFormArrayItemBuilder< - ReactiveProductDetailsOFormArrayBuilderT>( + ReactiveProductDetailsOFormArrayBuilderT + >( formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1248,34 +1270,50 @@ class ReactiveProductDetailsOFormArrayBuilder2< } class ReactiveProductDetailsOFormFormGroupArrayBuilder< - ReactiveProductDetailsOFormFormGroupArrayBuilderT, - P extends Product, - C extends Cart> extends StatelessWidget { + ReactiveProductDetailsOFormFormGroupArrayBuilderT, + P extends Product, + C extends Cart +> + extends StatelessWidget { const ReactiveProductDetailsOFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(ProductDetailsOForm formModel)? + getExtended; - final ExtendedControl?>, - List>? extended; - - final ExtendedControl?>, - List> - Function(ProductDetailsOForm formModel)? getExtended; - - final Widget Function(BuildContext context, List itemList, - ProductDetailsOForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + ProductDetailsOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveProductDetailsOFormFormGroupArrayBuilderT? item, - ProductDetailsOForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveProductDetailsOFormFormGroupArrayBuilderT? item, + ProductDetailsOForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -1290,26 +1328,18 @@ class ReactiveProductDetailsOFormFormGroupArrayBuilder< return StreamBuilder?>?>( stream: value.control.valueChanges, builder: (context, snapshot) { - final itemList = (value.value() ?? - []) - .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) - .values - .toList(); - - return builder?.call( - context, - itemList, - formModel, - ) ?? + final itemList = + (value.value() ?? + []) + .asMap() + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) + .values + .toList(); + + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); @@ -1318,16 +1348,17 @@ class ReactiveProductDetailsOFormFormGroupArrayBuilder< class ReactiveIdOFormConsumer

extends StatelessWidget { - const ReactiveIdOFormConsumer({ - Key? key, - required this.builder, - this.child, - }) : super(key: key); + const ReactiveIdOFormConsumer({Key? key, required this.builder, this.child}) + : super(key: key); final Widget? child; final Widget Function( - BuildContext context, IdOForm formModel, Widget? child) builder; + BuildContext context, + IdOForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -1347,11 +1378,7 @@ class IdOFormInheritedStreamer

required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final IdOForm form; } @@ -1384,8 +1411,10 @@ class ReactiveIdOForm

?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - IdOFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + IdOFormInheritedStreamer + >(); return element == null ? null : (element.widget as IdOFormInheritedStreamer).form; @@ -1433,7 +1462,11 @@ class IdOFormBuilder

extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, IdOForm formModel, Widget? child) builder; + BuildContext context, + IdOForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, IdOForm formModel)? initState; @@ -1449,8 +1482,11 @@ class _IdOFormBuilderState

@override void initState() { - _formModel = - IdOForm(IdOForm.formElements(widget.model), null, null); + _formModel = IdOForm( + IdOForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -1524,8 +1560,12 @@ class _IdOFormBuilderState

} } -class ReactiveIdOFormArrayBuilder extends StatelessWidget { +class ReactiveIdOFormArrayBuilder< + ReactiveIdOFormArrayBuilderT, + P extends Product, + C extends Cart +> + extends StatelessWidget { const ReactiveIdOFormArrayBuilder({ Key? key, this.control, @@ -1534,30 +1574,39 @@ class ReactiveIdOFormArrayBuilder? formControl; final FormArray? Function( - IdOForm formModel)? control; + IdOForm formModel, + )? + control; final Widget Function( - BuildContext context, List itemList, IdOForm formModel)? - builder; + BuildContext context, + List itemList, + IdOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveIdOFormArrayBuilderT? item, - IdOForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveIdOFormArrayBuilderT? item, + IdOForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function(FormControl control)? - controlFilter; + controlFilter; @override Widget build(BuildContext context) { @@ -1573,18 +1622,9 @@ class ReactiveIdOFormArrayBuilder( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1592,8 +1632,12 @@ class ReactiveIdOFormArrayBuilder extends StatelessWidget { +class ReactiveIdOFormArrayBuilder2< + ReactiveIdOFormArrayBuilderT, + P extends Product, + C extends Cart +> + extends StatelessWidget { const ReactiveIdOFormArrayBuilder2({ Key? key, this.control, @@ -1602,35 +1646,41 @@ class ReactiveIdOFormArrayBuilder2? formControl; final FormArray? Function( - IdOForm formModel)? control; + IdOForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - IdOForm formModel - }) params)? builder; + ({BuildContext context, List itemList, IdOForm formModel}) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveIdOFormArrayBuilderT? item, - IdOForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveIdOFormArrayBuilderT? item, + IdOForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function(FormControl control)? - controlFilter; + controlFilter; @override Widget build(BuildContext context) { @@ -1647,23 +1697,17 @@ class ReactiveIdOFormArrayBuilder2 builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1672,35 +1716,50 @@ class ReactiveIdOFormArrayBuilder2 extends StatelessWidget { + ReactiveIdOFormFormGroupArrayBuilderT, + P extends Product, + C extends Cart +> + extends StatelessWidget { const ReactiveIdOFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? extended; - - final ExtendedControl?>, - List> - Function(IdOForm formModel)? getExtended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(IdOForm formModel)? + getExtended; final Widget Function( - BuildContext context, List itemList, IdOForm formModel)? - builder; + BuildContext context, + List itemList, + IdOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveIdOFormFormGroupArrayBuilderT? item, - IdOForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveIdOFormFormGroupArrayBuilderT? item, + IdOForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -1718,23 +1777,14 @@ class ReactiveIdOFormFormGroupArrayBuilder< final itemList = (value.value() ?? []) .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) .values .toList(); - return builder?.call( - context, - itemList, - formModel, - ) ?? + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/reactive_forms_generator/example/lib/docs/profile/profile.gform.dart b/packages/reactive_forms_generator/example/lib/docs/profile/profile.gform.dart index 5c35b886..d281b721 100644 --- a/packages/reactive_forms_generator/example/lib/docs/profile/profile.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/profile/profile.gform.dart @@ -19,7 +19,11 @@ class ReactiveProfileFormConsumer extends StatelessWidget { final Widget? child; final Widget Function( - BuildContext context, ProfileForm formModel, Widget? child) builder; + BuildContext context, + ProfileForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -38,11 +42,7 @@ class ProfileFormInheritedStreamer extends InheritedStreamer { required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final ProfileForm form; } @@ -64,18 +64,17 @@ class ReactiveProfileForm extends StatelessWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - static ProfileForm? of( - BuildContext context, { - bool listen = true, - }) { + static ProfileForm? of(BuildContext context, {bool listen = true}) { if (listen) { return context .dependOnInheritedWidgetOfExactType() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - ProfileFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + ProfileFormInheritedStreamer + >(); return element == null ? null : (element.widget as ProfileFormInheritedStreamer).form; @@ -121,7 +120,11 @@ class ProfileFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, ProfileForm formModel, Widget? child) builder; + BuildContext context, + ProfileForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, ProfileForm formModel)? initState; @@ -136,8 +139,11 @@ class _ProfileFormBuilderState extends State { @override void initState() { - _formModel = - ProfileForm(ProfileForm.formElements(widget.model), null, null); + _formModel = ProfileForm( + ProfileForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -214,11 +220,7 @@ class _ProfileFormBuilderState extends State { final _logProfileForm = Logger.detached('ProfileForm'); class ProfileForm implements FormModel { - ProfileForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + ProfileForm(this.form, this.path, this._formModel) : initial = form.rawValue; static const String idControlName = "id"; @@ -244,7 +246,7 @@ class ProfileForm implements FormModel { final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -321,8 +323,6 @@ class ProfileForm implements FormModel { TimerSetting get _timerRawValue => timerForm.rawModel; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsId { try { form.control(idControlPath()); @@ -332,8 +332,6 @@ class ProfileForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsAnotherId { try { form.control(anotherIdControlPath()); @@ -343,8 +341,6 @@ class ProfileForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsName { try { form.control(nameControlPath()); @@ -354,8 +350,6 @@ class ProfileForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsChartingOrder { try { form.control(chartingOrderControlPath()); @@ -365,8 +359,6 @@ class ProfileForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsNumberingStandard { try { form.control(numberingStandardControlPath()); @@ -376,8 +368,6 @@ class ProfileForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsMeasurementType { try { form.control(measurementTypeControlPath()); @@ -387,8 +377,6 @@ class ProfileForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsAudioGuidance { try { form.control(audioGuidanceControlPath()); @@ -398,8 +386,6 @@ class ProfileForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsIncidenceFilter { try { form.control(incidenceFilterControlPath()); @@ -409,8 +395,6 @@ class ProfileForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsThreshold { try { form.control(thresholdControlPath()); @@ -420,8 +404,6 @@ class ProfileForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsTimer { try { form.control(timerControlPath()); @@ -479,8 +461,11 @@ class ProfileForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - idControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + idControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void anotherIdValueUpdate( @@ -488,8 +473,11 @@ class ProfileForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - anotherIdControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + anotherIdControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void nameValueUpdate( @@ -497,8 +485,11 @@ class ProfileForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - nameControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + nameControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void chartingOrderValueUpdate( @@ -506,8 +497,11 @@ class ProfileForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - chartingOrderControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + chartingOrderControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void numberingStandardValueUpdate( @@ -515,8 +509,11 @@ class ProfileForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - numberingStandardControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + numberingStandardControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void measurementTypeValueUpdate( @@ -524,8 +521,11 @@ class ProfileForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - measurementTypeControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + measurementTypeControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void audioGuidanceValueUpdate( @@ -533,8 +533,11 @@ class ProfileForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - audioGuidanceControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + audioGuidanceControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void incidenceFilterValueUpdate( @@ -543,9 +546,10 @@ class ProfileForm implements FormModel { bool emitEvent = true, }) { incidenceFilterControl.updateValue( - IncidenceFilterForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + IncidenceFilterForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void thresholdValueUpdate( @@ -554,9 +558,10 @@ class ProfileForm implements FormModel { bool emitEvent = true, }) { thresholdControl.updateValue( - ThresholdSettingForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + ThresholdSettingForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void timerValueUpdate( @@ -564,8 +569,11 @@ class ProfileForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - timerControl.updateValue(TimerSettingForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + timerControl.updateValue( + TimerSettingForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void idValuePatch( @@ -573,8 +581,11 @@ class ProfileForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - idControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + idControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void anotherIdValuePatch( @@ -582,8 +593,11 @@ class ProfileForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - anotherIdControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + anotherIdControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void nameValuePatch( @@ -591,8 +605,11 @@ class ProfileForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - nameControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + nameControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void chartingOrderValuePatch( @@ -600,8 +617,11 @@ class ProfileForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - chartingOrderControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + chartingOrderControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void numberingStandardValuePatch( @@ -609,8 +629,11 @@ class ProfileForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - numberingStandardControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + numberingStandardControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void measurementTypeValuePatch( @@ -618,8 +641,11 @@ class ProfileForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - measurementTypeControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + measurementTypeControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void audioGuidanceValuePatch( @@ -627,8 +653,11 @@ class ProfileForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - audioGuidanceControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + audioGuidanceControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void incidenceFilterValuePatch( @@ -637,9 +666,10 @@ class ProfileForm implements FormModel { bool emitEvent = true, }) { incidenceFilterControl.updateValue( - IncidenceFilterForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + IncidenceFilterForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void thresholdValuePatch( @@ -648,9 +678,10 @@ class ProfileForm implements FormModel { bool emitEvent = true, }) { thresholdControl.updateValue( - ThresholdSettingForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + ThresholdSettingForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void timerValuePatch( @@ -658,8 +689,11 @@ class ProfileForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - timerControl.updateValue(TimerSettingForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + timerControl.updateValue( + TimerSettingForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void idValueReset( @@ -668,14 +702,13 @@ class ProfileForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - idControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => idControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void anotherIdValueReset( String value, { @@ -683,14 +716,13 @@ class ProfileForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - anotherIdControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => anotherIdControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void nameValueReset( String value, { @@ -698,14 +730,13 @@ class ProfileForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - nameControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => nameControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void chartingOrderValueReset( ChartingOrderValue value, { @@ -713,14 +744,13 @@ class ProfileForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - chartingOrderControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => chartingOrderControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void numberingStandardValueReset( NumberingStandard value, { @@ -728,14 +758,13 @@ class ProfileForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - numberingStandardControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => numberingStandardControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void measurementTypeValueReset( MeasurementType value, { @@ -743,14 +772,13 @@ class ProfileForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - measurementTypeControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => measurementTypeControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void audioGuidanceValueReset( bool value, { @@ -758,14 +786,13 @@ class ProfileForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - audioGuidanceControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => audioGuidanceControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void incidenceFilterValueReset( IncidenceFilter value, { @@ -773,11 +800,11 @@ class ProfileForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - incidenceFilterControl.reset( - value: IncidenceFilterForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => incidenceFilterControl.reset( + value: IncidenceFilterForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); void thresholdValueReset( ThresholdSetting value, { @@ -785,11 +812,11 @@ class ProfileForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - thresholdControl.reset( - value: ThresholdSettingForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => thresholdControl.reset( + value: ThresholdSettingForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); void timerValueReset( TimerSetting value, { @@ -797,11 +824,11 @@ class ProfileForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - timerControl.reset( - value: TimerSettingForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => timerControl.reset( + value: TimerSettingForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); FormControl get idControl => form.control(idControlPath()) as FormControl; @@ -836,7 +863,10 @@ class ProfileForm implements FormModel { FormGroup get timerControl => form.control(timerControlPath()) as FormGroup; IncidenceFilterForm get incidenceFilterForm => IncidenceFilterForm( - form, pathBuilder('incidenceFilter'), _formModel ?? this); + form, + pathBuilder('incidenceFilter'), + _formModel ?? this, + ); ThresholdSettingForm get thresholdForm => ThresholdSettingForm(form, pathBuilder('threshold'), _formModel ?? this); @@ -855,10 +885,7 @@ class ProfileForm implements FormModel { emitEvent: emitEvent, ); } else { - idControl.markAsEnabled( - updateParent: updateParent, - emitEvent: emitEvent, - ); + idControl.markAsEnabled(updateParent: updateParent, emitEvent: emitEvent); } } @@ -1035,37 +1062,38 @@ class ProfileForm implements FormModel { StackTrace.current, ); } - return Profile(_idValue, - anotherId: _anotherIdValue, - name: _nameValue, - chartingOrder: _chartingOrderValue, - numberingStandard: _numberingStandardValue, - incidenceFilter: _incidenceFilterValue, - measurementType: _measurementTypeValue, - threshold: _thresholdValue, - timer: _timerValue, - audioGuidance: _audioGuidanceValue); + return Profile( + _idValue, + anotherId: _anotherIdValue, + name: _nameValue, + chartingOrder: _chartingOrderValue, + numberingStandard: _numberingStandardValue, + incidenceFilter: _incidenceFilterValue, + measurementType: _measurementTypeValue, + threshold: _thresholdValue, + timer: _timerValue, + audioGuidance: _audioGuidanceValue, + ); } @override Profile get rawModel { - return Profile(_idRawValue, - anotherId: _anotherIdRawValue, - name: _nameRawValue, - chartingOrder: _chartingOrderRawValue, - numberingStandard: _numberingStandardRawValue, - incidenceFilter: _incidenceFilterRawValue, - measurementType: _measurementTypeRawValue, - threshold: _thresholdRawValue, - timer: _timerRawValue, - audioGuidance: _audioGuidanceRawValue); + return Profile( + _idRawValue, + anotherId: _anotherIdRawValue, + name: _nameRawValue, + chartingOrder: _chartingOrderRawValue, + numberingStandard: _numberingStandardRawValue, + incidenceFilter: _incidenceFilterRawValue, + measurementType: _measurementTypeRawValue, + threshold: _thresholdRawValue, + timer: _timerRawValue, + audioGuidance: _audioGuidanceRawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; @@ -1075,7 +1103,9 @@ class ProfileForm implements FormModel { thresholdForm.toggleDisabled(); timerForm.toggleDisabled(); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { incidenceFilterForm.toggleDisabled(); thresholdForm.toggleDisabled(); @@ -1136,9 +1166,11 @@ class ProfileForm implements FormModel { Profile? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(ProfileForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + ProfileForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -1156,17 +1188,14 @@ class ProfileForm implements FormModel { Profile? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -1217,77 +1246,85 @@ class ProfileForm implements FormModel { String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(Profile? profile) => FormGroup({ - idControlName: FormControl( - value: profile?.id, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - anotherIdControlName: FormControl( - value: profile?.anotherId, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - nameControlName: FormControl( - value: profile?.name, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - chartingOrderControlName: FormControl( - value: profile?.chartingOrder, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - numberingStandardControlName: FormControl( - value: profile?.numberingStandard, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - measurementTypeControlName: FormControl( - value: profile?.measurementType, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - audioGuidanceControlName: FormControl( - value: profile?.audioGuidance, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - incidenceFilterControlName: - IncidenceFilterForm.formElements(profile?.incidenceFilter), - thresholdControlName: - ThresholdSettingForm.formElements(profile?.threshold), - timerControlName: TimerSettingForm.formElements(profile?.timer) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(Profile? profile) => FormGroup( + { + idControlName: FormControl( + value: profile?.id, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + anotherIdControlName: FormControl( + value: profile?.anotherId, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + nameControlName: FormControl( + value: profile?.name, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + chartingOrderControlName: FormControl( + value: profile?.chartingOrder, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + numberingStandardControlName: FormControl( + value: profile?.numberingStandard, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + measurementTypeControlName: FormControl( + value: profile?.measurementType, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + audioGuidanceControlName: FormControl( + value: profile?.audioGuidance, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + incidenceFilterControlName: IncidenceFilterForm.formElements( + profile?.incidenceFilter, + ), + thresholdControlName: ThresholdSettingForm.formElements( + profile?.threshold, + ), + timerControlName: TimerSettingForm.formElements(profile?.timer), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } final _logIncidenceFilterForm = Logger.detached('IncidenceFilterForm'); class IncidenceFilterForm implements FormModel { - IncidenceFilterForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + IncidenceFilterForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String isMobilityEnabledControlName = "isMobilityEnabled"; @@ -1305,7 +1342,7 @@ class IncidenceFilterForm final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -1358,8 +1395,6 @@ class IncidenceFilterForm bool get _isPlaqueEnabledRawValue => isPlaqueEnabledControl.value ?? true; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsIsMobilityEnabled { try { form.control(isMobilityEnabledControlPath()); @@ -1369,8 +1404,6 @@ class IncidenceFilterForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsIsFurcationEnabled { try { form.control(isFurcationEnabledControlPath()); @@ -1380,8 +1413,6 @@ class IncidenceFilterForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsIsBleedingEnabled { try { form.control(isBleedingEnabledControlPath()); @@ -1391,8 +1422,6 @@ class IncidenceFilterForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsIsSuppurationEnabled { try { form.control(isSuppurationEnabledControlPath()); @@ -1402,8 +1431,6 @@ class IncidenceFilterForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsIsCalculusEnabled { try { form.control(isCalculusEnabledControlPath()); @@ -1413,8 +1440,6 @@ class IncidenceFilterForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsIsPlaqueEnabled { try { form.control(isPlaqueEnabledControlPath()); @@ -1461,8 +1486,11 @@ class IncidenceFilterForm bool updateParent = true, bool emitEvent = true, }) { - isMobilityEnabledControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + isMobilityEnabledControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void isFurcationEnabledValueUpdate( @@ -1470,8 +1498,11 @@ class IncidenceFilterForm bool updateParent = true, bool emitEvent = true, }) { - isFurcationEnabledControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + isFurcationEnabledControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void isBleedingEnabledValueUpdate( @@ -1479,8 +1510,11 @@ class IncidenceFilterForm bool updateParent = true, bool emitEvent = true, }) { - isBleedingEnabledControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + isBleedingEnabledControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void isSuppurationEnabledValueUpdate( @@ -1488,8 +1522,11 @@ class IncidenceFilterForm bool updateParent = true, bool emitEvent = true, }) { - isSuppurationEnabledControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + isSuppurationEnabledControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void isCalculusEnabledValueUpdate( @@ -1497,8 +1534,11 @@ class IncidenceFilterForm bool updateParent = true, bool emitEvent = true, }) { - isCalculusEnabledControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + isCalculusEnabledControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void isPlaqueEnabledValueUpdate( @@ -1506,8 +1546,11 @@ class IncidenceFilterForm bool updateParent = true, bool emitEvent = true, }) { - isPlaqueEnabledControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + isPlaqueEnabledControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void isMobilityEnabledValuePatch( @@ -1515,8 +1558,11 @@ class IncidenceFilterForm bool updateParent = true, bool emitEvent = true, }) { - isMobilityEnabledControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + isMobilityEnabledControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void isFurcationEnabledValuePatch( @@ -1524,8 +1570,11 @@ class IncidenceFilterForm bool updateParent = true, bool emitEvent = true, }) { - isFurcationEnabledControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + isFurcationEnabledControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void isBleedingEnabledValuePatch( @@ -1533,8 +1582,11 @@ class IncidenceFilterForm bool updateParent = true, bool emitEvent = true, }) { - isBleedingEnabledControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + isBleedingEnabledControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void isSuppurationEnabledValuePatch( @@ -1542,8 +1594,11 @@ class IncidenceFilterForm bool updateParent = true, bool emitEvent = true, }) { - isSuppurationEnabledControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + isSuppurationEnabledControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void isCalculusEnabledValuePatch( @@ -1551,8 +1606,11 @@ class IncidenceFilterForm bool updateParent = true, bool emitEvent = true, }) { - isCalculusEnabledControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + isCalculusEnabledControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void isPlaqueEnabledValuePatch( @@ -1560,8 +1618,11 @@ class IncidenceFilterForm bool updateParent = true, bool emitEvent = true, }) { - isPlaqueEnabledControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + isPlaqueEnabledControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void isMobilityEnabledValueReset( @@ -1570,14 +1631,13 @@ class IncidenceFilterForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - isMobilityEnabledControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => isMobilityEnabledControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void isFurcationEnabledValueReset( bool value, { @@ -1585,14 +1645,13 @@ class IncidenceFilterForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - isFurcationEnabledControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => isFurcationEnabledControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void isBleedingEnabledValueReset( bool value, { @@ -1600,14 +1659,13 @@ class IncidenceFilterForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - isBleedingEnabledControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => isBleedingEnabledControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void isSuppurationEnabledValueReset( bool value, { @@ -1615,14 +1673,13 @@ class IncidenceFilterForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - isSuppurationEnabledControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => isSuppurationEnabledControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void isCalculusEnabledValueReset( bool value, { @@ -1630,14 +1687,13 @@ class IncidenceFilterForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - isCalculusEnabledControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => isCalculusEnabledControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void isPlaqueEnabledValueReset( bool value, { @@ -1645,14 +1701,13 @@ class IncidenceFilterForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - isPlaqueEnabledControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => isPlaqueEnabledControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get isMobilityEnabledControl => form.control(isMobilityEnabledControlPath()) as FormControl; @@ -1792,37 +1847,38 @@ class IncidenceFilterForm ); } return IncidenceFilter( - isMobilityEnabled: _isMobilityEnabledValue, - isFurcationEnabled: _isFurcationEnabledValue, - isBleedingEnabled: _isBleedingEnabledValue, - isSuppurationEnabled: _isSuppurationEnabledValue, - isCalculusEnabled: _isCalculusEnabledValue, - isPlaqueEnabled: _isPlaqueEnabledValue); + isMobilityEnabled: _isMobilityEnabledValue, + isFurcationEnabled: _isFurcationEnabledValue, + isBleedingEnabled: _isBleedingEnabledValue, + isSuppurationEnabled: _isSuppurationEnabledValue, + isCalculusEnabled: _isCalculusEnabledValue, + isPlaqueEnabled: _isPlaqueEnabledValue, + ); } @override IncidenceFilter get rawModel { return IncidenceFilter( - isMobilityEnabled: _isMobilityEnabledRawValue, - isFurcationEnabled: _isFurcationEnabledRawValue, - isBleedingEnabled: _isBleedingEnabledRawValue, - isSuppurationEnabled: _isSuppurationEnabledRawValue, - isCalculusEnabled: _isCalculusEnabledRawValue, - isPlaqueEnabled: _isPlaqueEnabledRawValue); + isMobilityEnabled: _isMobilityEnabledRawValue, + isFurcationEnabled: _isFurcationEnabledRawValue, + isBleedingEnabled: _isBleedingEnabledRawValue, + isSuppurationEnabled: _isSuppurationEnabledRawValue, + isCalculusEnabled: _isCalculusEnabledRawValue, + isPlaqueEnabled: _isPlaqueEnabledRawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { @@ -1880,9 +1936,11 @@ class IncidenceFilterForm IncidenceFilter? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(IncidenceFilterForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + IncidenceFilterForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -1900,17 +1958,14 @@ class IncidenceFilterForm IncidenceFilter? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -1961,65 +2016,70 @@ class IncidenceFilterForm String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(IncidenceFilter? incidenceFilter) => FormGroup({ - isMobilityEnabledControlName: FormControl( - value: incidenceFilter?.isMobilityEnabled, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - isFurcationEnabledControlName: FormControl( - value: incidenceFilter?.isFurcationEnabled, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - isBleedingEnabledControlName: FormControl( - value: incidenceFilter?.isBleedingEnabled, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - isSuppurationEnabledControlName: FormControl( - value: incidenceFilter?.isSuppurationEnabled, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - isCalculusEnabledControlName: FormControl( - value: incidenceFilter?.isCalculusEnabled, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - isPlaqueEnabledControlName: FormControl( - value: incidenceFilter?.isPlaqueEnabled, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(IncidenceFilter? incidenceFilter) => FormGroup( + { + isMobilityEnabledControlName: FormControl( + value: incidenceFilter?.isMobilityEnabled, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + isFurcationEnabledControlName: FormControl( + value: incidenceFilter?.isFurcationEnabled, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + isBleedingEnabledControlName: FormControl( + value: incidenceFilter?.isBleedingEnabled, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + isSuppurationEnabledControlName: FormControl( + value: incidenceFilter?.isSuppurationEnabled, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + isCalculusEnabledControlName: FormControl( + value: incidenceFilter?.isCalculusEnabled, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + isPlaqueEnabledControlName: FormControl( + value: incidenceFilter?.isPlaqueEnabled, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } final _logThresholdSettingForm = Logger.detached('ThresholdSettingForm'); class ThresholdSettingForm implements FormModel { - ThresholdSettingForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + ThresholdSettingForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String isEnabledControlName = "isEnabled"; @@ -2029,7 +2089,7 @@ class ThresholdSettingForm final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -2049,8 +2109,6 @@ class ThresholdSettingForm int get _valueRawValue => valueControl.value ?? 2; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsIsEnabled { try { form.control(isEnabledControlPath()); @@ -2060,8 +2118,6 @@ class ThresholdSettingForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsValue { try { form.control(valueControlPath()); @@ -2084,8 +2140,11 @@ class ThresholdSettingForm bool updateParent = true, bool emitEvent = true, }) { - isEnabledControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + isEnabledControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void valueValueUpdate( @@ -2093,8 +2152,11 @@ class ThresholdSettingForm bool updateParent = true, bool emitEvent = true, }) { - valueControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + valueControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void isEnabledValuePatch( @@ -2102,8 +2164,11 @@ class ThresholdSettingForm bool updateParent = true, bool emitEvent = true, }) { - isEnabledControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + isEnabledControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void valueValuePatch( @@ -2111,8 +2176,11 @@ class ThresholdSettingForm bool updateParent = true, bool emitEvent = true, }) { - valueControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + valueControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void isEnabledValueReset( @@ -2121,14 +2189,13 @@ class ThresholdSettingForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - isEnabledControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => isEnabledControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void valueValueReset( int value, { @@ -2136,14 +2203,13 @@ class ThresholdSettingForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - valueControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => valueControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get isEnabledControl => form.control(isEnabledControlPath()) as FormControl; @@ -2204,21 +2270,22 @@ class ThresholdSettingForm @override ThresholdSetting get rawModel { return ThresholdSetting( - isEnabled: _isEnabledRawValue, value: _valueRawValue); + isEnabled: _isEnabledRawValue, + value: _valueRawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { @@ -2276,9 +2343,11 @@ class ThresholdSettingForm ThresholdSetting? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(ThresholdSettingForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + ThresholdSettingForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -2296,17 +2365,14 @@ class ThresholdSettingForm ThresholdSetting? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -2358,36 +2424,37 @@ class ThresholdSettingForm [path, pathItem].whereType().join("."); static FormGroup formElements(ThresholdSetting? thresholdSetting) => - FormGroup({ - isEnabledControlName: FormControl( + FormGroup( + { + isEnabledControlName: FormControl( value: thresholdSetting?.isEnabled, validators: [], asyncValidators: [], asyncValidatorsDebounceTime: 250, disabled: false, - touched: false), - valueControlName: FormControl( + touched: false, + ), + valueControlName: FormControl( value: thresholdSetting?.value, validators: [], asyncValidators: [], asyncValidatorsDebounceTime: 250, disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } final _logTimerSettingForm = Logger.detached('TimerSettingForm'); class TimerSettingForm implements FormModel { - TimerSettingForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + TimerSettingForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String isEnabledControlName = "isEnabled"; @@ -2397,7 +2464,7 @@ class TimerSettingForm implements FormModel { final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -2417,8 +2484,6 @@ class TimerSettingForm implements FormModel { int get _valueRawValue => valueControl.value ?? 5; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsIsEnabled { try { form.control(isEnabledControlPath()); @@ -2428,8 +2493,6 @@ class TimerSettingForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsValue { try { form.control(valueControlPath()); @@ -2452,8 +2515,11 @@ class TimerSettingForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - isEnabledControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + isEnabledControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void valueValueUpdate( @@ -2461,8 +2527,11 @@ class TimerSettingForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - valueControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + valueControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void isEnabledValuePatch( @@ -2470,8 +2539,11 @@ class TimerSettingForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - isEnabledControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + isEnabledControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void valueValuePatch( @@ -2479,8 +2551,11 @@ class TimerSettingForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - valueControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + valueControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void isEnabledValueReset( @@ -2489,14 +2564,13 @@ class TimerSettingForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - isEnabledControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => isEnabledControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void valueValueReset( int value, { @@ -2504,14 +2578,13 @@ class TimerSettingForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - valueControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => valueControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get isEnabledControl => form.control(isEnabledControlPath()) as FormControl; @@ -2575,17 +2648,16 @@ class TimerSettingForm implements FormModel { } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { @@ -2643,9 +2715,11 @@ class TimerSettingForm implements FormModel { TimerSetting? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(TimerSettingForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + TimerSettingForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -2663,17 +2737,14 @@ class TimerSettingForm implements FormModel { TimerSetting? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -2724,26 +2795,30 @@ class TimerSettingForm implements FormModel { String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(TimerSetting? timerSetting) => FormGroup({ - isEnabledControlName: FormControl( - value: timerSetting?.isEnabled, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - valueControlName: FormControl( - value: timerSetting?.value, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(TimerSetting? timerSetting) => FormGroup( + { + isEnabledControlName: FormControl( + value: timerSetting?.isEnabled, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + valueControlName: FormControl( + value: timerSetting?.value, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } class ReactiveProfileFormArrayBuilder @@ -2756,30 +2831,39 @@ class ReactiveProfileFormArrayBuilder required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - ProfileForm formModel)? control; + ProfileForm formModel, + )? + control; final Widget Function( - BuildContext context, List itemList, ProfileForm formModel)? - builder; + BuildContext context, + List itemList, + ProfileForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveProfileFormArrayBuilderT? item, - ProfileForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveProfileFormArrayBuilderT? item, + ProfileForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function(FormControl control)? - controlFilter; + controlFilter; @override Widget build(BuildContext context) { @@ -2795,18 +2879,9 @@ class ReactiveProfileFormArrayBuilder return ReactiveFormArrayItemBuilder( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -2824,35 +2899,41 @@ class ReactiveProfileFormArrayBuilder2 required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - ProfileForm formModel)? control; + ProfileForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - ProfileForm formModel - }) params)? builder; + ({BuildContext context, List itemList, ProfileForm formModel}) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveProfileFormArrayBuilderT? item, - ProfileForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveProfileFormArrayBuilderT? item, + ProfileForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function(FormControl control)? - controlFilter; + controlFilter; @override Widget build(BuildContext context) { @@ -2869,23 +2950,17 @@ class ReactiveProfileFormArrayBuilder2 formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -2894,33 +2969,48 @@ class ReactiveProfileFormArrayBuilder2 } class ReactiveProfileFormFormGroupArrayBuilder< - ReactiveProfileFormFormGroupArrayBuilderT> extends StatelessWidget { + ReactiveProfileFormFormGroupArrayBuilderT +> + extends StatelessWidget { const ReactiveProfileFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? extended; - - final ExtendedControl?>, - List> - Function(ProfileForm formModel)? getExtended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(ProfileForm formModel)? + getExtended; final Widget Function( - BuildContext context, List itemList, ProfileForm formModel)? - builder; + BuildContext context, + List itemList, + ProfileForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveProfileFormFormGroupArrayBuilderT? item, - ProfileForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveProfileFormFormGroupArrayBuilderT? item, + ProfileForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -2938,23 +3028,14 @@ class ReactiveProfileFormFormGroupArrayBuilder< final itemList = (value.value() ?? []) .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) .values .toList(); - return builder?.call( - context, - itemList, - formModel, - ) ?? + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/reactive_forms_generator/example/lib/docs/profile/profile_output.gform.dart b/packages/reactive_forms_generator/example/lib/docs/profile/profile_output.gform.dart index 571b230a..00d4c835 100644 --- a/packages/reactive_forms_generator/example/lib/docs/profile/profile_output.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/profile/profile_output.gform.dart @@ -19,7 +19,11 @@ class ReactiveProfileOFormConsumer extends StatelessWidget { final Widget? child; final Widget Function( - BuildContext context, ProfileOForm formModel, Widget? child) builder; + BuildContext context, + ProfileOForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -38,11 +42,7 @@ class ProfileOFormInheritedStreamer extends InheritedStreamer { required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final ProfileOForm form; } @@ -64,18 +64,17 @@ class ReactiveProfileOForm extends StatelessWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - static ProfileOForm? of( - BuildContext context, { - bool listen = true, - }) { + static ProfileOForm? of(BuildContext context, {bool listen = true}) { if (listen) { return context .dependOnInheritedWidgetOfExactType() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - ProfileOFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + ProfileOFormInheritedStreamer + >(); return element == null ? null : (element.widget as ProfileOFormInheritedStreamer).form; @@ -122,7 +121,11 @@ class ProfileOFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, ProfileOForm formModel, Widget? child) builder; + BuildContext context, + ProfileOForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, ProfileOForm formModel)? initState; @@ -137,8 +140,11 @@ class _ProfileOFormBuilderState extends State { @override void initState() { - _formModel = - ProfileOForm(ProfileOForm.formElements(widget.model), null, null); + _formModel = ProfileOForm( + ProfileOForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -215,11 +221,7 @@ class _ProfileOFormBuilderState extends State { final _logProfileOForm = Logger.detached('ProfileOForm'); class ProfileOForm implements FormModel { - ProfileOForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + ProfileOForm(this.form, this.path, this._formModel) : initial = form.rawValue; static const String idControlName = "id"; @@ -245,7 +247,7 @@ class ProfileOForm implements FormModel { final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -322,8 +324,6 @@ class ProfileOForm implements FormModel { TimerSettingO get _timerRawValue => timerForm.rawModel; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsId { try { form.control(idControlPath()); @@ -333,8 +333,6 @@ class ProfileOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsAnotherId { try { form.control(anotherIdControlPath()); @@ -344,8 +342,6 @@ class ProfileOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsName { try { form.control(nameControlPath()); @@ -355,8 +351,6 @@ class ProfileOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsChartingOrder { try { form.control(chartingOrderControlPath()); @@ -366,8 +360,6 @@ class ProfileOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsNumberingStandard { try { form.control(numberingStandardControlPath()); @@ -377,8 +369,6 @@ class ProfileOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsMeasurementType { try { form.control(measurementTypeControlPath()); @@ -388,8 +378,6 @@ class ProfileOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsAudioGuidance { try { form.control(audioGuidanceControlPath()); @@ -399,8 +387,6 @@ class ProfileOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsIncidenceFilter { try { form.control(incidenceFilterControlPath()); @@ -410,8 +396,6 @@ class ProfileOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsThreshold { try { form.control(thresholdControlPath()); @@ -421,8 +405,6 @@ class ProfileOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsTimer { try { form.control(timerControlPath()); @@ -480,8 +462,11 @@ class ProfileOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - idControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + idControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void anotherIdValueUpdate( @@ -489,8 +474,11 @@ class ProfileOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - anotherIdControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + anotherIdControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void nameValueUpdate( @@ -498,8 +486,11 @@ class ProfileOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - nameControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + nameControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void chartingOrderValueUpdate( @@ -507,8 +498,11 @@ class ProfileOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - chartingOrderControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + chartingOrderControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void numberingStandardValueUpdate( @@ -516,8 +510,11 @@ class ProfileOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - numberingStandardControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + numberingStandardControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void measurementTypeValueUpdate( @@ -525,8 +522,11 @@ class ProfileOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - measurementTypeControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + measurementTypeControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void audioGuidanceValueUpdate( @@ -534,8 +534,11 @@ class ProfileOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - audioGuidanceControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + audioGuidanceControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void incidenceFilterValueUpdate( @@ -544,9 +547,10 @@ class ProfileOForm implements FormModel { bool emitEvent = true, }) { incidenceFilterControl.updateValue( - IncidenceFilterOForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + IncidenceFilterOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void thresholdValueUpdate( @@ -555,9 +559,10 @@ class ProfileOForm implements FormModel { bool emitEvent = true, }) { thresholdControl.updateValue( - ThresholdSettingOForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + ThresholdSettingOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void timerValueUpdate( @@ -565,8 +570,11 @@ class ProfileOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - timerControl.updateValue(TimerSettingOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + timerControl.updateValue( + TimerSettingOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void idValuePatch( @@ -574,8 +582,11 @@ class ProfileOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - idControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + idControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void anotherIdValuePatch( @@ -583,8 +594,11 @@ class ProfileOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - anotherIdControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + anotherIdControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void nameValuePatch( @@ -592,8 +606,11 @@ class ProfileOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - nameControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + nameControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void chartingOrderValuePatch( @@ -601,8 +618,11 @@ class ProfileOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - chartingOrderControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + chartingOrderControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void numberingStandardValuePatch( @@ -610,8 +630,11 @@ class ProfileOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - numberingStandardControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + numberingStandardControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void measurementTypeValuePatch( @@ -619,8 +642,11 @@ class ProfileOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - measurementTypeControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + measurementTypeControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void audioGuidanceValuePatch( @@ -628,8 +654,11 @@ class ProfileOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - audioGuidanceControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + audioGuidanceControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void incidenceFilterValuePatch( @@ -638,9 +667,10 @@ class ProfileOForm implements FormModel { bool emitEvent = true, }) { incidenceFilterControl.updateValue( - IncidenceFilterOForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + IncidenceFilterOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void thresholdValuePatch( @@ -649,9 +679,10 @@ class ProfileOForm implements FormModel { bool emitEvent = true, }) { thresholdControl.updateValue( - ThresholdSettingOForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + ThresholdSettingOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void timerValuePatch( @@ -659,8 +690,11 @@ class ProfileOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - timerControl.updateValue(TimerSettingOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + timerControl.updateValue( + TimerSettingOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void idValueReset( @@ -669,14 +703,13 @@ class ProfileOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - idControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => idControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void anotherIdValueReset( String value, { @@ -684,14 +717,13 @@ class ProfileOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - anotherIdControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => anotherIdControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void nameValueReset( String value, { @@ -699,14 +731,13 @@ class ProfileOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - nameControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => nameControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void chartingOrderValueReset( ChartingOrderValue value, { @@ -714,14 +745,13 @@ class ProfileOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - chartingOrderControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => chartingOrderControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void numberingStandardValueReset( NumberingStandard value, { @@ -729,14 +759,13 @@ class ProfileOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - numberingStandardControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => numberingStandardControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void measurementTypeValueReset( MeasurementType value, { @@ -744,14 +773,13 @@ class ProfileOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - measurementTypeControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => measurementTypeControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void audioGuidanceValueReset( bool value, { @@ -759,14 +787,13 @@ class ProfileOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - audioGuidanceControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => audioGuidanceControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void incidenceFilterValueReset( IncidenceFilterO value, { @@ -774,11 +801,11 @@ class ProfileOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - incidenceFilterControl.reset( - value: IncidenceFilterOForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => incidenceFilterControl.reset( + value: IncidenceFilterOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); void thresholdValueReset( ThresholdSettingO value, { @@ -786,11 +813,11 @@ class ProfileOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - thresholdControl.reset( - value: ThresholdSettingOForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => thresholdControl.reset( + value: ThresholdSettingOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); void timerValueReset( TimerSettingO value, { @@ -798,11 +825,11 @@ class ProfileOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - timerControl.reset( - value: TimerSettingOForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => timerControl.reset( + value: TimerSettingOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); FormControl get idControl => form.control(idControlPath()) as FormControl; @@ -837,7 +864,10 @@ class ProfileOForm implements FormModel { FormGroup get timerControl => form.control(timerControlPath()) as FormGroup; IncidenceFilterOForm get incidenceFilterForm => IncidenceFilterOForm( - form, pathBuilder('incidenceFilter'), _formModel ?? this); + form, + pathBuilder('incidenceFilter'), + _formModel ?? this, + ); ThresholdSettingOForm get thresholdForm => ThresholdSettingOForm(form, pathBuilder('threshold'), _formModel ?? this); @@ -856,10 +886,7 @@ class ProfileOForm implements FormModel { emitEvent: emitEvent, ); } else { - idControl.markAsEnabled( - updateParent: updateParent, - emitEvent: emitEvent, - ); + idControl.markAsEnabled(updateParent: updateParent, emitEvent: emitEvent); } } @@ -1037,37 +1064,38 @@ class ProfileOForm implements FormModel { StackTrace.current, ); } - return ProfileOOutput(_idValue, - anotherId: _anotherIdValue, - name: _nameValue, - chartingOrder: _chartingOrderValue, - numberingStandard: _numberingStandardValue, - incidenceFilter: _incidenceFilterValue, - measurementType: _measurementTypeValue, - threshold: _thresholdValue, - timer: _timerValue, - audioGuidance: _audioGuidanceValue); + return ProfileOOutput( + _idValue, + anotherId: _anotherIdValue, + name: _nameValue, + chartingOrder: _chartingOrderValue, + numberingStandard: _numberingStandardValue, + incidenceFilter: _incidenceFilterValue, + measurementType: _measurementTypeValue, + threshold: _thresholdValue, + timer: _timerValue, + audioGuidance: _audioGuidanceValue, + ); } @override ProfileO get rawModel { - return ProfileO(_idRawValue, - anotherId: _anotherIdRawValue, - name: _nameRawValue, - chartingOrder: _chartingOrderRawValue, - numberingStandard: _numberingStandardRawValue, - incidenceFilter: _incidenceFilterRawValue, - measurementType: _measurementTypeRawValue, - threshold: _thresholdRawValue, - timer: _timerRawValue, - audioGuidance: _audioGuidanceRawValue); + return ProfileO( + _idRawValue, + anotherId: _anotherIdRawValue, + name: _nameRawValue, + chartingOrder: _chartingOrderRawValue, + numberingStandard: _numberingStandardRawValue, + incidenceFilter: _incidenceFilterRawValue, + measurementType: _measurementTypeRawValue, + threshold: _thresholdRawValue, + timer: _timerRawValue, + audioGuidance: _audioGuidanceRawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; @@ -1077,7 +1105,9 @@ class ProfileOForm implements FormModel { thresholdForm.toggleDisabled(); timerForm.toggleDisabled(); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { incidenceFilterForm.toggleDisabled(); thresholdForm.toggleDisabled(); @@ -1138,9 +1168,11 @@ class ProfileOForm implements FormModel { ProfileO? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(ProfileOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + ProfileOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -1158,17 +1190,14 @@ class ProfileOForm implements FormModel { ProfileO? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -1219,77 +1248,85 @@ class ProfileOForm implements FormModel { String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(ProfileO? profileO) => FormGroup({ - idControlName: FormControl( - value: profileO?.id, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - anotherIdControlName: FormControl( - value: profileO?.anotherId, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - nameControlName: FormControl( - value: profileO?.name, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - chartingOrderControlName: FormControl( - value: profileO?.chartingOrder, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - numberingStandardControlName: FormControl( - value: profileO?.numberingStandard, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - measurementTypeControlName: FormControl( - value: profileO?.measurementType, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - audioGuidanceControlName: FormControl( - value: profileO?.audioGuidance, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - incidenceFilterControlName: - IncidenceFilterOForm.formElements(profileO?.incidenceFilter), - thresholdControlName: - ThresholdSettingOForm.formElements(profileO?.threshold), - timerControlName: TimerSettingOForm.formElements(profileO?.timer) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(ProfileO? profileO) => FormGroup( + { + idControlName: FormControl( + value: profileO?.id, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + anotherIdControlName: FormControl( + value: profileO?.anotherId, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + nameControlName: FormControl( + value: profileO?.name, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + chartingOrderControlName: FormControl( + value: profileO?.chartingOrder, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + numberingStandardControlName: FormControl( + value: profileO?.numberingStandard, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + measurementTypeControlName: FormControl( + value: profileO?.measurementType, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + audioGuidanceControlName: FormControl( + value: profileO?.audioGuidance, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + incidenceFilterControlName: IncidenceFilterOForm.formElements( + profileO?.incidenceFilter, + ), + thresholdControlName: ThresholdSettingOForm.formElements( + profileO?.threshold, + ), + timerControlName: TimerSettingOForm.formElements(profileO?.timer), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } final _logIncidenceFilterOForm = Logger.detached('IncidenceFilterOForm'); class IncidenceFilterOForm implements FormModel { - IncidenceFilterOForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + IncidenceFilterOForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String isMobilityEnabledControlName = "isMobilityEnabled"; @@ -1307,7 +1344,7 @@ class IncidenceFilterOForm final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -1360,8 +1397,6 @@ class IncidenceFilterOForm bool get _isPlaqueEnabledRawValue => isPlaqueEnabledControl.value ?? true; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsIsMobilityEnabled { try { form.control(isMobilityEnabledControlPath()); @@ -1371,8 +1406,6 @@ class IncidenceFilterOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsIsFurcationEnabled { try { form.control(isFurcationEnabledControlPath()); @@ -1382,8 +1415,6 @@ class IncidenceFilterOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsIsBleedingEnabled { try { form.control(isBleedingEnabledControlPath()); @@ -1393,8 +1424,6 @@ class IncidenceFilterOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsIsSuppurationEnabled { try { form.control(isSuppurationEnabledControlPath()); @@ -1404,8 +1433,6 @@ class IncidenceFilterOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsIsCalculusEnabled { try { form.control(isCalculusEnabledControlPath()); @@ -1415,8 +1442,6 @@ class IncidenceFilterOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsIsPlaqueEnabled { try { form.control(isPlaqueEnabledControlPath()); @@ -1463,8 +1488,11 @@ class IncidenceFilterOForm bool updateParent = true, bool emitEvent = true, }) { - isMobilityEnabledControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + isMobilityEnabledControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void isFurcationEnabledValueUpdate( @@ -1472,8 +1500,11 @@ class IncidenceFilterOForm bool updateParent = true, bool emitEvent = true, }) { - isFurcationEnabledControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + isFurcationEnabledControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void isBleedingEnabledValueUpdate( @@ -1481,8 +1512,11 @@ class IncidenceFilterOForm bool updateParent = true, bool emitEvent = true, }) { - isBleedingEnabledControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + isBleedingEnabledControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void isSuppurationEnabledValueUpdate( @@ -1490,8 +1524,11 @@ class IncidenceFilterOForm bool updateParent = true, bool emitEvent = true, }) { - isSuppurationEnabledControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + isSuppurationEnabledControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void isCalculusEnabledValueUpdate( @@ -1499,8 +1536,11 @@ class IncidenceFilterOForm bool updateParent = true, bool emitEvent = true, }) { - isCalculusEnabledControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + isCalculusEnabledControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void isPlaqueEnabledValueUpdate( @@ -1508,8 +1548,11 @@ class IncidenceFilterOForm bool updateParent = true, bool emitEvent = true, }) { - isPlaqueEnabledControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + isPlaqueEnabledControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void isMobilityEnabledValuePatch( @@ -1517,8 +1560,11 @@ class IncidenceFilterOForm bool updateParent = true, bool emitEvent = true, }) { - isMobilityEnabledControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + isMobilityEnabledControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void isFurcationEnabledValuePatch( @@ -1526,8 +1572,11 @@ class IncidenceFilterOForm bool updateParent = true, bool emitEvent = true, }) { - isFurcationEnabledControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + isFurcationEnabledControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void isBleedingEnabledValuePatch( @@ -1535,8 +1584,11 @@ class IncidenceFilterOForm bool updateParent = true, bool emitEvent = true, }) { - isBleedingEnabledControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + isBleedingEnabledControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void isSuppurationEnabledValuePatch( @@ -1544,8 +1596,11 @@ class IncidenceFilterOForm bool updateParent = true, bool emitEvent = true, }) { - isSuppurationEnabledControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + isSuppurationEnabledControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void isCalculusEnabledValuePatch( @@ -1553,8 +1608,11 @@ class IncidenceFilterOForm bool updateParent = true, bool emitEvent = true, }) { - isCalculusEnabledControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + isCalculusEnabledControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void isPlaqueEnabledValuePatch( @@ -1562,8 +1620,11 @@ class IncidenceFilterOForm bool updateParent = true, bool emitEvent = true, }) { - isPlaqueEnabledControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + isPlaqueEnabledControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void isMobilityEnabledValueReset( @@ -1572,14 +1633,13 @@ class IncidenceFilterOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - isMobilityEnabledControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => isMobilityEnabledControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void isFurcationEnabledValueReset( bool value, { @@ -1587,14 +1647,13 @@ class IncidenceFilterOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - isFurcationEnabledControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => isFurcationEnabledControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void isBleedingEnabledValueReset( bool value, { @@ -1602,14 +1661,13 @@ class IncidenceFilterOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - isBleedingEnabledControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => isBleedingEnabledControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void isSuppurationEnabledValueReset( bool value, { @@ -1617,14 +1675,13 @@ class IncidenceFilterOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - isSuppurationEnabledControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => isSuppurationEnabledControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void isCalculusEnabledValueReset( bool value, { @@ -1632,14 +1689,13 @@ class IncidenceFilterOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - isCalculusEnabledControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => isCalculusEnabledControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void isPlaqueEnabledValueReset( bool value, { @@ -1647,14 +1703,13 @@ class IncidenceFilterOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - isPlaqueEnabledControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => isPlaqueEnabledControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get isMobilityEnabledControl => form.control(isMobilityEnabledControlPath()) as FormControl; @@ -1795,37 +1850,38 @@ class IncidenceFilterOForm ); } return IncidenceFilterOOutput( - isMobilityEnabled: _isMobilityEnabledValue, - isFurcationEnabled: _isFurcationEnabledValue, - isBleedingEnabled: _isBleedingEnabledValue, - isSuppurationEnabled: _isSuppurationEnabledValue, - isCalculusEnabled: _isCalculusEnabledValue, - isPlaqueEnabled: _isPlaqueEnabledValue); + isMobilityEnabled: _isMobilityEnabledValue, + isFurcationEnabled: _isFurcationEnabledValue, + isBleedingEnabled: _isBleedingEnabledValue, + isSuppurationEnabled: _isSuppurationEnabledValue, + isCalculusEnabled: _isCalculusEnabledValue, + isPlaqueEnabled: _isPlaqueEnabledValue, + ); } @override IncidenceFilterO get rawModel { return IncidenceFilterO( - isMobilityEnabled: _isMobilityEnabledRawValue, - isFurcationEnabled: _isFurcationEnabledRawValue, - isBleedingEnabled: _isBleedingEnabledRawValue, - isSuppurationEnabled: _isSuppurationEnabledRawValue, - isCalculusEnabled: _isCalculusEnabledRawValue, - isPlaqueEnabled: _isPlaqueEnabledRawValue); + isMobilityEnabled: _isMobilityEnabledRawValue, + isFurcationEnabled: _isFurcationEnabledRawValue, + isBleedingEnabled: _isBleedingEnabledRawValue, + isSuppurationEnabled: _isSuppurationEnabledRawValue, + isCalculusEnabled: _isCalculusEnabledRawValue, + isPlaqueEnabled: _isPlaqueEnabledRawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { @@ -1883,9 +1939,11 @@ class IncidenceFilterOForm IncidenceFilterO? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(IncidenceFilterOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + IncidenceFilterOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -1903,17 +1961,14 @@ class IncidenceFilterOForm IncidenceFilterO? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -1965,65 +2020,70 @@ class IncidenceFilterOForm [path, pathItem].whereType().join("."); static FormGroup formElements(IncidenceFilterO? incidenceFilterO) => - FormGroup({ - isMobilityEnabledControlName: FormControl( + FormGroup( + { + isMobilityEnabledControlName: FormControl( value: incidenceFilterO?.isMobilityEnabled, validators: [], asyncValidators: [], asyncValidatorsDebounceTime: 250, disabled: false, - touched: false), - isFurcationEnabledControlName: FormControl( + touched: false, + ), + isFurcationEnabledControlName: FormControl( value: incidenceFilterO?.isFurcationEnabled, validators: [], asyncValidators: [], asyncValidatorsDebounceTime: 250, disabled: false, - touched: false), - isBleedingEnabledControlName: FormControl( + touched: false, + ), + isBleedingEnabledControlName: FormControl( value: incidenceFilterO?.isBleedingEnabled, validators: [], asyncValidators: [], asyncValidatorsDebounceTime: 250, disabled: false, - touched: false), - isSuppurationEnabledControlName: FormControl( + touched: false, + ), + isSuppurationEnabledControlName: FormControl( value: incidenceFilterO?.isSuppurationEnabled, validators: [], asyncValidators: [], asyncValidatorsDebounceTime: 250, disabled: false, - touched: false), - isCalculusEnabledControlName: FormControl( + touched: false, + ), + isCalculusEnabledControlName: FormControl( value: incidenceFilterO?.isCalculusEnabled, validators: [], asyncValidators: [], asyncValidatorsDebounceTime: 250, disabled: false, - touched: false), - isPlaqueEnabledControlName: FormControl( + touched: false, + ), + isPlaqueEnabledControlName: FormControl( value: incidenceFilterO?.isPlaqueEnabled, validators: [], asyncValidators: [], asyncValidatorsDebounceTime: 250, disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } final _logThresholdSettingOForm = Logger.detached('ThresholdSettingOForm'); class ThresholdSettingOForm implements FormModel { - ThresholdSettingOForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + ThresholdSettingOForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String isEnabledControlName = "isEnabled"; @@ -2033,7 +2093,7 @@ class ThresholdSettingOForm final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -2053,8 +2113,6 @@ class ThresholdSettingOForm int get _valueRawValue => valueControl.value ?? 2; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsIsEnabled { try { form.control(isEnabledControlPath()); @@ -2064,8 +2122,6 @@ class ThresholdSettingOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsValue { try { form.control(valueControlPath()); @@ -2088,8 +2144,11 @@ class ThresholdSettingOForm bool updateParent = true, bool emitEvent = true, }) { - isEnabledControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + isEnabledControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void valueValueUpdate( @@ -2097,8 +2156,11 @@ class ThresholdSettingOForm bool updateParent = true, bool emitEvent = true, }) { - valueControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + valueControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void isEnabledValuePatch( @@ -2106,8 +2168,11 @@ class ThresholdSettingOForm bool updateParent = true, bool emitEvent = true, }) { - isEnabledControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + isEnabledControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void valueValuePatch( @@ -2115,8 +2180,11 @@ class ThresholdSettingOForm bool updateParent = true, bool emitEvent = true, }) { - valueControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + valueControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void isEnabledValueReset( @@ -2125,14 +2193,13 @@ class ThresholdSettingOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - isEnabledControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => isEnabledControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void valueValueReset( int value, { @@ -2140,14 +2207,13 @@ class ThresholdSettingOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - valueControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => valueControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get isEnabledControl => form.control(isEnabledControlPath()) as FormControl; @@ -2204,27 +2270,30 @@ class ThresholdSettingOForm ); } return ThresholdSettingOOutput( - isEnabled: _isEnabledValue, value: _valueValue); + isEnabled: _isEnabledValue, + value: _valueValue, + ); } @override ThresholdSettingO get rawModel { return ThresholdSettingO( - isEnabled: _isEnabledRawValue, value: _valueRawValue); + isEnabled: _isEnabledRawValue, + value: _valueRawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { @@ -2282,11 +2351,11 @@ class ThresholdSettingOForm ThresholdSettingO? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue( - ThresholdSettingOForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.updateValue( + ThresholdSettingOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -2304,17 +2373,14 @@ class ThresholdSettingOForm ThresholdSettingO? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -2366,37 +2432,38 @@ class ThresholdSettingOForm [path, pathItem].whereType().join("."); static FormGroup formElements(ThresholdSettingO? thresholdSettingO) => - FormGroup({ - isEnabledControlName: FormControl( + FormGroup( + { + isEnabledControlName: FormControl( value: thresholdSettingO?.isEnabled, validators: [], asyncValidators: [], asyncValidatorsDebounceTime: 250, disabled: false, - touched: false), - valueControlName: FormControl( + touched: false, + ), + valueControlName: FormControl( value: thresholdSettingO?.value, validators: [], asyncValidators: [], asyncValidatorsDebounceTime: 250, disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } final _logTimerSettingOForm = Logger.detached('TimerSettingOForm'); class TimerSettingOForm implements FormModel { - TimerSettingOForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + TimerSettingOForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String isEnabledControlName = "isEnabled"; @@ -2406,7 +2473,7 @@ class TimerSettingOForm final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -2426,8 +2493,6 @@ class TimerSettingOForm int get _valueRawValue => valueControl.value ?? 5; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsIsEnabled { try { form.control(isEnabledControlPath()); @@ -2437,8 +2502,6 @@ class TimerSettingOForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsValue { try { form.control(valueControlPath()); @@ -2461,8 +2524,11 @@ class TimerSettingOForm bool updateParent = true, bool emitEvent = true, }) { - isEnabledControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + isEnabledControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void valueValueUpdate( @@ -2470,8 +2536,11 @@ class TimerSettingOForm bool updateParent = true, bool emitEvent = true, }) { - valueControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + valueControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void isEnabledValuePatch( @@ -2479,8 +2548,11 @@ class TimerSettingOForm bool updateParent = true, bool emitEvent = true, }) { - isEnabledControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + isEnabledControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void valueValuePatch( @@ -2488,8 +2560,11 @@ class TimerSettingOForm bool updateParent = true, bool emitEvent = true, }) { - valueControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + valueControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void isEnabledValueReset( @@ -2498,14 +2573,13 @@ class TimerSettingOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - isEnabledControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => isEnabledControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void valueValueReset( int value, { @@ -2513,14 +2587,13 @@ class TimerSettingOForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - valueControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => valueControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get isEnabledControl => form.control(isEnabledControlPath()) as FormControl; @@ -2585,17 +2658,16 @@ class TimerSettingOForm } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { @@ -2653,9 +2725,11 @@ class TimerSettingOForm TimerSettingO? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(TimerSettingOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + TimerSettingOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -2673,17 +2747,14 @@ class TimerSettingOForm TimerSettingO? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -2734,44 +2805,49 @@ class TimerSettingOForm String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(TimerSettingO? timerSettingO) => FormGroup({ - isEnabledControlName: FormControl( - value: timerSettingO?.isEnabled, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - valueControlName: FormControl( - value: timerSettingO?.value, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(TimerSettingO? timerSettingO) => FormGroup( + { + isEnabledControlName: FormControl( + value: timerSettingO?.isEnabled, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + valueControlName: FormControl( + value: timerSettingO?.value, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } @freezed @Rf(output: true) abstract class ProfileOOutput with _$ProfileOOutput { const ProfileOOutput._(); - factory ProfileOOutput(String id, - {required String anotherId, - @RfControl() required String name, - @RfControl() - required ChartingOrderValue chartingOrder, - @RfControl() - required NumberingStandard numberingStandard, - required IncidenceFilterOOutput incidenceFilter, - @RfControl() required MeasurementType measurementType, - required ThresholdSettingOOutput threshold, - required TimerSettingOOutput timer, - @RfControl() required bool audioGuidance}) = _ProfileOOutput; + factory ProfileOOutput( + String id, { + required String anotherId, + @RfControl() required String name, + @RfControl() required ChartingOrderValue chartingOrder, + @RfControl() + required NumberingStandard numberingStandard, + required IncidenceFilterOOutput incidenceFilter, + @RfControl() required MeasurementType measurementType, + required ThresholdSettingOOutput threshold, + required TimerSettingOOutput timer, + @RfControl() required bool audioGuidance, + }) = _ProfileOOutput; factory ProfileOOutput.fromJson(Map json) => _$ProfileOOutputFromJson(json); } @@ -2780,9 +2856,10 @@ abstract class ProfileOOutput with _$ProfileOOutput { @RfGroup() abstract class ThresholdSettingOOutput with _$ThresholdSettingOOutput { static const values = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; - const factory ThresholdSettingOOutput( - {@RfControl() @Default(true) bool isEnabled, - @RfControl() @Default(2) int value}) = _ThresholdSettingOOutput; + const factory ThresholdSettingOOutput({ + @RfControl() @Default(true) bool isEnabled, + @RfControl() @Default(2) int value, + }) = _ThresholdSettingOOutput; factory ThresholdSettingOOutput.fromJson(Map json) => _$ThresholdSettingOOutputFromJson(json); } @@ -2791,9 +2868,10 @@ abstract class ThresholdSettingOOutput with _$ThresholdSettingOOutput { @RfGroup() abstract class TimerSettingOOutput with _$TimerSettingOOutput { static const values = [1, 2, 3, 4, 5, 6]; - const factory TimerSettingOOutput( - {@RfControl() @Default(false) bool isEnabled, - @RfControl() @Default(5) int value}) = _TimerSettingOOutput; + const factory TimerSettingOOutput({ + @RfControl() @Default(false) bool isEnabled, + @RfControl() @Default(5) int value, + }) = _TimerSettingOOutput; factory TimerSettingOOutput.fromJson(Map json) => _$TimerSettingOOutputFromJson(json); } @@ -2801,14 +2879,14 @@ abstract class TimerSettingOOutput with _$TimerSettingOOutput { @RfGroup() @freezed abstract class IncidenceFilterOOutput with _$IncidenceFilterOOutput { - const factory IncidenceFilterOOutput( - {@RfControl() @Default(true) bool isMobilityEnabled, - @RfControl() @Default(true) bool isFurcationEnabled, - @RfControl() @Default(true) bool isBleedingEnabled, - @RfControl() @Default(true) bool isSuppurationEnabled, - @RfControl() @Default(true) bool isCalculusEnabled, - @RfControl() @Default(true) bool isPlaqueEnabled}) = - _IncidenceFilterOOutput; + const factory IncidenceFilterOOutput({ + @RfControl() @Default(true) bool isMobilityEnabled, + @RfControl() @Default(true) bool isFurcationEnabled, + @RfControl() @Default(true) bool isBleedingEnabled, + @RfControl() @Default(true) bool isSuppurationEnabled, + @RfControl() @Default(true) bool isCalculusEnabled, + @RfControl() @Default(true) bool isPlaqueEnabled, + }) = _IncidenceFilterOOutput; factory IncidenceFilterOOutput.fromJson(Map json) => _$IncidenceFilterOOutputFromJson(json); } @@ -2823,30 +2901,39 @@ class ReactiveProfileOFormArrayBuilder required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - ProfileOForm formModel)? control; + ProfileOForm formModel, + )? + control; final Widget Function( - BuildContext context, List itemList, ProfileOForm formModel)? - builder; + BuildContext context, + List itemList, + ProfileOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveProfileOFormArrayBuilderT? item, - ProfileOForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveProfileOFormArrayBuilderT? item, + ProfileOForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function(FormControl control)? - controlFilter; + controlFilter; @override Widget build(BuildContext context) { @@ -2862,18 +2949,9 @@ class ReactiveProfileOFormArrayBuilder return ReactiveFormArrayItemBuilder( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -2891,35 +2969,41 @@ class ReactiveProfileOFormArrayBuilder2 required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - ProfileOForm formModel)? control; + ProfileOForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - ProfileOForm formModel - }) params)? builder; + ({BuildContext context, List itemList, ProfileOForm formModel}) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveProfileOFormArrayBuilderT? item, - ProfileOForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveProfileOFormArrayBuilderT? item, + ProfileOForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function(FormControl control)? - controlFilter; + controlFilter; @override Widget build(BuildContext context) { @@ -2936,23 +3020,17 @@ class ReactiveProfileOFormArrayBuilder2 formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -2961,33 +3039,48 @@ class ReactiveProfileOFormArrayBuilder2 } class ReactiveProfileOFormFormGroupArrayBuilder< - ReactiveProfileOFormFormGroupArrayBuilderT> extends StatelessWidget { + ReactiveProfileOFormFormGroupArrayBuilderT +> + extends StatelessWidget { const ReactiveProfileOFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? extended; - - final ExtendedControl?>, - List> - Function(ProfileOForm formModel)? getExtended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(ProfileOForm formModel)? + getExtended; final Widget Function( - BuildContext context, List itemList, ProfileOForm formModel)? - builder; + BuildContext context, + List itemList, + ProfileOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveProfileOFormFormGroupArrayBuilderT? item, - ProfileOForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveProfileOFormFormGroupArrayBuilderT? item, + ProfileOForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -3005,23 +3098,14 @@ class ReactiveProfileOFormFormGroupArrayBuilder< final itemList = (value.value() ?? []) .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) .values .toList(); - return builder?.call( - context, - itemList, - formModel, - ) ?? + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/reactive_forms_generator/example/lib/docs/recursive/recursive.gform.dart b/packages/reactive_forms_generator/example/lib/docs/recursive/recursive.gform.dart index 04e57ec1..ae9d87e2 100644 --- a/packages/reactive_forms_generator/example/lib/docs/recursive/recursive.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/recursive/recursive.gform.dart @@ -19,7 +19,11 @@ class ReactiveSecuredAreaFormConsumer extends StatelessWidget { final Widget? child; final Widget Function( - BuildContext context, SecuredAreaForm formModel, Widget? child) builder; + BuildContext context, + SecuredAreaForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -38,11 +42,7 @@ class SecuredAreaFormInheritedStreamer extends InheritedStreamer { required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final SecuredAreaForm form; } @@ -64,19 +64,19 @@ class ReactiveSecuredAreaForm extends StatelessWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - static SecuredAreaForm? of( - BuildContext context, { - bool listen = true, - }) { + static SecuredAreaForm? of(BuildContext context, {bool listen = true}) { if (listen) { return context .dependOnInheritedWidgetOfExactType< - SecuredAreaFormInheritedStreamer>() + SecuredAreaFormInheritedStreamer + >() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - SecuredAreaFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + SecuredAreaFormInheritedStreamer + >(); return element == null ? null : (element.widget as SecuredAreaFormInheritedStreamer).form; @@ -123,10 +123,14 @@ class SecuredAreaFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, SecuredAreaForm formModel, Widget? child) builder; + BuildContext context, + SecuredAreaForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, SecuredAreaForm formModel)? - initState; + initState; @override _SecuredAreaFormBuilderState createState() => _SecuredAreaFormBuilderState(); @@ -139,8 +143,11 @@ class _SecuredAreaFormBuilderState extends State { @override void initState() { - _formModel = - SecuredAreaForm(SecuredAreaForm.formElements(widget.model), null, null); + _formModel = SecuredAreaForm( + SecuredAreaForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -217,11 +224,8 @@ class _SecuredAreaFormBuilderState extends State { final _logSecuredAreaForm = Logger.detached('SecuredAreaForm'); class SecuredAreaForm implements FormModel { - SecuredAreaForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + SecuredAreaForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String idControlName = "id"; @@ -235,7 +239,7 @@ class SecuredAreaForm implements FormModel { final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -252,26 +256,28 @@ class SecuredAreaForm implements FormModel { String subSecuredAreasControlPath() => pathBuilder(subSecuredAreasControlName); - String? get _idValue => idControl.value; + String? get _idValue => containsId ? idControl.value : null; - SecuredArea? get _securedAreaValue => securedAreaForm.model; + SecuredArea? get _securedAreaValue => + containsSecuredArea ? securedAreaForm.model : null; - ParcelSystem? get _parcelSystemValue => parcelSystemForm.model; + ParcelSystem? get _parcelSystemValue => + containsParcelSystem ? parcelSystemForm.model : null; List get _subSecuredAreasValue => subSecuredAreasSecuredAreaForm.map((e) => e.model).toList(); - String? get _idRawValue => idControl.value; + String? get _idRawValue => containsId ? idControl.value : null; - SecuredArea? get _securedAreaRawValue => securedAreaForm.rawModel; + SecuredArea? get _securedAreaRawValue => + containsSecuredArea ? securedAreaForm.rawModel : null; - ParcelSystem? get _parcelSystemRawValue => parcelSystemForm.rawModel; + ParcelSystem? get _parcelSystemRawValue => + containsParcelSystem ? parcelSystemForm.rawModel : null; List get _subSecuredAreasRawValue => subSecuredAreasSecuredAreaForm.map((e) => e.rawModel).toList(); - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsId { try { form.control(idControlPath()); @@ -281,8 +287,6 @@ class SecuredAreaForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsSecuredArea { try { form.control(securedAreaControlPath()); @@ -292,8 +296,6 @@ class SecuredAreaForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsParcelSystem { try { form.control(parcelSystemControlPath()); @@ -303,8 +305,6 @@ class SecuredAreaForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsSubSecuredAreas { try { form.control(subSecuredAreasControlPath()); @@ -331,12 +331,7 @@ class SecuredAreaForm implements FormModel { void get subSecuredAreasFocus => form.focus(subSecuredAreasControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void idRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void idRemove({bool updateParent = true, bool emitEvent = true}) { if (containsId) { final controlPath = path; if (controlPath == null) { @@ -359,12 +354,7 @@ class SecuredAreaForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void securedAreaRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void securedAreaRemove({bool updateParent = true, bool emitEvent = true}) { if (containsSecuredArea) { final controlPath = path; if (controlPath == null) { @@ -387,12 +377,7 @@ class SecuredAreaForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void parcelSystemRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void parcelSystemRemove({bool updateParent = true, bool emitEvent = true}) { if (containsParcelSystem) { final controlPath = path; if (controlPath == null) { @@ -420,8 +405,11 @@ class SecuredAreaForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - idControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + idControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void securedAreaValueUpdate( @@ -429,8 +417,11 @@ class SecuredAreaForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - securedAreaControl.updateValue(SecuredAreaForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + securedAreaControl.updateValue( + SecuredAreaForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void parcelSystemValueUpdate( @@ -439,9 +430,10 @@ class SecuredAreaForm implements FormModel { bool emitEvent = true, }) { parcelSystemControl.updateValue( - ParcelSystemForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + ParcelSystemForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void subSecuredAreasValueUpdate( @@ -460,8 +452,9 @@ class SecuredAreaForm implements FormModel { final toAdd = []; localValue.asMap().forEach((k, v) { - final values = - subSecuredAreasControl.controls.map((e) => e.value).toList(); + final values = subSecuredAreasControl.controls + .map((e) => e.value) + .toList(); if (subSecuredAreasSecuredAreaForm.asMap().containsKey(k) && values.asMap().containsKey(k)) { @@ -473,17 +466,19 @@ class SecuredAreaForm implements FormModel { if (toUpdate.isNotEmpty) { subSecuredAreasControl.updateValue( - toUpdate - .map((e) => SecuredAreaForm.formElements(e).rawValue) - .toList(), - updateParent: updateParent, - emitEvent: emitEvent); + toUpdate.map((e) => SecuredAreaForm.formElements(e).rawValue).toList(), + updateParent: updateParent, + emitEvent: emitEvent, + ); } if (toAdd.isNotEmpty) { toAdd.forEach((e) { - subSecuredAreasControl.add(SecuredAreaForm.formElements(e), - updateParent: updateParent, emitEvent: emitEvent); + subSecuredAreasControl.add( + SecuredAreaForm.formElements(e), + updateParent: updateParent, + emitEvent: emitEvent, + ); }); } } @@ -508,13 +503,12 @@ class SecuredAreaForm implements FormModel { ); } - void subSecuredAreasClear({ - bool updateParent = true, - bool emitEvent = true, - }) { + void subSecuredAreasClear({bool updateParent = true, bool emitEvent = true}) { subSecuredAreasSecuredAreaForm.clear(); subSecuredAreasControl.clear( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } void idValuePatch( @@ -522,8 +516,11 @@ class SecuredAreaForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - idControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + idControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void securedAreaValuePatch( @@ -531,8 +528,11 @@ class SecuredAreaForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - securedAreaControl.updateValue(SecuredAreaForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + securedAreaControl.updateValue( + SecuredAreaForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void parcelSystemValuePatch( @@ -541,9 +541,10 @@ class SecuredAreaForm implements FormModel { bool emitEvent = true, }) { parcelSystemControl.updateValue( - ParcelSystemForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + ParcelSystemForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void subSecuredAreasValuePatch( @@ -554,18 +555,17 @@ class SecuredAreaForm implements FormModel { final keys = subSecuredAreasSecuredAreaForm.asMap().keys; final toPatch = []; - (value).asMap().forEach( - (k, v) { - if (keys.contains(k)) { - toPatch.add(v); - } - }, - ); + (value).asMap().forEach((k, v) { + if (keys.contains(k)) { + toPatch.add(v); + } + }); subSecuredAreasControl.patchValue( - toPatch.map((e) => SecuredAreaForm.formElements(e).rawValue).toList(), - updateParent: updateParent, - emitEvent: emitEvent); + toPatch.map((e) => SecuredAreaForm.formElements(e).rawValue).toList(), + updateParent: updateParent, + emitEvent: emitEvent, + ); } void idValueReset( @@ -574,14 +574,13 @@ class SecuredAreaForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - idControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => idControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void securedAreaValueReset( SecuredArea? value, { @@ -589,11 +588,11 @@ class SecuredAreaForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - securedAreaControl.reset( - value: SecuredAreaForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => securedAreaControl.reset( + value: SecuredAreaForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); void parcelSystemValueReset( ParcelSystem? value, { @@ -601,11 +600,11 @@ class SecuredAreaForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - parcelSystemControl.reset( - value: ParcelSystemForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => parcelSystemControl.reset( + value: ParcelSystemForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); void subSecuredAreasValueReset( List value, { @@ -613,13 +612,11 @@ class SecuredAreaForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - subSecuredAreasControl.reset( - value: value - .map((e) => SecuredAreaForm.formElements(e).rawValue) - .toList(), - updateParent: updateParent, - emitEvent: emitEvent); + }) => subSecuredAreasControl.reset( + value: value.map((e) => SecuredAreaForm.formElements(e).rawValue).toList(), + updateParent: updateParent, + emitEvent: emitEvent, + ); FormControl get idControl => form.control(idControlPath()) as FormControl; @@ -645,10 +642,16 @@ class SecuredAreaForm implements FormModel { return values .asMap() - .map((k, v) => MapEntry( + .map( + (k, v) => MapEntry( k, SecuredAreaForm( - form, pathBuilder("subSecuredAreas.$k"), _formModel ?? this))) + form, + pathBuilder("subSecuredAreas.$k"), + _formModel ?? this, + ), + ), + ) .values .toList(); } @@ -664,10 +667,7 @@ class SecuredAreaForm implements FormModel { emitEvent: emitEvent, ); } else { - idControl.markAsEnabled( - updateParent: updateParent, - emitEvent: emitEvent, - ); + idControl.markAsEnabled(updateParent: updateParent, emitEvent: emitEvent); } } @@ -726,11 +726,12 @@ class SecuredAreaForm implements FormModel { } ExtendedControl?>, List> - get subSecuredAreasExtendedControl => - ExtendedControl?>, List>( - form.control(subSecuredAreasControlPath()) - as FormArray>, - () => subSecuredAreasSecuredAreaForm); + get subSecuredAreasExtendedControl => + ExtendedControl?>, List>( + form.control(subSecuredAreasControlPath()) + as FormArray>, + () => subSecuredAreasSecuredAreaForm, + ); void addSubSecuredAreasItem(SecuredArea value) { subSecuredAreasControl.add(SecuredAreaForm.formElements(value)); @@ -758,26 +759,25 @@ class SecuredAreaForm implements FormModel { ); } return SecuredArea( - id: _idValue, - securedArea: _securedAreaValue, - parcelSystem: _parcelSystemValue, - subSecuredAreas: _subSecuredAreasValue); + id: _idValue, + securedArea: _securedAreaValue, + parcelSystem: _parcelSystemValue, + subSecuredAreas: _subSecuredAreasValue, + ); } @override SecuredArea get rawModel { return SecuredArea( - id: _idRawValue, - securedArea: _securedAreaRawValue, - parcelSystem: _parcelSystemRawValue, - subSecuredAreas: _subSecuredAreasRawValue); + id: _idRawValue, + securedArea: _securedAreaRawValue, + parcelSystem: _parcelSystemRawValue, + subSecuredAreas: _subSecuredAreasRawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; @@ -787,7 +787,9 @@ class SecuredAreaForm implements FormModel { securedAreaForm.toggleDisabled(); parcelSystemForm.toggleDisabled(); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { subSecuredAreasSecuredAreaForm.forEach((e) => e.toggleDisabled()); securedAreaForm.toggleDisabled(); @@ -848,9 +850,11 @@ class SecuredAreaForm implements FormModel { SecuredArea? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(SecuredAreaForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + SecuredAreaForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -868,17 +872,14 @@ class SecuredAreaForm implements FormModel { SecuredArea? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -929,41 +930,44 @@ class SecuredAreaForm implements FormModel { String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(SecuredArea? securedArea) => FormGroup({ - idControlName: FormControl( - value: securedArea?.id, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - subSecuredAreasControlName: FormArray( - (securedArea?.subSecuredAreas ?? []) - .map((e) => SecuredAreaForm.formElements(e)) - .toList(), - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false), - securedAreaControlName: - SecuredAreaForm.formElements(securedArea?.securedArea), - parcelSystemControlName: - ParcelSystemForm.formElements(securedArea?.parcelSystem) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(SecuredArea? securedArea) => FormGroup( + { + idControlName: FormControl( + value: securedArea?.id, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + subSecuredAreasControlName: FormArray( + (securedArea?.subSecuredAreas ?? []) + .map((e) => SecuredAreaForm.formElements(e)) + .toList(), + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ), + securedAreaControlName: SecuredAreaForm.formElements( + securedArea?.securedArea, + ), + parcelSystemControlName: ParcelSystemForm.formElements( + securedArea?.parcelSystem, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } final _logParcelSystemForm = Logger.detached('ParcelSystemForm'); class ParcelSystemForm implements FormModel { - ParcelSystemForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + ParcelSystemForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String hasParcelSystemControlName = "hasParcelSystem"; @@ -973,7 +977,7 @@ class ParcelSystemForm implements FormModel { final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -994,8 +998,6 @@ class ParcelSystemForm implements FormModel { ParcelSystemData get _dataRawValue => dataForm.rawModel; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsHasParcelSystem { try { form.control(hasParcelSystemControlPath()); @@ -1005,8 +1007,6 @@ class ParcelSystemForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsData { try { form.control(dataControlPath()); @@ -1030,8 +1030,11 @@ class ParcelSystemForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - hasParcelSystemControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + hasParcelSystemControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void dataValueUpdate( @@ -1039,8 +1042,11 @@ class ParcelSystemForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - dataControl.updateValue(ParcelSystemDataForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + dataControl.updateValue( + ParcelSystemDataForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void hasParcelSystemValuePatch( @@ -1048,8 +1054,11 @@ class ParcelSystemForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - hasParcelSystemControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + hasParcelSystemControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void dataValuePatch( @@ -1057,8 +1066,11 @@ class ParcelSystemForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - dataControl.updateValue(ParcelSystemDataForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + dataControl.updateValue( + ParcelSystemDataForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void hasParcelSystemValueReset( @@ -1067,14 +1079,13 @@ class ParcelSystemForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - hasParcelSystemControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => hasParcelSystemControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void dataValueReset( ParcelSystemData value, { @@ -1082,11 +1093,11 @@ class ParcelSystemForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - dataControl.reset( - value: ParcelSystemDataForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => dataControl.reset( + value: ParcelSystemDataForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); FormControl get hasParcelSystemControl => form.control(hasParcelSystemControlPath()) as FormControl; @@ -1144,20 +1155,21 @@ class ParcelSystemForm implements FormModel { ); } return ParcelSystem( - hasParcelSystem: _hasParcelSystemValue, data: _dataValue); + hasParcelSystem: _hasParcelSystemValue, + data: _dataValue, + ); } @override ParcelSystem get rawModel { return ParcelSystem( - hasParcelSystem: _hasParcelSystemRawValue, data: _dataRawValue); + hasParcelSystem: _hasParcelSystemRawValue, + data: _dataRawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; @@ -1165,7 +1177,9 @@ class ParcelSystemForm implements FormModel { dataForm.toggleDisabled(); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { dataForm.toggleDisabled(); currentForm.controls.forEach((key, control) { @@ -1224,9 +1238,11 @@ class ParcelSystemForm implements FormModel { ParcelSystem? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(ParcelSystemForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + ParcelSystemForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -1244,17 +1260,14 @@ class ParcelSystemForm implements FormModel { ParcelSystem? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -1305,31 +1318,31 @@ class ParcelSystemForm implements FormModel { String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(ParcelSystem? parcelSystem) => FormGroup({ - hasParcelSystemControlName: FormControl( - value: parcelSystem?.hasParcelSystem, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - dataControlName: ParcelSystemDataForm.formElements(parcelSystem?.data) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(ParcelSystem? parcelSystem) => FormGroup( + { + hasParcelSystemControlName: FormControl( + value: parcelSystem?.hasParcelSystem, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + dataControlName: ParcelSystemDataForm.formElements(parcelSystem?.data), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } final _logParcelSystemDataForm = Logger.detached('ParcelSystemDataForm'); class ParcelSystemDataForm implements FormModel { - ParcelSystemDataForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + ParcelSystemDataForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String idControlName = "id"; @@ -1337,7 +1350,7 @@ class ParcelSystemDataForm final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -1347,12 +1360,10 @@ class ParcelSystemDataForm String idControlPath() => pathBuilder(idControlName); - String? get _idValue => idControl.value; + String? get _idValue => containsId ? idControl.value : null; - String? get _idRawValue => idControl.value; + String? get _idRawValue => containsId ? idControl.value : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsId { try { form.control(idControlPath()); @@ -1366,12 +1377,7 @@ class ParcelSystemDataForm void get idFocus => form.focus(idControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void idRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void idRemove({bool updateParent = true, bool emitEvent = true}) { if (containsId) { final controlPath = path; if (controlPath == null) { @@ -1399,8 +1405,11 @@ class ParcelSystemDataForm bool updateParent = true, bool emitEvent = true, }) { - idControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + idControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void idValuePatch( @@ -1408,8 +1417,11 @@ class ParcelSystemDataForm bool updateParent = true, bool emitEvent = true, }) { - idControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + idControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void idValueReset( @@ -1418,14 +1430,13 @@ class ParcelSystemDataForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - idControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => idControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get idControl => form.control(idControlPath()) as FormControl; @@ -1441,10 +1452,7 @@ class ParcelSystemDataForm emitEvent: emitEvent, ); } else { - idControl.markAsEnabled( - updateParent: updateParent, - emitEvent: emitEvent, - ); + idControl.markAsEnabled(updateParent: updateParent, emitEvent: emitEvent); } } @@ -1468,17 +1476,16 @@ class ParcelSystemDataForm } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { @@ -1536,9 +1543,11 @@ class ParcelSystemDataForm ParcelSystemData? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(ParcelSystemDataForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + ParcelSystemDataForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -1556,17 +1565,14 @@ class ParcelSystemDataForm ParcelSystemData? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -1618,19 +1624,22 @@ class ParcelSystemDataForm [path, pathItem].whereType().join("."); static FormGroup formElements(ParcelSystemData? parcelSystemData) => - FormGroup({ - idControlName: FormControl( + FormGroup( + { + idControlName: FormControl( value: parcelSystemData?.id, validators: [], asyncValidators: [], asyncValidatorsDebounceTime: 250, disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } class ReactiveSecuredAreaFormArrayBuilder @@ -1643,29 +1652,41 @@ class ReactiveSecuredAreaFormArrayBuilder required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - SecuredAreaForm formModel)? control; + SecuredAreaForm formModel, + )? + control; - final Widget Function(BuildContext context, List itemList, - SecuredAreaForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + SecuredAreaForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveSecuredAreaFormArrayBuilderT? item, - SecuredAreaForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveSecuredAreaFormArrayBuilderT? item, + SecuredAreaForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -1681,18 +1702,9 @@ class ReactiveSecuredAreaFormArrayBuilder return ReactiveFormArrayItemBuilder( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1710,35 +1722,43 @@ class ReactiveSecuredAreaFormArrayBuilder2 required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - SecuredAreaForm formModel)? control; + SecuredAreaForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - SecuredAreaForm formModel - }) params)? builder; + ({BuildContext context, List itemList, SecuredAreaForm formModel}) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveSecuredAreaFormArrayBuilderT? item, - SecuredAreaForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveSecuredAreaFormArrayBuilderT? item, + SecuredAreaForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -1755,23 +1775,17 @@ class ReactiveSecuredAreaFormArrayBuilder2 formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1780,32 +1794,48 @@ class ReactiveSecuredAreaFormArrayBuilder2 } class ReactiveSecuredAreaFormFormGroupArrayBuilder< - ReactiveSecuredAreaFormFormGroupArrayBuilderT> extends StatelessWidget { + ReactiveSecuredAreaFormFormGroupArrayBuilderT +> + extends StatelessWidget { const ReactiveSecuredAreaFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? extended; - - final ExtendedControl?>, - List> - Function(SecuredAreaForm formModel)? getExtended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(SecuredAreaForm formModel)? + getExtended; - final Widget Function(BuildContext context, List itemList, - SecuredAreaForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + SecuredAreaForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveSecuredAreaFormFormGroupArrayBuilderT? item, - SecuredAreaForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveSecuredAreaFormFormGroupArrayBuilderT? item, + SecuredAreaForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -1823,23 +1853,14 @@ class ReactiveSecuredAreaFormFormGroupArrayBuilder< final itemList = (value.value() ?? []) .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) .values .toList(); - return builder?.call( - context, - itemList, - formModel, - ) ?? + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/reactive_forms_generator/example/lib/docs/renamed_basic/renamed_basic.gform.dart b/packages/reactive_forms_generator/example/lib/docs/renamed_basic/renamed_basic.gform.dart index 88a9a92c..b4496240 100644 --- a/packages/reactive_forms_generator/example/lib/docs/renamed_basic/renamed_basic.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/renamed_basic/renamed_basic.gform.dart @@ -19,7 +19,11 @@ class ReactiveSomeWiredNameFormConsumer extends StatelessWidget { final Widget? child; final Widget Function( - BuildContext context, SomeWiredNameForm formModel, Widget? child) builder; + BuildContext context, + SomeWiredNameForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -38,11 +42,7 @@ class SomeWiredNameFormInheritedStreamer extends InheritedStreamer { required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final SomeWiredNameForm form; } @@ -64,19 +64,19 @@ class ReactiveSomeWiredNameForm extends StatelessWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - static SomeWiredNameForm? of( - BuildContext context, { - bool listen = true, - }) { + static SomeWiredNameForm? of(BuildContext context, {bool listen = true}) { if (listen) { return context .dependOnInheritedWidgetOfExactType< - SomeWiredNameFormInheritedStreamer>() + SomeWiredNameFormInheritedStreamer + >() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - SomeWiredNameFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + SomeWiredNameFormInheritedStreamer + >(); return element == null ? null : (element.widget as SomeWiredNameFormInheritedStreamer).form; @@ -124,10 +124,14 @@ class SomeWiredNameFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, SomeWiredNameForm formModel, Widget? child) builder; + BuildContext context, + SomeWiredNameForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, SomeWiredNameForm formModel)? - initState; + initState; @override _SomeWiredNameFormBuilderState createState() => @@ -142,7 +146,10 @@ class _SomeWiredNameFormBuilderState extends State { @override void initState() { _formModel = SomeWiredNameForm( - SomeWiredNameForm.formElements(widget.model), null, null); + SomeWiredNameForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -219,11 +226,8 @@ class _SomeWiredNameFormBuilderState extends State { final _logSomeWiredNameForm = Logger.detached('SomeWiredNameForm'); class SomeWiredNameForm implements FormModel { - SomeWiredNameForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + SomeWiredNameForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String emailControlName = "email"; @@ -233,7 +237,7 @@ class SomeWiredNameForm implements FormModel { final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -253,8 +257,6 @@ class SomeWiredNameForm implements FormModel { String get _passwordRawValue => passwordControl.value ?? ""; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsEmail { try { form.control(emailControlPath()); @@ -264,8 +266,6 @@ class SomeWiredNameForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsPassword { try { form.control(passwordControlPath()); @@ -288,8 +288,11 @@ class SomeWiredNameForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - emailControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void passwordValueUpdate( @@ -297,8 +300,11 @@ class SomeWiredNameForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - passwordControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + passwordControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailValuePatch( @@ -306,8 +312,11 @@ class SomeWiredNameForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - emailControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void passwordValuePatch( @@ -315,8 +324,11 @@ class SomeWiredNameForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - passwordControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + passwordControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailValueReset( @@ -325,14 +337,13 @@ class SomeWiredNameForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - emailControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => emailControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void passwordValueReset( String value, { @@ -340,14 +351,13 @@ class SomeWiredNameForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - passwordControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => passwordControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get emailControl => form.control(emailControlPath()) as FormControl; @@ -411,17 +421,16 @@ class SomeWiredNameForm implements FormModel { } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { @@ -479,9 +488,11 @@ class SomeWiredNameForm implements FormModel { RenamedBasic? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(SomeWiredNameForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + SomeWiredNameForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -499,17 +510,14 @@ class SomeWiredNameForm implements FormModel { RenamedBasic? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -560,30 +568,36 @@ class SomeWiredNameForm implements FormModel { String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(RenamedBasic? renamedBasic) => FormGroup({ - emailControlName: FormControl( - value: renamedBasic?.email, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - passwordControlName: FormControl( - value: renamedBasic?.password, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(RenamedBasic? renamedBasic) => FormGroup( + { + emailControlName: FormControl( + value: renamedBasic?.email, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + passwordControlName: FormControl( + value: renamedBasic?.password, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } class ReactiveSomeWiredNameFormArrayBuilder< - ReactiveSomeWiredNameFormArrayBuilderT> extends StatelessWidget { + ReactiveSomeWiredNameFormArrayBuilderT +> + extends StatelessWidget { const ReactiveSomeWiredNameFormArrayBuilder({ Key? key, this.control, @@ -592,30 +606,41 @@ class ReactiveSomeWiredNameFormArrayBuilder< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - SomeWiredNameForm formModel)? control; + SomeWiredNameForm formModel, + )? + control; - final Widget Function(BuildContext context, List itemList, - SomeWiredNameForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + SomeWiredNameForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveSomeWiredNameFormArrayBuilderT? item, - SomeWiredNameForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveSomeWiredNameFormArrayBuilderT? item, + SomeWiredNameForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -631,18 +656,9 @@ class ReactiveSomeWiredNameFormArrayBuilder< return ReactiveFormArrayItemBuilder( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -651,7 +667,9 @@ class ReactiveSomeWiredNameFormArrayBuilder< } class ReactiveSomeWiredNameFormArrayBuilder2< - ReactiveSomeWiredNameFormArrayBuilderT> extends StatelessWidget { + ReactiveSomeWiredNameFormArrayBuilderT +> + extends StatelessWidget { const ReactiveSomeWiredNameFormArrayBuilder2({ Key? key, this.control, @@ -660,36 +678,43 @@ class ReactiveSomeWiredNameFormArrayBuilder2< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - SomeWiredNameForm formModel)? control; + SomeWiredNameForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - SomeWiredNameForm formModel - }) params)? builder; + ({BuildContext context, List itemList, SomeWiredNameForm formModel}) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveSomeWiredNameFormArrayBuilderT? item, - SomeWiredNameForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveSomeWiredNameFormArrayBuilderT? item, + SomeWiredNameForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -706,23 +731,17 @@ class ReactiveSomeWiredNameFormArrayBuilder2< formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -731,32 +750,48 @@ class ReactiveSomeWiredNameFormArrayBuilder2< } class ReactiveSomeWiredNameFormFormGroupArrayBuilder< - ReactiveSomeWiredNameFormFormGroupArrayBuilderT> extends StatelessWidget { + ReactiveSomeWiredNameFormFormGroupArrayBuilderT +> + extends StatelessWidget { const ReactiveSomeWiredNameFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? extended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(SomeWiredNameForm formModel)? + getExtended; - final ExtendedControl?>, - List> - Function(SomeWiredNameForm formModel)? getExtended; - - final Widget Function(BuildContext context, List itemList, - SomeWiredNameForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + SomeWiredNameForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveSomeWiredNameFormFormGroupArrayBuilderT? item, - SomeWiredNameForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveSomeWiredNameFormFormGroupArrayBuilderT? item, + SomeWiredNameForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -771,26 +806,18 @@ class ReactiveSomeWiredNameFormFormGroupArrayBuilder< return StreamBuilder?>?>( stream: value.control.valueChanges, builder: (context, snapshot) { - final itemList = (value.value() ?? - []) - .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) - .values - .toList(); - - return builder?.call( - context, - itemList, - formModel, - ) ?? + final itemList = + (value.value() ?? + []) + .asMap() + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) + .values + .toList(); + + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/reactive_forms_generator/example/lib/docs/renamed_basic/renamed_basic_output.gform.dart b/packages/reactive_forms_generator/example/lib/docs/renamed_basic/renamed_basic_output.gform.dart index e7c5b6b8..f06e64ca 100644 --- a/packages/reactive_forms_generator/example/lib/docs/renamed_basic/renamed_basic_output.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/renamed_basic/renamed_basic_output.gform.dart @@ -19,7 +19,11 @@ class ReactiveSomeWiredNameFormConsumer extends StatelessWidget { final Widget? child; final Widget Function( - BuildContext context, SomeWiredNameForm formModel, Widget? child) builder; + BuildContext context, + SomeWiredNameForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -38,11 +42,7 @@ class SomeWiredNameFormInheritedStreamer extends InheritedStreamer { required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final SomeWiredNameForm form; } @@ -64,19 +64,19 @@ class ReactiveSomeWiredNameForm extends StatelessWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - static SomeWiredNameForm? of( - BuildContext context, { - bool listen = true, - }) { + static SomeWiredNameForm? of(BuildContext context, {bool listen = true}) { if (listen) { return context .dependOnInheritedWidgetOfExactType< - SomeWiredNameFormInheritedStreamer>() + SomeWiredNameFormInheritedStreamer + >() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - SomeWiredNameFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + SomeWiredNameFormInheritedStreamer + >(); return element == null ? null : (element.widget as SomeWiredNameFormInheritedStreamer).form; @@ -124,10 +124,14 @@ class SomeWiredNameFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, SomeWiredNameForm formModel, Widget? child) builder; + BuildContext context, + SomeWiredNameForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, SomeWiredNameForm formModel)? - initState; + initState; @override _SomeWiredNameFormBuilderState createState() => @@ -142,7 +146,10 @@ class _SomeWiredNameFormBuilderState extends State { @override void initState() { _formModel = SomeWiredNameForm( - SomeWiredNameForm.formElements(widget.model), null, null); + SomeWiredNameForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -220,11 +227,8 @@ final _logSomeWiredNameForm = Logger.detached('SomeWiredNameForm'); class SomeWiredNameForm implements FormModel { - SomeWiredNameForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + SomeWiredNameForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String emailControlName = "email"; @@ -234,7 +238,7 @@ class SomeWiredNameForm final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -250,12 +254,11 @@ class SomeWiredNameForm String get _passwordValue => passwordControl.value as String; - String? get _emailRawValue => emailControl.value; + String? get _emailRawValue => containsEmail ? emailControl.value : null; - String? get _passwordRawValue => passwordControl.value; + String? get _passwordRawValue => + containsPassword ? passwordControl.value : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsEmail { try { form.control(emailControlPath()); @@ -265,8 +268,6 @@ class SomeWiredNameForm } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsPassword { try { form.control(passwordControlPath()); @@ -284,12 +285,7 @@ class SomeWiredNameForm void get passwordFocus => form.focus(passwordControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void emailRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void emailRemove({bool updateParent = true, bool emitEvent = true}) { if (containsEmail) { final controlPath = path; if (controlPath == null) { @@ -312,12 +308,7 @@ class SomeWiredNameForm } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void passwordRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void passwordRemove({bool updateParent = true, bool emitEvent = true}) { if (containsPassword) { final controlPath = path; if (controlPath == null) { @@ -345,8 +336,11 @@ class SomeWiredNameForm bool updateParent = true, bool emitEvent = true, }) { - emailControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void passwordValueUpdate( @@ -354,8 +348,11 @@ class SomeWiredNameForm bool updateParent = true, bool emitEvent = true, }) { - passwordControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + passwordControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailValuePatch( @@ -363,8 +360,11 @@ class SomeWiredNameForm bool updateParent = true, bool emitEvent = true, }) { - emailControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + emailControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void passwordValuePatch( @@ -372,8 +372,11 @@ class SomeWiredNameForm bool updateParent = true, bool emitEvent = true, }) { - passwordControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + passwordControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void emailValueReset( @@ -382,14 +385,13 @@ class SomeWiredNameForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - emailControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => emailControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void passwordValueReset( String? value, { @@ -397,14 +399,13 @@ class SomeWiredNameForm bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - passwordControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => passwordControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get emailControl => form.control(emailControlPath()) as FormControl; @@ -469,17 +470,16 @@ class SomeWiredNameForm } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { @@ -537,9 +537,11 @@ class SomeWiredNameForm RenamedBasicO? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(SomeWiredNameForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + SomeWiredNameForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -557,17 +559,14 @@ class SomeWiredNameForm RenamedBasicO? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -618,39 +617,46 @@ class SomeWiredNameForm String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(RenamedBasicO? renamedBasicO) => FormGroup({ - emailControlName: FormControl( - value: renamedBasicO?.email, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - passwordControlName: FormControl( - value: renamedBasicO?.password, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(RenamedBasicO? renamedBasicO) => FormGroup( + { + emailControlName: FormControl( + value: renamedBasicO?.email, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + passwordControlName: FormControl( + value: renamedBasicO?.password, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } @Rf(output: true, name: 'SomeWiredName') class RenamedBasicOOutput { final String email; final String password; - RenamedBasicOOutput( - {@RfControl(validators: [RequiredValidator()]) required this.email, - @RfControl(validators: [RequiredValidator()]) required this.password}); + RenamedBasicOOutput({ + @RfControl(validators: [RequiredValidator()]) required this.email, + @RfControl(validators: [RequiredValidator()]) required this.password, + }); } class ReactiveSomeWiredNameFormArrayBuilder< - ReactiveSomeWiredNameFormArrayBuilderT> extends StatelessWidget { + ReactiveSomeWiredNameFormArrayBuilderT +> + extends StatelessWidget { const ReactiveSomeWiredNameFormArrayBuilder({ Key? key, this.control, @@ -659,30 +665,41 @@ class ReactiveSomeWiredNameFormArrayBuilder< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - SomeWiredNameForm formModel)? control; + SomeWiredNameForm formModel, + )? + control; - final Widget Function(BuildContext context, List itemList, - SomeWiredNameForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + SomeWiredNameForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveSomeWiredNameFormArrayBuilderT? item, - SomeWiredNameForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveSomeWiredNameFormArrayBuilderT? item, + SomeWiredNameForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -698,18 +715,9 @@ class ReactiveSomeWiredNameFormArrayBuilder< return ReactiveFormArrayItemBuilder( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -718,7 +726,9 @@ class ReactiveSomeWiredNameFormArrayBuilder< } class ReactiveSomeWiredNameFormArrayBuilder2< - ReactiveSomeWiredNameFormArrayBuilderT> extends StatelessWidget { + ReactiveSomeWiredNameFormArrayBuilderT +> + extends StatelessWidget { const ReactiveSomeWiredNameFormArrayBuilder2({ Key? key, this.control, @@ -727,36 +737,43 @@ class ReactiveSomeWiredNameFormArrayBuilder2< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - SomeWiredNameForm formModel)? control; + SomeWiredNameForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - SomeWiredNameForm formModel - }) params)? builder; + ({BuildContext context, List itemList, SomeWiredNameForm formModel}) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveSomeWiredNameFormArrayBuilderT? item, - SomeWiredNameForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveSomeWiredNameFormArrayBuilderT? item, + SomeWiredNameForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -773,23 +790,17 @@ class ReactiveSomeWiredNameFormArrayBuilder2< formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -798,32 +809,48 @@ class ReactiveSomeWiredNameFormArrayBuilder2< } class ReactiveSomeWiredNameFormFormGroupArrayBuilder< - ReactiveSomeWiredNameFormFormGroupArrayBuilderT> extends StatelessWidget { + ReactiveSomeWiredNameFormFormGroupArrayBuilderT +> + extends StatelessWidget { const ReactiveSomeWiredNameFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(SomeWiredNameForm formModel)? + getExtended; - final ExtendedControl?>, - List>? extended; - - final ExtendedControl?>, - List> - Function(SomeWiredNameForm formModel)? getExtended; - - final Widget Function(BuildContext context, List itemList, - SomeWiredNameForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + SomeWiredNameForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveSomeWiredNameFormFormGroupArrayBuilderT? item, - SomeWiredNameForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveSomeWiredNameFormFormGroupArrayBuilderT? item, + SomeWiredNameForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -838,26 +865,18 @@ class ReactiveSomeWiredNameFormFormGroupArrayBuilder< return StreamBuilder?>?>( stream: value.control.valueChanges, builder: (context, snapshot) { - final itemList = (value.value() ?? - []) - .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) - .values - .toList(); - - return builder?.call( - context, - itemList, - formModel, - ) ?? + final itemList = + (value.value() ?? + []) + .asMap() + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) + .values + .toList(); + + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/reactive_forms_generator/example/lib/docs/user_profile/user_profile.gform.dart b/packages/reactive_forms_generator/example/lib/docs/user_profile/user_profile.gform.dart index bd6524f8..e6fc5097 100644 --- a/packages/reactive_forms_generator/example/lib/docs/user_profile/user_profile.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/user_profile/user_profile.gform.dart @@ -19,7 +19,11 @@ class ReactiveUserProfileFormConsumer extends StatelessWidget { final Widget? child; final Widget Function( - BuildContext context, UserProfileForm formModel, Widget? child) builder; + BuildContext context, + UserProfileForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -38,11 +42,7 @@ class UserProfileFormInheritedStreamer extends InheritedStreamer { required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final UserProfileForm form; } @@ -64,19 +64,19 @@ class ReactiveUserProfileForm extends StatelessWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - static UserProfileForm? of( - BuildContext context, { - bool listen = true, - }) { + static UserProfileForm? of(BuildContext context, {bool listen = true}) { if (listen) { return context .dependOnInheritedWidgetOfExactType< - UserProfileFormInheritedStreamer>() + UserProfileFormInheritedStreamer + >() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - UserProfileFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + UserProfileFormInheritedStreamer + >(); return element == null ? null : (element.widget as UserProfileFormInheritedStreamer).form; @@ -123,10 +123,14 @@ class UserProfileFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, UserProfileForm formModel, Widget? child) builder; + BuildContext context, + UserProfileForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, UserProfileForm formModel)? - initState; + initState; @override _UserProfileFormBuilderState createState() => _UserProfileFormBuilderState(); @@ -139,8 +143,11 @@ class _UserProfileFormBuilderState extends State { @override void initState() { - _formModel = - UserProfileForm(UserProfileForm.formElements(widget.model), null, null); + _formModel = UserProfileForm( + UserProfileForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -217,11 +224,8 @@ class _UserProfileFormBuilderState extends State { final _logUserProfileForm = Logger.detached('UserProfileForm'); class UserProfileForm implements FormModel { - UserProfileForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + UserProfileForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String idControlName = "id"; @@ -237,7 +241,7 @@ class UserProfileForm implements FormModel { final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -263,7 +267,7 @@ class UserProfileForm implements FormModel { Address get _homeValue => homeForm.model; - Address? get _officeValue => officeForm.model; + Address? get _officeValue => containsOffice ? officeForm.model : null; String get _idRawValue => idControl.value as String; @@ -273,10 +277,8 @@ class UserProfileForm implements FormModel { Address get _homeRawValue => homeForm.rawModel; - Address? get _officeRawValue => officeForm.rawModel; + Address? get _officeRawValue => containsOffice ? officeForm.rawModel : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsId { try { form.control(idControlPath()); @@ -286,8 +288,6 @@ class UserProfileForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsFirstName { try { form.control(firstNameControlPath()); @@ -297,8 +297,6 @@ class UserProfileForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsLastName { try { form.control(lastNameControlPath()); @@ -308,8 +306,6 @@ class UserProfileForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsHome { try { form.control(homeControlPath()); @@ -319,8 +315,6 @@ class UserProfileForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsOffice { try { form.control(officeControlPath()); @@ -350,12 +344,7 @@ class UserProfileForm implements FormModel { void get officeFocus => form.focus(officeControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void officeRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void officeRemove({bool updateParent = true, bool emitEvent = true}) { if (containsOffice) { final controlPath = path; if (controlPath == null) { @@ -383,8 +372,11 @@ class UserProfileForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - idControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + idControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void firstNameValueUpdate( @@ -392,8 +384,11 @@ class UserProfileForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - firstNameControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + firstNameControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void lastNameValueUpdate( @@ -401,8 +396,11 @@ class UserProfileForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - lastNameControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + lastNameControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void homeValueUpdate( @@ -410,8 +408,11 @@ class UserProfileForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - homeControl.updateValue(AddressForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + homeControl.updateValue( + AddressForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void officeValueUpdate( @@ -419,8 +420,11 @@ class UserProfileForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - officeControl.updateValue(AddressForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + officeControl.updateValue( + AddressForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void idValuePatch( @@ -428,8 +432,11 @@ class UserProfileForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - idControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + idControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void firstNameValuePatch( @@ -437,8 +444,11 @@ class UserProfileForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - firstNameControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + firstNameControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void lastNameValuePatch( @@ -446,8 +456,11 @@ class UserProfileForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - lastNameControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + lastNameControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void homeValuePatch( @@ -455,8 +468,11 @@ class UserProfileForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - homeControl.updateValue(AddressForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + homeControl.updateValue( + AddressForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void officeValuePatch( @@ -464,8 +480,11 @@ class UserProfileForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - officeControl.updateValue(AddressForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + officeControl.updateValue( + AddressForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void idValueReset( @@ -474,14 +493,13 @@ class UserProfileForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - idControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => idControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void firstNameValueReset( String value, { @@ -489,14 +507,13 @@ class UserProfileForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - firstNameControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => firstNameControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void lastNameValueReset( String value, { @@ -504,14 +521,13 @@ class UserProfileForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - lastNameControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => lastNameControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void homeValueReset( Address value, { @@ -519,11 +535,11 @@ class UserProfileForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - homeControl.reset( - value: AddressForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => homeControl.reset( + value: AddressForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); void officeValueReset( Address? value, { @@ -531,11 +547,11 @@ class UserProfileForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - officeControl.reset( - value: AddressForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => officeControl.reset( + value: AddressForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); FormControl get idControl => form.control(idControlPath()) as FormControl; @@ -567,10 +583,7 @@ class UserProfileForm implements FormModel { emitEvent: emitEvent, ); } else { - idControl.markAsEnabled( - updateParent: updateParent, - emitEvent: emitEvent, - ); + idControl.markAsEnabled(updateParent: updateParent, emitEvent: emitEvent); } } @@ -658,28 +671,27 @@ class UserProfileForm implements FormModel { ); } return UserProfile( - id: _idValue, - firstName: _firstNameValue, - lastName: _lastNameValue, - home: _homeValue, - office: _officeValue); + id: _idValue, + firstName: _firstNameValue, + lastName: _lastNameValue, + home: _homeValue, + office: _officeValue, + ); } @override UserProfile get rawModel { return UserProfile( - id: _idRawValue, - firstName: _firstNameRawValue, - lastName: _lastNameRawValue, - home: _homeRawValue, - office: _officeRawValue); + id: _idRawValue, + firstName: _firstNameRawValue, + lastName: _lastNameRawValue, + home: _homeRawValue, + office: _officeRawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; @@ -688,7 +700,9 @@ class UserProfileForm implements FormModel { homeForm.toggleDisabled(); officeForm.toggleDisabled(); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { homeForm.toggleDisabled(); officeForm.toggleDisabled(); @@ -748,9 +762,11 @@ class UserProfileForm implements FormModel { UserProfile? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(UserProfileForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + UserProfileForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -768,17 +784,14 @@ class UserProfileForm implements FormModel { UserProfile? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -829,45 +842,46 @@ class UserProfileForm implements FormModel { String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(UserProfile? userProfile) => FormGroup({ - idControlName: FormControl( - value: userProfile?.id, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - firstNameControlName: FormControl( - value: userProfile?.firstName, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - lastNameControlName: FormControl( - value: userProfile?.lastName, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - homeControlName: AddressForm.formElements(userProfile?.home), - officeControlName: AddressForm.formElements(userProfile?.office) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(UserProfile? userProfile) => FormGroup( + { + idControlName: FormControl( + value: userProfile?.id, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + firstNameControlName: FormControl( + value: userProfile?.firstName, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + lastNameControlName: FormControl( + value: userProfile?.lastName, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + homeControlName: AddressForm.formElements(userProfile?.home), + officeControlName: AddressForm.formElements(userProfile?.office), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } final _logAddressForm = Logger.detached('AddressForm'); class AddressForm implements FormModel { - AddressForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + AddressForm(this.form, this.path, this._formModel) : initial = form.rawValue; static const String streetControlName = "street"; @@ -879,7 +893,7 @@ class AddressForm implements FormModel { final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -893,20 +907,18 @@ class AddressForm implements FormModel { String zipControlPath() => pathBuilder(zipControlName); - String? get _streetValue => streetControl.value; + String? get _streetValue => containsStreet ? streetControl.value : null; - String? get _cityValue => cityControl.value; + String? get _cityValue => containsCity ? cityControl.value : null; - String? get _zipValue => zipControl.value; + String? get _zipValue => containsZip ? zipControl.value : null; - String? get _streetRawValue => streetControl.value; + String? get _streetRawValue => containsStreet ? streetControl.value : null; - String? get _cityRawValue => cityControl.value; + String? get _cityRawValue => containsCity ? cityControl.value : null; - String? get _zipRawValue => zipControl.value; + String? get _zipRawValue => containsZip ? zipControl.value : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsStreet { try { form.control(streetControlPath()); @@ -916,8 +928,6 @@ class AddressForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsCity { try { form.control(cityControlPath()); @@ -927,8 +937,6 @@ class AddressForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsZip { try { form.control(zipControlPath()); @@ -950,12 +958,7 @@ class AddressForm implements FormModel { void get zipFocus => form.focus(zipControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void streetRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void streetRemove({bool updateParent = true, bool emitEvent = true}) { if (containsStreet) { final controlPath = path; if (controlPath == null) { @@ -978,12 +981,7 @@ class AddressForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void cityRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void cityRemove({bool updateParent = true, bool emitEvent = true}) { if (containsCity) { final controlPath = path; if (controlPath == null) { @@ -1006,12 +1004,7 @@ class AddressForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void zipRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void zipRemove({bool updateParent = true, bool emitEvent = true}) { if (containsZip) { final controlPath = path; if (controlPath == null) { @@ -1039,8 +1032,11 @@ class AddressForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - streetControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + streetControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void cityValueUpdate( @@ -1048,8 +1044,11 @@ class AddressForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - cityControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + cityControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void zipValueUpdate( @@ -1057,8 +1056,11 @@ class AddressForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - zipControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + zipControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void streetValuePatch( @@ -1066,8 +1068,11 @@ class AddressForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - streetControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + streetControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void cityValuePatch( @@ -1075,8 +1080,11 @@ class AddressForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - cityControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + cityControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void zipValuePatch( @@ -1084,8 +1092,11 @@ class AddressForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - zipControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + zipControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void streetValueReset( @@ -1094,14 +1105,13 @@ class AddressForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - streetControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => streetControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void cityValueReset( String? value, { @@ -1109,14 +1119,13 @@ class AddressForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - cityControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => cityControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void zipValueReset( String? value, { @@ -1124,14 +1133,13 @@ class AddressForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - zipControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => zipControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get streetControl => form.control(streetControlPath()) as FormControl; @@ -1213,21 +1221,23 @@ class AddressForm implements FormModel { @override Address get rawModel { return Address( - street: _streetRawValue, city: _cityRawValue, zip: _zipRawValue); + street: _streetRawValue, + city: _cityRawValue, + zip: _zipRawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { @@ -1285,9 +1295,11 @@ class AddressForm implements FormModel { Address? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(AddressForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + AddressForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -1305,17 +1317,14 @@ class AddressForm implements FormModel { Address? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -1366,33 +1375,38 @@ class AddressForm implements FormModel { String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(Address? address) => FormGroup({ - streetControlName: FormControl( - value: address?.street, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - cityControlName: FormControl( - value: address?.city, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - zipControlName: FormControl( - value: address?.zip, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(Address? address) => FormGroup( + { + streetControlName: FormControl( + value: address?.street, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + cityControlName: FormControl( + value: address?.city, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + zipControlName: FormControl( + value: address?.zip, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } class ReactiveUserProfileFormArrayBuilder @@ -1405,29 +1419,41 @@ class ReactiveUserProfileFormArrayBuilder required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - UserProfileForm formModel)? control; + UserProfileForm formModel, + )? + control; - final Widget Function(BuildContext context, List itemList, - UserProfileForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + UserProfileForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveUserProfileFormArrayBuilderT? item, - UserProfileForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveUserProfileFormArrayBuilderT? item, + UserProfileForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -1443,18 +1469,9 @@ class ReactiveUserProfileFormArrayBuilder return ReactiveFormArrayItemBuilder( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1472,35 +1489,43 @@ class ReactiveUserProfileFormArrayBuilder2 required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - UserProfileForm formModel)? control; + UserProfileForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - UserProfileForm formModel - }) params)? builder; + ({BuildContext context, List itemList, UserProfileForm formModel}) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveUserProfileFormArrayBuilderT? item, - UserProfileForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveUserProfileFormArrayBuilderT? item, + UserProfileForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -1517,23 +1542,17 @@ class ReactiveUserProfileFormArrayBuilder2 formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1542,32 +1561,48 @@ class ReactiveUserProfileFormArrayBuilder2 } class ReactiveUserProfileFormFormGroupArrayBuilder< - ReactiveUserProfileFormFormGroupArrayBuilderT> extends StatelessWidget { + ReactiveUserProfileFormFormGroupArrayBuilderT +> + extends StatelessWidget { const ReactiveUserProfileFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(UserProfileForm formModel)? + getExtended; - final ExtendedControl?>, - List>? extended; - - final ExtendedControl?>, - List> - Function(UserProfileForm formModel)? getExtended; - - final Widget Function(BuildContext context, List itemList, - UserProfileForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + UserProfileForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveUserProfileFormFormGroupArrayBuilderT? item, - UserProfileForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveUserProfileFormFormGroupArrayBuilderT? item, + UserProfileForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -1585,23 +1620,14 @@ class ReactiveUserProfileFormFormGroupArrayBuilder< final itemList = (value.value() ?? []) .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) .values .toList(); - return builder?.call( - context, - itemList, - formModel, - ) ?? + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/reactive_forms_generator/example/lib/docs/user_profile/user_profile_output.gform.dart b/packages/reactive_forms_generator/example/lib/docs/user_profile/user_profile_output.gform.dart index 0eadc98e..0820b57e 100644 --- a/packages/reactive_forms_generator/example/lib/docs/user_profile/user_profile_output.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/user_profile/user_profile_output.gform.dart @@ -19,7 +19,11 @@ class ReactiveUserProfileOFormConsumer extends StatelessWidget { final Widget? child; final Widget Function( - BuildContext context, UserProfileOForm formModel, Widget? child) builder; + BuildContext context, + UserProfileOForm formModel, + Widget? child, + ) + builder; @override Widget build(BuildContext context) { @@ -38,11 +42,7 @@ class UserProfileOFormInheritedStreamer extends InheritedStreamer { required this.form, required Stream stream, required Widget child, - }) : super( - stream, - child, - key: key, - ); + }) : super(stream, child, key: key); final UserProfileOForm form; } @@ -64,19 +64,19 @@ class ReactiveUserProfileOForm extends StatelessWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; - static UserProfileOForm? of( - BuildContext context, { - bool listen = true, - }) { + static UserProfileOForm? of(BuildContext context, {bool listen = true}) { if (listen) { return context .dependOnInheritedWidgetOfExactType< - UserProfileOFormInheritedStreamer>() + UserProfileOFormInheritedStreamer + >() ?.form; } - final element = context.getElementForInheritedWidgetOfExactType< - UserProfileOFormInheritedStreamer>(); + final element = context + .getElementForInheritedWidgetOfExactType< + UserProfileOFormInheritedStreamer + >(); return element == null ? null : (element.widget as UserProfileOFormInheritedStreamer).form; @@ -124,10 +124,14 @@ class UserProfileOFormBuilder extends StatefulWidget { final void Function(FormGroup formGroup, bool didPop)? onPopInvoked; final Widget Function( - BuildContext context, UserProfileOForm formModel, Widget? child) builder; + BuildContext context, + UserProfileOForm formModel, + Widget? child, + ) + builder; final void Function(BuildContext context, UserProfileOForm formModel)? - initState; + initState; @override _UserProfileOFormBuilderState createState() => @@ -142,7 +146,10 @@ class _UserProfileOFormBuilderState extends State { @override void initState() { _formModel = UserProfileOForm( - UserProfileOForm.formElements(widget.model), null, null); + UserProfileOForm.formElements(widget.model), + null, + null, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -219,11 +226,8 @@ class _UserProfileOFormBuilderState extends State { final _logUserProfileOForm = Logger.detached('UserProfileOForm'); class UserProfileOForm implements FormModel { - UserProfileOForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + UserProfileOForm(this.form, this.path, this._formModel) + : initial = form.rawValue; static const String idControlName = "id"; @@ -239,7 +243,7 @@ class UserProfileOForm implements FormModel { final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -265,20 +269,20 @@ class UserProfileOForm implements FormModel { AddressOOutput get _homeValue => homeForm.model; - AddressOOutput? get _officeValue => officeForm.model; + AddressOOutput? get _officeValue => containsOffice ? officeForm.model : null; String get _idRawValue => idControl.value as String; - String? get _firstNameRawValue => firstNameControl.value; + String? get _firstNameRawValue => + containsFirstName ? firstNameControl.value : null; - String? get _lastNameRawValue => lastNameControl.value; + String? get _lastNameRawValue => + containsLastName ? lastNameControl.value : null; AddressO get _homeRawValue => homeForm.rawModel; - AddressO? get _officeRawValue => officeForm.rawModel; + AddressO? get _officeRawValue => containsOffice ? officeForm.rawModel : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsId { try { form.control(idControlPath()); @@ -288,8 +292,6 @@ class UserProfileOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsFirstName { try { form.control(firstNameControlPath()); @@ -299,8 +301,6 @@ class UserProfileOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsLastName { try { form.control(lastNameControlPath()); @@ -310,8 +310,6 @@ class UserProfileOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsHome { try { form.control(homeControlPath()); @@ -321,8 +319,6 @@ class UserProfileOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsOffice { try { form.control(officeControlPath()); @@ -352,12 +348,7 @@ class UserProfileOForm implements FormModel { void get officeFocus => form.focus(officeControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void firstNameRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void firstNameRemove({bool updateParent = true, bool emitEvent = true}) { if (containsFirstName) { final controlPath = path; if (controlPath == null) { @@ -380,12 +371,7 @@ class UserProfileOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void lastNameRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void lastNameRemove({bool updateParent = true, bool emitEvent = true}) { if (containsLastName) { final controlPath = path; if (controlPath == null) { @@ -408,12 +394,7 @@ class UserProfileOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void officeRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void officeRemove({bool updateParent = true, bool emitEvent = true}) { if (containsOffice) { final controlPath = path; if (controlPath == null) { @@ -441,8 +422,11 @@ class UserProfileOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - idControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + idControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void firstNameValueUpdate( @@ -450,8 +434,11 @@ class UserProfileOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - firstNameControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + firstNameControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void lastNameValueUpdate( @@ -459,8 +446,11 @@ class UserProfileOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - lastNameControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + lastNameControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void homeValueUpdate( @@ -468,8 +458,11 @@ class UserProfileOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - homeControl.updateValue(AddressOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + homeControl.updateValue( + AddressOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void officeValueUpdate( @@ -477,8 +470,11 @@ class UserProfileOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - officeControl.updateValue(AddressOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + officeControl.updateValue( + AddressOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void idValuePatch( @@ -486,8 +482,11 @@ class UserProfileOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - idControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + idControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void firstNameValuePatch( @@ -495,8 +494,11 @@ class UserProfileOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - firstNameControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + firstNameControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void lastNameValuePatch( @@ -504,8 +506,11 @@ class UserProfileOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - lastNameControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + lastNameControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void homeValuePatch( @@ -513,8 +518,11 @@ class UserProfileOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - homeControl.updateValue(AddressOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + homeControl.updateValue( + AddressOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void officeValuePatch( @@ -522,8 +530,11 @@ class UserProfileOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - officeControl.updateValue(AddressOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + officeControl.updateValue( + AddressOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void idValueReset( @@ -532,14 +543,13 @@ class UserProfileOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - idControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => idControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void firstNameValueReset( String? value, { @@ -547,14 +557,13 @@ class UserProfileOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - firstNameControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => firstNameControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void lastNameValueReset( String? value, { @@ -562,14 +571,13 @@ class UserProfileOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - lastNameControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => lastNameControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void homeValueReset( AddressO value, { @@ -577,11 +585,11 @@ class UserProfileOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - homeControl.reset( - value: AddressOForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => homeControl.reset( + value: AddressOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); void officeValueReset( AddressO? value, { @@ -589,11 +597,11 @@ class UserProfileOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - officeControl.reset( - value: AddressOForm.formElements(value).rawValue, - updateParent: updateParent, - emitEvent: emitEvent); + }) => officeControl.reset( + value: AddressOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); FormControl get idControl => form.control(idControlPath()) as FormControl; @@ -625,10 +633,7 @@ class UserProfileOForm implements FormModel { emitEvent: emitEvent, ); } else { - idControl.markAsEnabled( - updateParent: updateParent, - emitEvent: emitEvent, - ); + idControl.markAsEnabled(updateParent: updateParent, emitEvent: emitEvent); } } @@ -717,28 +722,27 @@ class UserProfileOForm implements FormModel { ); } return UserProfileOOutput( - id: _idValue, - firstName: _firstNameValue, - lastName: _lastNameValue, - home: _homeValue, - office: _officeValue); + id: _idValue, + firstName: _firstNameValue, + lastName: _lastNameValue, + home: _homeValue, + office: _officeValue, + ); } @override UserProfileO get rawModel { return UserProfileO( - id: _idRawValue, - firstName: _firstNameRawValue, - lastName: _lastNameRawValue, - home: _homeRawValue, - office: _officeRawValue); + id: _idRawValue, + firstName: _firstNameRawValue, + lastName: _lastNameRawValue, + home: _homeRawValue, + office: _officeRawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; @@ -747,7 +751,9 @@ class UserProfileOForm implements FormModel { homeForm.toggleDisabled(); officeForm.toggleDisabled(); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { homeForm.toggleDisabled(); officeForm.toggleDisabled(); @@ -807,9 +813,11 @@ class UserProfileOForm implements FormModel { UserProfileO? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(UserProfileOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + UserProfileOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -827,17 +835,14 @@ class UserProfileOForm implements FormModel { UserProfileO? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -888,45 +893,46 @@ class UserProfileOForm implements FormModel { String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(UserProfileO? userProfileO) => FormGroup({ - idControlName: FormControl( - value: userProfileO?.id, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - firstNameControlName: FormControl( - value: userProfileO?.firstName, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - lastNameControlName: FormControl( - value: userProfileO?.lastName, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - homeControlName: AddressOForm.formElements(userProfileO?.home), - officeControlName: AddressOForm.formElements(userProfileO?.office) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(UserProfileO? userProfileO) => FormGroup( + { + idControlName: FormControl( + value: userProfileO?.id, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + firstNameControlName: FormControl( + value: userProfileO?.firstName, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + lastNameControlName: FormControl( + value: userProfileO?.lastName, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + homeControlName: AddressOForm.formElements(userProfileO?.home), + officeControlName: AddressOForm.formElements(userProfileO?.office), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } final _logAddressOForm = Logger.detached('AddressOForm'); class AddressOForm implements FormModel { - AddressOForm( - this.form, - this.path, - this._formModel, - ) : initial = form.rawValue; + AddressOForm(this.form, this.path, this._formModel) : initial = form.rawValue; static const String streetControlName = "street"; @@ -938,7 +944,7 @@ class AddressOForm implements FormModel { final String? path; -// ignore: unused_field + // ignore: unused_field final FormModel? _formModel; final Map _disabled = {}; @@ -952,20 +958,18 @@ class AddressOForm implements FormModel { String zipControlPath() => pathBuilder(zipControlName); - String? get _streetValue => streetControl.value; + String? get _streetValue => containsStreet ? streetControl.value : null; String get _cityValue => cityControl.value as String; - String? get _zipValue => zipControl.value; + String? get _zipValue => containsZip ? zipControl.value : null; - String? get _streetRawValue => streetControl.value; + String? get _streetRawValue => containsStreet ? streetControl.value : null; - String? get _cityRawValue => cityControl.value; + String? get _cityRawValue => containsCity ? cityControl.value : null; - String? get _zipRawValue => zipControl.value; + String? get _zipRawValue => containsZip ? zipControl.value : null; - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsStreet { try { form.control(streetControlPath()); @@ -975,8 +979,6 @@ class AddressOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsCity { try { form.control(cityControlPath()); @@ -986,8 +988,6 @@ class AddressOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step') bool get containsZip { try { form.control(zipControlPath()); @@ -1009,12 +1009,7 @@ class AddressOForm implements FormModel { void get zipFocus => form.focus(zipControlPath()); - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void streetRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void streetRemove({bool updateParent = true, bool emitEvent = true}) { if (containsStreet) { final controlPath = path; if (controlPath == null) { @@ -1037,12 +1032,7 @@ class AddressOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void cityRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void cityRemove({bool updateParent = true, bool emitEvent = true}) { if (containsCity) { final controlPath = path; if (controlPath == null) { @@ -1065,12 +1055,7 @@ class AddressOForm implements FormModel { } } - @Deprecated( - 'Generator completely wraps the form so manual fields removal could lead to unexpected crashes') - void zipRemove({ - bool updateParent = true, - bool emitEvent = true, - }) { + void zipRemove({bool updateParent = true, bool emitEvent = true}) { if (containsZip) { final controlPath = path; if (controlPath == null) { @@ -1098,8 +1083,11 @@ class AddressOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - streetControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + streetControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void cityValueUpdate( @@ -1107,8 +1095,11 @@ class AddressOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - cityControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + cityControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void zipValueUpdate( @@ -1116,8 +1107,11 @@ class AddressOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - zipControl.updateValue(value, - updateParent: updateParent, emitEvent: emitEvent); + zipControl.updateValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void streetValuePatch( @@ -1125,8 +1119,11 @@ class AddressOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - streetControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + streetControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void cityValuePatch( @@ -1134,8 +1131,11 @@ class AddressOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - cityControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + cityControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void zipValuePatch( @@ -1143,8 +1143,11 @@ class AddressOForm implements FormModel { bool updateParent = true, bool emitEvent = true, }) { - zipControl.patchValue(value, - updateParent: updateParent, emitEvent: emitEvent); + zipControl.patchValue( + value, + updateParent: updateParent, + emitEvent: emitEvent, + ); } void streetValueReset( @@ -1153,14 +1156,13 @@ class AddressOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - streetControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => streetControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void cityValueReset( String? value, { @@ -1168,14 +1170,13 @@ class AddressOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - cityControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => cityControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); void zipValueReset( String? value, { @@ -1183,14 +1184,13 @@ class AddressOForm implements FormModel { bool emitEvent = true, bool removeFocus = false, bool? disabled, - }) => - zipControl.reset( - value: value, - updateParent: updateParent, - emitEvent: emitEvent, - removeFocus: removeFocus, - disabled: disabled, - ); + }) => zipControl.reset( + value: value, + updateParent: updateParent, + emitEvent: emitEvent, + removeFocus: removeFocus, + disabled: disabled, + ); FormControl get streetControl => form.control(streetControlPath()) as FormControl; @@ -1268,27 +1268,32 @@ class AddressOForm implements FormModel { ); } return AddressOOutput( - street: _streetValue, city: _cityValue, zip: _zipValue); + street: _streetValue, + city: _cityValue, + zip: _zipValue, + ); } @override AddressO get rawModel { return AddressO( - street: _streetRawValue, city: _cityRawValue, zip: _zipRawValue); + street: _streetRawValue, + city: _cityRawValue, + zip: _zipRawValue, + ); } @override - void toggleDisabled({ - bool updateParent = true, - bool emitEvent = true, - }) { + void toggleDisabled({bool updateParent = true, bool emitEvent = true}) { if (_disabled.isEmpty) { currentForm.controls.forEach((key, control) { _disabled[key] = control.disabled; }); currentForm.markAsDisabled( - updateParent: updateParent, emitEvent: emitEvent); + updateParent: updateParent, + emitEvent: emitEvent, + ); } else { currentForm.controls.forEach((key, control) { if (_disabled[key] == false) { @@ -1346,9 +1351,11 @@ class AddressOForm implements FormModel { AddressO? value, { bool updateParent = true, bool emitEvent = true, - }) => - currentForm.updateValue(AddressOForm.formElements(value).rawValue, - updateParent: updateParent, emitEvent: emitEvent); + }) => currentForm.updateValue( + AddressOForm.formElements(value).rawValue, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override void upsertValue( @@ -1366,17 +1373,14 @@ class AddressOForm implements FormModel { AddressO? value, bool updateParent = true, bool emitEvent = true, - }) => - currentForm.reset( - value: value != null ? formElements(value).rawValue : null, - updateParent: updateParent, - emitEvent: emitEvent); + }) => currentForm.reset( + value: value != null ? formElements(value).rawValue : null, + updateParent: updateParent, + emitEvent: emitEvent, + ); @override - void updateInitial( - Map? value, - String? path, - ) { + void updateInitial(Map? value, String? path) { if (_formModel != null) { _formModel?.updateInitial(currentForm.rawValue, path); return; @@ -1427,33 +1431,38 @@ class AddressOForm implements FormModel { String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); - static FormGroup formElements(AddressO? addressO) => FormGroup({ - streetControlName: FormControl( - value: addressO?.street, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - cityControlName: FormControl( - value: addressO?.city, - validators: [RequiredValidator()], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false), - zipControlName: FormControl( - value: addressO?.zip, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false, - touched: false) - }, - validators: [], - asyncValidators: [], - asyncValidatorsDebounceTime: 250, - disabled: false); + static FormGroup formElements(AddressO? addressO) => FormGroup( + { + streetControlName: FormControl( + value: addressO?.street, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + cityControlName: FormControl( + value: addressO?.city, + validators: [RequiredValidator()], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + zipControlName: FormControl( + value: addressO?.zip, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + touched: false, + ), + }, + validators: [], + asyncValidators: [], + asyncValidatorsDebounceTime: 250, + disabled: false, + ); } @Rf(output: true) @@ -1463,12 +1472,13 @@ class UserProfileOOutput { final String lastName; final AddressOOutput home; final AddressOOutput? office; - UserProfileOOutput( - {required this.id, - @RfControl(validators: [RequiredValidator()]) required this.firstName, - @RfControl(validators: [RequiredValidator()]) required this.lastName, - required this.home, - this.office}); + UserProfileOOutput({ + required this.id, + @RfControl(validators: [RequiredValidator()]) required this.firstName, + @RfControl(validators: [RequiredValidator()]) required this.lastName, + required this.home, + this.office, + }); } @RfGroup() @@ -1476,14 +1486,17 @@ class AddressOOutput { final String? street; final String city; final String? zip; - AddressOOutput( - {@RfControl() this.street, - @RfControl(validators: [RequiredValidator()]) required this.city, - @RfControl() this.zip}); + AddressOOutput({ + @RfControl() this.street, + @RfControl(validators: [RequiredValidator()]) required this.city, + @RfControl() this.zip, + }); } class ReactiveUserProfileOFormArrayBuilder< - ReactiveUserProfileOFormArrayBuilderT> extends StatelessWidget { + ReactiveUserProfileOFormArrayBuilderT +> + extends StatelessWidget { const ReactiveUserProfileOFormArrayBuilder({ Key? key, this.control, @@ -1492,30 +1505,41 @@ class ReactiveUserProfileOFormArrayBuilder< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - UserProfileOForm formModel)? control; + UserProfileOForm formModel, + )? + control; - final Widget Function(BuildContext context, List itemList, - UserProfileOForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + UserProfileOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - FormControl control, - ReactiveUserProfileOFormArrayBuilderT? item, - UserProfileOForm formModel) itemBuilder; + BuildContext context, + int i, + FormControl control, + ReactiveUserProfileOFormArrayBuilderT? item, + UserProfileOForm formModel, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -1531,18 +1555,9 @@ class ReactiveUserProfileOFormArrayBuilder< return ReactiveFormArrayItemBuilder( formControl: formControl ?? control?.call(formModel), builder: builder != null - ? (context, itemList) => builder( - context, - itemList, - formModel, - ) + ? (context, itemList) => builder(context, itemList, formModel) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => + itemBuilder: (context, i, control, item) => itemBuilder(context, i, control, item, formModel), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1551,7 +1566,9 @@ class ReactiveUserProfileOFormArrayBuilder< } class ReactiveUserProfileOFormArrayBuilder2< - ReactiveUserProfileOFormArrayBuilderT> extends StatelessWidget { + ReactiveUserProfileOFormArrayBuilderT +> + extends StatelessWidget { const ReactiveUserProfileOFormArrayBuilder2({ Key? key, this.control, @@ -1560,36 +1577,43 @@ class ReactiveUserProfileOFormArrayBuilder2< required this.itemBuilder, this.emptyBuilder, this.controlFilter, - }) : assert(control != null || formControl != null, - "You have to specify `control` or `formControl`!"), - super(key: key); + }) : assert( + control != null || formControl != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); final FormArray? formControl; final FormArray? Function( - UserProfileOForm formModel)? control; + UserProfileOForm formModel, + )? + control; final Widget Function( - ({ - BuildContext context, - List itemList, - UserProfileOForm formModel - }) params)? builder; + ({BuildContext context, List itemList, UserProfileOForm formModel}) + params, + )? + builder; final Widget Function( - ({ - BuildContext context, - int i, - FormControl control, - ReactiveUserProfileOFormArrayBuilderT? item, - UserProfileOForm formModel - }) params) itemBuilder; + ({ + BuildContext context, + int i, + FormControl control, + ReactiveUserProfileOFormArrayBuilderT? item, + UserProfileOForm formModel, + }) + params, + ) + itemBuilder; final Widget Function(BuildContext context)? emptyBuilder; final bool Function( - FormControl control)? - controlFilter; + FormControl control, + )? + controlFilter; @override Widget build(BuildContext context) { @@ -1606,23 +1630,17 @@ class ReactiveUserProfileOFormArrayBuilder2< formControl: formControl ?? control?.call(formModel), builder: builder != null ? (context, itemList) => builder(( - context: context, - itemList: itemList, - formModel: formModel, - )) + context: context, + itemList: itemList, + formModel: formModel, + )) : null, - itemBuilder: ( - context, - i, - control, - item, - ) => - itemBuilder(( + itemBuilder: (context, i, control, item) => itemBuilder(( context: context, i: i, control: control, item: item, - formModel: formModel + formModel: formModel, )), emptyBuilder: emptyBuilder, controlFilter: controlFilter, @@ -1631,32 +1649,48 @@ class ReactiveUserProfileOFormArrayBuilder2< } class ReactiveUserProfileOFormFormGroupArrayBuilder< - ReactiveUserProfileOFormFormGroupArrayBuilderT> extends StatelessWidget { + ReactiveUserProfileOFormFormGroupArrayBuilderT +> + extends StatelessWidget { const ReactiveUserProfileOFormFormGroupArrayBuilder({ Key? key, this.extended, this.getExtended, this.builder, required this.itemBuilder, - }) : assert(extended != null || getExtended != null, - "You have to specify `control` or `formControl`!"), - super(key: key); - - final ExtendedControl?>, - List>? extended; - - final ExtendedControl?>, - List> - Function(UserProfileOForm formModel)? getExtended; + }) : assert( + extended != null || getExtended != null, + "You have to specify `control` or `formControl`!", + ), + super(key: key); + + final ExtendedControl< + List?>, + List + >? + extended; + + final ExtendedControl< + List?>, + List + > + Function(UserProfileOForm formModel)? + getExtended; - final Widget Function(BuildContext context, List itemList, - UserProfileOForm formModel)? builder; + final Widget Function( + BuildContext context, + List itemList, + UserProfileOForm formModel, + )? + builder; final Widget Function( - BuildContext context, - int i, - ReactiveUserProfileOFormFormGroupArrayBuilderT? item, - UserProfileOForm formModel) itemBuilder; + BuildContext context, + int i, + ReactiveUserProfileOFormFormGroupArrayBuilderT? item, + UserProfileOForm formModel, + ) + itemBuilder; @override Widget build(BuildContext context) { @@ -1671,26 +1705,18 @@ class ReactiveUserProfileOFormFormGroupArrayBuilder< return StreamBuilder?>?>( stream: value.control.valueChanges, builder: (context, snapshot) { - final itemList = (value.value() ?? - []) - .asMap() - .map((i, item) => MapEntry( - i, - itemBuilder( - context, - i, - item, - formModel, - ), - )) - .values - .toList(); - - return builder?.call( - context, - itemList, - formModel, - ) ?? + final itemList = + (value.value() ?? + []) + .asMap() + .map( + (i, item) => + MapEntry(i, itemBuilder(context, i, item, formModel)), + ) + .values + .toList(); + + return builder?.call(context, itemList, formModel) ?? Column(children: itemList); }, ); diff --git a/packages/reactive_forms_generator/example/lib/drawer.dart b/packages/reactive_forms_generator/example/lib/drawer.dart index 9394e6f8..6ee792de 100644 --- a/packages/reactive_forms_generator/example/lib/drawer.dart +++ b/packages/reactive_forms_generator/example/lib/drawer.dart @@ -51,6 +51,12 @@ class AppDrawer extends StatelessWidget { onTap: () => Navigator.of(context).pushReplacementNamed(Routes.group), ), + ListTile( + title: const Text('Group Contains'), + onTap: () => Navigator.of( + context, + ).pushReplacementNamed(Routes.groupContains), + ), ListTile( title: const Text('Mailing list'), onTap: () => Navigator.of( diff --git a/packages/reactive_forms_generator/example/lib/main.dart b/packages/reactive_forms_generator/example/lib/main.dart index 30afd6c9..023a5930 100644 --- a/packages/reactive_forms_generator/example/lib/main.dart +++ b/packages/reactive_forms_generator/example/lib/main.dart @@ -18,6 +18,7 @@ import 'package:example/docs/user_profile/user_profile_form.dart'; import 'package:flutter/material.dart'; import 'docs/animated_url_list/animated_url_list_form.dart'; +import 'docs/group/group_contains_form.dart'; void main() { runApp(const MyApp()); @@ -45,6 +46,7 @@ class MyApp extends StatelessWidget { const LoginExtendedNullableFormWidget(), Routes.arrayNullable: (_) => const ArrayNullableFormWidget(), Routes.group: (_) => const GroupFormWidget(), + Routes.groupContains: (_) => const GroupContainsFormWidget(), Routes.deliveryList: (_) => const DeliveryListFormWidget(), Routes.deliveryPoint: (_) => const DeliveryPointWidget(), Routes.freezed: (_) => const FreezedFormWidget(), @@ -55,7 +57,7 @@ class MyApp extends StatelessWidget { Routes.modelImplements: (_) => const ModelImplementsWidget(), Routes.nested: (_) => const NestedFormWidget(), }, - home: const AnimatedUrlListFormWidget(), + home: const GroupContainsFormWidget(), ); } } @@ -75,6 +77,8 @@ class Routes { static const group = '/group'; + static const groupContains = '/group-contains'; + static const deliveryList = '/delivery-list'; static const deliveryPoint = '/delivery-point'; diff --git a/packages/reactive_forms_generator/example/test/doc/login/login_form_test.dart b/packages/reactive_forms_generator/example/test/doc/login/login_form_test.dart index 8ae9b56e..89518a06 100644 --- a/packages/reactive_forms_generator/example/test/doc/login/login_form_test.dart +++ b/packages/reactive_forms_generator/example/test/doc/login/login_form_test.dart @@ -48,7 +48,10 @@ void main() { ); await tester.pumpAndSettle(); - await tester.enterText(find.byKey(email.itemKey), mockedLogin.email); + await tester.enterText( + find.byKey(email.itemKey), + mockedLogin.email ?? '', + ); await tester.enterText( find.byKey(password.itemKey), diff --git a/packages/reactive_forms_generator/lib/builder.dart b/packages/reactive_forms_generator/lib/builder.dart index 3618d20f..9163beb3 100644 --- a/packages/reactive_forms_generator/lib/builder.dart +++ b/packages/reactive_forms_generator/lib/builder.dart @@ -11,7 +11,7 @@ Builder reactiveFormsGenerator(BuilderOptions options) { if (options.config['format'] == false) return str; return DartFormatter( - languageVersion: DartFormatter.latestShortStyleLanguageVersion, + languageVersion: DartFormatter.latestLanguageVersion, ).format(str); }, '.gform.dart', diff --git a/packages/reactive_forms_generator/lib/src/extensions.dart b/packages/reactive_forms_generator/lib/src/extensions.dart index c76c82e1..2485191d 100644 --- a/packages/reactive_forms_generator/lib/src/extensions.dart +++ b/packages/reactive_forms_generator/lib/src/extensions.dart @@ -229,6 +229,11 @@ extension ParameterElementExt on FormalParameterElement { (type as TypeImpl).appendTo(builder); return builder.toString(); } + + bool isOutputNullable(List requiredValidators) { + final type = toReferenceType(requiredValidators); + return type.endsWith('?'); + } } extension FieldElementExt on FieldElement { diff --git a/packages/reactive_forms_generator/lib/src/reactive_forms_generator/contains_method.dart b/packages/reactive_forms_generator/lib/src/reactive_forms_generator/contains_method.dart index 3fdaaa2b..16c7b57c 100644 --- a/packages/reactive_forms_generator/lib/src/reactive_forms_generator/contains_method.dart +++ b/packages/reactive_forms_generator/lib/src/reactive_forms_generator/contains_method.dart @@ -11,13 +11,6 @@ class ContainsMethod extends ReactiveFormGeneratorMethod { ..name = field.containsMethodName ..type = MethodType.getter ..returns = const Reference('bool') - ..annotations.add( - const CodeExpression( - Code( - "Deprecated('Generator completely wraps the form and ensures at startup that all controls are present inside the form so we do not need this additional step')", - ), - ), - ) ..body = Code('''try { form.control(${field.fieldControlPath}()); return true; diff --git a/packages/reactive_forms_generator/lib/src/reactive_forms_generator/field_value_method.dart b/packages/reactive_forms_generator/lib/src/reactive_forms_generator/field_value_method.dart index 0ee738cd..225f9bc9 100644 --- a/packages/reactive_forms_generator/lib/src/reactive_forms_generator/field_value_method.dart +++ b/packages/reactive_forms_generator/lib/src/reactive_forms_generator/field_value_method.dart @@ -17,11 +17,20 @@ import 'package:reactive_forms_generator/src/reactive_form_generator_method.dart class FieldValueMethod extends ReactiveFormGeneratorMethod { FieldValueMethod(super.field, super.output, super.requiredValidators); + Code _code(String code) { + if (field.isNullable && !output || + output && field.isOutputNullable(requiredValidators)) { + code = '${field.containsMethodName} ? $code : null'; + } + + return Code(code); + } + @override Method? formGroupMethod() { - return methodEntity.rebuild( - (b) => b..body = Code('${field.fieldName}Form.$fieldModelName'), - ); + var code = '${field.fieldName}Form.$fieldModelName'; + + return methodEntity.rebuild((b) => b..body = _code(code)); } @override @@ -31,13 +40,13 @@ class FieldValueMethod extends ReactiveFormGeneratorMethod { final code = '${field.fieldControlName}.rawValue.whereType<${type.getName(withNullability: true)}>().toList()'; - return methodEntity.rebuild((b) => b..body = Code(code)); + return methodEntity.rebuild((b) => b..body = _code(code)); } @override Method? formGroupArrayMethod() => methodEntity.rebuild( (b) => b - ..body = Code( + ..body = _code( '${field.name}${field.className}.map((e) => e.$fieldModelName).toList()', ), ); @@ -68,7 +77,7 @@ class FieldValueMethod extends ReactiveFormGeneratorMethod { } } - return methodEntity.rebuild((b) => b..body = Code(code)); + return methodEntity.rebuild((b) => b..body = _code(code)); } String get fieldValueName => field.fieldValueName; diff --git a/packages/reactive_forms_generator/lib/src/reactive_forms_generator/remove_method.dart b/packages/reactive_forms_generator/lib/src/reactive_forms_generator/remove_method.dart index 417eb608..30a23d9b 100644 --- a/packages/reactive_forms_generator/lib/src/reactive_forms_generator/remove_method.dart +++ b/packages/reactive_forms_generator/lib/src/reactive_forms_generator/remove_method.dart @@ -31,13 +31,6 @@ class RemoveMethod extends ReactiveFormGeneratorMethod { ..type = const Reference('bool'), ), ]) - ..annotations.add( - const CodeExpression( - Code( - "Deprecated('Generator completely wraps the form so manual fields removal could lead to unexpected crashes')", - ), - ), - ) ..returns = const Reference('void') ..body = Code(''' if (${field.containsMethodName}) { diff --git a/packages/reactive_forms_generator/pubspec.yaml b/packages/reactive_forms_generator/pubspec.yaml index ea45a04d..a17ddf77 100644 --- a/packages/reactive_forms_generator/pubspec.yaml +++ b/packages/reactive_forms_generator/pubspec.yaml @@ -2,7 +2,7 @@ name: reactive_forms_generator description: Generator for reactive_forms. Generates form classes based on model. repository: https://github.com/artflutter/reactive_forms_generator -version: 7.5.0-beta0 +version: 7.5.0-beta1 environment: sdk: ">=3.8.0 <4.0.0"