@strictly/react-form 0.0.9 → 0.0.11

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 (60) hide show
  1. package/.out/core/mobx/field_adapter_builder.js +18 -6
  2. package/.out/core/mobx/form_presenter.d.ts +5 -5
  3. package/.out/core/mobx/form_presenter.js +232 -127
  4. package/.out/core/mobx/hooks.d.ts +24 -4
  5. package/.out/core/mobx/hooks.js +26 -5
  6. package/.out/core/mobx/merge_field_adapters_with_validators.js +1 -5
  7. package/.out/core/mobx/specs/fixtures.js +2 -1
  8. package/.out/core/mobx/specs/form_presenter.tests.js +16 -8
  9. package/.out/core/mobx/specs/sub_form_field_adapters.tests.js +2 -1
  10. package/.out/core/mobx/sub_form_field_adapters.d.ts +2 -2
  11. package/.out/field_converters/integer_to_string_converter.js +12 -4
  12. package/.out/field_converters/maybe_identity_converter.js +12 -4
  13. package/.out/field_converters/nullable_to_boolean_converter.js +24 -7
  14. package/.out/field_converters/select_value_type_converter.js +36 -12
  15. package/.out/mantine/create_checkbox.js +8 -4
  16. package/.out/mantine/create_fields_view.d.ts +9 -1
  17. package/.out/mantine/create_fields_view.js +20 -5
  18. package/.out/mantine/create_form.js +1 -1
  19. package/.out/mantine/create_radio_group.js +8 -4
  20. package/.out/mantine/create_text_input.js +8 -4
  21. package/.out/mantine/create_value_input.js +8 -4
  22. package/.out/mantine/hooks.d.ts +2 -1
  23. package/.out/mantine/hooks.js +219 -93
  24. package/.out/mantine/specs/checkbox_hooks.stories.js +13 -1
  25. package/.out/mantine/specs/checkbox_hooks.tests.js +22 -9
  26. package/.out/mantine/specs/create_fields_view.tests.d.ts +1 -0
  27. package/.out/mantine/specs/create_fields_view.tests.js +17 -0
  28. package/.out/mantine/specs/fields_view_hooks.stories.d.ts +6 -2
  29. package/.out/mantine/specs/fields_view_hooks.stories.js +39 -7
  30. package/.out/mantine/specs/fields_view_hooks.tests.js +30 -1
  31. package/.out/mantine/specs/radio_group_hooks.stories.js +13 -1
  32. package/.out/mantine/specs/radio_group_hooks.tests.js +23 -10
  33. package/.out/mantine/specs/select_hooks.stories.js +13 -1
  34. package/.out/mantine/specs/text_input_hooks.stories.js +13 -1
  35. package/.out/mantine/specs/text_input_hooks.tests.js +18 -7
  36. package/.out/mantine/specs/value_input_hooks.stories.js +14 -2
  37. package/.out/tsconfig.tsbuildinfo +1 -1
  38. package/.out/tsup.config.js +2 -9
  39. package/.out/types/merge_validators.js +1 -4
  40. package/.out/util/partial.js +5 -5
  41. package/.out/vitest.workspace.js +2 -10
  42. package/.turbo/turbo-build.log +9 -9
  43. package/.turbo/turbo-check-types.log +1 -1
  44. package/.turbo/turbo-release$colon$exports.log +1 -1
  45. package/core/mobx/form_presenter.ts +15 -14
  46. package/core/mobx/hooks.tsx +197 -0
  47. package/core/mobx/specs/form_presenter.tests.ts +24 -5
  48. package/core/mobx/sub_form_field_adapters.ts +14 -3
  49. package/dist/index.cjs +395 -277
  50. package/dist/index.d.cts +52 -26
  51. package/dist/index.d.ts +52 -26
  52. package/dist/index.js +398 -276
  53. package/mantine/create_fields_view.tsx +66 -31
  54. package/mantine/hooks.tsx +9 -6
  55. package/mantine/specs/__snapshots__/fields_view_hooks.tests.tsx.snap +194 -197
  56. package/mantine/specs/create_fields_view.tests.ts +29 -0
  57. package/mantine/specs/fields_view_hooks.stories.tsx +58 -15
  58. package/mantine/specs/fields_view_hooks.tests.tsx +26 -0
  59. package/package.json +1 -1
  60. package/core/mobx/hooks.ts +0 -112
@@ -1,15 +1,41 @@
1
1
  import { composeStories } from '@storybook/react'
2
2
  import { toArray } from '@strictly/base'
3
3
  import {
4
+ fireEvent,
4
5
  render,
5
6
  } from '@testing-library/react'
6
7
  import * as stories from './fields_view_hooks.stories'
7
8
 
8
9
  const composedStories = composeStories(stories)
10
+ const {
11
+ Empty,
12
+ } = composedStories
9
13
 
10
14
  describe('field view hooks', function () {
11
15
  it.each(toArray(composedStories))('renders %s', function (_name, Story) {
12
16
  const wrapper = render(<Story />)
13
17
  expect(wrapper.container).toMatchSnapshot()
14
18
  })
19
+
20
+ describe('callbackMapper', () => {
21
+ it.each(
22
+ [
23
+ [
24
+ '$',
25
+ stories.ParentFieldLabel(),
26
+ ],
27
+ [
28
+ '$.a',
29
+ stories.SubFieldLabel(),
30
+ ],
31
+ ],
32
+ )('calls back with the correct paths for field at %s', async (valuePath, labelText) => {
33
+ const onClickField = vi.fn()
34
+ const wrapper = render(<Empty onClickField={onClickField} />)
35
+ const element = await wrapper.findByLabelText(labelText)
36
+ fireEvent.click(element)
37
+ expect(onClickField).toHaveBeenCalledOnce()
38
+ expect(onClickField).toHaveBeenCalledWith(valuePath)
39
+ })
40
+ })
15
41
  })
package/package.json CHANGED
@@ -70,7 +70,7 @@
70
70
  "test:watch": "vitest"
71
71
  },
72
72
  "type": "module",
73
- "version": "0.0.9",
73
+ "version": "0.0.11",
74
74
  "exports": {
75
75
  ".": {
76
76
  "import": {
@@ -1,112 +0,0 @@
1
- import {
2
- type ReadonlyTypeOfType,
3
- type ValueOfType,
4
- } from '@strictly/define'
5
- import { type FieldsViewProps } from 'core/props'
6
- import {
7
- useCallback,
8
- useMemo,
9
- } from 'react'
10
- import { type FormPresenter } from './form_presenter'
11
- import {
12
- type ToValueOfPresenterValuePath,
13
- type ValuePathsOfPresenter,
14
- } from './types'
15
-
16
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
17
- type ValueOfPresenter<P extends FormPresenter<any, any, any, any>> = P extends FormPresenter<infer T, any, any, any>
18
- ? ValueOfType<ReadonlyTypeOfType<T>>
19
- : never
20
-
21
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
22
- type ModelOfPresenter<P extends FormPresenter<any, any, any, any>> = ReturnType<P['createModel']>
23
-
24
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
25
- export function useDefaultMobxFormHooks<P extends FormPresenter<any, any, any, any>>(
26
- presenter: P,
27
- value: ValueOfPresenter<P>,
28
- {
29
- onValidFieldSubmit,
30
- onValidFormSubmit,
31
- }: {
32
- onValidFieldSubmit?: <Path extends ValuePathsOfPresenter<P>>(model: ModelOfPresenter<P>, valuePath: Path) => void,
33
- onValidFormSubmit?: (model: ModelOfPresenter<P>, value: ValueOfPresenter<P>) => void,
34
- } = {},
35
- ): {
36
- model: ModelOfPresenter<P>,
37
- onFormSubmit?: () => void,
38
- } & Omit<FieldsViewProps<ModelOfPresenter<P>['fields']>, 'fields'> {
39
- const model = useMemo(function () {
40
- // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
41
- return presenter.createModel(value) as ReturnType<P['createModel']>
42
- }, [
43
- presenter,
44
- value,
45
- ])
46
-
47
- const onFieldValueChange = useCallback(
48
- function<Path extends ValuePathsOfPresenter<P>> (
49
- path: Path,
50
- value: ToValueOfPresenterValuePath<P, Path>,
51
- ) {
52
- presenter.clearFieldError(model, path)
53
- presenter.setFieldValue<Path>(model, path, value)
54
- },
55
- [
56
- presenter,
57
- model,
58
- ],
59
- )
60
-
61
- const onFieldSubmit = useCallback(
62
- function<Path extends ValuePathsOfPresenter<P>> (valuePath: Path) {
63
- if (presenter.validateField(model, valuePath)) {
64
- onValidFieldSubmit?.(model, valuePath)
65
- }
66
- return false
67
- },
68
- [
69
- presenter,
70
- model,
71
- onValidFieldSubmit,
72
- ],
73
- )
74
-
75
- const onFieldBlur = useCallback(
76
- function<Path extends ValuePathsOfPresenter<P>> (path: Path) {
77
- // work around potential loss of focus prior to state potentially invalidating change triggering
78
- // (e.g. changing a discriminator)
79
- // TODO debounce?
80
- setTimeout(function () {
81
- if (presenter.isValuePathActive(model, path)) {
82
- presenter.validateField(model, path)
83
- }
84
- }, 100)
85
- },
86
- [
87
- presenter,
88
- model,
89
- ],
90
- )
91
-
92
- const onFormSubmit = useCallback(
93
- function () {
94
- if (presenter.validateAll(model)) {
95
- onValidFormSubmit?.(model, model.value)
96
- }
97
- },
98
- [
99
- presenter,
100
- model,
101
- onValidFormSubmit,
102
- ],
103
- )
104
-
105
- return {
106
- model,
107
- onFieldValueChange,
108
- onFieldSubmit,
109
- onFieldBlur,
110
- onFormSubmit,
111
- }
112
- }