@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 • 3.19 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ToggleWithLabelControlled = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = __importDefault(require("react"));
const useStyles_1 = require("../../hooks/useStyles/useStyles");
const positions_1 = require("../../types/positions/positions");
const errorBoundary_1 = require("../../provider/errorBoundary/errorBoundary");
const fallbackComponent_1 = require("../../provider/errorBoundary/fallbackComponent");
const toggleWithLabelStandAlone_1 = require("./toggleWithLabelStandAlone");
const TOGGLE_WITH_LABEL_STYLES = 'TOGGLE_WITH_LABEL_STYLES';
const ToggleWithLabelControlledComponent = react_1.default.forwardRef(({ variant, ctv, onFieldSetClick, onClick, ...props }, ref) => {
const styles = (0, useStyles_1.useStyles)(TOGGLE_WITH_LABEL_STYLES, variant, ctv);
const handleFieldSetClick = (e) => {
if (props.disabled) {
return;
}
// When comming from handleOnClick, the onChange is already called so we avoid calling it again
const avoidCallingOnChange = e?.avoidCallingOnChange;
// Do not use !avoidCallingOnChange to call onChange when avoidCallingOnChange is undefined
if (avoidCallingOnChange !== true) {
let newPosition = positions_1.POSITIONS.LEFT;
if ((props.togglePosition === positions_1.POSITIONS.LEFT && !props.hasThreePositions) ||
props.togglePosition === positions_1.POSITIONS.CENTER) {
newPosition = positions_1.POSITIONS.RIGHT;
}
else if (props.togglePosition === positions_1.POSITIONS.LEFT && props.hasThreePositions) {
newPosition = positions_1.POSITIONS.CENTER;
}
props.onChange?.(newPosition);
}
onFieldSetClick?.(e);
};
const handleClick = (e, position) => {
Object.defineProperties(e, {
avoidCallingOnChange: {
value: true,
},
});
onClick?.(e, position);
};
return ((0, jsx_runtime_1.jsx)(toggleWithLabelStandAlone_1.ToggleWithLabelStandAlone, { ref: ref, styles: styles, onClick: handleClick, onFieldSetClick: handleFieldSetClick, ...props }));
});
ToggleWithLabelControlledComponent.displayName = 'ToggleWithLabelControlledComponent';
const ToggleWithLabelBoundary = (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)(toggleWithLabelStandAlone_1.ToggleWithLabelStandAlone, { ...props, ref: ref }) }), children: (0, jsx_runtime_1.jsx)(ToggleWithLabelControlledComponent, { ...props, ref: ref }) }));
/**
* @deprecated This component has been deprecated and will be removed in the next MAJOR release. Will include all this on the Toggle component
*/
exports.ToggleWithLabelControlled = react_1.default.forwardRef(ToggleWithLabelBoundary);
//# sourceMappingURL=toggleWithLabelControlled.js.map