@airplane/views
Version:
A React library for building Airplane views. Views components are optimized in style and functionality to produce internal apps that are easy to build and maintain.
46 lines (45 loc) • 1.06 kB
JavaScript
import { useEffect, useCallback, useMemo } from "react";
const useInput = (props, state, dispatch, getChangeValue) => {
const {
value,
disabled,
showErrors,
errors
} = state;
useEffect(() => {
dispatch({
type: "validate",
required: props.required,
validate: props.validate
});
}, [dispatch, props.required, props.validate, value]);
const propsOnChange = props.onChange;
const onChange = useCallback((v) => {
dispatch({
type: "setValue",
value: getChangeValue(v)
});
if (propsOnChange) {
propsOnChange(v);
}
}, [dispatch, getChangeValue, propsOnChange]);
const inputProps = useMemo(() => ({
error: getErrorMessage(showErrors, errors),
onChange,
value,
disabled
}), [showErrors, errors, onChange, value, disabled]);
return {
inputProps
};
};
const getErrorMessage = (showErrors, errors) => {
if (showErrors && errors.length > 0) {
return errors.join("\n");
}
return void 0;
};
export {
useInput
};
//# sourceMappingURL=useInput.js.map