@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.
49 lines (48 loc) • 1.7 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 clipMinAndMax = (value, min, max) => {
if (value === void 0) {
return value;
} else if (min !== void 0 && value < min) {
return min;
} else if (max !== void 0 && value > max) {
return max;
} else {
return value;
}
};
const getUseNumberInputState = (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 state = useMemo(() => ({
...internalState,
...stateSetters,
setValue: (v) => stateSetters.setValue(clipMinAndMax(v, options == null ? void 0 : options.min, options == null ? void 0 : options.max)),
id,
componentType
}), [internalState, id, stateSetters, options == null ? void 0 : options.min, options == null ? void 0 : options.max]);
useSyncComponentState(id, state);
return {
state,
dispatch
};
};
const useNumberInputState = getUseNumberInputState(ComponentType.NumberInput);
const useSliderState = getUseNumberInputState(ComponentType.Slider);
export {
getUseNumberInputState,
useNumberInputState,
useSliderState
};
//# sourceMappingURL=useNumberInputState.js.map