[d565449] | 1 | "use strict";
|
---|
| 2 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
| 3 | exports.useMultiStateValidator = void 0;
|
---|
| 4 | var react_1 = require("react");
|
---|
| 5 | function useMultiStateValidator(states, validator, initialValidity) {
|
---|
| 6 | if (initialValidity === void 0) { initialValidity = [undefined]; }
|
---|
| 7 | if (typeof states !== 'object') {
|
---|
| 8 | throw new Error('states expected to be an object or array, got ' + typeof states);
|
---|
| 9 | }
|
---|
| 10 | var validatorInner = react_1.useRef(validator);
|
---|
| 11 | var statesInner = react_1.useRef(states);
|
---|
| 12 | validatorInner.current = validator;
|
---|
| 13 | statesInner.current = states;
|
---|
| 14 | var _a = react_1.useState(initialValidity), validity = _a[0], setValidity = _a[1];
|
---|
| 15 | var validate = react_1.useCallback(function () {
|
---|
| 16 | if (validatorInner.current.length >= 2) {
|
---|
| 17 | validatorInner.current(statesInner.current, setValidity);
|
---|
| 18 | }
|
---|
| 19 | else {
|
---|
| 20 | setValidity(validatorInner.current(statesInner.current));
|
---|
| 21 | }
|
---|
| 22 | }, [setValidity]);
|
---|
| 23 | react_1.useEffect(function () {
|
---|
| 24 | validate();
|
---|
| 25 | }, Object.values(states));
|
---|
| 26 | return [validity, validate];
|
---|
| 27 | }
|
---|
| 28 | exports.useMultiStateValidator = useMultiStateValidator;
|
---|