@itwin/itwinui-react
Version:
A react component library for iTwinUI
77 lines (76 loc) • 2.14 kB
JavaScript
import * as React from 'react';
import cx from 'classnames';
import { Box, SvgCheckmark, useFutureFlag } from '../../utils/index.js';
export const ToggleSwitch = React.forwardRef((props, ref) => {
let {
disabled = false,
labelPosition = 'right',
label,
className,
style,
size = 'default',
labelProps = {},
wrapperProps,
icon: iconProp,
...rest
} = props;
let { consistentPropsSpread } = useFutureFlag('ToggleSwitch') || {};
let shouldApplyClassNameAndStyleOnInput =
null != wrapperProps || consistentPropsSpread;
let shouldShowIcon =
void 0 === iconProp || (null !== iconProp && 'small' !== size);
return React.createElement(
Box,
{
as: label ? 'label' : 'div',
style: shouldApplyClassNameAndStyleOnInput ? void 0 : style,
...wrapperProps,
className: cx(
'iui-toggle-switch-wrapper',
{
'iui-disabled': disabled,
'iui-label-on-right': label && 'right' === labelPosition,
'iui-label-on-left': label && 'left' === labelPosition,
},
shouldApplyClassNameAndStyleOnInput ? void 0 : className,
wrapperProps?.className,
),
'data-iui-size': size,
},
React.createElement(Box, {
as: 'input',
type: 'checkbox',
role: 'switch',
style: shouldApplyClassNameAndStyleOnInput ? style : void 0,
...rest,
className: cx(
'iui-toggle-switch',
shouldApplyClassNameAndStyleOnInput ? className : void 0,
),
disabled: disabled,
ref: ref,
}),
shouldShowIcon &&
React.createElement(
Box,
{
as: 'span',
className: 'iui-toggle-switch-icon',
'aria-hidden': true,
},
iconProp || React.createElement(SvgCheckmark, null),
),
label &&
React.createElement(
Box,
{
as: 'span',
...labelProps,
className: cx('iui-toggle-switch-label', labelProps?.className),
},
label,
),
);
});
if ('development' === process.env.NODE_ENV)
ToggleSwitch.displayName = 'ToggleSwitch';