// @flow import * as React from 'react'; import classNames from 'classnames'; import './Toggle.scss'; type Props = { className?: string, /** Optional attribute used for targeting */ 'data-target-id'?: string, /** Description of the input */ description?: React.Node, isDisabled?: boolean, // @TODO: eventually call this `disabled` /** Toggle state */ isOn?: boolean, // @TODO: eventually call this `checked` /** If set to true, the toggle will be aligned to the right */ isToggleRightAligned?: boolean, /** Label displayed for the input */ label: React.Node, /** Name of the input */ name?: string, /** blur callback function called with event as the argument */ onBlur?: (e: SyntheticInputEvent) => any, /** change callback function called with event as the argument */ onChange?: (e: SyntheticInputEvent) => any, /** focus callback function called with event as the argument */ onFocus?: (e: SyntheticInputEvent) => any, /** mouse enter callback function called with event as the argument */ onMouseEnter?: (e: SyntheticInputEvent) => any, /** mouse leave callback function called with event as the argument */ onMouseLeave?: (e: SyntheticInputEvent) => any, /** optional value for the toggles checkbox */ value?: any, }; const Toggle = React.forwardRef( ( { className = '', 'data-target-id': dataTargetId, description, isDisabled, isOn, isToggleRightAligned = false, label, name, onBlur, onChange, onFocus, onMouseEnter, onMouseLeave, ...rest }: Props, ref, ) => { const classes = classNames('toggle-container', className, { 'is-toggle-right-aligned': isToggleRightAligned, }); const toggleElements = [
,
{label}
, ]; if (isToggleRightAligned) { toggleElements.reverse(); } return (
{/* eslint-disable-next-line jsx-a11y/label-has-for */} {description ?
{description}
: null}
); }, ); Toggle.displayName = 'Toggle'; export type ToggleProps = Props; export default Toggle;