Skip to content

Commit

Permalink
Merge pull request #74 from netglade/fix/glade-model-provider-constru…
Browse files Browse the repository at this point in the history
…ctor-key

fix: GladeFormProvider is missing key property #73
  • Loading branch information
mathew65 authored Nov 21, 2024
2 parents ce2e195 + 953864b commit 9ad0082
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 22 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ jobs:
uses: CQLabs/setup-dcm@v1
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
version: 1.24.2

- uses: bluefireteam/melos-action@v2

Expand Down
1 change: 1 addition & 0 deletions dcm_global.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
version: "1.24.2"
2 changes: 1 addition & 1 deletion glade_forms/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ On each input we can define
- **validator** - Input's value must satisfy validation to be *valid* input.
- **translateError** - If there are validation errors, this function is use to translate those errors.
- **dependencies** (WIP) - Each input can depend on another inputs for listening changes.
- **stringTovalueConverter** - If input is used by TextField and `T` is not a `String`, value converter should be provided.
- **stringToValueConverter** - If input is used by TextField and `T` is not a `String`, value converter should be provided.
- **valueComparator** - Sometimes it is handy to provide `initialValue` which will be never updated after input is mutated. `valueComparator` should be provided to compare `initialValue` and `value` if `T` is not comparable type by default. Note that GladeForms handle deep equality of collections and assumes that complex types are comparable by values.
- **valueTransform** - transform `T` value into different `T` value. An example of usage can be sanitazation of string input (trim(),...).
- **defaultTranslation** - If error's translations are simple, the default translation settings can be set instead of custom `translateError` method.
Expand Down
34 changes: 17 additions & 17 deletions glade_forms/lib/src/core/glade_input.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ class GladeInput<T> {
final ValidatorInstance<T> validatorInstance;

@protected
final StringToTypeConverter<T>? stringTovalueConverter;
final StringToTypeConverter<T>? stringToValueConverter;

// ignore: prefer-correct-callback-field-name, ok name
final InputDependenciesFactory dependenciesFactory;

/// An input's identification.
///
/// Used within listener changes and dependency related funcions such as validation.
/// Used within listener changes and dependency related functions such as validation.
final String inputKey;

// ignore: prefer-correct-callback-field-name, ok name
Expand Down Expand Up @@ -120,7 +120,7 @@ class GladeInput<T> {
bool get hasConversionError => __conversionError != null;

/// String representattion of [value].
String get stringValue => stringTovalueConverter?.convertBack(value) ?? value.toString();
String get stringValue => stringToValueConverter?.convertBack(value) ?? value.toString();

bool get _valueIsSameAsInitialValue {
if (identical(value, initialValue)) return true;
Expand Down Expand Up @@ -153,7 +153,7 @@ class GladeInput<T> {
required this.valueComparator,
required String? inputKey,
required this.translateError,
required this.stringTovalueConverter,
required this.stringToValueConverter,
required InputDependenciesFactory? dependenciesFactory,
required this.defaultTranslations,
required this.onChange,
Expand All @@ -178,7 +178,7 @@ class GladeInput<T> {
? TextEditingController(
text: switch (value) {
final String? x => x,
!= null => stringTovalueConverter?.convertBack(value),
!= null => stringToValueConverter?.convertBack(value),
_ => null,
},
)
Expand Down Expand Up @@ -234,7 +234,7 @@ class GladeInput<T> {
translateError: translateError,
valueComparator: valueComparator,
inputKey: inputKey,
stringTovalueConverter: valueConverter,
stringToValueConverter: valueConverter,
dependenciesFactory: dependencies,
onChange: onChange,
onDependencyChange: onDependencyChange,
Expand Down Expand Up @@ -358,7 +358,7 @@ class GladeInput<T> {
valueComparator: valueComparator,
inputKey: inputKey,
dependenciesFactory: dependencies,
stringTovalueConverter: GladeTypeConverters.intConverter,
stringToValueConverter: GladeTypeConverters.intConverter,
onChange: onChange,
onDependencyChange: onDependencyChange,
textEditingController: textEditingController,
Expand Down Expand Up @@ -439,7 +439,7 @@ class GladeInput<T> {
textEditingController: textEditingController,
useTextEditingController: useTextEditingController,
valueComparator: valueComparator,
stringTovalueConverter: null,
stringToValueConverter: null,
valueTransform: valueTransform,
trackUnchanged: trackUnchanged,
);
Expand All @@ -457,7 +457,7 @@ class GladeInput<T> {
// ignore: avoid-non-null-assertion, it is not null
if (hasConversionError) return _translateConversionError(__conversionError!);

return validatorResult.isInvalid ? _translate() ?? '' : '';
return validatorResult.isInvalid ? (_translate() ?? '') : '';
}

/// Shorthand validator for TextFieldForm inputs.
Expand All @@ -466,10 +466,10 @@ class GladeInput<T> {
/// If there are multiple errors they are concenated into one string with [delimiter].
String? textFormFieldInputValidatorCustom(String? value, {String delimiter = '.'}) {
assert(
TypeHelper.typesEqual<T, String>() || TypeHelper.typesEqual<T, String?>() || stringTovalueConverter != null,
TypeHelper.typesEqual<T, String>() || TypeHelper.typesEqual<T, String?>() || stringToValueConverter != null,
'For non-string values [converter] must be provided. TInput type: $T',
);
final converter = stringTovalueConverter ?? _defaultConverter;
final converter = stringToValueConverter ?? _defaultConverter;

try {
final convertedValue = converter.convert(value);
Expand Down Expand Up @@ -497,11 +497,11 @@ class GladeInput<T> {

void updateValueWithString(String? strValue, {bool shouldTriggerOnChange = true}) {
assert(
TypeHelper.typesEqual<T, String>() || TypeHelper.typesEqual<T, String?>() || stringTovalueConverter != null,
TypeHelper.typesEqual<T, String>() || TypeHelper.typesEqual<T, String?>() || stringToValueConverter != null,
'For non-string values [converter] must be provided. TInput type: ${T.runtimeType}',
);

final converter = stringTovalueConverter ?? _defaultConverter;
final converter = stringToValueConverter ?? _defaultConverter;

try {
if (_useTextEditingController) {
Expand Down Expand Up @@ -589,7 +589,7 @@ class GladeInput<T> {
String? inputKey,
ValueComparator<T>? valueComparator,
ValidatorInstance<T>? validatorInstance,
StringToTypeConverter<T>? stringTovalueConverter,
StringToTypeConverter<T>? stringToValueConverter,
InputDependenciesFactory? dependenciesFactory,
T? initialValue,
ErrorTranslator<T>? translateError,
Expand All @@ -608,7 +608,7 @@ class GladeInput<T> {
value: value ?? this.value,
valueComparator: valueComparator ?? this.valueComparator,
validatorInstance: validatorInstance ?? this.validatorInstance,
stringTovalueConverter: stringTovalueConverter ?? this.stringTovalueConverter,
stringToValueConverter: stringToValueConverter ?? this.stringToValueConverter,
dependenciesFactory: dependenciesFactory ?? this.dependenciesFactory,
inputKey: inputKey ?? this.inputKey,
initialValue: initialValue ?? this.initialValue,
Expand All @@ -629,7 +629,7 @@ class GladeInput<T> {
}

void _syncValueWithController(T value, {required bool shouldTriggerOnChange}) {
final converter = stringTovalueConverter ?? _defaultConverter;
final converter = stringToValueConverter ?? _defaultConverter;
try {
_controllerTriggersOnChange = shouldTriggerOnChange;

Expand All @@ -647,7 +647,7 @@ class GladeInput<T> {

// If using text controller - sync its value
void _onTextControllerChange() {
final converter = stringTovalueConverter ?? _defaultConverter;
final converter = stringToValueConverter ?? _defaultConverter;

try {
final convertedValue = converter.convert(controller?.text);
Expand Down
8 changes: 4 additions & 4 deletions glade_forms/lib/src/widgets/glade_model_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ class GladeModelProvider<M extends GladeModel> extends StatelessWidget {
final M? value;
final Widget child;

factory GladeModelProvider({required CreateModelFunction<M> create, required Widget child}) =>
GladeModelProvider._(create: create, child: child);
factory GladeModelProvider({required CreateModelFunction<M> create, required Widget child, Key? key}) =>
GladeModelProvider._(create: create, key: key, child: child);

const GladeModelProvider._({
required this.child,
Expand All @@ -20,8 +20,8 @@ class GladeModelProvider<M extends GladeModel> extends StatelessWidget {
super.key,
});

factory GladeModelProvider.value({required M value, required Widget child}) =>
GladeModelProvider._(value: value, child: child);
factory GladeModelProvider.value({required M value, required Widget child, Key? key}) =>
GladeModelProvider._(value: value, key: key, child: child);

@override
Widget build(BuildContext context) {
Expand Down

0 comments on commit 9ad0082

Please sign in to comment.