@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.
39 lines (38 loc) • 1.3 kB
JavaScript
import { useReducer, useMemo } from "react";
import { useStateSetters } from "../input/useStateSetters.js";
import { ComponentType, useSyncComponentState } from "../../context/context.js";
import { reducer } from "./reducer.js";
const getUseBooleanState = (componentType) => (id, options) => {
var _a, _b;
const initialState = {
value: ((_a = options == null ? void 0 : options.initialState) == null ? void 0 : _a.value) ?? false,
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 state = useMemo(() => ({
...internalState,
...stateSetters,
checked: internalState.value || false,
setChecked: stateSetters.setValue,
id,
componentType
}), [internalState, id, stateSetters]);
useSyncComponentState(id, state);
return {
state,
dispatch
};
};
const useSwitchState = getUseBooleanState(ComponentType.Switch);
const useCheckboxState = getUseBooleanState(ComponentType.Checkbox);
export {
getUseBooleanState,
useCheckboxState,
useSwitchState
};
//# sourceMappingURL=useBooleanState.js.map