@strictly/react-form 0.0.9 → 0.0.10
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/form_presenter.d.ts +5 -5
- package/.out/core/mobx/form_presenter.js +8 -6
- package/.out/core/mobx/hooks.d.ts +24 -4
- package/.out/core/mobx/hooks.js +24 -3
- package/.out/core/mobx/specs/form_presenter.tests.js +10 -5
- package/.out/core/mobx/sub_form_field_adapters.d.ts +2 -2
- package/.out/mantine/create_fields_view.d.ts +9 -1
- package/.out/mantine/create_fields_view.js +13 -1
- package/.out/mantine/hooks.d.ts +2 -1
- package/.out/mantine/hooks.js +1 -1
- package/.out/mantine/specs/create_fields_view.tests.d.ts +1 -0
- package/.out/mantine/specs/create_fields_view.tests.js +17 -0
- package/.out/mantine/specs/fields_view_hooks.stories.d.ts +6 -2
- package/.out/mantine/specs/fields_view_hooks.stories.js +26 -7
- package/.out/mantine/specs/fields_view_hooks.tests.js +21 -1
- package/.out/tsconfig.tsbuildinfo +1 -1
- package/.turbo/turbo-build.log +7 -7
- package/.turbo/turbo-check-types.log +1 -1
- package/.turbo/turbo-release$colon$exports.log +1 -1
- package/core/mobx/form_presenter.ts +15 -14
- package/core/mobx/hooks.tsx +196 -0
- package/core/mobx/specs/form_presenter.tests.ts +24 -5
- package/core/mobx/sub_form_field_adapters.ts +14 -3
- package/dist/index.cjs +233 -197
- package/dist/index.d.cts +52 -26
- package/dist/index.d.ts +52 -26
- package/dist/index.js +232 -196
- package/mantine/create_fields_view.tsx +66 -31
- package/mantine/hooks.tsx +9 -6
- package/mantine/specs/__snapshots__/fields_view_hooks.tests.tsx.snap +194 -197
- package/mantine/specs/create_fields_view.tests.ts +29 -0
- package/mantine/specs/fields_view_hooks.stories.tsx +58 -15
- package/mantine/specs/fields_view_hooks.tests.tsx +26 -0
- package/package.json +1 -1
- package/core/mobx/hooks.ts +0 -112
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { type StringConcatOf } from '@strictly/base'
|
|
1
2
|
import type { FieldsViewProps } from 'core/props'
|
|
2
3
|
import { observer } from 'mobx-react'
|
|
3
4
|
import type {
|
|
@@ -10,6 +11,26 @@ import type { SubFormFields } from 'types/sub_form_fields'
|
|
|
10
11
|
import type { ValueTypeOfField } from 'types/value_type_of_field'
|
|
11
12
|
import type { MantineFieldComponent } from './types'
|
|
12
13
|
|
|
14
|
+
export type CallbackMapper<ValuePath extends string> = {
|
|
15
|
+
<
|
|
16
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
17
|
+
Cb extends (...args: any[]) => any,
|
|
18
|
+
>(cb: Cb): Parameters<Cb> extends [infer SubFormValuePath extends string, ...(infer Rest)]
|
|
19
|
+
? SubFormValuePath extends StringConcatOf<ValuePath, infer Postfix>
|
|
20
|
+
? (valuePath: `$${Postfix}`, ...rest: Rest) => ReturnType<Cb>
|
|
21
|
+
: never
|
|
22
|
+
: never,
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export type FieldsView<
|
|
26
|
+
ValuePath extends string = string,
|
|
27
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
28
|
+
C extends ComponentType<any> = ComponentType<any>,
|
|
29
|
+
> = {
|
|
30
|
+
Component: C,
|
|
31
|
+
callbackMapper: CallbackMapper<ValuePath>,
|
|
32
|
+
}
|
|
33
|
+
|
|
13
34
|
export function createFieldsView<
|
|
14
35
|
F extends Fields,
|
|
15
36
|
K extends keyof AllFieldsOfFields<F>,
|
|
@@ -18,7 +39,7 @@ export function createFieldsView<
|
|
|
18
39
|
valuePath: K,
|
|
19
40
|
FieldsView: ComponentType<P>,
|
|
20
41
|
observableProps: FieldsViewProps<F>,
|
|
21
|
-
): MantineFieldComponent<FieldsViewProps<P['fields']>, P, never
|
|
42
|
+
): FieldsView<K, MantineFieldComponent<FieldsViewProps<P['fields']>, P, never>> {
|
|
22
43
|
function toKey(subKey: string | number | symbol): string {
|
|
23
44
|
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
24
45
|
return (subKey as string).replace('$', valuePath as string)
|
|
@@ -49,36 +70,50 @@ export function createFieldsView<
|
|
|
49
70
|
}
|
|
50
71
|
|
|
51
72
|
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
(
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
73
|
+
const Component = observer(
|
|
74
|
+
function (props: ComponentProps<MantineFieldComponent<FieldsViewProps<P['fields']>, P, never>>) {
|
|
75
|
+
// convert fields to sub-fields
|
|
76
|
+
const subFields = Object.entries(observableProps.fields).reduce<Record<string, unknown>>(
|
|
77
|
+
(acc, [
|
|
78
|
+
fieldKey,
|
|
79
|
+
fieldValue,
|
|
80
|
+
]) => {
|
|
81
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
82
|
+
if (fieldKey.startsWith(valuePath as string)) {
|
|
83
|
+
acc[toSubKey(fieldKey)] = fieldValue
|
|
84
|
+
}
|
|
85
|
+
return acc
|
|
86
|
+
},
|
|
59
87
|
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
}
|
|
63
|
-
return acc
|
|
64
|
-
},
|
|
65
|
-
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
66
|
-
{} as P['fields'],
|
|
67
|
-
)
|
|
88
|
+
{} as P['fields'],
|
|
89
|
+
)
|
|
68
90
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
91
|
+
return (
|
|
92
|
+
<FieldsView
|
|
93
|
+
{
|
|
94
|
+
// maybe we can do this in a more type safe way
|
|
95
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/consistent-type-assertions
|
|
96
|
+
...props as any
|
|
97
|
+
}
|
|
98
|
+
fields={subFields}
|
|
99
|
+
onFieldBlur={onFieldBlur}
|
|
100
|
+
onFieldFocus={onFieldFocus}
|
|
101
|
+
onFieldSubmit={onFieldSubmit}
|
|
102
|
+
onFieldValueChange={onFieldValueChange}
|
|
103
|
+
/>
|
|
104
|
+
)
|
|
105
|
+
},
|
|
106
|
+
) as unknown as MantineFieldComponent<FieldsViewProps<P['fields']>, P, never>
|
|
107
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/consistent-type-assertions
|
|
108
|
+
const callbackMapper: CallbackMapper<K> = ((callback: (valuePath: string, ...args: any[]) => any) => {
|
|
109
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
110
|
+
return (subFormValuePath: string, ...args: any[]) => {
|
|
111
|
+
const valuePath = toKey(subFormValuePath)
|
|
112
|
+
return callback(valuePath, ...args)
|
|
113
|
+
}
|
|
114
|
+
}) as CallbackMapper<K>
|
|
115
|
+
return {
|
|
116
|
+
Component,
|
|
117
|
+
callbackMapper,
|
|
118
|
+
}
|
|
84
119
|
}
|
package/mantine/hooks.tsx
CHANGED
|
@@ -44,7 +44,10 @@ import {
|
|
|
44
44
|
createCheckbox,
|
|
45
45
|
type SuppliedCheckboxProps,
|
|
46
46
|
} from './create_checkbox'
|
|
47
|
-
import {
|
|
47
|
+
import {
|
|
48
|
+
createFieldsView,
|
|
49
|
+
type FieldsView,
|
|
50
|
+
} from './create_fields_view'
|
|
48
51
|
import { createForm } from './create_form'
|
|
49
52
|
import {
|
|
50
53
|
createList,
|
|
@@ -185,7 +188,7 @@ class MantineFormImpl<
|
|
|
185
188
|
// the cache cannot reference keys, so we just use any
|
|
186
189
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
187
190
|
[keyof AllFieldsOfFields<F>, ComponentType<any>, FieldsViewProps<F>],
|
|
188
|
-
|
|
191
|
+
FieldsView
|
|
189
192
|
> = new Cache(
|
|
190
193
|
createFieldsView.bind(this),
|
|
191
194
|
)
|
|
@@ -392,11 +395,11 @@ class MantineFormImpl<
|
|
|
392
395
|
fieldsView<
|
|
393
396
|
K extends keyof AllFieldsOfFields<F>,
|
|
394
397
|
P extends FieldsViewProps<Fields> = FieldsViewProps<SubFormFields<F, K>>,
|
|
395
|
-
>(valuePath: K, FieldsView: ComponentType<P>): MantineFieldComponent<
|
|
398
|
+
>(valuePath: K, FieldsView: ComponentType<P>): FieldsView<K, MantineFieldComponent<
|
|
396
399
|
FieldsViewProps<P['fields']>,
|
|
397
400
|
P,
|
|
398
401
|
never
|
|
399
|
-
|
|
402
|
+
>> {
|
|
400
403
|
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
401
404
|
return this.fieldsViewCache.retrieveOrCreate(
|
|
402
405
|
valuePath,
|
|
@@ -404,11 +407,11 @@ class MantineFormImpl<
|
|
|
404
407
|
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
405
408
|
FieldsView as ComponentType,
|
|
406
409
|
this,
|
|
407
|
-
) as unknown as MantineFieldComponent<
|
|
410
|
+
) as unknown as FieldsView<K, MantineFieldComponent<
|
|
408
411
|
FieldsViewProps<P['fields']>,
|
|
409
412
|
P,
|
|
410
413
|
never
|
|
411
|
-
|
|
414
|
+
>>
|
|
412
415
|
}
|
|
413
416
|
|
|
414
417
|
form<
|