UNPKG

@fluentui/react-northstar

Version:
174 lines (172 loc) 6.44 kB
import _invoke from "lodash/invoke"; import { radioGroupItemBehavior } from '@fluentui/accessibility'; import { Ref } from '@fluentui/react-component-ref'; import * as customPropTypes from '@fluentui/react-proptypes'; import * as React from 'react'; import * as PropTypes from 'prop-types'; import { createShorthandFactory, commonPropTypes, shouldPreventDefaultOnKeyDown } from '../../utils'; import { Box } from '../Box/Box'; import { useAutoControlled, getElementType, useAccessibility, useFluentContext, useStyles, useTelemetry, useUnhandledProps } from '@fluentui/react-bindings'; import { RadioButtonIcon } from '@fluentui/react-icons-northstar'; export var radioGroupItemClassName = 'ui-radiogroup__item'; export var radioGroupItemSlotClassNames = { indicator: radioGroupItemClassName + "__indicator", label: radioGroupItemClassName + "__label" }; /** * A RadioGroupItem represents single input element within a RadioGroup. * * @accessibility * Radio items need to be grouped to correctly handle accessibility. */ export var RadioGroupItem = /*#__PURE__*/function () { var RadioGroupItem = /*#__PURE__*/React.forwardRef(function (props, ref) { var context = useFluentContext(); var _useTelemetry = useTelemetry(RadioGroupItem.displayName, context.telemetry), setStart = _useTelemetry.setStart, setEnd = _useTelemetry.setEnd; setStart(); var label = props.label, checkedIndicator = props.checkedIndicator, indicator = props.indicator, disabled = props.disabled, vertical = props.vertical, className = props.className, design = props.design, styles = props.styles, variables = props.variables, shouldFocus = props.shouldFocus; var elementRef = React.useRef(); var ElementType = getElementType(props); var unhandledProps = useUnhandledProps(RadioGroupItem.handledProps, props); var _useAutoControlled = useAutoControlled({ defaultValue: props.defaultChecked, value: props.checked, initialValue: false }), checked = _useAutoControlled[0], setChecked = _useAutoControlled[1]; var prevChecked = React.useRef(checked); var handleClick = function handleClick(e) { _invoke(props, 'onClick', e, props); setChecked(function (prevChecked) { return !prevChecked; }); }; // This behavior is not conformant with native input radio, it was added to avoid breaking change // and it should be fixed to be conformant with native, only calling onChange when item is clicked (checked will always be true) React.useEffect(function () { if (prevChecked.current !== checked) { _invoke(props, 'onChange', undefined, Object.assign({}, props, { checked: checked })); prevChecked.current = checked; } }); React.useEffect(function () { if (checked && shouldFocus) elementRef.current.focus(); }, [checked, shouldFocus]); var _useStyles = useStyles(RadioGroupItem.displayName, { className: radioGroupItemClassName, mapPropsToStyles: function mapPropsToStyles() { return { vertical: vertical, disabled: disabled, checked: checked }; }, mapPropsToInlineStyles: function mapPropsToInlineStyles() { return { className: className, design: design, styles: styles, variables: variables }; }, rtl: context.rtl }), classes = _useStyles.classes, resolvedStyles = _useStyles.styles; var getA11yProps = useAccessibility(props.accessibility, { debugName: RadioGroupItem.displayName, actionHandlers: { performClick: function performClick(e) { if (shouldPreventDefaultOnKeyDown(e)) { e.preventDefault(); } handleClick(e); } }, mapPropsToBehavior: function mapPropsToBehavior() { return { checked: checked, disabled: disabled }; }, rtl: context.rtl }); var handleChange = function handleChange(e) { // RadioGroupItem component doesn't present any `input` component in markup, however all of our // components should handle events transparently. _invoke(props, 'onChange', e, Object.assign({}, props, { checked: checked })); }; var element = getA11yProps.unstable_wrapWithFocusZone( /*#__PURE__*/React.createElement(Ref, { innerRef: elementRef }, /*#__PURE__*/React.createElement(ElementType, getA11yProps('root', Object.assign({ className: classes.root, onClick: handleClick, onChange: handleChange, ref: ref }, unhandledProps)), Box.create(checked ? checkedIndicator : indicator, { defaultProps: function defaultProps() { return { className: radioGroupItemSlotClassNames.indicator, styles: resolvedStyles.indicator }; } }), Box.create(label, { defaultProps: function defaultProps() { return { as: 'span', className: radioGroupItemSlotClassNames.label, styles: resolvedStyles.label }; } })))); setEnd(); return element; }); RadioGroupItem.displayName = 'RadioGroupItem'; RadioGroupItem.propTypes = Object.assign({}, commonPropTypes.createCommon({ content: false }), { checked: PropTypes.bool, defaultChecked: PropTypes.bool, disabled: PropTypes.bool, indicator: customPropTypes.shorthandAllowingChildren, checkedIndicator: customPropTypes.shorthandAllowingChildren, label: customPropTypes.itemShorthand, name: PropTypes.string, onClick: PropTypes.func, onChange: PropTypes.func, shouldFocus: PropTypes.bool, value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]), vertical: PropTypes.bool }); RadioGroupItem.defaultProps = { accessibility: radioGroupItemBehavior, indicator: /*#__PURE__*/React.createElement(RadioButtonIcon, { outline: true }), checkedIndicator: /*#__PURE__*/React.createElement(RadioButtonIcon, null) }; RadioGroupItem.handledProps = Object.keys(RadioGroupItem.propTypes); RadioGroupItem.create = createShorthandFactory({ Component: RadioGroupItem, mappedProp: 'label' }); return RadioGroupItem; }(); //# sourceMappingURL=RadioGroupItem.js.map