@strictly/react-form 0.0.5 → 0.0.6
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/hooks.d.ts +10 -0
- package/.out/core/mobx/hooks.js +47 -0
- package/.out/core/props.d.ts +2 -2
- package/.out/index.d.ts +1 -0
- package/.out/index.js +1 -0
- package/.out/mantine/create_fields_view.d.ts +7 -0
- package/.out/mantine/{create_sub_form.js → create_fields_view.js} +4 -5
- package/.out/mantine/create_form.d.ts +7 -0
- package/.out/mantine/create_form.js +13 -0
- package/.out/mantine/hooks.d.ts +6 -4
- package/.out/mantine/hooks.js +17 -7
- package/.out/mantine/specs/checkbox_hooks.stories.d.ts +2 -2
- package/.out/mantine/specs/checkbox_hooks.stories.js +2 -2
- package/.out/mantine/specs/{sub_form_hooks.stories.d.ts → fields_view_hooks.stories.d.ts} +2 -2
- package/.out/mantine/specs/{sub_form_hooks.stories.js → fields_view_hooks.stories.js} +9 -8
- package/.out/mantine/specs/fields_view_hooks.tests.d.ts +1 -0
- package/.out/mantine/specs/fields_view_hooks.tests.js +12 -0
- package/.out/mantine/specs/form_hooks.stories.d.ts +12 -0
- package/.out/mantine/specs/form_hooks.stories.js +60 -0
- package/.out/mantine/specs/form_hooks.tests.d.ts +1 -0
- package/.out/mantine/specs/form_hooks.tests.js +12 -0
- package/.out/mantine/specs/list_hooks.stories.d.ts +2 -2
- package/.out/mantine/specs/list_hooks.stories.js +2 -2
- package/.out/mantine/specs/radio_group_hooks.stories.d.ts +2 -2
- package/.out/mantine/specs/radio_group_hooks.stories.js +2 -2
- package/.out/mantine/specs/select_hooks.stories.d.ts +2 -2
- package/.out/mantine/specs/select_hooks.stories.js +2 -2
- package/.out/mantine/specs/text_input_hooks.stories.d.ts +2 -2
- package/.out/mantine/specs/text_input_hooks.stories.js +2 -2
- package/.out/mantine/specs/value_input_hooks.stories.d.ts +2 -2
- package/.out/mantine/specs/value_input_hooks.stories.js +2 -2
- package/.out/tsconfig.tsbuildinfo +1 -1
- package/.turbo/turbo-build.log +8 -8
- package/.turbo/turbo-check-types.log +1 -1
- package/.turbo/turbo-release$colon$exports.log +1 -1
- package/core/mobx/hooks.ts +94 -0
- package/core/props.ts +2 -2
- package/dist/index.cjs +167 -77
- package/dist/index.d.cts +42 -34
- package/dist/index.d.ts +42 -34
- package/dist/index.js +162 -68
- package/index.ts +1 -0
- package/mantine/{create_sub_form.tsx → create_fields_view.tsx} +27 -16
- package/mantine/create_form.tsx +43 -0
- package/mantine/hooks.tsx +48 -14
- package/mantine/specs/__snapshots__/fields_view_hooks.tests.tsx.snap +460 -0
- package/mantine/specs/__snapshots__/form_hooks.tests.tsx.snap +273 -0
- package/mantine/specs/checkbox_hooks.stories.tsx +4 -4
- package/mantine/specs/{sub_form_hooks.stories.tsx → fields_view_hooks.stories.tsx} +23 -11
- package/mantine/specs/fields_view_hooks.tests.tsx +15 -0
- package/mantine/specs/form_hooks.stories.tsx +107 -0
- package/mantine/specs/form_hooks.tests.tsx +15 -0
- package/mantine/specs/list_hooks.stories.tsx +4 -4
- package/mantine/specs/radio_group_hooks.stories.tsx +4 -4
- package/mantine/specs/select_hooks.stories.tsx +4 -4
- package/mantine/specs/text_input_hooks.stories.tsx +4 -4
- package/mantine/specs/value_input_hooks.stories.tsx +4 -4
- package/package.json +1 -1
- package/.out/mantine/create_sub_form.d.ts +0 -6
|
@@ -1,20 +1,24 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { FieldsViewProps } from 'core/props'
|
|
2
2
|
import { observer } from 'mobx-react'
|
|
3
|
-
import type {
|
|
3
|
+
import type {
|
|
4
|
+
ComponentProps,
|
|
5
|
+
ComponentType,
|
|
6
|
+
} from 'react'
|
|
4
7
|
import type { AllFieldsOfFields } from 'types/all_fields_of_fields'
|
|
5
8
|
import type { Fields } from 'types/field'
|
|
6
9
|
import type { SubFormFields } from 'types/sub_form_fields'
|
|
7
10
|
import type { ValueTypeOfField } from 'types/value_type_of_field'
|
|
11
|
+
import type { MantineFieldComponent } from './types'
|
|
8
12
|
|
|
9
|
-
export function
|
|
13
|
+
export function createFieldsView<
|
|
10
14
|
F extends Fields,
|
|
11
15
|
K extends keyof AllFieldsOfFields<F>,
|
|
12
|
-
|
|
16
|
+
P extends FieldsViewProps<Fields> = FieldsViewProps<SubFormFields<F, K>>,
|
|
13
17
|
>(
|
|
14
18
|
valuePath: K,
|
|
15
|
-
|
|
16
|
-
observableProps:
|
|
17
|
-
) {
|
|
19
|
+
FieldsView: ComponentType<P>,
|
|
20
|
+
observableProps: FieldsViewProps<F>,
|
|
21
|
+
): MantineFieldComponent<FieldsViewProps<P['fields']>, P> {
|
|
18
22
|
function toKey(subKey: string | number | symbol): string {
|
|
19
23
|
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
20
24
|
return (subKey as string).replace('$', valuePath as string)
|
|
@@ -25,25 +29,27 @@ export function createSubForm<
|
|
|
25
29
|
return (key as string).replace(valuePath as string, '$')
|
|
26
30
|
}
|
|
27
31
|
|
|
28
|
-
function onFieldValueChange<SubK extends keyof
|
|
32
|
+
function onFieldValueChange<SubK extends keyof P['fields']>(
|
|
29
33
|
subKey: SubK,
|
|
30
|
-
value: ValueTypeOfField<
|
|
34
|
+
value: ValueTypeOfField<P['fields'][SubK]>,
|
|
31
35
|
) {
|
|
32
36
|
// convert from subKey to key
|
|
33
37
|
observableProps.onFieldValueChange(toKey(subKey), value)
|
|
34
38
|
}
|
|
35
|
-
function onFieldBlur(subKey: keyof
|
|
39
|
+
function onFieldBlur(subKey: keyof P['fields']) {
|
|
36
40
|
observableProps.onFieldBlur?.(toKey(subKey))
|
|
37
41
|
}
|
|
38
42
|
|
|
39
|
-
function onFieldFocus(subKey: keyof
|
|
43
|
+
function onFieldFocus(subKey: keyof P['fields']) {
|
|
40
44
|
observableProps.onFieldFocus?.(toKey(subKey))
|
|
41
45
|
}
|
|
42
46
|
|
|
43
|
-
function onFieldSubmit(subKey: keyof
|
|
47
|
+
function onFieldSubmit(subKey: keyof P['fields']) {
|
|
44
48
|
observableProps.onFieldSubmit?.(toKey(subKey))
|
|
45
49
|
}
|
|
46
|
-
|
|
50
|
+
|
|
51
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
52
|
+
return observer(function (props: ComponentProps<MantineFieldComponent<FieldsViewProps<P['fields']>, P>>) {
|
|
47
53
|
// convert fields to sub-fields
|
|
48
54
|
const subFields = Object.entries(observableProps.fields).reduce<Record<string, unknown>>((acc, [
|
|
49
55
|
fieldKey,
|
|
@@ -57,14 +63,19 @@ export function createSubForm<
|
|
|
57
63
|
}, {})
|
|
58
64
|
|
|
59
65
|
return (
|
|
60
|
-
<
|
|
66
|
+
<FieldsView
|
|
67
|
+
{
|
|
68
|
+
// maybe we can do this in a more type safe way
|
|
69
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/consistent-type-assertions
|
|
70
|
+
...props as any
|
|
71
|
+
}
|
|
61
72
|
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
62
|
-
fields={subFields as
|
|
73
|
+
fields={subFields as P['fields']}
|
|
63
74
|
onFieldBlur={onFieldBlur}
|
|
64
75
|
onFieldFocus={onFieldFocus}
|
|
65
76
|
onFieldSubmit={onFieldSubmit}
|
|
66
77
|
onFieldValueChange={onFieldValueChange}
|
|
67
78
|
/>
|
|
68
79
|
)
|
|
69
|
-
})
|
|
80
|
+
}) as unknown as MantineFieldComponent<FieldsViewProps<P['fields']>, P>
|
|
70
81
|
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type FieldsViewProps,
|
|
3
|
+
type FormProps,
|
|
4
|
+
} from 'core/props'
|
|
5
|
+
import { observer } from 'mobx-react'
|
|
6
|
+
import {
|
|
7
|
+
type ComponentProps,
|
|
8
|
+
type ComponentType,
|
|
9
|
+
useCallback,
|
|
10
|
+
} from 'react'
|
|
11
|
+
import { type AllFieldsOfFields } from 'types/all_fields_of_fields'
|
|
12
|
+
import { type Fields } from 'types/field'
|
|
13
|
+
import { type ValueTypeOfField } from 'types/value_type_of_field'
|
|
14
|
+
import { type MantineFieldComponent } from './types'
|
|
15
|
+
|
|
16
|
+
export function createForm<
|
|
17
|
+
F extends Fields,
|
|
18
|
+
K extends keyof AllFieldsOfFields<F>,
|
|
19
|
+
P extends FormProps<ValueTypeOfField<F[K]>> = FormProps<ValueTypeOfField<F[K]>>,
|
|
20
|
+
>(
|
|
21
|
+
valuePath: K,
|
|
22
|
+
Form: ComponentType<P>,
|
|
23
|
+
observableProps: FieldsViewProps<F>,
|
|
24
|
+
): MantineFieldComponent<FormProps<ValueTypeOfField<F[K]>>, P> {
|
|
25
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
26
|
+
return observer((props: ComponentProps<MantineFieldComponent<FormProps<ValueTypeOfField<F[K]>>, P>>) => {
|
|
27
|
+
const { value } = observableProps.fields[valuePath]
|
|
28
|
+
const onValueChange = useCallback((value: ValueTypeOfField<F[K]>) => {
|
|
29
|
+
observableProps.onFieldValueChange(valuePath, value)
|
|
30
|
+
}, [])
|
|
31
|
+
return (
|
|
32
|
+
<Form
|
|
33
|
+
{
|
|
34
|
+
// maybe we can do this in a more type safe way
|
|
35
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/consistent-type-assertions
|
|
36
|
+
...props as any
|
|
37
|
+
}
|
|
38
|
+
onValueChange={onValueChange}
|
|
39
|
+
value={value}
|
|
40
|
+
/>
|
|
41
|
+
)
|
|
42
|
+
}) as MantineFieldComponent<FormProps<ValueTypeOfField<F[K]>>, P>
|
|
43
|
+
}
|
package/mantine/hooks.tsx
CHANGED
|
@@ -15,7 +15,10 @@ import {
|
|
|
15
15
|
Cache,
|
|
16
16
|
type ElementOfArray,
|
|
17
17
|
} from '@strictly/base'
|
|
18
|
-
import {
|
|
18
|
+
import {
|
|
19
|
+
type FieldsViewProps,
|
|
20
|
+
type FormProps,
|
|
21
|
+
} from 'core/props'
|
|
19
22
|
import {
|
|
20
23
|
observable,
|
|
21
24
|
runInAction,
|
|
@@ -41,6 +44,8 @@ import {
|
|
|
41
44
|
createCheckbox,
|
|
42
45
|
type SuppliedCheckboxProps,
|
|
43
46
|
} from './create_checkbox'
|
|
47
|
+
import { createFieldsView } from './create_fields_view'
|
|
48
|
+
import { createForm } from './create_form'
|
|
44
49
|
import {
|
|
45
50
|
createList,
|
|
46
51
|
DefaultList,
|
|
@@ -58,7 +63,6 @@ import {
|
|
|
58
63
|
createRadioGroup,
|
|
59
64
|
type SuppliedRadioGroupProps,
|
|
60
65
|
} from './create_radio_group'
|
|
61
|
-
import { createSubForm } from './create_sub_form'
|
|
62
66
|
import {
|
|
63
67
|
createTextInput,
|
|
64
68
|
type SuppliedTextInputProps,
|
|
@@ -78,7 +82,7 @@ function SimpleSelect(props: SelectProps & {
|
|
|
78
82
|
return <Select {...props} />
|
|
79
83
|
}
|
|
80
84
|
|
|
81
|
-
export function
|
|
85
|
+
export function useMantineFormFields<
|
|
82
86
|
F extends Fields,
|
|
83
87
|
>({
|
|
84
88
|
onFieldValueChange,
|
|
@@ -86,7 +90,7 @@ export function useMantineForm<
|
|
|
86
90
|
onFieldFocus,
|
|
87
91
|
onFieldSubmit,
|
|
88
92
|
fields,
|
|
89
|
-
}:
|
|
93
|
+
}: FieldsViewProps<F>): MantineFormImpl<F> {
|
|
90
94
|
const form = useMemo(
|
|
91
95
|
function () {
|
|
92
96
|
return new MantineFormImpl(fields)
|
|
@@ -177,14 +181,20 @@ class MantineFormImpl<
|
|
|
177
181
|
> = new Cache(
|
|
178
182
|
createList.bind(this),
|
|
179
183
|
)
|
|
180
|
-
private readonly
|
|
184
|
+
private readonly fieldsViewCache: Cache<
|
|
181
185
|
// the cache cannot reference keys, so we just use any
|
|
182
186
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
183
|
-
[keyof AllFieldsOfFields<F>, ComponentType<any>,
|
|
187
|
+
[keyof AllFieldsOfFields<F>, ComponentType<any>, FieldsViewProps<F>],
|
|
184
188
|
ComponentType
|
|
185
189
|
> = new Cache(
|
|
186
|
-
|
|
190
|
+
createFieldsView.bind(this),
|
|
187
191
|
)
|
|
192
|
+
private readonly formCache: Cache<
|
|
193
|
+
// the cache cannot reference keys, so we just use any
|
|
194
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
195
|
+
[keyof AllFieldsOfFields<F>, ComponentType<any>, FieldsViewProps<F>],
|
|
196
|
+
ComponentType
|
|
197
|
+
> = new Cache(createForm.bind(this))
|
|
188
198
|
|
|
189
199
|
@observable.ref
|
|
190
200
|
accessor fields: F
|
|
@@ -376,17 +386,41 @@ class MantineFormImpl<
|
|
|
376
386
|
>
|
|
377
387
|
}
|
|
378
388
|
|
|
379
|
-
|
|
380
|
-
subForm<
|
|
389
|
+
fieldsView<
|
|
381
390
|
K extends keyof AllFieldsOfFields<F>,
|
|
382
|
-
|
|
383
|
-
>(valuePath: K,
|
|
384
|
-
|
|
391
|
+
P extends FieldsViewProps<Fields> = FieldsViewProps<SubFormFields<F, K>>,
|
|
392
|
+
>(valuePath: K, FieldsView: ComponentType<P>): MantineFieldComponent<
|
|
393
|
+
FieldsViewProps<P['fields']>,
|
|
394
|
+
P
|
|
395
|
+
> {
|
|
396
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
397
|
+
return this.fieldsViewCache.retrieveOrCreate(
|
|
398
|
+
valuePath,
|
|
399
|
+
// strip props from component since we lose information in the cache
|
|
400
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
401
|
+
FieldsView as ComponentType,
|
|
402
|
+
this,
|
|
403
|
+
) as unknown as MantineFieldComponent<
|
|
404
|
+
FieldsViewProps<P['fields']>,
|
|
405
|
+
P
|
|
406
|
+
>
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
form<
|
|
410
|
+
K extends keyof AllFieldsOfFields<F>,
|
|
411
|
+
P extends FormProps<ValueTypeOfField<F[K]>> = FormProps<ValueTypeOfField<F[K]>>,
|
|
412
|
+
>(
|
|
413
|
+
valuePath: K,
|
|
414
|
+
Form: ComponentType<P>,
|
|
415
|
+
): MantineFieldComponent<FormProps<ValueTypeOfField<F[K]>>, P> {
|
|
416
|
+
// strip props from component since we lose information in the cache
|
|
417
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
418
|
+
return this.formCache.retrieveOrCreate(
|
|
385
419
|
valuePath,
|
|
386
420
|
// strip props from component since we lose information in the cache
|
|
387
421
|
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
388
|
-
|
|
422
|
+
Form as ComponentType,
|
|
389
423
|
this,
|
|
390
|
-
)
|
|
424
|
+
) as unknown as MantineFieldComponent<FormProps<ValueTypeOfField<F[K]>>, P>
|
|
391
425
|
}
|
|
392
426
|
}
|