@rolster/react-forms 18.2.10 → 18.4.0

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 (33) hide show
  1. package/dist/cjs/index.js +300 -226
  2. package/dist/cjs/index.js.map +1 -1
  3. package/dist/es/index.js +292 -214
  4. package/dist/es/index.js.map +1 -1
  5. package/dist/esm/form-array/form-array-control.hook.d.ts +24 -20
  6. package/dist/esm/form-array/form-array-control.hook.js +34 -38
  7. package/dist/esm/form-array/form-array-control.hook.js.map +1 -1
  8. package/dist/esm/form-array/form-array-group.hook.d.ts +14 -13
  9. package/dist/esm/form-array/form-array-group.hook.js +36 -15
  10. package/dist/esm/form-array/form-array-group.hook.js.map +1 -1
  11. package/dist/esm/form-array/form-array.hook.d.ts +5 -11
  12. package/dist/esm/form-array/form-array.hook.js +62 -62
  13. package/dist/esm/form-array/form-array.hook.js.map +1 -1
  14. package/dist/esm/form-array/index.d.ts +2 -2
  15. package/dist/esm/form-array/index.js +2 -2
  16. package/dist/esm/form-array/index.js.map +1 -1
  17. package/dist/esm/form-control.hook.d.ts +16 -11
  18. package/dist/esm/form-control.hook.js +45 -54
  19. package/dist/esm/form-control.hook.js.map +1 -1
  20. package/dist/esm/form-group.hook.d.ts +3 -3
  21. package/dist/esm/form-group.hook.js +21 -21
  22. package/dist/esm/form-group.hook.js.map +1 -1
  23. package/dist/esm/form-ref.hook.d.ts +10 -10
  24. package/dist/esm/form-ref.hook.js +18 -18
  25. package/dist/esm/form-ref.hook.js.map +1 -1
  26. package/dist/esm/index.d.ts +2 -3
  27. package/dist/esm/index.js +2 -2
  28. package/dist/esm/index.js.map +1 -1
  29. package/dist/esm/types.d.ts +25 -11
  30. package/package.json +6 -3
  31. package/dist/esm/form-array/types.d.ts +0 -23
  32. package/dist/esm/form-array/types.js +0 -2
  33. package/dist/esm/form-array/types.js.map +0 -1
@@ -1,95 +1,95 @@
1
- import { createFormArrayProps } from '@rolster/helpers-forms';
2
- import { arrayIsValid, controlsToState, groupAllChecked, groupPartialChecked } from '@rolster/helpers-forms/helpers';
3
- import { useEffect, useState } from 'react';
4
- import { RolsterArrayControl } from './form-array-control.hook';
1
+ import { createFormArrayOptions } from '@rolster/forms/arguments';
2
+ import { arrayIsValid, controlsToState, groupAllChecked, groupPartialChecked } from '@rolster/forms/helpers';
3
+ import { useEffect, useRef, useState } from 'react';
5
4
  import { RolsterArrayGroup } from './form-array-group.hook';
6
- export function cloneFormArrayControl(control, changes) {
7
- return new RolsterArrayControl({ ...control, ...changes });
8
- }
9
- export function cloneFormArrayGroup(group, changes) {
10
- return new RolsterArrayGroup({ ...group, ...changes });
11
- }
12
- export function cloneFormControlForArrayGroup(group, control, changes) {
13
- const newControl = cloneFormArrayControl(control, changes);
14
- const { uuid } = newControl;
15
- const controls = Object.entries(group.controls).reduce((controls, [key, control]) => {
16
- controls[key] = control.uuid === uuid ? newControl : control;
17
- return controls;
18
- }, {});
19
- return new RolsterArrayGroup({ ...group, controls });
20
- }
21
- export function useFormArray(arrayProps, arrayValidators) {
22
- const props = createFormArrayProps(arrayProps, arrayValidators);
23
- const [currentState] = useState(props.groups);
24
- const [state, setState] = useState([]);
25
- const [validators, setValidators] = useState(props.validators);
26
- const [controls, setControls] = useState([]);
27
- const [groups, setGroups] = useState(props.groups || []);
5
+ export function useFormArray(options, arrayValidators) {
6
+ const arrayOptions = createFormArrayOptions(options, arrayValidators);
7
+ const { validators } = arrayOptions;
8
+ const groups = arrayOptions.groups || [];
9
+ const [arrayState, setArrayState] = useState({
10
+ controls: groups.map(({ controls }) => controls),
11
+ disabled: false,
12
+ groups,
13
+ state: groups.map(({ controls }) => controlsToState(controls)),
14
+ validators
15
+ });
16
+ const currentState = useRef(groups);
28
17
  useEffect(() => {
29
- setControls(groups.map(({ controls }) => controls));
30
- setState(groups.map(({ controls }) => controlsToState(controls)));
31
- }, [groups]);
18
+ const subscriber = (options) => {
19
+ setArrayState((state) => ({
20
+ ...state,
21
+ groups: arrayState.groups.map((group) => group.uuid === options.uuid
22
+ ? new RolsterArrayGroup(options)
23
+ : group)
24
+ }));
25
+ };
26
+ arrayState.groups.forEach((group) => {
27
+ group.subscribe(subscriber);
28
+ });
29
+ }, [arrayState]);
32
30
  const errors = validators ? arrayIsValid({ groups, validators }) : [];
33
31
  const error = errors[0];
34
32
  const valid = errors.length === 0 && groupAllChecked(groups, 'valid');
35
- const touched = groupPartialChecked(groups, 'touched');
36
- const toucheds = groupAllChecked(groups, 'touched');
37
- const dirties = groupAllChecked(groups, 'dirty');
38
33
  const dirty = groupPartialChecked(groups, 'dirty');
34
+ const dirtyAll = groupAllChecked(groups, 'dirty');
35
+ const touched = groupPartialChecked(groups, 'touched');
36
+ const touchedAll = groupAllChecked(groups, 'touched');
37
+ function disable() {
38
+ setArrayState((state) => ({ ...state, disabled: true }));
39
+ }
40
+ function enable() {
41
+ setArrayState((state) => ({ ...state, disabled: false }));
42
+ }
43
+ function setGroups(groups) {
44
+ setArrayState((currentState) => ({
45
+ ...currentState,
46
+ groups,
47
+ controls: groups.map(({ controls }) => controls),
48
+ state: groups.map(({ controls }) => controlsToState(controls))
49
+ }));
50
+ }
39
51
  function push(group) {
40
- setGroups([...groups, group]);
52
+ setGroups([...arrayState.groups, group]);
41
53
  }
42
- function merge(newGroups) {
43
- setGroups([...groups, ...newGroups]);
54
+ function merge(groups) {
55
+ setGroups([...arrayState.groups, ...groups]);
44
56
  }
45
57
  function set(groups) {
46
58
  setGroups(groups);
47
59
  }
48
- function refreshControl(control, changes) {
49
- if (control.group) {
50
- const group = cloneFormControlForArrayGroup(control.group, control, changes);
51
- setGroups(groups.map((currentGroup) => currentGroup.uuid === group.uuid ? group : currentGroup));
52
- }
53
- }
54
- function refreshGroup(group, changes) {
55
- const newGroup = cloneFormArrayGroup(group, changes);
56
- const { uuid } = newGroup;
57
- setGroups(groups.map((currentGroup) => currentGroup.uuid === uuid ? newGroup : currentGroup));
58
- }
59
- function remove(group) {
60
- setGroups(groups.filter(({ uuid }) => group.uuid !== uuid));
60
+ function remove({ uuid }) {
61
+ setGroups(arrayState.groups.filter((group) => group.uuid !== uuid));
61
62
  }
62
63
  function reset() {
63
- setGroups(currentState || []);
64
+ setGroups(currentState.current);
65
+ }
66
+ function setValidators(validators) {
67
+ setArrayState((state) => ({ ...state, validators }));
64
68
  }
65
- const formArray = {
66
- controls,
69
+ return {
70
+ ...arrayState,
67
71
  dirty,
68
- dirties,
72
+ dirtyAll,
73
+ disable,
74
+ enable,
75
+ enabled: !arrayState.disabled,
69
76
  error,
70
77
  errors,
71
- groups,
72
78
  invalid: !valid,
73
79
  merge,
74
80
  pristine: !dirty,
75
- pristines: !dirties,
81
+ pristineAll: !dirtyAll,
76
82
  push,
77
- refreshControl,
78
- refreshGroup,
79
83
  remove,
80
84
  reset,
81
85
  set,
82
86
  setValidators,
83
- state,
84
87
  touched,
85
- toucheds,
88
+ touchedAll,
86
89
  untouched: !touched,
87
- untoucheds: !toucheds,
90
+ untouchedAll: !touchedAll,
88
91
  valid,
89
- value: state,
90
92
  wrong: touched && !valid
91
93
  };
92
- groups.forEach((group) => (group.parent = formArray));
93
- return formArray;
94
94
  }
95
95
  //# sourceMappingURL=form-array.hook.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"form-array.hook.js","sourceRoot":"","sources":["../../../src/form-array/form-array.hook.ts"],"names":[],"mappings":"AAAA,OAAO,EAML,oBAAoB,EACrB,MAAM,wBAAwB,CAAC;AAChC,OAAO,EACL,YAAY,EACZ,eAAe,EACf,eAAe,EACf,mBAAmB,EACpB,MAAM,gCAAgC,CAAC;AACxC,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAO5C,OAAO,EAAE,mBAAmB,EAAE,MAAM,2BAA2B,CAAC;AAChE,OAAO,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAQ5D,MAAM,UAAU,qBAAqB,CAKnC,OAA6C,EAC7C,OAAiD;IAEjD,OAAO,IAAI,mBAAmB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,KAAK,CAAC,IAAI,KAAK,IAAI,CAAC,CAAC,CAAC;IACtE,CAAC;IAED,SAAS,KAAK;QACZ,SAAS,CAAC,YAAY,CAAC,OAAO,CAAC,CAAC;IAClC,CAAC;IAED,SAAS,aAAa,CAAC,UAAqC;QAC1D,aAAa,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,EAAE,GAAG,KAAK,EAAE,UAAU,EAAE,CAAC,CAAC,CAAC;IACvD,CAAC;IAED,OAAO;QACL,GAAG,UAAU;QACb,KAAK;QACL,QAAQ;QACR,OAAO;QACP,MAAM;QACN,OAAO,EAAE,CAAC,UAAU,CAAC,QAAQ;QAC7B,KAAK;QACL,MAAM;QACN,OAAO,EAAE,CAAC,KAAK;QACf,KAAK;QACL,QAAQ,EAAE,CAAC,KAAK;QAChB,WAAW,EAAE,CAAC,QAAQ;QACtB,IAAI;QACJ,MAAM;QACN,KAAK;QACL,GAAG;QACH,aAAa;QACb,OAAO;QACP,UAAU;QACV,SAAS,EAAE,CAAC,OAAO;QACnB,YAAY,EAAE,CAAC,UAAU;QACzB,KAAK;QACL,KAAK,EAAE,OAAO,IAAI,CAAC,KAAK;KACzB,CAAC;AACJ,CAAC"}
@@ -1,3 +1,3 @@
1
- export { useFormArrayControl, useInputArrayControl } from './form-array-control.hook';
1
+ export { useReactArrayControl, useFormArrayControl, useInputArrayControl } from './form-array-control.hook';
2
2
  export { useFormArrayGroup } from './form-array-group.hook';
3
- export { cloneFormArrayControl, cloneFormArrayGroup, cloneFormControlForArrayGroup, useFormArray } from './form-array.hook';
3
+ export { useFormArray } from './form-array.hook';
@@ -1,4 +1,4 @@
1
- export { useFormArrayControl, useInputArrayControl } from './form-array-control.hook';
1
+ export { useReactArrayControl, useFormArrayControl, useInputArrayControl } from './form-array-control.hook';
2
2
  export { useFormArrayGroup } from './form-array-group.hook';
3
- export { cloneFormArrayControl, cloneFormArrayGroup, cloneFormControlForArrayGroup, useFormArray } from './form-array.hook';
3
+ export { useFormArray } from './form-array.hook';
4
4
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/form-array/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,mBAAmB,EACnB,oBAAoB,EACrB,MAAM,2BAA2B,CAAC;AACnC,OAAO,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAC5D,OAAO,EACL,qBAAqB,EACrB,mBAAmB,EACnB,6BAA6B,EAC7B,YAAY,EACb,MAAM,mBAAmB,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/form-array/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,oBAAoB,EACpB,mBAAmB,EACnB,oBAAoB,EACrB,MAAM,2BAA2B,CAAC;AACnC,OAAO,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAC5D,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC"}
@@ -1,16 +1,21 @@
1
- import { FormControlProps, FormState } from '@rolster/helpers-forms';
1
+ import { FormControlOptions } from '@rolster/forms';
2
2
  import { ValidatorFn } from '@rolster/validators';
3
3
  import { ReactFormControl, ReactHtmlControl, ReactInputControl } from './types';
4
- interface ReactControlProps<T = any> extends FormControlProps<T> {
4
+ interface ReactControlOptions<T = any> extends FormControlOptions<T> {
5
5
  touched?: boolean;
6
6
  }
7
- export declare function useReactControl<E extends HTMLElement, T = any>(): ReactFormControl<E, T>;
8
- export declare function useReactControl<E extends HTMLElement, T = any>(props: ReactControlProps<T>): ReactFormControl<E, T>;
9
- export declare function useReactControl<E extends HTMLElement, T = any>(state: FormState<T>, validators?: ValidatorFn<T>[]): ReactFormControl<E, T>;
10
- export declare function useFormControl<T = any>(): ReactHtmlControl<T>;
11
- export declare function useFormControl<T = any>(props: ReactControlProps<T>): ReactHtmlControl<T>;
12
- export declare function useFormControl<T = any>(state: FormState<T>, validators?: ValidatorFn<T>[]): ReactHtmlControl<T>;
13
- export declare function useInputControl<T = any>(): ReactInputControl<T>;
14
- export declare function useInputControl<T = any>(props: ReactControlProps<T>): ReactInputControl<T>;
15
- export declare function useInputControl<T = any>(state: FormState<T>, validators?: ValidatorFn<T>[]): ReactInputControl<T>;
7
+ type ReactStateOptions<T> = Omit<ReactControlOptions<T>, 'validators'>;
8
+ type ReactValidatorsOptions<T> = Omit<ReactControlOptions<T>, 'state'>;
9
+ export declare function useReactControl<E extends HTMLElement, T>(): ReactFormControl<E, T | undefined>;
10
+ export declare function useReactControl<E extends HTMLElement, T>(options: ReactStateOptions<T>): ReactFormControl<E, T>;
11
+ export declare function useReactControl<E extends HTMLElement, T>(options: ReactValidatorsOptions<T>): ReactFormControl<E, T | undefined>;
12
+ export declare function useReactControl<E extends HTMLElement, T>(state: T, validators?: ValidatorFn<T>[]): ReactFormControl<E, T>;
13
+ export declare function useFormControl<T>(): ReactHtmlControl<T | undefined>;
14
+ export declare function useFormControl<T>(options: ReactStateOptions<T>): ReactHtmlControl<T>;
15
+ export declare function useFormControl<T>(options: ReactValidatorsOptions<T>): ReactHtmlControl<T | undefined>;
16
+ export declare function useFormControl<T>(state: T, validators?: ValidatorFn<T>[]): ReactHtmlControl<T>;
17
+ export declare function useInputControl<T>(): ReactInputControl<T | undefined>;
18
+ export declare function useInputControl<T>(options: ReactStateOptions<T>): ReactInputControl<T>;
19
+ export declare function useInputControl<T>(options: ReactValidatorsOptions<T>): ReactInputControl<T | undefined>;
20
+ export declare function useInputControl<T>(state: T, validators?: ValidatorFn<T>[]): ReactInputControl<T>;
16
21
  export {};
@@ -1,87 +1,78 @@
1
- import { createFormControlProps } from '@rolster/helpers-forms';
2
- import { controlIsValid } from '@rolster/helpers-forms/helpers';
3
- import { useEffect, useRef, useState } from 'react';
4
- function useControl(controlProps, controlValidators) {
5
- const props = createFormControlProps(controlProps, controlValidators);
6
- const [state, setCurrentState] = useState(props.state);
7
- const [value, setValue] = useState(props.state);
8
- const [touched, setTouched] = useState(props.touched || false);
9
- const [dirty, setDirty] = useState(false);
10
- const [focused, setFocused] = useState(false);
11
- const [disabled, setDisabled] = useState(false);
12
- const [initialValue] = useState(props.state);
13
- const [validators, setValidators] = useState(props.validators);
1
+ import { createFormControlOptions } from '@rolster/forms/arguments';
2
+ import { controlIsValid } from '@rolster/forms/helpers';
3
+ import { useRef, useState } from 'react';
4
+ function useControl(controlOptions, controlValidators) {
5
+ const { state, touched, validators } = createFormControlOptions(controlOptions, controlValidators);
6
+ const [controlState, setControlState] = useState({
7
+ dirty: false,
8
+ disabled: false,
9
+ focused: false,
10
+ state: state,
11
+ touched: !!touched,
12
+ validators: validators
13
+ });
14
+ const initialState = useRef(state);
14
15
  const elementRef = useRef(null);
15
16
  const errors = validators ? controlIsValid({ state, validators }) : [];
16
- const error = errors[0];
17
17
  const valid = errors.length === 0;
18
- useEffect(() => {
19
- if (state !== null && state !== undefined) {
20
- setValue(state);
21
- }
22
- }, [state]);
23
18
  function focus() {
24
- setFocused(true);
19
+ setControlState((state) => ({ ...state, focused: true }));
25
20
  }
26
21
  function blur() {
27
- setFocused(false);
22
+ setControlState((state) => ({ ...state, focused: false, touched: true }));
28
23
  }
29
24
  function disable() {
30
- setDisabled(true);
25
+ setControlState((state) => ({ ...state, disabled: true }));
31
26
  }
32
27
  function enable() {
33
- setDisabled(false);
34
- }
35
- function touch() {
36
- setTouched(true);
37
- }
38
- function untouch() {
39
- setTouched(false);
28
+ setControlState((state) => ({ ...state, disabled: false }));
40
29
  }
41
30
  function setState(state) {
42
- setDirty(true);
43
- setCurrentState(state);
31
+ setControlState((currentState) => ({
32
+ ...currentState,
33
+ dirty: true,
34
+ state
35
+ }));
36
+ }
37
+ function setValidators(validators) {
38
+ setControlState((state) => ({ ...state, validators }));
44
39
  }
45
40
  function reset() {
46
- setTouched(false);
47
- setDirty(false);
48
- setCurrentState(initialValue);
41
+ setControlState((currentState) => ({
42
+ ...currentState,
43
+ dirty: false,
44
+ state: initialState.current,
45
+ touched: false
46
+ }));
49
47
  }
50
48
  return {
49
+ ...controlState,
51
50
  blur,
52
- dirty,
53
51
  disable,
54
- disabled,
55
52
  elementRef,
56
53
  enable,
57
- enabled: !disabled,
58
- error,
54
+ enabled: !controlState.disabled,
55
+ error: errors[0],
59
56
  errors,
60
57
  focus,
61
- focused,
62
58
  invalid: !valid,
63
- pristine: !dirty,
59
+ pristine: !controlState.dirty,
64
60
  reset,
65
61
  setState,
66
62
  setValidators,
67
- state,
68
- touch,
69
- touched,
70
- unfocused: !focused,
71
- untouch,
72
- untouched: !touched,
63
+ unfocused: !controlState.focused,
64
+ untouched: !controlState.touched,
73
65
  valid,
74
- value,
75
- wrong: touched && !valid
66
+ wrong: controlState.touched && !valid
76
67
  };
77
68
  }
78
- export function useReactControl(controlProps, controlValidators) {
79
- return useControl(controlProps, controlValidators);
69
+ export function useReactControl(options, validators) {
70
+ return useControl(options, validators);
80
71
  }
81
- export function useFormControl(controlProps, controlValidators) {
82
- return useControl(controlProps, controlValidators);
72
+ export function useFormControl(options, validators) {
73
+ return useControl(options, validators);
83
74
  }
84
- export function useInputControl(controlProps, controlValidators) {
85
- return useControl(controlProps, controlValidators);
75
+ export function useInputControl(options, validators) {
76
+ return useControl(options, validators);
86
77
  }
87
78
  //# sourceMappingURL=form-control.hook.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"form-control.hook.js","sourceRoot":"","sources":["../../src/form-control.hook.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,sBAAsB,EACvB,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAAE,cAAc,EAAE,MAAM,gCAAgC,CAAC;AAEhE,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAOpD,SAAS,UAAU,CACjB,YAAkD,EAClD,iBAAoC;IAEpC,MAAM,KAAK,GAAG,sBAAsB,CAClC,YAAY,EACZ,iBAAiB,CAClB,CAAC;IAEF,MAAM,CAAC,KAAK,EAAE,eAAe,CAAC,GAAG,QAAQ,CAAe,KAAK,CAAC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,SAAS,EAAE,CAAC,YAAY,CAAC,OAAO;QAChC,SAAS,EAAE,CAAC,YAAY,CAAC,OAAO;QAChC,KAAK;QACL,KAAK,EAAE,YAAY,CAAC,OAAO,IAAI,CAAC,KAAK;KACtC,CAAC;AACJ,CAAC;AAmBD,MAAM,UAAU,eAAe,CAC7B,OAAoC,EACpC,UAA6B;IAE7B,OAAO,UAAU,CAAC,OAAO,EAAE,UAAU,CAAC,CAAC;AACzC,CAAC;AAaD,MAAM,UAAU,cAAc,CAC5B,OAAoC,EACpC,UAA6B;IAE7B,OAAO,UAAU,CAAc,OAAO,EAAE,UAAU,CAAC,CAAC;AACtD,CAAC;AAaD,MAAM,UAAU,eAAe,CAC7B,OAAoC,EACpC,UAA6B;IAE7B,OAAO,UAAU,CAAmB,OAAO,EAAE,UAAU,CAAC,CAAC;AAC3D,CAAC"}
@@ -1,4 +1,4 @@
1
- import { FormGroupProps, ValidatorGroupFn } from '@rolster/helpers-forms';
1
+ import { FormGroupOptions, ValidatorGroupFn } from '@rolster/forms';
2
2
  import { ReactControls, ReactGroup } from './types';
3
- export declare function useFormGroup<T extends ReactControls>(props: FormGroupProps<T>): ReactGroup<T>;
4
- export declare function useFormGroup<T extends ReactControls>(controls: T, validators?: ValidatorGroupFn<T>[]): ReactGroup<T>;
3
+ export declare function useFormGroup<C extends ReactControls>(options: FormGroupOptions<C>): ReactGroup<C>;
4
+ export declare function useFormGroup<C extends ReactControls>(controls: C, validators?: ValidatorGroupFn<C>[]): ReactGroup<C>;
@@ -1,39 +1,39 @@
1
- import { createFormGroupProps } from '@rolster/helpers-forms';
2
- import { controlsAllChecked, controlsPartialChecked, controlsToState, controlsToValue, groupIsValid } from '@rolster/helpers-forms/helpers';
1
+ import { createFormGroupOptions } from '@rolster/forms/arguments';
2
+ import { controlsAllChecked, controlsPartialChecked, controlsToState, groupIsValid } from '@rolster/forms/helpers';
3
3
  import { useState } from 'react';
4
- export function useFormGroup(groupProps, groupValidators) {
5
- const props = createFormGroupProps(groupProps, groupValidators);
6
- const [validators, setValidators] = useState(props.validators);
7
- const { controls } = props;
8
- const errors = (() => validators ? groupIsValid({ controls, validators }) : [])();
9
- const valid = (() => errors.length === 0 && controlsAllChecked(controls, 'valid'))();
10
- const touched = (() => controlsPartialChecked(controls, 'touched'))();
11
- const toucheds = (() => controlsAllChecked(controls, 'touched'))();
12
- const dirty = (() => controlsPartialChecked(controls, 'dirty'))();
13
- const dirties = (() => controlsAllChecked(controls, 'dirty'))();
14
- const state = (() => controlsToState(controls))();
15
- const value = (() => controlsToValue(controls))();
4
+ export function useFormGroup(options, groupValidators) {
5
+ const groupOptions = createFormGroupOptions(options, groupValidators);
6
+ const [validators, setValidators] = useState(groupOptions.validators);
7
+ const { controls } = groupOptions;
8
+ const errors = validators ? groupIsValid({ controls, validators }) : [];
9
+ const valid = errors.length === 0 && controlsAllChecked(controls, 'valid');
10
+ const state = controlsToState(controls);
11
+ const dirty = controlsPartialChecked(controls, 'dirty');
12
+ const dirtyAll = controlsAllChecked(controls, 'dirty');
13
+ const touched = controlsPartialChecked(controls, 'touched');
14
+ const touchedAll = controlsAllChecked(controls, 'touched');
16
15
  function reset() {
17
- Object.values(controls).forEach((control) => control.reset());
16
+ Object.values(controls).forEach((control) => {
17
+ control.reset();
18
+ });
18
19
  }
19
20
  return {
20
21
  controls,
21
22
  dirty,
22
- dirties,
23
+ dirtyAll,
23
24
  error: errors[0],
24
25
  errors,
25
26
  invalid: !valid,
26
27
  pristine: !dirty,
27
- pristines: !dirties,
28
+ pristineAll: !dirtyAll,
28
29
  reset,
29
- state,
30
30
  setValidators,
31
+ state,
31
32
  touched,
32
- toucheds,
33
+ touchedAll,
33
34
  untouched: !touched,
34
- untoucheds: !toucheds,
35
+ untouchedAll: !touchedAll,
35
36
  valid,
36
- value,
37
37
  wrong: touched && !valid
38
38
  };
39
39
  }
@@ -1 +1 @@
1
- {"version":3,"file":"form-group.hook.js","sourceRoot":"","sources":["../../src/form-group.hook.ts"],"names":[],"mappings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} from '@rolster/validators';
3
3
  import { ReactInputControl } from './types';
4
- interface ReactRefProps<T = any> extends FormControlProps<T> {
5
- setValue: (inputControl: ReactInputControl<T>, value: string) => void;
4
+ interface ReactRefOptions<T = any> extends FormControlOptions<T> {
5
+ setValue: (control: ReactInputControl<T>, value: string) => void;
6
6
  touched?: boolean;
7
7
  }
8
- type InputRefProps<T = any> = Omit<ReactRefProps<T>, 'setValue'>;
9
- type TextRefProps = InputRefProps<string>;
10
- type NumberRefProps = InputRefProps<number>;
8
+ type InputRefOptions<T = any> = Omit<ReactRefOptions<T>, 'setValue'>;
9
+ type TextRefOptions = InputRefOptions<string>;
10
+ type NumberRefOptions = InputRefOptions<number>;
11
11
  export declare function useTextRefControl(): ReactInputControl<string>;
12
- export declare function useTextRefControl(props: TextRefProps): ReactInputControl<string>;
13
- export declare function useTextRefControl(state: FormState<string>, validators?: ValidatorFn<string>[]): ReactInputControl<string>;
12
+ export declare function useTextRefControl(options: TextRefOptions): ReactInputControl<string>;
13
+ export declare function useTextRefControl(state: string, validators?: ValidatorFn<string>[]): ReactInputControl<string>;
14
14
  export declare function useNumberRefControl(): ReactInputControl<number>;
15
- export declare function useNumberRefControl(props: NumberRefProps): ReactInputControl<number>;
16
- export declare function useNumberRefControl(state: FormState<number>, validators?: ValidatorFn<number>[]): ReactInputControl<number>;
15
+ export declare function useNumberRefControl(options: NumberRefOptions): ReactInputControl<number>;
16
+ export declare function useNumberRefControl(state: number, validators?: ValidatorFn<number>[]): ReactInputControl<number>;
17
17
  export {};
@@ -1,36 +1,36 @@
1
- import { createFormControlProps } from '@rolster/helpers-forms';
1
+ import { createFormControlOptions } from '@rolster/forms/arguments';
2
2
  import { useEffect } from 'react';
3
3
  import { useInputControl } from './form-control.hook';
4
- function useInputRefControl(props) {
5
- const { setValue, state, validators } = props;
6
- const inputControl = useInputControl(state, validators);
4
+ function useInputRefControl(options) {
5
+ const control = useInputControl(options);
7
6
  useEffect(() => {
8
- const { elementRef } = inputControl;
7
+ const { elementRef } = control;
9
8
  elementRef?.current?.addEventListener('focus', () => {
10
- inputControl.focus();
9
+ control.focus();
11
10
  });
12
11
  elementRef?.current?.addEventListener('blur', () => {
13
- inputControl.blur();
14
- if (!inputControl.touched) {
15
- inputControl.touch();
16
- }
12
+ control.blur();
17
13
  });
18
14
  elementRef?.current?.addEventListener('change', ({ target }) => {
19
- setValue(inputControl, target.value);
15
+ options.setValue(control, target.value);
20
16
  });
21
17
  }, []);
22
- return inputControl;
18
+ return control;
23
19
  }
24
- export function useTextRefControl(controlProps, controlValidators) {
20
+ export function useTextRefControl(options, validators) {
25
21
  return useInputRefControl({
26
- ...createFormControlProps(controlProps, controlValidators),
27
- setValue: (inputControl, value) => inputControl.setState(value)
22
+ ...createFormControlOptions(options, validators),
23
+ setValue: (control, value) => {
24
+ control.setState(value);
25
+ }
28
26
  });
29
27
  }
30
- export function useNumberRefControl(controlProps, controlValidators) {
28
+ export function useNumberRefControl(options, validators) {
31
29
  return useInputRefControl({
32
- ...createFormControlProps(controlProps, controlValidators),
33
- setValue: (inputControl, value) => inputControl.setState(+value)
30
+ ...createFormControlOptions(options, validators),
31
+ setValue: (control, value) => {
32
+ control.setState(+value);
33
+ }
34
34
  });
35
35
  }
36
36
  //# sourceMappingURL=form-ref.hook.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"form-ref.hook.js","sourceRoot":"","sources":["../../src/form-ref.hook.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,sBAAsB,EACvB,MAAM,wBAAwB,CAAC;AAEhC,OAAO,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAClC,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AAatD,SAAS,kBAAkB,CAAU,KAAuB;IAC1D,MAAM,EAAE,QAAQ,EAAE,KAAK,EAAE,UAAU,EAAE,GAAG,KAAK,CAAC;IAE9C,MAAM,YAAY,GAAG,eAAe,CAAC,KAAK,EAAE,UAAU,CAAC,CAAC;IAExD,SAAS,CAAC,G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ACxB,GAAG,wBAAwB,CACzB,OAAO,EACP,UAAU,CACX;QACD,QAAQ,EAAE,CAAC,OAAO,EAAE,KAAK,EAAE,EAAE;YAC3B,OAAO,CAAC,QAAQ,CAAC,CAAC,KAAK,CAAC,CAAC;QAC3B,CAAC;KACF,CAAC,CAAC;AACL,CAAC"}
@@ -1,5 +1,4 @@
1
- export { cloneFormArrayControl, cloneFormArrayGroup, cloneFormControlForArrayGroup, useFormArray, useFormArrayControl, useFormArrayGroup, useInputArrayControl } from './form-array';
1
+ export { useFormArray, useFormArrayControl, useFormArrayGroup, useInputArrayControl, useReactArrayControl } from './form-array';
2
2
  export { useFormControl, useInputControl, useReactControl } from './form-control.hook';
3
3
  export { useFormGroup } from './form-group.hook';
4
- export { useNumberRefControl, useTextRefControl } from './form-ref.hook';
5
- export { ReactArrayControl, ReactArrayControls, ReactArrayGroup, ReactArrayHtmlControl, ReactArrayInputControl, ReactControl, ReactControls, ReactFormArray, ReactFormControl, ReactGroup, ReactHtmlControl, ReactInputControl } from './types';
4
+ export * from './types';
package/dist/esm/index.js CHANGED
@@ -1,5 +1,5 @@
1
- export { cloneFormArrayControl, cloneFormArrayGroup, cloneFormControlForArrayGroup, useFormArray, useFormArrayControl, useFormArrayGroup, useInputArrayControl } from './form-array';
1
+ export { useFormArray, useFormArrayControl, useFormArrayGroup, useInputArrayControl, useReactArrayControl } from './form-array';
2
2
  export { useFormControl, useInputControl, useReactControl } from './form-control.hook';
3
3
  export { useFormGroup } from './form-group.hook';
4
- export { useNumberRefControl, useTextRefControl } from './form-ref.hook';
4
+ export * from './types';
5
5
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,qBAAqB,EACrB,mBAAmB,EACnB,6BAA6B,EAC7B,YAAY,EACZ,mBAAmB,EACnB,iBAAiB,EACjB,oBAAoB,EACrB,MAAM,cAAc,CAAC;AACtB,OAAO,EACL,cAAc,EACd,eAAe,EACf,eAAe,EAChB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAAE,mBAAmB,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,YAAY,EACZ,mBAAmB,EACnB,iBAAiB,EACjB,oBAAoB,EACpB,oBAAoB,EACrB,MAAM,cAAc,CAAC;AACtB,OAAO,EACL,cAAc,EACd,eAAe,EACf,eAAe,EAChB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACjD,cAAc,SAAS,CAAC"}