@rolster/react-forms 18.8.2 → 18.9.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.
- package/dist/cjs/index.js +419 -419
- package/dist/cjs/index.js.map +1 -1
- package/dist/es/index.js +419 -419
- package/dist/es/index.js.map +1 -1
- package/dist/esm/form-array/form-array-control.d.ts +67 -67
- package/dist/esm/form-array/form-array-control.js +95 -95
- package/dist/esm/form-array/form-array-group.d.ts +32 -32
- package/dist/esm/form-array/form-array-group.js +54 -54
- package/dist/esm/form-array/form-array-list.d.ts +26 -26
- package/dist/esm/form-array/form-array-list.js +66 -66
- package/dist/esm/form-array/form-array.d.ts +5 -5
- package/dist/esm/form-array/form-array.js +100 -100
- package/dist/esm/form-array/index.d.ts +4 -4
- package/dist/esm/form-array/index.js +4 -4
- package/dist/esm/form-control.d.ts +27 -27
- package/dist/esm/form-control.js +90 -90
- package/dist/esm/form-group.d.ts +4 -4
- package/dist/esm/form-group.js +39 -39
- package/dist/esm/form-ref.d.ts +17 -17
- package/dist/esm/form-ref.js +35 -35
- package/dist/esm/index.d.ts +4 -4
- package/dist/esm/index.js +4 -4
- package/dist/esm/types.d.ts +51 -51
- package/dist/esm/types.js +1 -1
- package/package.json +5 -5
|
@@ -1,55 +1,55 @@
|
|
|
1
|
-
import { createFormGroupOptions } from '@rolster/forms/arguments';
|
|
2
|
-
import { controlsAllChecked, controlsPartialChecked, controlsToValue, groupIsValid } from '@rolster/forms/helpers';
|
|
3
|
-
import { v4 as uuid } from 'uuid';
|
|
4
|
-
export class RolsterArrayGroup {
|
|
5
|
-
constructor(options) {
|
|
6
|
-
const { controls, resource, uuid, validators } = options;
|
|
7
|
-
this.uuid = uuid;
|
|
8
|
-
this.controls = controls;
|
|
9
|
-
this.validators = validators;
|
|
10
|
-
this.resource = resource;
|
|
11
|
-
this.errors = validators ? groupIsValid({ controls, validators }) : [];
|
|
12
|
-
this.error = this.errors[0];
|
|
13
|
-
this.valid =
|
|
14
|
-
this.errors.length === 0 && controlsAllChecked(controls, 'valid');
|
|
15
|
-
this.invalid = !this.valid;
|
|
16
|
-
this.dirty = controlsPartialChecked(controls, 'dirty');
|
|
17
|
-
this.dirtyAll = controlsAllChecked(controls, 'dirty');
|
|
18
|
-
this.touched = controlsPartialChecked(controls, 'touched');
|
|
19
|
-
this.touchedAll = controlsAllChecked(controls, 'touched');
|
|
20
|
-
this.untouched = !this.touched;
|
|
21
|
-
this.untouchedAll = !this.touchedAll;
|
|
22
|
-
this.pristine = !this.dirty;
|
|
23
|
-
this.pristineAll = !this.dirtyAll;
|
|
24
|
-
this.wrong = this.touched && this.invalid;
|
|
25
|
-
this.value = controlsToValue(controls);
|
|
26
|
-
const subscriber = (options) => {
|
|
27
|
-
this.refresh({
|
|
28
|
-
controls: Object.entries(this.controls).reduce((controls, [key, control]) => {
|
|
29
|
-
controls[key] =
|
|
30
|
-
control.uuid === options.uuid ? control.clone(options) : control;
|
|
31
|
-
return controls;
|
|
32
|
-
}, {})
|
|
33
|
-
});
|
|
34
|
-
};
|
|
35
|
-
Object.values(controls).forEach((control) => {
|
|
36
|
-
control.subscribe(subscriber);
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
setValidators(validators) {
|
|
40
|
-
this.refresh({ validators });
|
|
41
|
-
}
|
|
42
|
-
subscribe(subscriber) {
|
|
43
|
-
this.subscriber = subscriber;
|
|
44
|
-
}
|
|
45
|
-
refresh(changes) {
|
|
46
|
-
this.subscriber && this.subscriber({ ...this, ...changes });
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
export function formArrayGroup(options, validators) {
|
|
50
|
-
return new RolsterArrayGroup({
|
|
51
|
-
...createFormGroupOptions(options, validators),
|
|
52
|
-
uuid: uuid()
|
|
53
|
-
});
|
|
54
|
-
}
|
|
1
|
+
import { createFormGroupOptions } from '@rolster/forms/arguments';
|
|
2
|
+
import { controlsAllChecked, controlsPartialChecked, controlsToValue, groupIsValid } from '@rolster/forms/helpers';
|
|
3
|
+
import { v4 as uuid } from 'uuid';
|
|
4
|
+
export class RolsterArrayGroup {
|
|
5
|
+
constructor(options) {
|
|
6
|
+
const { controls, resource, uuid, validators } = options;
|
|
7
|
+
this.uuid = uuid;
|
|
8
|
+
this.controls = controls;
|
|
9
|
+
this.validators = validators;
|
|
10
|
+
this.resource = resource;
|
|
11
|
+
this.errors = validators ? groupIsValid({ controls, validators }) : [];
|
|
12
|
+
this.error = this.errors[0];
|
|
13
|
+
this.valid =
|
|
14
|
+
this.errors.length === 0 && controlsAllChecked(controls, 'valid');
|
|
15
|
+
this.invalid = !this.valid;
|
|
16
|
+
this.dirty = controlsPartialChecked(controls, 'dirty');
|
|
17
|
+
this.dirtyAll = controlsAllChecked(controls, 'dirty');
|
|
18
|
+
this.touched = controlsPartialChecked(controls, 'touched');
|
|
19
|
+
this.touchedAll = controlsAllChecked(controls, 'touched');
|
|
20
|
+
this.untouched = !this.touched;
|
|
21
|
+
this.untouchedAll = !this.touchedAll;
|
|
22
|
+
this.pristine = !this.dirty;
|
|
23
|
+
this.pristineAll = !this.dirtyAll;
|
|
24
|
+
this.wrong = this.touched && this.invalid;
|
|
25
|
+
this.value = controlsToValue(controls);
|
|
26
|
+
const subscriber = (options) => {
|
|
27
|
+
this.refresh({
|
|
28
|
+
controls: Object.entries(this.controls).reduce((controls, [key, control]) => {
|
|
29
|
+
controls[key] =
|
|
30
|
+
control.uuid === options.uuid ? control.clone(options) : control;
|
|
31
|
+
return controls;
|
|
32
|
+
}, {})
|
|
33
|
+
});
|
|
34
|
+
};
|
|
35
|
+
Object.values(controls).forEach((control) => {
|
|
36
|
+
control.subscribe(subscriber);
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
setValidators(validators) {
|
|
40
|
+
this.refresh({ validators });
|
|
41
|
+
}
|
|
42
|
+
subscribe(subscriber) {
|
|
43
|
+
this.subscriber = subscriber;
|
|
44
|
+
}
|
|
45
|
+
refresh(changes) {
|
|
46
|
+
this.subscriber && this.subscriber({ ...this, ...changes });
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
export function formArrayGroup(options, validators) {
|
|
50
|
+
return new RolsterArrayGroup({
|
|
51
|
+
...createFormGroupOptions(options, validators),
|
|
52
|
+
uuid: uuid()
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
55
|
//# sourceMappingURL=form-array-group.js.map
|
|
@@ -1,26 +1,26 @@
|
|
|
1
|
-
import { ArrayControlsValue, ArrayListValueToControls } from '@rolster/forms';
|
|
2
|
-
import { ValidatorFn } from '@rolster/validators';
|
|
3
|
-
import { ReactArrayControls, ReactArrayList, ReactArrayListOptions } from '../types';
|
|
4
|
-
import { RolsterReactArrayControl } from './form-array-control';
|
|
5
|
-
type RolsterListOptions<C extends ReactArrayControls = ReactArrayControls> = Omit<ReactArrayListOptions<C>, 'value'>;
|
|
6
|
-
export declare class RolsterArrayList<E extends HTMLElement = HTMLElement, C extends ReactArrayControls = ReactArrayControls> extends RolsterReactArrayControl<E, ArrayControlsValue<C>[]> implements ReactArrayList<C> {
|
|
7
|
-
readonly controls: C[];
|
|
8
|
-
readonly invalid: boolean;
|
|
9
|
-
readonly valid: boolean;
|
|
10
|
-
readonly wrong: boolean;
|
|
11
|
-
protected valueToControls: ArrayListValueToControls<C>;
|
|
12
|
-
constructor(options: RolsterListOptions<C>);
|
|
13
|
-
setValue(value: ArrayControlsValue<C>[]): void;
|
|
14
|
-
clone(options: ReactArrayListOptions<C>): RolsterArrayList<E, C>;
|
|
15
|
-
push(controls: C): void;
|
|
16
|
-
remove(controls: C): void;
|
|
17
|
-
protected refresh(changes: Partial<ReactArrayListOptions<C>>): void;
|
|
18
|
-
private subscribeControls;
|
|
19
|
-
}
|
|
20
|
-
interface ReactListOptions<C extends ReactArrayControls = ReactArrayControls> {
|
|
21
|
-
touched?: boolean;
|
|
22
|
-
validators?: ValidatorFn<ArrayControlsValue<C>[] | undefined>[];
|
|
23
|
-
value?: ArrayControlsValue<C>[] | undefined;
|
|
24
|
-
}
|
|
25
|
-
export declare function formArrayList<C extends ReactArrayControls = ReactArrayControls>(valueToControls: ArrayListValueToControls<C>, options?: ReactListOptions<C>): ReactArrayList<C>;
|
|
26
|
-
export {};
|
|
1
|
+
import { ArrayControlsValue, ArrayListValueToControls } from '@rolster/forms';
|
|
2
|
+
import { ValidatorFn } from '@rolster/validators';
|
|
3
|
+
import { ReactArrayControls, ReactArrayList, ReactArrayListOptions } from '../types';
|
|
4
|
+
import { RolsterReactArrayControl } from './form-array-control';
|
|
5
|
+
type RolsterListOptions<C extends ReactArrayControls = ReactArrayControls> = Omit<ReactArrayListOptions<C>, 'value'>;
|
|
6
|
+
export declare class RolsterArrayList<E extends HTMLElement = HTMLElement, C extends ReactArrayControls = ReactArrayControls> extends RolsterReactArrayControl<E, ArrayControlsValue<C>[]> implements ReactArrayList<C> {
|
|
7
|
+
readonly controls: C[];
|
|
8
|
+
readonly invalid: boolean;
|
|
9
|
+
readonly valid: boolean;
|
|
10
|
+
readonly wrong: boolean;
|
|
11
|
+
protected valueToControls: ArrayListValueToControls<C>;
|
|
12
|
+
constructor(options: RolsterListOptions<C>);
|
|
13
|
+
setValue(value: ArrayControlsValue<C>[]): void;
|
|
14
|
+
clone(options: ReactArrayListOptions<C>): RolsterArrayList<E, C>;
|
|
15
|
+
push(controls: C): void;
|
|
16
|
+
remove(controls: C): void;
|
|
17
|
+
protected refresh(changes: Partial<ReactArrayListOptions<C>>): void;
|
|
18
|
+
private subscribeControls;
|
|
19
|
+
}
|
|
20
|
+
interface ReactListOptions<C extends ReactArrayControls = ReactArrayControls> {
|
|
21
|
+
touched?: boolean;
|
|
22
|
+
validators?: ValidatorFn<ArrayControlsValue<C>[] | undefined>[];
|
|
23
|
+
value?: ArrayControlsValue<C>[] | undefined;
|
|
24
|
+
}
|
|
25
|
+
export declare function formArrayList<C extends ReactArrayControls = ReactArrayControls>(valueToControls: ArrayListValueToControls<C>, options?: ReactListOptions<C>): ReactArrayList<C>;
|
|
26
|
+
export {};
|
|
@@ -1,67 +1,67 @@
|
|
|
1
|
-
import { controlsAllChecked, controlsToValue } from '@rolster/forms/helpers';
|
|
2
|
-
import { v4 as uuid } from 'uuid';
|
|
3
|
-
import { RolsterReactArrayControl } from './form-array-control';
|
|
4
|
-
export class RolsterArrayList extends RolsterReactArrayControl {
|
|
5
|
-
constructor(options) {
|
|
6
|
-
const value = options.controls.map((controls) => controlsToValue(controls));
|
|
7
|
-
super({ ...options, value });
|
|
8
|
-
this.valueToControls = options.valueToControls;
|
|
9
|
-
this.controls = options.controls;
|
|
10
|
-
this.valid =
|
|
11
|
-
this.errors.length === 0 &&
|
|
12
|
-
this.controls.reduce((valid, controls) => valid && controlsAllChecked(controls, 'valid'), true);
|
|
13
|
-
this.invalid = !this.valid;
|
|
14
|
-
this.wrong = this.touched && this.invalid;
|
|
15
|
-
options.controls.forEach((controls) => {
|
|
16
|
-
this.subscribeControls(controls);
|
|
17
|
-
});
|
|
18
|
-
}
|
|
19
|
-
setValue(value) {
|
|
20
|
-
this.refresh({
|
|
21
|
-
controls: value.map((value) => this.valueToControls(value))
|
|
22
|
-
});
|
|
23
|
-
}
|
|
24
|
-
clone(options) {
|
|
25
|
-
return new RolsterArrayList(options);
|
|
26
|
-
}
|
|
27
|
-
push(controls) {
|
|
28
|
-
this.refresh({
|
|
29
|
-
controls: [...this.controls, controls]
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
remove(controls) {
|
|
33
|
-
this.refresh({
|
|
34
|
-
controls: this.controls.filter((currentControls) => currentControls !== controls)
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
refresh(changes) {
|
|
38
|
-
super.refresh(changes);
|
|
39
|
-
}
|
|
40
|
-
subscribeControls(newControls) {
|
|
41
|
-
Object.values(newControls).forEach((control) => {
|
|
42
|
-
control.subscribe((options) => {
|
|
43
|
-
const controls = this.controls.map((currentControls) => currentControls !== newControls
|
|
44
|
-
? currentControls
|
|
45
|
-
: Object.entries(newControls).reduce((controls, [key, control]) => {
|
|
46
|
-
controls[key] =
|
|
47
|
-
control.uuid === options.uuid
|
|
48
|
-
? control.clone(options)
|
|
49
|
-
: control;
|
|
50
|
-
return controls;
|
|
51
|
-
}, {}));
|
|
52
|
-
this.refresh({ controls });
|
|
53
|
-
});
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
export function formArrayList(valueToControls, options) {
|
|
58
|
-
const value = options?.value || [];
|
|
59
|
-
return new RolsterArrayList({
|
|
60
|
-
...options,
|
|
61
|
-
valueToControls,
|
|
62
|
-
controls: value.map((value) => valueToControls(value)),
|
|
63
|
-
initialValue: value,
|
|
64
|
-
uuid: uuid()
|
|
65
|
-
});
|
|
66
|
-
}
|
|
1
|
+
import { controlsAllChecked, controlsToValue } from '@rolster/forms/helpers';
|
|
2
|
+
import { v4 as uuid } from 'uuid';
|
|
3
|
+
import { RolsterReactArrayControl } from './form-array-control';
|
|
4
|
+
export class RolsterArrayList extends RolsterReactArrayControl {
|
|
5
|
+
constructor(options) {
|
|
6
|
+
const value = options.controls.map((controls) => controlsToValue(controls));
|
|
7
|
+
super({ ...options, value });
|
|
8
|
+
this.valueToControls = options.valueToControls;
|
|
9
|
+
this.controls = options.controls;
|
|
10
|
+
this.valid =
|
|
11
|
+
this.errors.length === 0 &&
|
|
12
|
+
this.controls.reduce((valid, controls) => valid && controlsAllChecked(controls, 'valid'), true);
|
|
13
|
+
this.invalid = !this.valid;
|
|
14
|
+
this.wrong = this.touched && this.invalid;
|
|
15
|
+
options.controls.forEach((controls) => {
|
|
16
|
+
this.subscribeControls(controls);
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
setValue(value) {
|
|
20
|
+
this.refresh({
|
|
21
|
+
controls: value.map((value) => this.valueToControls(value))
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
clone(options) {
|
|
25
|
+
return new RolsterArrayList(options);
|
|
26
|
+
}
|
|
27
|
+
push(controls) {
|
|
28
|
+
this.refresh({
|
|
29
|
+
controls: [...this.controls, controls]
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
remove(controls) {
|
|
33
|
+
this.refresh({
|
|
34
|
+
controls: this.controls.filter((currentControls) => currentControls !== controls)
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
refresh(changes) {
|
|
38
|
+
super.refresh(changes);
|
|
39
|
+
}
|
|
40
|
+
subscribeControls(newControls) {
|
|
41
|
+
Object.values(newControls).forEach((control) => {
|
|
42
|
+
control.subscribe((options) => {
|
|
43
|
+
const controls = this.controls.map((currentControls) => currentControls !== newControls
|
|
44
|
+
? currentControls
|
|
45
|
+
: Object.entries(newControls).reduce((controls, [key, control]) => {
|
|
46
|
+
controls[key] =
|
|
47
|
+
control.uuid === options.uuid
|
|
48
|
+
? control.clone(options)
|
|
49
|
+
: control;
|
|
50
|
+
return controls;
|
|
51
|
+
}, {}));
|
|
52
|
+
this.refresh({ controls });
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
export function formArrayList(valueToControls, options) {
|
|
58
|
+
const value = options?.value || [];
|
|
59
|
+
return new RolsterArrayList({
|
|
60
|
+
...options,
|
|
61
|
+
valueToControls,
|
|
62
|
+
controls: value.map((value) => valueToControls(value)),
|
|
63
|
+
initialValue: value,
|
|
64
|
+
uuid: uuid()
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
67
|
//# sourceMappingURL=form-array-list.js.map
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { FormArrayOptions, ValidatorArrayFn } from '@rolster/forms';
|
|
2
|
-
import { ReactArrayControls, ReactArrayGroup, ReactFormArray } from '../types';
|
|
3
|
-
export declare function useFormArray<C extends ReactArrayControls, R = any, G extends ReactArrayGroup<C, R> = ReactArrayGroup<C, R>>(): ReactFormArray<C, R, G>;
|
|
4
|
-
export declare function useFormArray<C extends ReactArrayControls, R = any, G extends ReactArrayGroup<C, R> = ReactArrayGroup<C, R>>(options: FormArrayOptions<C, R, G>): ReactFormArray<C, R, G>;
|
|
5
|
-
export declare function useFormArray<C extends ReactArrayControls, R = any, G extends ReactArrayGroup<C, R> = ReactArrayGroup<C, R>>(groups: ReactArrayGroup<C, R>[], validators?: ValidatorArrayFn<C, R>[]): ReactFormArray<C, R, G>;
|
|
1
|
+
import { FormArrayOptions, ValidatorArrayFn } from '@rolster/forms';
|
|
2
|
+
import { ReactArrayControls, ReactArrayGroup, ReactFormArray } from '../types';
|
|
3
|
+
export declare function useFormArray<C extends ReactArrayControls, R = any, G extends ReactArrayGroup<C, R> = ReactArrayGroup<C, R>>(): ReactFormArray<C, R, G>;
|
|
4
|
+
export declare function useFormArray<C extends ReactArrayControls, R = any, G extends ReactArrayGroup<C, R> = ReactArrayGroup<C, R>>(options: FormArrayOptions<C, R, G>): ReactFormArray<C, R, G>;
|
|
5
|
+
export declare function useFormArray<C extends ReactArrayControls, R = any, G extends ReactArrayGroup<C, R> = ReactArrayGroup<C, R>>(groups: ReactArrayGroup<C, R>[], validators?: ValidatorArrayFn<C, R>[]): ReactFormArray<C, R, G>;
|
|
@@ -1,101 +1,101 @@
|
|
|
1
|
-
import { createFormArrayOptions } from '@rolster/forms/arguments';
|
|
2
|
-
import { arrayIsValid, controlsToValue, groupAllChecked, groupPartialChecked, hasError as rolsterHasError, someErrors as rolsterSomeErrors } from '@rolster/forms/helpers';
|
|
3
|
-
import { useEffect, useRef, useState } from 'react';
|
|
4
|
-
import { RolsterArrayGroup } from './form-array-group';
|
|
5
|
-
export function useFormArray(options, arrayValidators) {
|
|
6
|
-
const arrayOptions = createFormArrayOptions(options, arrayValidators);
|
|
7
|
-
const groups = arrayOptions.groups || [];
|
|
8
|
-
const currentState = useRef(groups);
|
|
9
|
-
const [state, setState] = useState({
|
|
10
|
-
controls: groups.map(({ controls }) => controls),
|
|
11
|
-
disabled: false,
|
|
12
|
-
groups,
|
|
13
|
-
value: groups.map(({ controls }) => controlsToValue(controls)),
|
|
14
|
-
validators: arrayOptions.validators
|
|
15
|
-
});
|
|
16
|
-
const errors = state.validators
|
|
17
|
-
? arrayIsValid({ groups, validators: state.validators })
|
|
18
|
-
: [];
|
|
19
|
-
const error = errors[0];
|
|
20
|
-
const valid = errors.length === 0 && groupAllChecked(state.groups, 'valid');
|
|
21
|
-
const dirty = groupPartialChecked(state.groups, 'dirty');
|
|
22
|
-
const dirtyAll = groupAllChecked(state.groups, 'dirty');
|
|
23
|
-
const touched = groupPartialChecked(state.groups, 'touched');
|
|
24
|
-
const touchedAll = groupAllChecked(state.groups, 'touched');
|
|
25
|
-
useEffect(() => {
|
|
26
|
-
const subscriber = (options) => {
|
|
27
|
-
setGroups(state.groups.map((group) => group.uuid === options.uuid
|
|
28
|
-
? new RolsterArrayGroup(options)
|
|
29
|
-
: group));
|
|
30
|
-
};
|
|
31
|
-
state.groups.forEach((group) => {
|
|
32
|
-
group.subscribe(subscriber);
|
|
33
|
-
});
|
|
34
|
-
}, [state.groups]);
|
|
35
|
-
function disable() {
|
|
36
|
-
setState((state) => ({ ...state, disabled: true }));
|
|
37
|
-
}
|
|
38
|
-
function enable() {
|
|
39
|
-
setState((state) => ({ ...state, disabled: false }));
|
|
40
|
-
}
|
|
41
|
-
function setGroups(groups) {
|
|
42
|
-
setState((currentState) => ({
|
|
43
|
-
...currentState,
|
|
44
|
-
groups,
|
|
45
|
-
controls: groups.map(({ controls }) => controls),
|
|
46
|
-
value: groups.map(({ controls }) => controlsToValue(controls))
|
|
47
|
-
}));
|
|
48
|
-
}
|
|
49
|
-
function push(group) {
|
|
50
|
-
setGroups([...state.groups, group]);
|
|
51
|
-
}
|
|
52
|
-
function merge(groups) {
|
|
53
|
-
setGroups([...state.groups, ...groups]);
|
|
54
|
-
}
|
|
55
|
-
function set(groups) {
|
|
56
|
-
setGroups(groups);
|
|
57
|
-
}
|
|
58
|
-
function remove({ uuid }) {
|
|
59
|
-
setGroups(state.groups.filter((group) => group.uuid !== uuid));
|
|
60
|
-
}
|
|
61
|
-
function hasError(key) {
|
|
62
|
-
return rolsterHasError(errors, key);
|
|
63
|
-
}
|
|
64
|
-
function someErrors(keys) {
|
|
65
|
-
return rolsterSomeErrors(errors, keys);
|
|
66
|
-
}
|
|
67
|
-
function reset() {
|
|
68
|
-
setGroups(currentState.current);
|
|
69
|
-
}
|
|
70
|
-
function setValidators(validators) {
|
|
71
|
-
setState((state) => ({ ...state, validators }));
|
|
72
|
-
}
|
|
73
|
-
return {
|
|
74
|
-
...state,
|
|
75
|
-
dirty,
|
|
76
|
-
dirtyAll,
|
|
77
|
-
disable,
|
|
78
|
-
enable,
|
|
79
|
-
enabled: !state.disabled,
|
|
80
|
-
error,
|
|
81
|
-
errors,
|
|
82
|
-
hasError,
|
|
83
|
-
invalid: !valid,
|
|
84
|
-
merge,
|
|
85
|
-
pristine: !dirty,
|
|
86
|
-
pristineAll: !dirtyAll,
|
|
87
|
-
push,
|
|
88
|
-
remove,
|
|
89
|
-
reset,
|
|
90
|
-
set,
|
|
91
|
-
setValidators,
|
|
92
|
-
someErrors,
|
|
93
|
-
touched,
|
|
94
|
-
touchedAll,
|
|
95
|
-
untouched: !touched,
|
|
96
|
-
untouchedAll: !touchedAll,
|
|
97
|
-
valid,
|
|
98
|
-
wrong: touched && !valid
|
|
99
|
-
};
|
|
100
|
-
}
|
|
1
|
+
import { createFormArrayOptions } from '@rolster/forms/arguments';
|
|
2
|
+
import { arrayIsValid, controlsToValue, groupAllChecked, groupPartialChecked, hasError as rolsterHasError, someErrors as rolsterSomeErrors } from '@rolster/forms/helpers';
|
|
3
|
+
import { useEffect, useRef, useState } from 'react';
|
|
4
|
+
import { RolsterArrayGroup } from './form-array-group';
|
|
5
|
+
export function useFormArray(options, arrayValidators) {
|
|
6
|
+
const arrayOptions = createFormArrayOptions(options, arrayValidators);
|
|
7
|
+
const groups = arrayOptions.groups || [];
|
|
8
|
+
const currentState = useRef(groups);
|
|
9
|
+
const [state, setState] = useState({
|
|
10
|
+
controls: groups.map(({ controls }) => controls),
|
|
11
|
+
disabled: false,
|
|
12
|
+
groups,
|
|
13
|
+
value: groups.map(({ controls }) => controlsToValue(controls)),
|
|
14
|
+
validators: arrayOptions.validators
|
|
15
|
+
});
|
|
16
|
+
const errors = state.validators
|
|
17
|
+
? arrayIsValid({ groups, validators: state.validators })
|
|
18
|
+
: [];
|
|
19
|
+
const error = errors[0];
|
|
20
|
+
const valid = errors.length === 0 && groupAllChecked(state.groups, 'valid');
|
|
21
|
+
const dirty = groupPartialChecked(state.groups, 'dirty');
|
|
22
|
+
const dirtyAll = groupAllChecked(state.groups, 'dirty');
|
|
23
|
+
const touched = groupPartialChecked(state.groups, 'touched');
|
|
24
|
+
const touchedAll = groupAllChecked(state.groups, 'touched');
|
|
25
|
+
useEffect(() => {
|
|
26
|
+
const subscriber = (options) => {
|
|
27
|
+
setGroups(state.groups.map((group) => group.uuid === options.uuid
|
|
28
|
+
? new RolsterArrayGroup(options)
|
|
29
|
+
: group));
|
|
30
|
+
};
|
|
31
|
+
state.groups.forEach((group) => {
|
|
32
|
+
group.subscribe(subscriber);
|
|
33
|
+
});
|
|
34
|
+
}, [state.groups]);
|
|
35
|
+
function disable() {
|
|
36
|
+
setState((state) => ({ ...state, disabled: true }));
|
|
37
|
+
}
|
|
38
|
+
function enable() {
|
|
39
|
+
setState((state) => ({ ...state, disabled: false }));
|
|
40
|
+
}
|
|
41
|
+
function setGroups(groups) {
|
|
42
|
+
setState((currentState) => ({
|
|
43
|
+
...currentState,
|
|
44
|
+
groups,
|
|
45
|
+
controls: groups.map(({ controls }) => controls),
|
|
46
|
+
value: groups.map(({ controls }) => controlsToValue(controls))
|
|
47
|
+
}));
|
|
48
|
+
}
|
|
49
|
+
function push(group) {
|
|
50
|
+
setGroups([...state.groups, group]);
|
|
51
|
+
}
|
|
52
|
+
function merge(groups) {
|
|
53
|
+
setGroups([...state.groups, ...groups]);
|
|
54
|
+
}
|
|
55
|
+
function set(groups) {
|
|
56
|
+
setGroups(groups);
|
|
57
|
+
}
|
|
58
|
+
function remove({ uuid }) {
|
|
59
|
+
setGroups(state.groups.filter((group) => group.uuid !== uuid));
|
|
60
|
+
}
|
|
61
|
+
function hasError(key) {
|
|
62
|
+
return rolsterHasError(errors, key);
|
|
63
|
+
}
|
|
64
|
+
function someErrors(keys) {
|
|
65
|
+
return rolsterSomeErrors(errors, keys);
|
|
66
|
+
}
|
|
67
|
+
function reset() {
|
|
68
|
+
setGroups(currentState.current);
|
|
69
|
+
}
|
|
70
|
+
function setValidators(validators) {
|
|
71
|
+
setState((state) => ({ ...state, validators }));
|
|
72
|
+
}
|
|
73
|
+
return {
|
|
74
|
+
...state,
|
|
75
|
+
dirty,
|
|
76
|
+
dirtyAll,
|
|
77
|
+
disable,
|
|
78
|
+
enable,
|
|
79
|
+
enabled: !state.disabled,
|
|
80
|
+
error,
|
|
81
|
+
errors,
|
|
82
|
+
hasError,
|
|
83
|
+
invalid: !valid,
|
|
84
|
+
merge,
|
|
85
|
+
pristine: !dirty,
|
|
86
|
+
pristineAll: !dirtyAll,
|
|
87
|
+
push,
|
|
88
|
+
remove,
|
|
89
|
+
reset,
|
|
90
|
+
set,
|
|
91
|
+
setValidators,
|
|
92
|
+
someErrors,
|
|
93
|
+
touched,
|
|
94
|
+
touchedAll,
|
|
95
|
+
untouched: !touched,
|
|
96
|
+
untouchedAll: !touchedAll,
|
|
97
|
+
valid,
|
|
98
|
+
wrong: touched && !valid
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
101
|
//# sourceMappingURL=form-array.js.map
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { formArrayControl, inputArrayControl, reactArrayControl } from './form-array-control';
|
|
2
|
-
export { formArrayGroup } from './form-array-group';
|
|
3
|
-
export { formArrayList } from './form-array-list';
|
|
4
|
-
export { useFormArray } from './form-array';
|
|
1
|
+
export { formArrayControl, inputArrayControl, reactArrayControl } from './form-array-control';
|
|
2
|
+
export { formArrayGroup } from './form-array-group';
|
|
3
|
+
export { formArrayList } from './form-array-list';
|
|
4
|
+
export { useFormArray } from './form-array';
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export { formArrayControl, inputArrayControl, reactArrayControl } from './form-array-control';
|
|
2
|
-
export { formArrayGroup } from './form-array-group';
|
|
3
|
-
export { formArrayList } from './form-array-list';
|
|
4
|
-
export { useFormArray } from './form-array';
|
|
1
|
+
export { formArrayControl, inputArrayControl, reactArrayControl } from './form-array-control';
|
|
2
|
+
export { formArrayGroup } from './form-array-group';
|
|
3
|
+
export { formArrayList } from './form-array-list';
|
|
4
|
+
export { useFormArray } from './form-array';
|
|
5
5
|
//# sourceMappingURL=index.js.map
|
|
@@ -1,27 +1,27 @@
|
|
|
1
|
-
import { FormControlOptions } from '@rolster/forms';
|
|
2
|
-
import { ValidatorFn } from '@rolster/validators';
|
|
3
|
-
import { ReactFormControl, ReactHtmlControl, ReactInputControl } from './types';
|
|
4
|
-
interface ReactControlOptions<T = any> extends FormControlOptions<T> {
|
|
5
|
-
touched?: boolean;
|
|
6
|
-
}
|
|
7
|
-
type ReactValueOptions<T> = Omit<ReactControlOptions<T>, 'validators'>;
|
|
8
|
-
type ReactValidatorsOptions<T> = Omit<ReactControlOptions<T>, 'value'>;
|
|
9
|
-
export declare function useReactControl<E extends HTMLElement, T>(): ReactFormControl<E, T | undefined>;
|
|
10
|
-
export declare function useReactControl<E extends HTMLElement, T>(options: ReactValueOptions<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>(options: ReactControlOptions<T>): ReactFormControl<E, T>;
|
|
13
|
-
export declare function useReactControl<E extends HTMLElement, T>(value: undefined, validators?: ValidatorFn<T>[]): ReactFormControl<E, T | undefined>;
|
|
14
|
-
export declare function useReactControl<E extends HTMLElement, T>(value: T, validators?: ValidatorFn<T>[]): ReactFormControl<E, T>;
|
|
15
|
-
export declare function useFormControl<T>(): ReactHtmlControl<T | undefined>;
|
|
16
|
-
export declare function useFormControl<T>(options: ReactValueOptions<T>): ReactHtmlControl<T>;
|
|
17
|
-
export declare function useFormControl<T>(options: ReactValidatorsOptions<T>): ReactHtmlControl<T | undefined>;
|
|
18
|
-
export declare function useFormControl<T>(options: ReactControlOptions<T>): ReactHtmlControl<T>;
|
|
19
|
-
export declare function useFormControl<T>(value: undefined, validators?: ValidatorFn<T>[]): ReactHtmlControl<T | undefined>;
|
|
20
|
-
export declare function useFormControl<T>(value: T, validators?: ValidatorFn<T>[]): ReactHtmlControl<T>;
|
|
21
|
-
export declare function useInputControl<T>(): ReactInputControl<T | undefined>;
|
|
22
|
-
export declare function useInputControl<T>(options: ReactValueOptions<T>): ReactInputControl<T>;
|
|
23
|
-
export declare function useInputControl<T>(options: ReactValidatorsOptions<T>): ReactInputControl<T | undefined>;
|
|
24
|
-
export declare function useInputControl<T>(options: ReactControlOptions<T>): ReactInputControl<T>;
|
|
25
|
-
export declare function useInputControl<T>(value: undefined, validators?: ValidatorFn<T>[]): ReactInputControl<T | undefined>;
|
|
26
|
-
export declare function useInputControl<T>(value: T, validators?: ValidatorFn<T>[]): ReactInputControl<T>;
|
|
27
|
-
export {};
|
|
1
|
+
import { FormControlOptions } from '@rolster/forms';
|
|
2
|
+
import { ValidatorFn } from '@rolster/validators';
|
|
3
|
+
import { ReactFormControl, ReactHtmlControl, ReactInputControl } from './types';
|
|
4
|
+
interface ReactControlOptions<T = any> extends FormControlOptions<T> {
|
|
5
|
+
touched?: boolean;
|
|
6
|
+
}
|
|
7
|
+
type ReactValueOptions<T> = Omit<ReactControlOptions<T>, 'validators'>;
|
|
8
|
+
type ReactValidatorsOptions<T> = Omit<ReactControlOptions<T>, 'value'>;
|
|
9
|
+
export declare function useReactControl<E extends HTMLElement, T>(): ReactFormControl<E, T | undefined>;
|
|
10
|
+
export declare function useReactControl<E extends HTMLElement, T>(options: ReactValueOptions<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>(options: ReactControlOptions<T>): ReactFormControl<E, T>;
|
|
13
|
+
export declare function useReactControl<E extends HTMLElement, T>(value: undefined, validators?: ValidatorFn<T>[]): ReactFormControl<E, T | undefined>;
|
|
14
|
+
export declare function useReactControl<E extends HTMLElement, T>(value: T, validators?: ValidatorFn<T>[]): ReactFormControl<E, T>;
|
|
15
|
+
export declare function useFormControl<T>(): ReactHtmlControl<T | undefined>;
|
|
16
|
+
export declare function useFormControl<T>(options: ReactValueOptions<T>): ReactHtmlControl<T>;
|
|
17
|
+
export declare function useFormControl<T>(options: ReactValidatorsOptions<T>): ReactHtmlControl<T | undefined>;
|
|
18
|
+
export declare function useFormControl<T>(options: ReactControlOptions<T>): ReactHtmlControl<T>;
|
|
19
|
+
export declare function useFormControl<T>(value: undefined, validators?: ValidatorFn<T>[]): ReactHtmlControl<T | undefined>;
|
|
20
|
+
export declare function useFormControl<T>(value: T, validators?: ValidatorFn<T>[]): ReactHtmlControl<T>;
|
|
21
|
+
export declare function useInputControl<T>(): ReactInputControl<T | undefined>;
|
|
22
|
+
export declare function useInputControl<T>(options: ReactValueOptions<T>): ReactInputControl<T>;
|
|
23
|
+
export declare function useInputControl<T>(options: ReactValidatorsOptions<T>): ReactInputControl<T | undefined>;
|
|
24
|
+
export declare function useInputControl<T>(options: ReactControlOptions<T>): ReactInputControl<T>;
|
|
25
|
+
export declare function useInputControl<T>(value: undefined, validators?: ValidatorFn<T>[]): ReactInputControl<T | undefined>;
|
|
26
|
+
export declare function useInputControl<T>(value: T, validators?: ValidatorFn<T>[]): ReactInputControl<T>;
|
|
27
|
+
export {};
|