UNPKG

wix-style-react

Version:
53 lines 2.6 kB
import React from 'react'; import PropTypes from 'prop-types'; import ToggleSwitchCore from './ToggleSwitchCore'; import { ToggleOff, ToggleOn, ToggleOffSmall, ToggleOnSmall, } from '@wix/wix-ui-icons-common/system'; import { generateDataAttr } from '../utils/generateDataAttr'; import { SIZES } from './constants'; import { st, classes } from './ToggleSwitch.st.css'; import { WixStyleReactContext } from '../WixStyleReactProvider/context'; const checkedIconMap = { [SIZES.small]: undefined, [SIZES.medium]: React.createElement(ToggleOnSmall, null), [SIZES.large]: React.createElement(ToggleOn, null), }; const uncheckedIconMap = { [SIZES.small]: undefined, [SIZES.medium]: React.createElement(ToggleOffSmall, null), [SIZES.large]: React.createElement(ToggleOff, null), }; /** toggle switch */ class ToggleSwitch extends React.PureComponent { render() { const { size, skin, styles: stylesProp, // Should not allow inline styles (applied in core component) dataHook, className, ...rest } = this.props; return (React.createElement(WixStyleReactContext.Consumer, null, ({ newColorsBranding }) => (React.createElement(ToggleSwitchCore, { className: st(classes.root, { skin, size, newColorsBranding }, className), ...generateDataAttr(this.props, ['skin', 'size']), "data-hook": dataHook, checkedIcon: checkedIconMap[size], uncheckedIcon: uncheckedIconMap[size], ...rest })))); } } ToggleSwitch.displayName = 'ToggleSwitch'; ToggleSwitch.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', 'urgent']), /** 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, }; ToggleSwitch.defaultProps = { skin: 'standard', size: 'large', }; export default ToggleSwitch; //# sourceMappingURL=ToggleSwitch.js.map