UNPKG

@prokodo/ui

Version:

UI components for production-grade Next.js + Headless CMS (Strapi, Contentful, Headless WordPress) websites by prokodo – built for Core Web Vitals & SEO.

63 lines (62 loc) 1.54 kB
"use client"; var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); import { jsx } from "react/jsx-runtime"; import { memo, useState, useEffect, useCallback } from "react"; import { SwitchView } from "./Switch.view.js"; function SwitchClient(props) { const { checked: controlledChecked, onChange, onFocus, onBlur } = props; const [isChecked, setIsChecked] = useState( controlledChecked ?? false ); useEffect(() => { if (controlledChecked !== void 0) { setIsChecked(controlledChecked); } }, [controlledChecked]); const handleChange = useCallback( (e) => { const newValue = e.target.checked; setIsChecked(newValue); onChange == null ? void 0 : onChange(e, newValue); }, [onChange] ); const [isFocused, setIsFocused] = useState(false); const handleFocus = useCallback( (e) => { setIsFocused(true); onFocus == null ? void 0 : onFocus(e); }, [onFocus] ); const handleBlur = useCallback( (e) => { setIsFocused(false); onBlur == null ? void 0 : onBlur(e); }, [onBlur] ); return /* @__PURE__ */ jsx( SwitchView, { ...props, checked: void 0, isChecked, isFocused, onBlurInternal: handleBlur, onChangeInternal: handleChange, onFocusInternal: handleFocus } ); } __name(SwitchClient, "SwitchClient"); const SwitchClient$1 = memo(SwitchClient); export { SwitchClient$1 as default };