@strictly/react-form 0.0.12 → 0.0.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.out/core/mobx/field_adapter.d.ts +1 -0
- package/.out/core/mobx/form_model.d.ts +3 -1
- package/.out/core/mobx/form_model.js +23 -15
- package/.out/core/mobx/hooks.d.ts +2 -2
- package/.out/core/mobx/merge_field_adapters_with_two_way_converter.d.ts +2 -2
- package/.out/core/mobx/specs/form_model.tests.js +26 -21
- package/.out/core/mobx/specs/sub_form_field_adapters.tests.js +14 -21
- package/.out/core/mobx/sub_form_field_adapters.d.ts +5 -6
- package/.out/core/mobx/sub_form_field_adapters.js +6 -11
- package/.out/core/mobx/types.d.ts +3 -3
- package/.out/index.d.ts +2 -0
- package/.out/index.js +2 -0
- package/.out/mantine/create_fields_view.js +3 -2
- package/.out/mantine/field_view.d.ts +18 -0
- package/.out/mantine/field_view.js +16 -0
- package/.out/tsconfig.tsbuildinfo +1 -1
- package/.out/util/empty.d.ts +1 -0
- package/.out/util/empty.js +3 -0
- package/.turbo/turbo-build.log +8 -8
- package/.turbo/turbo-check-types.log +1 -1
- package/core/mobx/field_adapter.ts +9 -0
- package/core/mobx/form_model.ts +26 -16
- package/core/mobx/hooks.tsx +2 -2
- package/core/mobx/merge_field_adapters_with_two_way_converter.ts +2 -1
- package/core/mobx/specs/form_model.tests.ts +35 -20
- package/core/mobx/specs/sub_form_field_adapters.tests.ts +14 -34
- package/core/mobx/sub_form_field_adapters.ts +11 -26
- package/core/mobx/types.ts +10 -7
- package/dist/index.cjs +94 -61
- package/dist/index.d.cts +32 -12
- package/dist/index.d.ts +32 -12
- package/dist/index.js +83 -51
- package/index.ts +2 -0
- package/mantine/create_fields_view.tsx +6 -2
- package/mantine/field_view.tsx +39 -0
- package/package.json +1 -1
- package/util/empty.tsx +3 -0
package/dist/index.d.ts
CHANGED
|
@@ -50,6 +50,7 @@ type FromOfFieldAdapter<C extends FieldAdapter> = C extends FieldAdapter<infer F
|
|
|
50
50
|
type ToOfFieldAdapter<C extends FieldAdapter> = C extends FieldAdapter<infer _F, infer To> ? To : never;
|
|
51
51
|
type ErrorOfFieldAdapter<C extends FieldAdapter> = C extends FieldAdapter<infer _From, infer _To, infer E> ? NonNullable<E> : never;
|
|
52
52
|
type ValuePathOfFieldAdapter<C extends FieldAdapter> = C extends FieldAdapter<infer _From, infer _To, infer _E, infer ValuePath> ? ValuePath : never;
|
|
53
|
+
type ContextOfFieldAdapter<F extends FieldAdapter> = F extends FieldAdapter<infer _From, infer _To, infer _E, infer _P, infer Context> ? Context : never;
|
|
53
54
|
|
|
54
55
|
declare class FieldAdapterBuilder<From, To, E, ValuePath extends string, Context> {
|
|
55
56
|
readonly convert: AnnotatedFieldConverter<From, To, ValuePath, Context>;
|
|
@@ -117,7 +118,7 @@ type FlattenedErrors<ValuePathsToAdapters extends Readonly<Record<string, FieldA
|
|
|
117
118
|
type ValuePathsToAdaptersOf<TypePathsToAdapters extends Partial<Readonly<Record<string, FieldAdapter>>>, ValuePathsToTypePaths extends Readonly<Record<string, string>>> = keyof TypePathsToAdapters extends ValueOf<ValuePathsToTypePaths> ? {
|
|
118
119
|
readonly [K in keyof ValuePathsToTypePaths as unknown extends TypePathsToAdapters[ValuePathsToTypePaths[K]] ? never : K]: NonNullable<TypePathsToAdapters[ValuePathsToTypePaths[K]]>;
|
|
119
120
|
} : never;
|
|
120
|
-
declare class FormModel<T extends Type, ValueToTypePaths extends Readonly<Record<string, string>>, TypePathsToAdapters extends FlattenedTypePathsToAdaptersOf<FlattenedValuesOfType<T, '*'>,
|
|
121
|
+
declare abstract class FormModel<T extends Type, ValueToTypePaths extends Readonly<Record<string, string>>, TypePathsToAdapters extends FlattenedTypePathsToAdaptersOf<FlattenedValuesOfType<T, '*'>, ContextType>, ContextType = {}, ValuePathsToAdapters extends ValuePathsToAdaptersOf<TypePathsToAdapters, ValueToTypePaths> = ValuePathsToAdaptersOf<TypePathsToAdapters, ValueToTypePaths>> {
|
|
121
122
|
readonly type: T;
|
|
122
123
|
protected readonly adapters: TypePathsToAdapters;
|
|
123
124
|
accessor value: MobxValueOfType<T>;
|
|
@@ -125,6 +126,8 @@ declare class FormModel<T extends Type, ValueToTypePaths extends Readonly<Record
|
|
|
125
126
|
accessor errors: FlattenedErrors<ValuePathsToAdapters>;
|
|
126
127
|
private readonly flattenedTypeDefs;
|
|
127
128
|
constructor(type: T, value: ValueOfType<ReadonlyTypeOfType<T>>, adapters: TypePathsToAdapters);
|
|
129
|
+
get context(): ContextType;
|
|
130
|
+
protected abstract toContext(value: ValueOfType<ReadonlyTypeOfType<T>>): ContextType;
|
|
128
131
|
get fields(): SimplifyDeep<FlattenedConvertedFieldsOf<ValuePathsToAdapters>>;
|
|
129
132
|
private get knownFields();
|
|
130
133
|
private maybeSynthesizeFieldByValuePath;
|
|
@@ -150,12 +153,12 @@ declare class FormModel<T extends Type, ValueToTypePaths extends Readonly<Record
|
|
|
150
153
|
/**
|
|
151
154
|
* Used to extract the supported value paths from a presenter
|
|
152
155
|
*/
|
|
153
|
-
type ValuePathsOfModel<
|
|
156
|
+
type ValuePathsOfModel<Model extends FormModel<any, any, any, any, any>> = Model extends FormModel<infer _1, infer _2, infer _3, infer _4, infer ValuePathsToAdapters> ? keyof ValuePathsToAdapters : never;
|
|
154
157
|
/**
|
|
155
158
|
* Used to extract the render type (so the value that is passed to the view) of a given value path
|
|
156
159
|
* from a presenter
|
|
157
160
|
*/
|
|
158
|
-
type ToValueOfModelValuePath<
|
|
161
|
+
type ToValueOfModelValuePath<Model extends FormModel<any, any, any, any, any>, K extends ValuePathsOfModel<Model>> = Model extends FormModel<infer _1, infer _2, infer _3, infer _4, infer ValuePathsToAdapters> ? ToOfFieldAdapter<ValuePathsToAdapters[K]> : never;
|
|
159
162
|
/**
|
|
160
163
|
* Extracts the form fields from the presenter. The recommended way is to
|
|
161
164
|
* define the form fields explicitly and use that type to enforce the types
|
|
@@ -163,10 +166,10 @@ type ToValueOfModelValuePath<Presenter extends FormModel<any, any, any, any>, K
|
|
|
163
166
|
* is less typing, albeit at the cost of potentially getting type errors
|
|
164
167
|
* reported a long way away from the source
|
|
165
168
|
*/
|
|
166
|
-
type FormFieldsOfModel<
|
|
169
|
+
type FormFieldsOfModel<Model extends FormModel<any, any, any, any, any>> = Model extends FormModel<infer _1, infer _2, infer _3, infer _4, infer ValuePathsToAdapters> ? FlattenedConvertedFieldsOf<ValuePathsToAdapters> : never;
|
|
167
170
|
|
|
168
|
-
type ValueOfModel<M extends FormModel<any, any, any, any>> = M extends FormModel<infer T, any, any, any> ? ValueOfType<ReadonlyTypeOfType<T>> : never;
|
|
169
|
-
declare function useDefaultMobxFormHooks<M extends FormModel<any, any, any, any>, F extends FormFieldsOfModel<M> = FormFieldsOfModel<M>>(model: M, { onValidFieldSubmit, onValidFormSubmit, }?: {
|
|
171
|
+
type ValueOfModel<M extends FormModel<any, any, any, any, any>> = M extends FormModel<infer T, any, any, any, any> ? ValueOfType<ReadonlyTypeOfType<T>> : never;
|
|
172
|
+
declare function useDefaultMobxFormHooks<M extends FormModel<any, any, any, any, any>, F extends FormFieldsOfModel<M> = FormFieldsOfModel<M>>(model: M, { onValidFieldSubmit, onValidFormSubmit, }?: {
|
|
170
173
|
onValidFieldSubmit?: <Path extends ValuePathsOfModel<M>>(valuePath: Path) => void;
|
|
171
174
|
onValidFormSubmit?: (value: ValueOfModel<M>) => void;
|
|
172
175
|
}): {
|
|
@@ -178,7 +181,7 @@ declare function useDefaultMobxFormHooks<M extends FormModel<any, any, any, any>
|
|
|
178
181
|
};
|
|
179
182
|
|
|
180
183
|
type MergedOfFieldAdaptersWithTwoWayConverter<FieldAdapters extends Readonly<Record<string, FieldAdapter>>, E, Context> = {
|
|
181
|
-
[K in keyof FieldAdapters]: FieldAdapter<FromOfFieldAdapter<FieldAdapters[K]>, ToOfFieldAdapter<FieldAdapters[K]>, ErrorOfFieldAdapter<FieldAdapters[K]> | E, ValuePathOfFieldAdapter<FieldAdapters[K]>, Context>;
|
|
184
|
+
[K in keyof FieldAdapters]: FieldAdapter<FromOfFieldAdapter<FieldAdapters[K]>, ToOfFieldAdapter<FieldAdapters[K]>, ErrorOfFieldAdapter<FieldAdapters[K]> | E, ValuePathOfFieldAdapter<FieldAdapters[K]>, ContextOfFieldAdapter<FieldAdapters[K]> & Context>;
|
|
182
185
|
};
|
|
183
186
|
type ValuePathsOfFieldAdapters<FieldAdapters extends Readonly<Record<string, FieldAdapter>>> = {
|
|
184
187
|
[K in keyof FieldAdapters]: ValuePathOfFieldAdapter<FieldAdapters[K]>;
|
|
@@ -196,11 +199,11 @@ type MergedOfFieldAdaptersWithValidators<FieldAdapters extends Readonly<Record<K
|
|
|
196
199
|
type MergedOfFieldAdapterWithValidator<A extends FieldAdapter, V extends Validator | undefined> = undefined extends V ? A : A extends FieldAdapter<infer From, infer To, infer E1, infer P1, infer C1> ? V extends Validator<From, infer E2, infer P2, infer C2> ? FieldAdapter<From, To, E1 | E2, P1 | P2, C1 | C2> : never : never;
|
|
197
200
|
declare function mergeAdaptersWithValidators<FieldAdapters extends Readonly<Record<Key, FieldAdapter>>, Validators extends Readonly<Record<string, Validator>>, Key extends keyof Validators = keyof Validators>(adapters: FieldAdapters, validators: Validators): MergedOfFieldAdaptersWithValidators<FieldAdapters, Validators, Key>;
|
|
198
201
|
|
|
199
|
-
type SubFormFieldAdapter<F extends FieldAdapter, ValuePath extends string
|
|
200
|
-
type SubFormFieldAdapters<SubAdapters extends Record<string, FieldAdapter>, TypePath extends string, ValuePath extends string
|
|
201
|
-
[K in keyof SubAdapters as K extends StringConcatOf<'$', infer TypePathSuffix> ? `${TypePath}${TypePathSuffix}` : never]: SubFormFieldAdapter<SubAdapters[K], ValuePath
|
|
202
|
+
type SubFormFieldAdapter<F extends FieldAdapter, ValuePath extends string> = FieldAdapter<FromOfFieldAdapter<F>, ToOfFieldAdapter<F>, ErrorOfFieldAdapter<F>, ValuePathOfFieldAdapter<F> extends StringConcatOf<'$', infer ValuePathSuffix> ? `${ValuePath}${ValuePathSuffix}` : string, ContextOfFieldAdapter<F>>;
|
|
203
|
+
type SubFormFieldAdapters<SubAdapters extends Record<string, FieldAdapter>, TypePath extends string, ValuePath extends string> = {
|
|
204
|
+
[K in keyof SubAdapters as K extends StringConcatOf<'$', infer TypePathSuffix> ? `${TypePath}${TypePathSuffix}` : never]: SubFormFieldAdapter<SubAdapters[K], ValuePath>;
|
|
202
205
|
};
|
|
203
|
-
declare function subFormFieldAdapters<SubAdapters extends Record<string, FieldAdapter>, TypePath extends string, TypePathsToValuePaths extends Record<TypePath, string
|
|
206
|
+
declare function subFormFieldAdapters<SubAdapters extends Record<string, FieldAdapter>, TypePath extends string, TypePathsToValuePaths extends Record<TypePath, string>>(subAdapters: SubAdapters, parentTypePath: TypePath): SubFormFieldAdapters<SubAdapters, TypePath, TypePathsToValuePaths[TypePath]>;
|
|
204
207
|
|
|
205
208
|
type FieldsViewProps<F extends Fields> = {
|
|
206
209
|
fields: F;
|
|
@@ -280,6 +283,21 @@ declare function DefaultErrorRenderer({ error, }: InternalErrorRendererProps<any
|
|
|
280
283
|
|
|
281
284
|
type ValueTypeOfField<F extends Field> = F extends Field<infer V> ? V : never;
|
|
282
285
|
|
|
286
|
+
/**
|
|
287
|
+
* Displays current value and error of a field
|
|
288
|
+
*/
|
|
289
|
+
declare function FieldView<F extends Fields, K extends keyof F>({ fields, valuePath, children, }: {
|
|
290
|
+
fields: F;
|
|
291
|
+
valuePath: K;
|
|
292
|
+
children: (props: {
|
|
293
|
+
value: ValueTypeOfField<F[K]>;
|
|
294
|
+
error: ErrorOfField<F[K]> | undefined;
|
|
295
|
+
ErrorSink: ComponentType<{
|
|
296
|
+
error: ErrorOfField<F[K]>;
|
|
297
|
+
}>;
|
|
298
|
+
}) => JSX.Element;
|
|
299
|
+
}): react_jsx_runtime.JSX.Element;
|
|
300
|
+
|
|
283
301
|
type AllFieldsOfFields<F extends Fields> = {
|
|
284
302
|
[K in keyof F as ValueTypeOfField<F[K]> extends any ? K : never]: F[K];
|
|
285
303
|
};
|
|
@@ -422,4 +440,6 @@ type MergedOfValidators<Validators1 extends Partial<Readonly<Record<Keys, Valida
|
|
|
422
440
|
type MergedOfValidator<Validator1 extends Validator, Validator2 extends Validator> = Validator1 extends Validator<infer V, infer E1, infer P, infer C> ? Validator2 extends Validator<V, infer E2, P, C> ? Validator<V, E1 | E2, P, C> : never : never;
|
|
423
441
|
declare function mergeValidators<Validators1 extends Partial<Readonly<Record<Keys, Validator>>>, Validators2 extends Partial<Readonly<Record<Keys, Validator>>>, Keys extends string = Extract<keyof Validators1 | keyof Validators2, string>>(validators1: Validators1, validators2: Validators2): MergedOfValidators<Validators1, Validators2, Keys>;
|
|
424
442
|
|
|
425
|
-
|
|
443
|
+
declare function Empty(): null;
|
|
444
|
+
|
|
445
|
+
export { AbstractSelectValueTypeConverter, type AnnotatedFieldConversion, type AnnotatedFieldConverter, type Annotation, type ContextOfFieldAdapter, DefaultErrorRenderer, Empty, type ErrorOfField, type ErrorOfFieldAdapter, type ErrorRenderer, type ErrorRendererProps, type Field, type FieldAdapter, type FieldAdaptersOfValues, type FieldValueFactory, FieldView, type Fields, type FieldsViewProps, type FlattenedAdaptersOfFields, type FlattenedConvertedFieldsOf, type FlattenedTypePathsToAdaptersOf, type FormFieldsOfFieldAdapters, type FormFieldsOfModel, FormModel, type FormProps, type FromOfFieldAdapter, IntegerToStringConverter, type MergedOfFieldAdaptersWithTwoWayConverter, type MergedOfFieldAdaptersWithValidators, type MergedOfValidator, type MergedOfValidators, NullableToBooleanConverter, type PartialComponent, SelectDiscriminatedUnionConverter, SelectLiteralConverter, SelectStringConverter, type ToOfFieldAdapter, type ToValueOfModelValuePath, TrimmingStringConverter, type TwoWayFieldConverter, type TwoWayFieldConverterWithValueFactory, type UnreliableFieldConversion, UnreliableFieldConversionType, type UnreliableFieldConverter, type UnsafePartialComponent, type ValuePathOfFieldAdapter, type ValuePathsOfModel, type ValuePathsToAdaptersOf, adapter, adapterFromPrototype, adapterFromTwoWayConverter, createPartialComponent, createPartialObserverComponent, createSimplePartialComponent, createUnsafePartialObserverComponent, identityAdapter, listAdapter, mergeAdaptersWithValidators, mergeFieldAdaptersWithTwoWayConverter, mergeValidators, prototypingFieldValueFactory, subFormFieldAdapters, trimmingStringAdapter, useDefaultMobxFormHooks, useMantineFormFields, usePartialComponent, usePartialObserverComponent, validatingConverter };
|
package/dist/index.js
CHANGED
|
@@ -326,8 +326,8 @@ import {
|
|
|
326
326
|
observable,
|
|
327
327
|
runInAction
|
|
328
328
|
} from "mobx";
|
|
329
|
-
var _accessors_dec, _knownFields_dec, _fields_dec, _errors_dec, _fieldOverrides_dec, _value_dec, _init, _value, _fieldOverrides, _errors;
|
|
330
|
-
_value_dec = [observable.ref], _fieldOverrides_dec = [observable.shallow], _errors_dec = [observable.shallow], _fields_dec = [computed], _knownFields_dec = [computed], _accessors_dec = [computed];
|
|
329
|
+
var _accessors_dec, _knownFields_dec, _fields_dec, _context_dec, _errors_dec, _fieldOverrides_dec, _value_dec, _init, _value, _fieldOverrides, _errors;
|
|
330
|
+
_value_dec = [observable.ref], _fieldOverrides_dec = [observable.shallow], _errors_dec = [observable.shallow], _context_dec = [computed.struct], _fields_dec = [computed], _knownFields_dec = [computed], _accessors_dec = [computed];
|
|
331
331
|
var FormModel = class {
|
|
332
332
|
constructor(type, value, adapters) {
|
|
333
333
|
this.type = type;
|
|
@@ -339,6 +339,7 @@ var FormModel = class {
|
|
|
339
339
|
__publicField(this, "flattenedTypeDefs");
|
|
340
340
|
this.value = mobxCopy(type, value);
|
|
341
341
|
this.flattenedTypeDefs = flattenTypesOfType(type);
|
|
342
|
+
const contextValue = this.toContext(value);
|
|
342
343
|
const conversions = flattenValueTo(
|
|
343
344
|
type,
|
|
344
345
|
this.value,
|
|
@@ -356,13 +357,16 @@ var FormModel = class {
|
|
|
356
357
|
if (revert == null) {
|
|
357
358
|
return;
|
|
358
359
|
}
|
|
359
|
-
return convert(value2, valuePath,
|
|
360
|
+
return convert(value2, valuePath, contextValue);
|
|
360
361
|
}
|
|
361
362
|
);
|
|
362
363
|
this.fieldOverrides = map(conversions, function(_k, v) {
|
|
363
364
|
return v && [v.value];
|
|
364
365
|
});
|
|
365
366
|
}
|
|
367
|
+
get context() {
|
|
368
|
+
return this.toContext(this.value);
|
|
369
|
+
}
|
|
366
370
|
get fields() {
|
|
367
371
|
return new Proxy(
|
|
368
372
|
this.knownFields,
|
|
@@ -430,11 +434,11 @@ var FormModel = class {
|
|
|
430
434
|
accessor != null ? accessor.value : fieldTypeDef != null ? mobxCopy(
|
|
431
435
|
fieldTypeDef,
|
|
432
436
|
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
433
|
-
create(valuePath, this.
|
|
434
|
-
) : create(valuePath, this.
|
|
437
|
+
create(valuePath, this.context)
|
|
438
|
+
) : create(valuePath, this.context),
|
|
435
439
|
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
436
440
|
valuePath,
|
|
437
|
-
this.
|
|
441
|
+
this.context
|
|
438
442
|
);
|
|
439
443
|
const error = this.errors[valuePath];
|
|
440
444
|
return {
|
|
@@ -492,7 +496,7 @@ var FormModel = class {
|
|
|
492
496
|
const element = elementValue != null ? elementValue[0] : elementAdapter.create(
|
|
493
497
|
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
494
498
|
elementTypePath,
|
|
495
|
-
this.
|
|
499
|
+
this.context
|
|
496
500
|
);
|
|
497
501
|
const originalList = accessor.value;
|
|
498
502
|
const newList = [
|
|
@@ -610,7 +614,7 @@ var FormModel = class {
|
|
|
610
614
|
internalSetFieldValue(valuePath, value, displayValidation) {
|
|
611
615
|
const { revert } = this.getAdapterForValuePath(valuePath);
|
|
612
616
|
assertExists(revert, "setting value not supported {}", valuePath);
|
|
613
|
-
const conversion = revert(value, valuePath, this.
|
|
617
|
+
const conversion = revert(value, valuePath, this.context);
|
|
614
618
|
const accessor = this.getAccessorForValuePath(valuePath);
|
|
615
619
|
return runInAction(() => {
|
|
616
620
|
this.fieldOverrides[valuePath] = [value];
|
|
@@ -650,10 +654,10 @@ var FormModel = class {
|
|
|
650
654
|
convert,
|
|
651
655
|
create
|
|
652
656
|
} = adapter2;
|
|
653
|
-
const value = create(valuePath, this.
|
|
657
|
+
const value = create(valuePath, this.context);
|
|
654
658
|
const {
|
|
655
659
|
value: displayValue
|
|
656
|
-
} = convert(value, valuePath, this.
|
|
660
|
+
} = convert(value, valuePath, this.context);
|
|
657
661
|
const key = valuePath;
|
|
658
662
|
runInAction(() => {
|
|
659
663
|
this.fieldOverrides[key] = [displayValue];
|
|
@@ -682,10 +686,10 @@ var FormModel = class {
|
|
|
682
686
|
const {
|
|
683
687
|
value: storedValue
|
|
684
688
|
} = convert(
|
|
685
|
-
accessor != null ? accessor.value : create(valuePath, this.
|
|
689
|
+
accessor != null ? accessor.value : create(valuePath, this.context),
|
|
686
690
|
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
687
691
|
valuePath,
|
|
688
|
-
this.
|
|
692
|
+
this.context
|
|
689
693
|
);
|
|
690
694
|
const value = fieldOverride != null ? fieldOverride[0] : storedValue;
|
|
691
695
|
const dirty = storedValue !== value;
|
|
@@ -693,12 +697,12 @@ var FormModel = class {
|
|
|
693
697
|
if (ignoreDefaultValue) {
|
|
694
698
|
const {
|
|
695
699
|
value: defaultDisplayValue
|
|
696
|
-
} = convert(create(valuePath, this.
|
|
700
|
+
} = convert(create(valuePath, this.context), valuePath, this.context);
|
|
697
701
|
if (defaultDisplayValue === value) {
|
|
698
702
|
return true;
|
|
699
703
|
}
|
|
700
704
|
}
|
|
701
|
-
const conversion = revert(value, valuePath, this.
|
|
705
|
+
const conversion = revert(value, valuePath, this.context);
|
|
702
706
|
return runInAction(() => {
|
|
703
707
|
switch (conversion.type) {
|
|
704
708
|
case 1 /* Failure */:
|
|
@@ -743,10 +747,10 @@ var FormModel = class {
|
|
|
743
747
|
const fieldOverride = this.fieldOverrides[adapterPath];
|
|
744
748
|
const {
|
|
745
749
|
value: storedValue
|
|
746
|
-
} = convert(accessor.value, valuePath, this.
|
|
750
|
+
} = convert(accessor.value, valuePath, this.context);
|
|
747
751
|
const value = fieldOverride != null ? fieldOverride[0] : storedValue;
|
|
748
752
|
const dirty = fieldOverride != null && fieldOverride[0] !== storedValue;
|
|
749
|
-
const conversion = revert(value, valuePath, this.
|
|
753
|
+
const conversion = revert(value, valuePath, this.context);
|
|
750
754
|
switch (conversion.type) {
|
|
751
755
|
case 1 /* Failure */:
|
|
752
756
|
this.errors[adapterPath] = conversion.error;
|
|
@@ -776,6 +780,7 @@ _errors = new WeakMap();
|
|
|
776
780
|
__decorateElement(_init, 4, "value", _value_dec, FormModel, _value);
|
|
777
781
|
__decorateElement(_init, 4, "fieldOverrides", _fieldOverrides_dec, FormModel, _fieldOverrides);
|
|
778
782
|
__decorateElement(_init, 4, "errors", _errors_dec, FormModel, _errors);
|
|
783
|
+
__decorateElement(_init, 2, "context", _context_dec, FormModel);
|
|
779
784
|
__decorateElement(_init, 2, "fields", _fields_dec, FormModel);
|
|
780
785
|
__decorateElement(_init, 2, "knownFields", _knownFields_dec, FormModel);
|
|
781
786
|
__decorateElement(_init, 2, "accessors", _accessors_dec, FormModel);
|
|
@@ -915,19 +920,12 @@ function mergeAdaptersWithValidators(adapters, validators) {
|
|
|
915
920
|
}
|
|
916
921
|
|
|
917
922
|
// core/mobx/sub_form_field_adapters.ts
|
|
918
|
-
|
|
919
|
-
flattenValuesOfType as flattenValuesOfType2
|
|
920
|
-
} from "@strictly/define";
|
|
921
|
-
function subFormFieldAdapters(subAdapters, parentTypePath, contextType) {
|
|
923
|
+
function subFormFieldAdapters(subAdapters, parentTypePath) {
|
|
922
924
|
const dotCount = parentTypePath.split(".").length;
|
|
923
|
-
function
|
|
925
|
+
function getSubValuePath(valuePath) {
|
|
924
926
|
const parentValuePath = valuePath.split(".").slice(0, dotCount).join(".");
|
|
925
927
|
const subValuePath = valuePath.replace(parentValuePath, "$");
|
|
926
|
-
|
|
927
|
-
return [
|
|
928
|
-
subValuePath,
|
|
929
|
-
subContext
|
|
930
|
-
];
|
|
928
|
+
return subValuePath;
|
|
931
929
|
}
|
|
932
930
|
return Object.entries(subAdapters).reduce((acc, [
|
|
933
931
|
subTypePath,
|
|
@@ -936,13 +934,13 @@ function subFormFieldAdapters(subAdapters, parentTypePath, contextType) {
|
|
|
936
934
|
const typePath = subTypePath.replace("$", parentTypePath);
|
|
937
935
|
const adaptedAdapter = {
|
|
938
936
|
convert: (from, valuePath, context) => {
|
|
939
|
-
return subAdapter.convert(from,
|
|
937
|
+
return subAdapter.convert(from, getSubValuePath(valuePath), context);
|
|
940
938
|
},
|
|
941
939
|
create: (valuePath, context) => {
|
|
942
|
-
return subAdapter.create(
|
|
940
|
+
return subAdapter.create(getSubValuePath(valuePath), context);
|
|
943
941
|
},
|
|
944
942
|
revert: subAdapter.revert && ((from, valuePath, context) => {
|
|
945
|
-
return subAdapter.revert(from,
|
|
943
|
+
return subAdapter.revert(from, getSubValuePath(valuePath), context);
|
|
946
944
|
})
|
|
947
945
|
};
|
|
948
946
|
acc[typePath] = adaptedAdapter;
|
|
@@ -1151,6 +1149,34 @@ function DefaultErrorRenderer({
|
|
|
1151
1149
|
return JSON.stringify(error);
|
|
1152
1150
|
}
|
|
1153
1151
|
|
|
1152
|
+
// mantine/field_view.tsx
|
|
1153
|
+
import { Observer } from "mobx-react";
|
|
1154
|
+
|
|
1155
|
+
// util/empty.tsx
|
|
1156
|
+
function Empty() {
|
|
1157
|
+
return null;
|
|
1158
|
+
}
|
|
1159
|
+
|
|
1160
|
+
// mantine/field_view.tsx
|
|
1161
|
+
import { jsx } from "react/jsx-runtime";
|
|
1162
|
+
function FieldView({
|
|
1163
|
+
fields,
|
|
1164
|
+
valuePath,
|
|
1165
|
+
children
|
|
1166
|
+
}) {
|
|
1167
|
+
return /* @__PURE__ */ jsx(Observer, { children: () => {
|
|
1168
|
+
const {
|
|
1169
|
+
value,
|
|
1170
|
+
error
|
|
1171
|
+
} = fields[valuePath];
|
|
1172
|
+
return children({
|
|
1173
|
+
value,
|
|
1174
|
+
error,
|
|
1175
|
+
ErrorSink: Empty
|
|
1176
|
+
});
|
|
1177
|
+
} });
|
|
1178
|
+
}
|
|
1179
|
+
|
|
1154
1180
|
// mantine/hooks.tsx
|
|
1155
1181
|
import {
|
|
1156
1182
|
Checkbox as CheckboxImpl,
|
|
@@ -1177,12 +1203,12 @@ import {
|
|
|
1177
1203
|
forwardRef,
|
|
1178
1204
|
useMemo
|
|
1179
1205
|
} from "react";
|
|
1180
|
-
import { jsx } from "react/jsx-runtime";
|
|
1206
|
+
import { jsx as jsx2 } from "react/jsx-runtime";
|
|
1181
1207
|
function createSimplePartialComponent(Component, curriedProps) {
|
|
1182
1208
|
return forwardRef(
|
|
1183
1209
|
function(exposedProps, ref) {
|
|
1184
1210
|
const C = Component;
|
|
1185
|
-
return /* @__PURE__ */
|
|
1211
|
+
return /* @__PURE__ */ jsx2(
|
|
1186
1212
|
C,
|
|
1187
1213
|
__spreadValues(__spreadValues({
|
|
1188
1214
|
ref
|
|
@@ -1222,7 +1248,7 @@ function createPartialComponent(Component, curriedPropsSource, additionalPropKey
|
|
|
1222
1248
|
]
|
|
1223
1249
|
);
|
|
1224
1250
|
const curriedProps = curriedPropsSource(additionalProps);
|
|
1225
|
-
return /* @__PURE__ */
|
|
1251
|
+
return /* @__PURE__ */ jsx2(
|
|
1226
1252
|
C,
|
|
1227
1253
|
__spreadValues(__spreadValues({
|
|
1228
1254
|
ref
|
|
@@ -1289,7 +1315,7 @@ function createUnsafePartialObserverComponent(Component, curriedPropsSource, add
|
|
|
1289
1315
|
]
|
|
1290
1316
|
);
|
|
1291
1317
|
const curriedProps = curriedPropsSource(additionalProps);
|
|
1292
|
-
return /* @__PURE__ */
|
|
1318
|
+
return /* @__PURE__ */ jsx2(
|
|
1293
1319
|
C,
|
|
1294
1320
|
__spreadValues(__spreadValues({
|
|
1295
1321
|
ref
|
|
@@ -1320,7 +1346,7 @@ function usePartialObserverComponent(curriedPropsSource, deps, Component, additi
|
|
|
1320
1346
|
}
|
|
1321
1347
|
|
|
1322
1348
|
// mantine/create_checkbox.tsx
|
|
1323
|
-
import { jsx as
|
|
1349
|
+
import { jsx as jsx3 } from "react/jsx-runtime";
|
|
1324
1350
|
function createCheckbox(valuePath, Checkbox) {
|
|
1325
1351
|
const onChange = (e) => {
|
|
1326
1352
|
var _a;
|
|
@@ -1357,7 +1383,7 @@ function createCheckbox(valuePath, Checkbox) {
|
|
|
1357
1383
|
checked: value,
|
|
1358
1384
|
disabled: readonly,
|
|
1359
1385
|
required,
|
|
1360
|
-
error: error && /* @__PURE__ */
|
|
1386
|
+
error: error && /* @__PURE__ */ jsx3(ErrorRenderer, { error }),
|
|
1361
1387
|
onChange,
|
|
1362
1388
|
onFocus,
|
|
1363
1389
|
onBlur,
|
|
@@ -1372,14 +1398,18 @@ function createCheckbox(valuePath, Checkbox) {
|
|
|
1372
1398
|
}
|
|
1373
1399
|
|
|
1374
1400
|
// mantine/create_fields_view.tsx
|
|
1401
|
+
import {
|
|
1402
|
+
jsonPathPrefix,
|
|
1403
|
+
jsonPathUnprefix
|
|
1404
|
+
} from "@strictly/define";
|
|
1375
1405
|
import { observer as observer2 } from "mobx-react";
|
|
1376
|
-
import { jsx as
|
|
1406
|
+
import { jsx as jsx4 } from "react/jsx-runtime";
|
|
1377
1407
|
function createFieldsView(valuePath, FieldsView, observableProps) {
|
|
1378
1408
|
function toKey(subKey) {
|
|
1379
|
-
return
|
|
1409
|
+
return jsonPathPrefix(valuePath, subKey);
|
|
1380
1410
|
}
|
|
1381
1411
|
function toSubKey(key) {
|
|
1382
|
-
return
|
|
1412
|
+
return jsonPathUnprefix(valuePath, key);
|
|
1383
1413
|
}
|
|
1384
1414
|
function onFieldValueChange(subKey, value) {
|
|
1385
1415
|
observableProps.onFieldValueChange(toKey(subKey), value);
|
|
@@ -1411,7 +1441,7 @@ function createFieldsView(valuePath, FieldsView, observableProps) {
|
|
|
1411
1441
|
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
1412
1442
|
{}
|
|
1413
1443
|
);
|
|
1414
|
-
return /* @__PURE__ */
|
|
1444
|
+
return /* @__PURE__ */ jsx4(
|
|
1415
1445
|
FieldsView,
|
|
1416
1446
|
__spreadProps(__spreadValues({}, props), {
|
|
1417
1447
|
fields: subFields,
|
|
@@ -1440,14 +1470,14 @@ import { observer as observer3 } from "mobx-react";
|
|
|
1440
1470
|
import {
|
|
1441
1471
|
useCallback as useCallback2
|
|
1442
1472
|
} from "react";
|
|
1443
|
-
import { jsx as
|
|
1473
|
+
import { jsx as jsx5 } from "react/jsx-runtime";
|
|
1444
1474
|
function createForm(valuePath, Form, observableProps) {
|
|
1445
1475
|
return observer3((props) => {
|
|
1446
1476
|
const { value } = observableProps.fields[valuePath];
|
|
1447
1477
|
const onValueChange = useCallback2((value2) => {
|
|
1448
1478
|
observableProps.onFieldValueChange(valuePath, value2);
|
|
1449
1479
|
}, []);
|
|
1450
|
-
return /* @__PURE__ */
|
|
1480
|
+
return /* @__PURE__ */ jsx5(
|
|
1451
1481
|
Form,
|
|
1452
1482
|
__spreadProps(__spreadValues({}, props), {
|
|
1453
1483
|
onValueChange,
|
|
@@ -1458,7 +1488,7 @@ function createForm(valuePath, Form, observableProps) {
|
|
|
1458
1488
|
}
|
|
1459
1489
|
|
|
1460
1490
|
// mantine/create_list.tsx
|
|
1461
|
-
import { Fragment, jsx as
|
|
1491
|
+
import { Fragment, jsx as jsx6 } from "react/jsx-runtime";
|
|
1462
1492
|
function createList(valuePath, List) {
|
|
1463
1493
|
const propSource = () => {
|
|
1464
1494
|
const values = [...this.fields[valuePath].value];
|
|
@@ -1474,7 +1504,7 @@ function DefaultList({
|
|
|
1474
1504
|
listPath,
|
|
1475
1505
|
children
|
|
1476
1506
|
}) {
|
|
1477
|
-
return /* @__PURE__ */
|
|
1507
|
+
return /* @__PURE__ */ jsx6(Fragment, { children: values.map(function(value, index) {
|
|
1478
1508
|
const valuePath = `${listPath}.${index}`;
|
|
1479
1509
|
return children(valuePath, value, index);
|
|
1480
1510
|
}) });
|
|
@@ -1513,7 +1543,7 @@ function createRadio(valuePath, value, Radio) {
|
|
|
1513
1543
|
}
|
|
1514
1544
|
|
|
1515
1545
|
// mantine/create_radio_group.tsx
|
|
1516
|
-
import { jsx as
|
|
1546
|
+
import { jsx as jsx7 } from "react/jsx-runtime";
|
|
1517
1547
|
function createRadioGroup(valuePath, RadioGroup) {
|
|
1518
1548
|
const onChange = (value) => {
|
|
1519
1549
|
var _a;
|
|
@@ -1545,7 +1575,7 @@ function createRadioGroup(valuePath, RadioGroup) {
|
|
|
1545
1575
|
name: valuePath,
|
|
1546
1576
|
value,
|
|
1547
1577
|
required,
|
|
1548
|
-
error: error && /* @__PURE__ */
|
|
1578
|
+
error: error && /* @__PURE__ */ jsx7(ErrorRenderer, { error }),
|
|
1549
1579
|
onChange,
|
|
1550
1580
|
onFocus,
|
|
1551
1581
|
onBlur,
|
|
@@ -1556,7 +1586,7 @@ function createRadioGroup(valuePath, RadioGroup) {
|
|
|
1556
1586
|
}
|
|
1557
1587
|
|
|
1558
1588
|
// mantine/create_text_input.tsx
|
|
1559
|
-
import { jsx as
|
|
1589
|
+
import { jsx as jsx8 } from "react/jsx-runtime";
|
|
1560
1590
|
function createTextInput(valuePath, TextInput) {
|
|
1561
1591
|
const onChange = (e) => {
|
|
1562
1592
|
var _a;
|
|
@@ -1597,7 +1627,7 @@ function createTextInput(valuePath, TextInput) {
|
|
|
1597
1627
|
value,
|
|
1598
1628
|
disabled: readonly,
|
|
1599
1629
|
required,
|
|
1600
|
-
error: error && /* @__PURE__ */
|
|
1630
|
+
error: error && /* @__PURE__ */ jsx8(ErrorRenderer, { error }),
|
|
1601
1631
|
onChange,
|
|
1602
1632
|
onFocus,
|
|
1603
1633
|
onBlur,
|
|
@@ -1612,7 +1642,7 @@ function createTextInput(valuePath, TextInput) {
|
|
|
1612
1642
|
}
|
|
1613
1643
|
|
|
1614
1644
|
// mantine/create_value_input.tsx
|
|
1615
|
-
import { jsx as
|
|
1645
|
+
import { jsx as jsx9 } from "react/jsx-runtime";
|
|
1616
1646
|
function createValueInput(valuePath, ValueInput) {
|
|
1617
1647
|
const onChange = (value) => {
|
|
1618
1648
|
var _a;
|
|
@@ -1650,7 +1680,7 @@ function createValueInput(valuePath, ValueInput) {
|
|
|
1650
1680
|
value,
|
|
1651
1681
|
disabled: readonly,
|
|
1652
1682
|
required,
|
|
1653
|
-
error: error && /* @__PURE__ */
|
|
1683
|
+
error: error && /* @__PURE__ */ jsx9(ErrorRenderer, { error }),
|
|
1654
1684
|
onChange,
|
|
1655
1685
|
onFocus,
|
|
1656
1686
|
onBlur,
|
|
@@ -1665,9 +1695,9 @@ function createValueInput(valuePath, ValueInput) {
|
|
|
1665
1695
|
}
|
|
1666
1696
|
|
|
1667
1697
|
// mantine/hooks.tsx
|
|
1668
|
-
import { jsx as
|
|
1698
|
+
import { jsx as jsx10 } from "react/jsx-runtime";
|
|
1669
1699
|
function SimpleSelect(props) {
|
|
1670
|
-
return /* @__PURE__ */
|
|
1700
|
+
return /* @__PURE__ */ jsx10(Select, __spreadValues({}, props));
|
|
1671
1701
|
}
|
|
1672
1702
|
function useMantineFormFields({
|
|
1673
1703
|
onFieldValueChange,
|
|
@@ -1876,6 +1906,8 @@ function mergeValidators(validators1, validators2) {
|
|
|
1876
1906
|
export {
|
|
1877
1907
|
AbstractSelectValueTypeConverter,
|
|
1878
1908
|
DefaultErrorRenderer,
|
|
1909
|
+
Empty,
|
|
1910
|
+
FieldView,
|
|
1879
1911
|
FormModel,
|
|
1880
1912
|
IntegerToStringConverter,
|
|
1881
1913
|
NullableToBooleanConverter,
|
package/index.ts
CHANGED
|
@@ -17,9 +17,11 @@ export * from './field_converters/trimming_string_converter'
|
|
|
17
17
|
export * from './field_converters/validating_converter'
|
|
18
18
|
export * from './field_value_factories/prototyping_field_value_factory'
|
|
19
19
|
export * from './mantine/error_renderer'
|
|
20
|
+
export * from './mantine/field_view'
|
|
20
21
|
export * from './mantine/hooks'
|
|
21
22
|
export * from './types/error_of_field'
|
|
22
23
|
export * from './types/field'
|
|
23
24
|
export * from './types/field_converters'
|
|
24
25
|
export * from './types/merge_validators'
|
|
26
|
+
export * from './util/empty'
|
|
25
27
|
export * from './util/partial'
|
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
import { type StringConcatOf } from '@strictly/base'
|
|
2
|
+
import {
|
|
3
|
+
jsonPathPrefix,
|
|
4
|
+
jsonPathUnprefix,
|
|
5
|
+
} from '@strictly/define'
|
|
2
6
|
import type { FieldsViewProps } from 'core/props'
|
|
3
7
|
import { observer } from 'mobx-react'
|
|
4
8
|
import type {
|
|
@@ -42,12 +46,12 @@ export function createFieldsView<
|
|
|
42
46
|
): FieldsView<K, MantineFieldComponent<FieldsViewProps<P['fields']>, P, never>> {
|
|
43
47
|
function toKey(subKey: string | number | symbol): string {
|
|
44
48
|
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
45
|
-
return (
|
|
49
|
+
return jsonPathPrefix(valuePath, subKey as string)
|
|
46
50
|
}
|
|
47
51
|
|
|
48
52
|
function toSubKey(key: string | number | symbol): string {
|
|
49
53
|
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
50
|
-
return (key as string)
|
|
54
|
+
return jsonPathUnprefix(valuePath, key as string)
|
|
51
55
|
}
|
|
52
56
|
|
|
53
57
|
function onFieldValueChange<SubK extends keyof P['fields']>(
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { Observer } from 'mobx-react'
|
|
2
|
+
import { type ComponentType } from 'react'
|
|
3
|
+
import { type ErrorOfField } from 'types/error_of_field'
|
|
4
|
+
import { type Fields } from 'types/field'
|
|
5
|
+
import { type ValueTypeOfField } from 'types/value_type_of_field'
|
|
6
|
+
import { Empty } from 'util/empty'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Displays current value and error of a field
|
|
10
|
+
*/
|
|
11
|
+
export function FieldView<F extends Fields, K extends keyof F>({
|
|
12
|
+
fields,
|
|
13
|
+
valuePath,
|
|
14
|
+
children,
|
|
15
|
+
}: {
|
|
16
|
+
fields: F,
|
|
17
|
+
valuePath: K,
|
|
18
|
+
children: (props: {
|
|
19
|
+
value: ValueTypeOfField<F[K]>,
|
|
20
|
+
error: ErrorOfField<F[K]> | undefined,
|
|
21
|
+
ErrorSink: ComponentType<{ error: ErrorOfField<F[K]> }>,
|
|
22
|
+
}) => JSX.Element,
|
|
23
|
+
}) {
|
|
24
|
+
return (
|
|
25
|
+
<Observer>
|
|
26
|
+
{() => {
|
|
27
|
+
const {
|
|
28
|
+
value,
|
|
29
|
+
error,
|
|
30
|
+
} = fields[valuePath]
|
|
31
|
+
return children({
|
|
32
|
+
value,
|
|
33
|
+
error,
|
|
34
|
+
ErrorSink: Empty,
|
|
35
|
+
})
|
|
36
|
+
}}
|
|
37
|
+
</Observer>
|
|
38
|
+
)
|
|
39
|
+
}
|
package/package.json
CHANGED
package/util/empty.tsx
ADDED