UNPKG

lucid-ui

Version:

A UI component library from AppNexus.

89 lines (87 loc) 3.52 kB
import _ from 'lodash'; import React from 'react'; import PropTypes from 'react-peek/prop-types'; import { lucidClassNames } from '../../util/style-helpers'; import { omitProps, } from '../../util/component-types'; const cx = lucidClassNames.bind('&-RadioButton'); const { bool, func, object, string } = PropTypes; export const defaultProps = { isDisabled: false, isSelected: false, onSelect: _.noop, }; export const RadioButton = (props) => { const { className, isDisabled, isSelected, onSelect, style, ...passThroughs } = props; const nativeElement = React.createRef(); function handleClicked(event) { if (!isDisabled && !isSelected) { onSelect(true, { event, props }); if (nativeElement.current) { nativeElement.current.focus(); } } } function handleSpanClick(e) { e.preventDefault(); } return (React.createElement("span", { className: cx('&', { '&-is-disabled': isDisabled, '&-is-selected': isSelected, }, className), onClick: handleClicked, style: style }, React.createElement("input", Object.assign({ onChange: _.noop }, omitProps(passThroughs, undefined, [ ..._.keys(RadioButton.propTypes), 'children', 'callbackId', ]), { checked: isSelected, className: cx('&-native'), disabled: isDisabled, ref: nativeElement, type: 'radio' })), React.createElement("span", { onClick: handleSpanClick, className: cx('&-visualization-glow') }), React.createElement("span", { onClick: handleSpanClick, className: cx('&-visualization-container') }), React.createElement("span", { onClick: handleSpanClick, className: cx('&-visualization-dot') }))); }; RadioButton.defaultProps = defaultProps; RadioButton.displayName = 'RadioButton'; RadioButton.peek = { description: ` RadioButton is a round two-state toggle used to create \`RadioButtonLabeled\`. It uses a hidden native checkbox control under the hood but leverages other HTML elements to visualize its state. `, notes: { overview: ` RadioButton is a round two-state toggle. Use \`RadioButtonLabeled\` or \`RadioGroup\` in your applications. `, intendedUse: ` Used to create \`RadioButtonLabeled\` and \`RadioGroup\`. `, technicalRecommendations: ` - Use the Selected state when a filter or setting will be applied. - Use the Unselected state when a filter or setting will not be applied. - Any props that are not explicitly defined in \`propTypes\` are passed to the native radio button control. `, }, categories: ['controls', 'toggles'], }; RadioButton.propTypes = { className: string ` Appended to the component-specific class names set on the root element. `, isDisabled: bool ` Indicates whether the component should appear and act disabled by having a "greyed out" palette and ignoring user interactions. `, isSelected: bool ` Indicates that the component is in the "selected" state when true and in the "unselected" state when false. `, name: string ` Optional name for the input element. `, onSelect: func ` Called when the user clicks on the component or when they press the space key while the component is in focus, and only called when the component is in the unselected state. Signature: \`(true, { event, props }) => {}\` `, style: object ` Passed through to the root element. `, }; export default RadioButton;