wix-style-react
Version:
86 lines (69 loc) • 2.54 kB
JavaScript
import React from 'react';
import PropTypes from 'prop-types';
import { ToggleSwitch as CoreToggleSwitch } from 'wix-ui-core/dist/src/components/toggle-switch';
import ToggleOff from 'wix-ui-icons-common/system/ToggleOff';
import ToggleOn from 'wix-ui-icons-common/system/ToggleOn';
import ToggleOffSmall from 'wix-ui-icons-common/system/ToggleOffSmall';
import ToggleOnSmall from 'wix-ui-icons-common/system/ToggleOnSmall';
import { generateDataAttr } from '../utils/generateDataAttr';
import { SIZES } from './constants';
import { st, classes } from './ToggleSwitch.st.css';
const checkedIconMap = {
[]: undefined,
[]: <ToggleOnSmall />,
[]: <ToggleOn />,
};
const uncheckedIconMap = {
[]: undefined,
[]: <ToggleOffSmall />,
[]: <ToggleOff />,
};
/** toggle switch */
class ToggleSwitch extends React.PureComponent {
static displayName = 'ToggleSwitch';
static propTypes = {
/** Applies a data-hook HTML attribute that can be used in the tests. */
dataHook: PropTypes.string,
/** Specifies a CSS class name to be appended to the component’s root element. */
className: PropTypes.string,
/** Assigns an unique identifier for the root element. */
id: PropTypes.string,
/** Controls the skin of a toggle. */
skin: PropTypes.oneOf(['standard', 'success', 'error']),
/** Controls the size of a toggle. */
size: PropTypes.oneOf(['small', 'medium', 'large']),
/** Specifies whether toggle is checked. */
checked: PropTypes.bool,
/** Specifies whether toggle is disabled. */
disabled: PropTypes.bool,
/** Defines a callback function which is called every time toggle state changes. */
onChange: PropTypes.func,
/** Indicates that element can be focused and where it participates in sequential keyboard navigation. */
tabIndex: PropTypes.number,
};
static defaultProps = {
skin: 'standard',
size: 'large',
};
render() {
const {
size,
skin,
styles: stylesProp, // Should not allow inline styles (applied in core component)
dataHook,
className,
...rest
} = this.props;
return (
<CoreToggleSwitch
className={st(classes.root, { skin, size }, className)}
{...generateDataAttr(this.props, ['skin', 'size'])}
data-hook={dataHook}
checkedIcon={checkedIconMap[size]}
uncheckedIcon={uncheckedIconMap[size]}
{...rest}
/>
);
}
}
export default ToggleSwitch;