1import { useCallback, useMemo, useState } from 'react';
2import { keyBy, get, every, some } from 'lodash';
4import { compose } from 'lib/lib';
5import { kDefaultValidator } from 'lib/validators';
6import useSetState from 'lib/useSetState';
8// interface InputConfig {
10// validators: Function[];
11// transformers: Function[];
12// initialValue: string;
18const defaultsFor = (configs, mapper) =>
20 (memo, config) => ({ ...memo, [config.name]: mapper(config) }),
25 * useForm manages a set of inputs for rendering them in a loop
27export default function useForm(configs = []) {
28 const byName = useMemo(() => keyBy(configs, 'name'), [configs]);
31 const [values, _setValues] = useState(() =>
32 defaultsFor(configs, config => config.initialValue)
34 const setValue = useCallback(
35 (name, value) => _setValues(values => ({ ...values, [name]: value })),
38 // ^ NB(shrugs): because we're not syncing the initialValue of _additional_
39 // configs that are added, we won't be able to set an initialValue for a
40 // dynamic form that then triggers validation. for initial configs,
41 // initialValue can be a validated string and everything will be happy,
42 // but for dynamically added input configs, we won't get validation, etc
43 // until there's a traditional state update.
45 // track focused states
46 const [focused, setFocused] = useSetState(() =>
47 defaultsFor(configs, config => config.autoFocus && !config.disabled)
49 const focuses = useMemo(
50 () => configs.map(config => focused[config.name] || false), //
54 // track whether or not input has been focused
55 const [hasBeenFocused, setHasBeenFocused] = useSetState({});
57 // build fn that transforms a value by input name
58 const transform = useCallback(
59 (name, value) => compose(...get(byName, [name, 'transformers'], []))(value),
63 // build fn that validates a value by input name
64 const validate = useCallback(
67 ...get(byName, [name, 'validators'], []),
73 // on change, transform and set value
74 const onChange = useCallback(
75 name => e => setValue(name, transform(name, e.target.value)), //
79 // on focus, update focus
80 const onFocus = useCallback(
81 name => e => setFocused({ [name]: true }), //
85 // on blur, defocus and set has been focused
86 const onBlur = useCallback(
88 setFocused({ [name]: false });
89 setHasBeenFocused({ [name]: true });
91 [setFocused, setHasBeenFocused]
94 // memo-compute validations of curent values
95 const validations = useMemo(
96 () => configs.map(config => validate(config.name, values[config.name])), //
97 [configs, validate, values]
100 // memo-compute the set of (perhaps changed by validation) data
101 const datas = useMemo(() => validations.map(v => v.data), [validations]);
103 // the input has errored if it
104 // 1) has been focused before
106 // a) did not pass validation and has an error text
107 // b) or has a specific error from parent state
108 // (and we implicitly return the error text at the end)
109 const errors = useMemo(
113 hasBeenFocused[config.name] && //
114 (validations[i].error || config.error)
116 [configs, validations, hasBeenFocused]
119 // this input has passed if
120 // 1) it has passed local validation
121 // 2) there are no errors
122 const passes = useMemo(
123 () => validations.map((v, i) => v.pass && !errors[i]),
124 [validations, errors]
127 // visibly tell the user that their input has passed if
128 // 1) they are or have interacted with the input before
129 // 2) and it has passed as defined above
130 const visiblePasses = useMemo(
134 (focused[config.name] || hasBeenFocused[config.name]) && //
137 [configs, hasBeenFocused, focused, passes]
140 // generate the list of input states
141 const inputs = useMemo(
144 ({ name, error, autoFocus, disabled, initialValue, ...rest }, i) => ({
149 visiblyPassed: visiblePasses[i],
152 autoFocus: autoFocus && !disabled,
155 // dom properties below:
157 value: values[name] || initialValue,
158 onChange: onChange(name),
159 onFocus: onFocus(name),
160 onBlur: onBlur(name),
161 autoFocus: autoFocus && !disabled,
179 // did all of the inputs pass validation?
180 const pass = useMemo(() => every(passes), [passes]);
181 // did any of the inputs error?
182 const error = useMemo(() => some(errors), [errors]);
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list