UNPKG

@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.

45 lines (44 loc) 1.48 kB
import { useReducer, useCallback, useMemo } from "react"; import { useStateSetters } from "../input/useStateSetters.js"; import { ComponentType, useSyncComponentState } from "../../context/context.js"; import { reducer } from "./reducer.js"; const getUseTextInputState = (componentType) => (id, options) => { var _a, _b; const initialState = { value: ((_a = options == null ? void 0 : options.initialState) == null ? void 0 : _a.value) ?? "", disabled: ((_b = options == null ? void 0 : options.initialState) == null ? void 0 : _b.disabled) ?? false }; const [internalState, dispatch] = useReducer(reducer, { ...initialState, showErrors: false, errors: [] }); const stateSetters = useStateSetters(dispatch, initialState); const f = options == null ? void 0 : options.focus; const focus = useCallback(() => { if (f) f(); }, [f]); const state = useMemo(() => ({ ...internalState, ...stateSetters, id, focus, componentType }), [internalState, id, focus, stateSetters]); useSyncComponentState(id, state); return { state, dispatch }; }; const useTextInputState = getUseTextInputState(ComponentType.TextInput); const useTextareaState = getUseTextInputState(ComponentType.Textarea); const useCodeInputState = getUseTextInputState(ComponentType.CodeInput); export { getUseTextInputState, useCodeInputState, useTextInputState, useTextareaState }; //# sourceMappingURL=useTextInputState.js.map