@rolster/react-forms 18.2.9 → 18.3.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 +230 -214
  2. package/dist/cjs/index.js.map +1 -1
  3. package/dist/es/index.js +231 -211
  4. package/dist/es/index.js.map +1 -1
  5. package/dist/esm/form-array/form-array-control.hook.d.ts +24 -18
  6. package/dist/esm/form-array/form-array-control.hook.js +32 -26
  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 +10 -9
  9. package/dist/esm/form-array/form-array-group.hook.js +33 -12
  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 +45 -55
  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 +49 -58
  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 +17 -17
  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 +21 -17
  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 +24 -10
  30. package/package.json +7 -5
  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,34 +1,31 @@
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
+ groups,
12
+ state: groups.map(({ controls }) => controlsToState(controls)),
13
+ validators
14
+ });
15
+ const currentState = useRef(groups);
28
16
  useEffect(() => {
29
- setControls(groups.map(({ controls }) => controls));
30
- setState(groups.map(({ controls }) => controlsToState(controls)));
31
- }, [groups]);
17
+ const subscriber = (options) => {
18
+ setArrayState((state) => ({
19
+ ...state,
20
+ groups: arrayState.groups.map((group) => group.uuid === options.uuid
21
+ ? new RolsterArrayGroup(options)
22
+ : group)
23
+ }));
24
+ };
25
+ arrayState.groups.forEach((group) => {
26
+ group.subscribe(subscriber);
27
+ });
28
+ }, [arrayState]);
32
29
  const errors = validators ? arrayIsValid({ groups, validators }) : [];
33
30
  const error = errors[0];
34
31
  const valid = errors.length === 0 && groupAllChecked(groups, 'valid');
@@ -36,60 +33,53 @@ export function useFormArray(arrayProps, arrayValidators) {
36
33
  const toucheds = groupAllChecked(groups, 'touched');
37
34
  const dirties = groupAllChecked(groups, 'dirty');
38
35
  const dirty = groupPartialChecked(groups, 'dirty');
36
+ function setGroups(groups) {
37
+ setArrayState((currentState) => ({
38
+ ...currentState,
39
+ groups,
40
+ controls: groups.map(({ controls }) => controls),
41
+ state: groups.map(({ controls }) => controlsToState(controls))
42
+ }));
43
+ }
39
44
  function push(group) {
40
- setGroups([...groups, group]);
45
+ setGroups([...arrayState.groups, group]);
41
46
  }
42
- function merge(newGroups) {
43
- setGroups([...groups, ...newGroups]);
47
+ function merge(groups) {
48
+ setGroups([...arrayState.groups, ...groups]);
44
49
  }
45
50
  function set(groups) {
46
51
  setGroups(groups);
47
52
  }
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));
53
+ function remove({ uuid }) {
54
+ setGroups(arrayState.groups.filter((group) => group.uuid !== uuid));
61
55
  }
62
56
  function reset() {
63
- setGroups(currentState || []);
57
+ setGroups(currentState.current);
64
58
  }
65
- const formArray = {
66
- controls,
59
+ function setValidators(validators) {
60
+ setArrayState((state) => ({ ...state, validators }));
61
+ }
62
+ return {
63
+ ...arrayState,
67
64
  dirty,
68
65
  dirties,
69
66
  error,
70
67
  errors,
71
- groups,
72
68
  invalid: !valid,
73
69
  merge,
74
70
  pristine: !dirty,
75
71
  pristines: !dirties,
76
72
  push,
77
- refreshControl,
78
- refreshGroup,
79
73
  remove,
80
74
  reset,
81
75
  set,
82
76
  setValidators,
83
- state,
84
77
  touched,
85
78
  toucheds,
86
79
  untouched: !touched,
87
80
  untoucheds: !toucheds,
88
81
  valid,
89
- value: state,
90
82
  wrong: touched && !valid
91
83
  };
92
- groups.forEach((group) => (group.parent = formArray));
93
- return formArray;
94
84
  }
95
85
  //# 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,CAAC,EAAE,GAAG,OAAO,EAAE,GAAG,OAAO,EAAE,CAAC,CAAC;AAC7D,CAAC;AAED,MAAM,UAAU,mBAAmB,CACjC,KAAmC,EACnC,OAAwC;IAExC,OAAO,IAAI,iBAAiB,CAAC,EAAE,GAAG,KAAK,EAAE,GAAG,OAAO,EAAE,CAAC,CAAC;AACzD,CAAC;AAED,MAAM,UAAU,6BAA6B,CAI3C,KAAmC,EACnC,OAAuC,EACvC,OAAiD;IAEjD,MAAM,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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,95 +1,86 @@
1
- import { createFormControlProps } from '@rolster/helpers-forms';
2
- import { controlIsValid } from '@rolster/helpers-forms/helpers';
3
- import { useEffect, useRef, useState } from 'react';
4
- import { BehaviorSubject } from 'rxjs';
5
- function useControl(controlProps, controlValidators) {
6
- const props = createFormControlProps(controlProps, controlValidators);
7
- const [state, setCurrentState] = useState(props.state);
8
- const [value, setValue] = useState(props.state);
9
- const [touched, setTouched] = useState(props.touched || false);
10
- const [dirty, setDirty] = useState(false);
11
- const [focused, setFocused] = useState(false);
12
- const [disabled, setDisabled] = useState(false);
13
- const [initialValue] = useState(props.state);
14
- const [validators, setValidators] = useState(props.validators);
15
- const [subscribers] = useState(new BehaviorSubject(props.state));
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);
16
15
  const elementRef = useRef(null);
17
- const errors = (() => validators ? controlIsValid({ state, validators }) : [])();
18
- const error = (() => errors[0])();
19
- const valid = (() => errors.length === 0)();
20
- useEffect(() => {
21
- if (state !== null && state !== undefined) {
22
- setValue(state);
23
- }
24
- subscribers.next(state);
25
- }, [state]);
16
+ const errors = validators ? controlIsValid({ state, validators }) : [];
17
+ const valid = errors.length === 0;
26
18
  function focus() {
27
- setFocused(true);
19
+ setControlState((state) => ({ ...state, focused: true }));
28
20
  }
29
21
  function blur() {
30
- setFocused(false);
22
+ setControlState((state) => ({ ...state, focused: false }));
31
23
  }
32
24
  function disable() {
33
- setDisabled(true);
25
+ setControlState((state) => ({ ...state, disabled: true }));
34
26
  }
35
27
  function enable() {
36
- setDisabled(false);
28
+ setControlState((state) => ({ ...state, disabled: false }));
37
29
  }
38
30
  function touch() {
39
- setTouched(true);
31
+ setControlState((state) => ({ ...state, touched: true }));
40
32
  }
41
33
  function untouch() {
42
- setTouched(false);
34
+ setControlState((state) => ({ ...state, touched: false }));
43
35
  }
44
36
  function setState(state) {
45
- setDirty(true);
46
- setCurrentState(state);
37
+ setControlState((currentState) => ({
38
+ ...currentState,
39
+ dirty: true,
40
+ state
41
+ }));
47
42
  }
48
- function reset() {
49
- setTouched(false);
50
- setDirty(false);
51
- setCurrentState(initialValue);
43
+ function setValidators(validators) {
44
+ setControlState((state) => ({ ...state, validators }));
52
45
  }
53
- function subscribe(subscriber) {
54
- const subscription = subscribers.subscribe(subscriber);
55
- return () => subscription.unsubscribe();
46
+ function reset() {
47
+ setControlState((currentState) => ({
48
+ ...currentState,
49
+ dirty: false,
50
+ state: initialState.current,
51
+ touched: false
52
+ }));
56
53
  }
57
54
  return {
55
+ ...controlState,
58
56
  blur,
59
- dirty,
60
57
  disable,
61
- disabled,
62
58
  elementRef,
63
59
  enable,
64
- enabled: !disabled,
65
- error,
60
+ enabled: !controlState.disabled,
61
+ error: errors[0],
66
62
  errors,
67
63
  focus,
68
- focused,
69
64
  invalid: !valid,
70
- pristine: !dirty,
65
+ pristine: !controlState.dirty,
71
66
  reset,
72
67
  setState,
73
68
  setValidators,
74
- state,
75
- subscribe,
76
69
  touch,
77
- touched,
78
- unfocused: !focused,
70
+ unfocused: !controlState.focused,
79
71
  untouch,
80
- untouched: !touched,
72
+ untouched: !controlState.touched,
81
73
  valid,
82
- value,
83
- wrong: touched && !valid
74
+ wrong: controlState.touched && !valid
84
75
  };
85
76
  }
86
- export function useReactControl(controlProps, controlValidators) {
87
- return useControl(controlProps, controlValidators);
77
+ export function useReactControl(options, validators) {
78
+ return useControl(options, validators);
88
79
  }
89
- export function useFormControl(controlProps, controlValidators) {
90
- return useControl(controlProps, controlValidators);
80
+ export function useFormControl(options, validators) {
81
+ return useControl(options, validators);
91
82
  }
92
- export function useInputControl(controlProps, controlValidators) {
93
- return useControl(controlProps, controlValidators);
83
+ export function useInputControl(options, validators) {
84
+ return useControl(options, validators);
94
85
  }
95
86
  //# 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,EAIL,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;AACpD,OAAO,EAAE,eAAe,EAAE,MAAM,MAAM,CAAC;AAOvC,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,KAAK,CAAC,CAAC;IACrE,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,QAAQ,CAAI,KAAK,CAAC,KAAU,CAAC,CAAC;IACxD,MAAM,CAAC,OAAO,EAAE,UAAU,CAAC,GAAG,QAAQ,CAAU,KAAK,CAAC,OAAO,IAAI,KAAK,CAAC,CAAC;IACxE,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,QAAQ,CAAU,KAAK,CAAC,CAAC;IACnD,MAAM,CAAC,OAAO,EAAE,UAAU,CAAC,GA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,KAAK,EAAE,YAAY,CAAC,OAAO;YAC3B,OAAO,EAAE,KAAK;SACf,CAAC,CAAC,CAAC;IACN,CAAC;IAED,OAAO;QACL,GAAG,YAAY;QACf,IAAI;QACJ,OAAO;QACP,UAAU;QACV,MAAM;QACN,OAAO,EAAE,CAAC,YAAY,CAAC,QAAQ;QAC/B,KAAK,EAAE,MAAM,CAAC,CAAC,CAAC;QAChB,MAAM;QACN,KAAK;QACL,OAAO,EAAE,CAAC,KAAK;QACf,QAAQ,EAAE,CAAC,YAAY,CAAC,KAAK;QAC7B,KAAK;QACL,QAAQ;QACR,aAAa;QACb,KAAK;QACL,SAAS,EAAE,CAAC,YAAY,CAAC,OAAO;QAChC,OAAO;QACP,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,20 +1,21 @@
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 dirties = controlsAllChecked(controls, 'dirty');
13
+ const touched = controlsPartialChecked(controls, 'touched');
14
+ const toucheds = 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,
@@ -26,14 +27,13 @@ export function useFormGroup(groupProps, groupValidators) {
26
27
  pristine: !dirty,
27
28
  pristines: !dirties,
28
29
  reset,
29
- state,
30
30
  setValidators,
31
+ state,
31
32
  touched,
32
33
  toucheds,
33
34
  untouched: !touched,
34
35
  untoucheds: !toucheds,
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":"AAAA,OAAO,EAGL,oBAAoB,EACrB,MAAM,wBAAwB,CAAC;AAChC,OAAO,EACL,kBAAkB,EAClB,sBAAsB,EACtB,eAAe,EACf,eAAe,EACf,YAAY,EACb,MAAM,gCAAgC,CAAC;AACxC,OAAO,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAUjC,MAAM,UAAU,YAAY,CAC1B,UAAiC,EACjC,eAAuC;IAEvC,MAAM,KAAK,GAAG,oBAAoB,CAChC,UAAU,EACV,eAAe,CAChB,CAAC;IAEF,MAAM,CAAC,UAAU,EAAE,aAAa,CAAC,GAAG,QAAQ,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC;IAE/D,MAAM,EAAE,QAAQ,EAAE,GAAG,KAAK,CAAC;IAE3B,MAAM,MAAM,GAAG,CAAC,GAAG,EAAE,CACnB,UAAU,CAAC,CAAC,CAAC,YAAY,CAAC,EAAE,QAAQ,EAAE,UAAU,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC;IAE9D,MAAM,KAAK,GAAG,CAAC,GAAG,EAAE,CAClB,MAAM,CAAC,MAAM,KAAK,CAAC,IAAI,kBAAkB,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC,EAAE,CAAC;IAElE,MAAM,OAAO,GAAG,CAAC,GAAG,EAAE,CAAC,sBAAsB,CAAC,QAAQ,EAAE,SAAS,CAAC,CAAC,EAAE,CAAC;IACtE,MAAM,QAAQ,GAAG,CAAC,GAAG,E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,CAAC,OAAO;QACnB,KAAK;QACL,aAAa;QACb,KAAK;QACL,OAAO;QACP,QAAQ;QACR,SAAS,EAAE,CAAC,OAAO;QACnB,UAAU,EAAE,CAAC,QAAQ;QACrB,KAAK;QACL,KAAK,EAAE,OAAO,IAAI,CAAC,KAAK;KACzB,CAAC;AACJ,CAAC"}
@@ -1,17 +1,17 @@
1
- import { FormControlProps, FormState } from '@rolster/helpers-forms';
1
+ import { FormControlOptions } from '@rolster/forms';
2
2
  import { ValidatorFn } 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,40 @@
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 { setValue, state, validators } = options;
6
+ const control = useInputControl(state, validators);
7
7
  useEffect(() => {
8
- const { elementRef } = inputControl;
8
+ const { elementRef } = control;
9
9
  elementRef?.current?.addEventListener('focus', () => {
10
- inputControl.focus();
10
+ control.focus();
11
11
  });
12
12
  elementRef?.current?.addEventListener('blur', () => {
13
- inputControl.blur();
14
- if (!inputControl.touched) {
15
- inputControl.touch();
13
+ control.blur();
14
+ if (!control.touched) {
15
+ control.touch();
16
16
  }
17
17
  });
18
18
  elementRef?.current?.addEventListener('change', ({ target }) => {
19
- setValue(inputControl, target.value);
19
+ setValue(control, target.value);
20
20
  });
21
21
  }, []);
22
- return inputControl;
22
+ return control;
23
23
  }
24
- export function useTextRefControl(controlProps, controlValidators) {
24
+ export function useTextRefControl(options, validators) {
25
25
  return useInputRefControl({
26
- ...createFormControlProps(controlProps, controlValidators),
27
- setValue: (inputControl, value) => inputControl.setState(value)
26
+ ...createFormControlOptions(options, validators),
27
+ setValue: (control, value) => {
28
+ control.setState(value);
29
+ }
28
30
  });
29
31
  }
30
- export function useNumberRefControl(controlProps, controlValidators) {
32
+ export function useNumberRefControl(options, validators) {
31
33
  return useInputRefControl({
32
- ...createFormControlProps(controlProps, controlValidators),
33
- setValue: (inputControl, value) => inputControl.setState(+value)
34
+ ...createFormControlOptions(options, validators),
35
+ setValue: (control, value) => {
36
+ control.setState(+value);
37
+ }
34
38
  });
35
39
  }
36
40
  //# sourceMappingURL=form-ref.hook.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"form-ref.hook.js","sourceRoot":"","sources":["../../src/form-ref.h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,EAAE,EAAE;YAC7D,QAAQ,CAAC,OAAO,EAAG,MAA2B,CAAC,KAAK,CAAC,CAAC;QACxD,CAAC,CAAC,CAAC;IACL,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,OAAO,OAAO,CAAC;AACjB,CAAC;AAUD,MAAM,UAAU,iBAAiB,CAC/B,OAAiC,EACjC,UAAkC;IAElC,OAAO,kBAAkB,CAAC;QACxB,GAAG,wBAAwB,CACzB,OAAO,EACP,UAAU,CACX;QACD,QAAQ,EAAE,CAAC,OAAO,EAAE,KAAK,EAAE,EAAE;YAC3B,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;QAC1B,CAAC;KACF,CAAC,CAAC;AACL,CAAC;AAUD,MAAM,UAAU,mBAAmB,CACjC,OAAmC,EACnC,UAAkC;IAElC,OAAO,kBAAkB,CAAC;QACxB,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"}