@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
46 lines • 2.58 kB
JavaScript
import { jsx as _jsx } from "react/jsx-runtime";
import React from 'react';
import { useStyles } from '../../hooks/useStyles/useStyles';
import { ErrorBoundary } from '../../provider/errorBoundary/errorBoundary';
import { FallbackComponent } from '../../provider/errorBoundary/fallbackComponent';
import { POSITIONS } from '../../types/positions/positions';
import { isKeyEnterPressed, isKeySpacePressed } from '../../utils/keyboard/keyboard.utility';
import { ToggleStandAlone } from './toggleStandAlone';
import { getToggleState } from './utils/getToggleState';
const TOGGLE_STYLES = 'TOGGLE_STYLES';
const ToggleControlledComponent = React.forwardRef(({ variant, ctv, hasThreePositions = false, togglePosition = hasThreePositions ? POSITIONS.CENTER : POSITIONS.LEFT, disabled = false, onClick, onChange, onKeyDown, ...props }, ref) => {
const styleVariant = useStyles(TOGGLE_STYLES, variant, ctv);
const handleKeyDown = e => {
if (!disabled && (isKeySpacePressed(e.key) || isKeyEnterPressed(e.key))) {
let newPosition = POSITIONS.LEFT;
if ((togglePosition === POSITIONS.LEFT && !hasThreePositions) ||
togglePosition === POSITIONS.CENTER) {
newPosition = POSITIONS.RIGHT;
}
else if (togglePosition === POSITIONS.LEFT && hasThreePositions) {
newPosition = POSITIONS.CENTER;
}
onChange?.(newPosition);
onKeyDown?.(e);
}
};
const handleClick = (e, position) => {
onChange?.(position);
onClick?.(e, position);
};
const state = getToggleState(hasThreePositions, togglePosition, disabled);
const styles = styleVariant[state];
return (_jsx(ToggleStandAlone, { ...props, ref: ref, disabled: disabled, hasThreePositions: hasThreePositions, styles: styles, togglePosition: togglePosition, onClick: handleClick, onKeyDown: handleKeyDown }));
});
ToggleControlledComponent.displayName = 'ToggleControlledComponent';
const ToggleBoundary = (props, ref) => (_jsx(ErrorBoundary, { fallBackComponent: _jsx(FallbackComponent, { children: _jsx(ToggleStandAlone, { ...props, ref: ref }) }), children: _jsx(ToggleControlledComponent, { ...props, ref: ref }) }));
const ToggleControlled = React.forwardRef(ToggleBoundary);
/**
* @description
* Toggle component is a component that can be used to create a toggle switch.
* @param {React.PropsWithChildren<IToggleControlled<V>>} props
* @returns {JSX.Element}
* @constructor
*/
export { ToggleControlled };
//# sourceMappingURL=toggleControlled.js.map