UNPKG

@kubit-ui-web/react-components

Version:

Kubit React Components is a customizable, accessible library of React web components, designed to enhance your application's user experience

56 lines 3.37 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.PillSelectorControlled = void 0; const jsx_runtime_1 = require("react/jsx-runtime"); const react_1 = __importDefault(require("react")); const useId_1 = require("../../hooks/useId/useId"); const useStyles_1 = require("../../hooks/useStyles/useStyles"); const errorBoundary_1 = require("../../provider/errorBoundary/errorBoundary"); const fallbackComponent_1 = require("../../provider/errorBoundary/fallbackComponent"); const resizeObserver_1 = require("../../utils/resizeObserver/resizeObserver"); const pillSelectorStandAlone_1 = require("./pillSelectorStandAlone"); const PILL_SELECTOR_STYLES = 'PILL_SELECTOR_STYLES'; const PillSelectorControlledComponent = react_1.default.forwardRef(({ ctv, ...props }, ref) => { const styles = (0, useStyles_1.useStyles)(PILL_SELECTOR_STYLES, props.variant, ctv); const id = (0, useId_1.useId)('name'); const measuredRef = react_1.default.useRef(null); // update measurements of the thumb const updateMeasurements = () => { // get the index of the selected pill const index = props.pills.findIndex(pill => pill.value.toString() === props.pillSelected?.[0].toString()) + 1; // get the size and the position of the thumb from the selected pill if (measuredRef.current) { const thumb = measuredRef.current.childNodes[0]; const selectedPill = measuredRef.current.childNodes[index]; thumb.style.width = `${selectedPill?.clientWidth}px`; thumb.style.height = `${selectedPill?.getBoundingClientRect().height}px`; thumb.style.left = `${selectedPill.offsetLeft}px`; } }; react_1.default.useImperativeHandle(ref, () => { return measuredRef.current; }, []); react_1.default.useEffect(() => { let resizeObserver; if (!props.multiSelect && measuredRef?.current && props.pillSelected?.[0] && styles?.thumb) { updateMeasurements(); resizeObserver = new resizeObserver_1.ResizeObserver(() => { updateMeasurements(); }); // Start observing the element resizeObserver.observe(measuredRef.current); } return () => { resizeObserver?.disconnect(); }; }, [props.multiSelect, measuredRef, props.pills, props.pillSelected?.[0]]); return ((0, jsx_runtime_1.jsx)(pillSelectorStandAlone_1.PillSelectorStandAlone, { ref: measuredRef, name: `pillSelector${id}`, styles: styles, ...props })); }); PillSelectorControlledComponent.displayName = 'PillSelectorControlledComponent'; const PillSelectorBoundary = (props, ref) => ((0, jsx_runtime_1.jsx)(errorBoundary_1.ErrorBoundary, { fallBackComponent: (0, jsx_runtime_1.jsx)(fallbackComponent_1.FallbackComponent, { children: (0, jsx_runtime_1.jsx)(pillSelectorStandAlone_1.PillSelectorStandAlone, { ...props, ref: ref }) }), children: (0, jsx_runtime_1.jsx)(PillSelectorControlledComponent, { ...props, ref: ref }) })); const PillSelectorControlled = react_1.default.forwardRef(PillSelectorBoundary); exports.PillSelectorControlled = PillSelectorControlled; //# sourceMappingURL=pillSelectorControlled.js.map