@strictly/react-form 0.0.8 → 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.
Files changed (44) hide show
  1. package/.out/core/mobx/field_adapter_builder.d.ts +4 -0
  2. package/.out/core/mobx/field_adapter_builder.js +31 -0
  3. package/.out/core/mobx/form_presenter.d.ts +5 -5
  4. package/.out/core/mobx/form_presenter.js +8 -6
  5. package/.out/core/mobx/hooks.d.ts +24 -4
  6. package/.out/core/mobx/hooks.js +24 -3
  7. package/.out/core/mobx/specs/form_presenter.tests.js +10 -5
  8. package/.out/core/mobx/sub_form_field_adapters.d.ts +2 -2
  9. package/.out/field_converters/chain_field_converter.js +3 -3
  10. package/.out/mantine/create_fields_view.d.ts +9 -1
  11. package/.out/mantine/create_fields_view.js +13 -1
  12. package/.out/mantine/error_renderer.d.ts +7 -3
  13. package/.out/mantine/hooks.d.ts +2 -1
  14. package/.out/mantine/hooks.js +1 -1
  15. package/.out/mantine/specs/create_fields_view.tests.js +17 -0
  16. package/.out/mantine/specs/fields_view_hooks.stories.d.ts +6 -2
  17. package/.out/mantine/specs/fields_view_hooks.stories.js +26 -7
  18. package/.out/mantine/specs/fields_view_hooks.tests.js +21 -1
  19. package/.out/tsconfig.tsbuildinfo +1 -1
  20. package/.out/types/specs/error_of_field.tests.d.ts +1 -0
  21. package/.out/types/specs/{error_type_of_field.tests.js → error_of_field.tests.js} +1 -1
  22. package/.turbo/turbo-build.log +8 -8
  23. package/.turbo/turbo-check-types.log +1 -1
  24. package/core/mobx/field_adapter_builder.ts +71 -0
  25. package/core/mobx/form_presenter.ts +15 -14
  26. package/core/mobx/hooks.tsx +196 -0
  27. package/core/mobx/specs/form_presenter.tests.ts +24 -5
  28. package/core/mobx/sub_form_field_adapters.ts +14 -3
  29. package/dist/index.cjs +290 -220
  30. package/dist/index.d.cts +63 -32
  31. package/dist/index.d.ts +63 -32
  32. package/dist/index.js +288 -219
  33. package/field_converters/chain_field_converter.ts +3 -3
  34. package/mantine/create_fields_view.tsx +66 -31
  35. package/mantine/error_renderer.ts +12 -3
  36. package/mantine/hooks.tsx +9 -6
  37. package/mantine/specs/__snapshots__/fields_view_hooks.tests.tsx.snap +194 -197
  38. package/mantine/specs/create_fields_view.tests.ts +29 -0
  39. package/mantine/specs/fields_view_hooks.stories.tsx +58 -15
  40. package/mantine/specs/fields_view_hooks.tests.tsx +26 -0
  41. package/package.json +1 -1
  42. package/types/specs/{error_type_of_field.tests.ts → error_of_field.tests.ts} +1 -1
  43. package/core/mobx/hooks.ts +0 -112
  44. /package/.out/{types/specs/error_type_of_field.tests.d.ts → mantine/specs/create_fields_view.tests.d.ts} +0 -0
@@ -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
- return observer(function (props: ComponentProps<MantineFieldComponent<FieldsViewProps<P['fields']>, P, never>>) {
53
- // convert fields to sub-fields
54
- const subFields = Object.entries(observableProps.fields).reduce<Record<string, unknown>>(
55
- (acc, [
56
- fieldKey,
57
- fieldValue,
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
- if (fieldKey.startsWith(valuePath as string)) {
61
- acc[toSubKey(fieldKey)] = fieldValue
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
- return (
70
- <FieldsView
71
- {
72
- // maybe we can do this in a more type safe way
73
- // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/consistent-type-assertions
74
- ...props as any
75
- }
76
- fields={subFields}
77
- onFieldBlur={onFieldBlur}
78
- onFieldFocus={onFieldFocus}
79
- onFieldSubmit={onFieldSubmit}
80
- onFieldValueChange={onFieldValueChange}
81
- />
82
- )
83
- }) as unknown as MantineFieldComponent<FieldsViewProps<P['fields']>, P, never>
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
  }
@@ -1,15 +1,24 @@
1
1
  import { type ComponentType } from 'react'
2
+ import { type ErrorOfField } from 'types/error_of_field'
3
+ import { type Fields } from 'types/field'
2
4
 
3
- export type ErrorRendererProps<E> = {
5
+ type InternalErrorRendererProps<E> = {
4
6
  error: E,
5
7
  }
6
8
 
9
+ export type ErrorRendererProps<
10
+ F extends Fields,
11
+ K extends keyof Fields,
12
+ > = InternalErrorRendererProps<
13
+ ErrorOfField<F[K]>
14
+ >
15
+
7
16
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
8
- export type ErrorRenderer<E = any> = ComponentType<ErrorRendererProps<E>>
17
+ export type ErrorRenderer<E = any> = ComponentType<InternalErrorRendererProps<E>>
9
18
 
10
19
  export function DefaultErrorRenderer({
11
20
  error,
12
21
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
13
- }: ErrorRendererProps<any>) {
22
+ }: InternalErrorRendererProps<any>) {
14
23
  return JSON.stringify(error)
15
24
  }
package/mantine/hooks.tsx CHANGED
@@ -44,7 +44,10 @@ import {
44
44
  createCheckbox,
45
45
  type SuppliedCheckboxProps,
46
46
  } from './create_checkbox'
47
- import { createFieldsView } from './create_fields_view'
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
- ComponentType
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<