main
Last change
on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago |
Update repo after prototype presentation
|
-
Property mode
set to
100644
|
File size:
991 bytes
|
Rev | Line | |
---|
[d565449] | 1 | import { useCallback, useEffect, useRef, useState } from 'react';
|
---|
| 2 | export function useMultiStateValidator(states, validator, initialValidity) {
|
---|
| 3 | if (initialValidity === void 0) { initialValidity = [undefined]; }
|
---|
| 4 | if (typeof states !== 'object') {
|
---|
| 5 | throw new Error('states expected to be an object or array, got ' + typeof states);
|
---|
| 6 | }
|
---|
| 7 | var validatorInner = useRef(validator);
|
---|
| 8 | var statesInner = useRef(states);
|
---|
| 9 | validatorInner.current = validator;
|
---|
| 10 | statesInner.current = states;
|
---|
| 11 | var _a = useState(initialValidity), validity = _a[0], setValidity = _a[1];
|
---|
| 12 | var validate = useCallback(function () {
|
---|
| 13 | if (validatorInner.current.length >= 2) {
|
---|
| 14 | validatorInner.current(statesInner.current, setValidity);
|
---|
| 15 | }
|
---|
| 16 | else {
|
---|
| 17 | setValidity(validatorInner.current(statesInner.current));
|
---|
| 18 | }
|
---|
| 19 | }, [setValidity]);
|
---|
| 20 | useEffect(function () {
|
---|
| 21 | validate();
|
---|
| 22 | }, Object.values(states));
|
---|
| 23 | return [validity, validate];
|
---|
| 24 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.