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.

65 lines (64 loc) 2.15 kB
import { jsx } from "react/jsx-runtime"; import { createStyles, Switch as Switch$1 } from "@mantine/core"; import { forwardRef, useCallback } from "react"; import { ComponentErrorBoundary } from "../errorBoundary/ComponentErrorBoundary.js"; import { useSwitchState } from "../../state/components/boolean/useBooleanState.js"; import { useRegisterFormInput } from "../../state/components/form/useRegisterFormInput.js"; import { useInput } from "../../state/components/input/useInput.js"; import { useComponentId } from "../../state/components/useId.js"; const useStyles = createStyles((theme) => ({ label: { color: theme.colors.gray[7] } })); const SwitchComponent = /* @__PURE__ */ forwardRef(({ width, ...props }, ref) => { const { classes } = useStyles(); const propsOnChange = props.onChange; const onChange = useCallback((e) => { propsOnChange == null ? void 0 : propsOnChange(e.currentTarget.checked); }, [propsOnChange]); return /* @__PURE__ */ jsx(Switch$1, { classNames: classes, ...props, ref, onChange }); }); SwitchComponent.displayName = "SwitchComponent"; const Switch = /* @__PURE__ */ forwardRef((props, ref) => /* @__PURE__ */ jsx(ComponentErrorBoundary, { componentName: DISPLAY_NAME, children: /* @__PURE__ */ jsx(SwitchWithoutRef, { ...props, innerRef: ref }) })); const DISPLAY_NAME = "Switch"; Switch.displayName = DISPLAY_NAME; const SwitchWithoutRef = (props) => { const id = useComponentId(props.id); const { state, dispatch } = useSwitchState(id, { initialState: { value: props.checked ?? props.defaultChecked, disabled: props.disabled ?? props.defaultDisabled } }); const { inputProps } = useInput(props, state, dispatch, (v) => v); const { value: checked, ...restInputProps } = inputProps; const { innerRef, validate: _, onChange: __, ...restProps } = props; useRegisterFormInput(id, "switch"); return /* @__PURE__ */ jsx(SwitchComponent, { checked, ref: innerRef, ...restInputProps, ...restProps }); }; export { Switch, SwitchComponent, SwitchWithoutRef, useStyles }; //# sourceMappingURL=Switch.js.map