@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
53 lines • 2.83 kB
JavaScript
import { jsx as _jsx } from "react/jsx-runtime";
import React from 'react';
import { useId } from '../../hooks/useId/useId';
import { useStyles } from '../../hooks/useStyles/useStyles';
import { ErrorBoundary } from '../../provider/errorBoundary/errorBoundary';
import { FallbackComponent } from '../../provider/errorBoundary/fallbackComponent';
import { ResizeObserver } from '../../utils/resizeObserver/resizeObserver';
import { PillSelectorStandAlone } from './pillSelectorStandAlone';
const PILL_SELECTOR_STYLES = 'PILL_SELECTOR_STYLES';
const PillSelectorControlledComponent = React.forwardRef(({ ctv, ...props }, ref) => {
const styles = useStyles(PILL_SELECTOR_STYLES, props.variant, ctv);
const id = useId('name');
const measuredRef = React.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.useImperativeHandle(ref, () => {
return measuredRef.current;
}, []);
React.useEffect(() => {
let resizeObserver;
if (!props.multiSelect && measuredRef?.current && props.pillSelected?.[0] && styles?.thumb) {
updateMeasurements();
resizeObserver = new ResizeObserver(() => {
updateMeasurements();
});
// Start observing the element
resizeObserver.observe(measuredRef.current);
}
return () => {
resizeObserver?.disconnect();
};
}, [props.multiSelect, measuredRef, props.pills, props.pillSelected?.[0]]);
return (_jsx(PillSelectorStandAlone, { ref: measuredRef, name: `pillSelector${id}`, styles: styles, ...props }));
});
PillSelectorControlledComponent.displayName = 'PillSelectorControlledComponent';
const PillSelectorBoundary = (props, ref) => (_jsx(ErrorBoundary, { fallBackComponent: _jsx(FallbackComponent, { children: _jsx(PillSelectorStandAlone, { ...props, ref: ref }) }), children: _jsx(PillSelectorControlledComponent, { ...props, ref: ref }) }));
const PillSelectorControlled = React.forwardRef(PillSelectorBoundary);
/**
* @deprecated Try the new PillSelectorV2 component
*/
export { PillSelectorControlled };
//# sourceMappingURL=pillSelectorControlled.js.map