UNPKG

@douyinfe/semi-ui

Version:

A modern, comprehensive, flexible design system and UI library. Connect DesignOps & DevOps. Quickly build beautiful React apps. Maintained by Douyin-fe team.

132 lines (131 loc) 4.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _noop2 = _interopRequireDefault(require("lodash/noop")); var _react = _interopRequireDefault(require("react")); var _propTypes = _interopRequireDefault(require("prop-types")); var _radioInnerFoundation = _interopRequireDefault(require("@douyinfe/semi-foundation/lib/cjs/radio/radioInnerFoundation")); var _baseComponent = _interopRequireDefault(require("../_base/baseComponent")); var _constants = require("@douyinfe/semi-foundation/lib/cjs/radio/constants"); var _context = _interopRequireDefault(require("./context")); var _classnames = _interopRequireDefault(require("classnames")); var _semiIcons = require("@douyinfe/semi-icons"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } class RadioInner extends _baseComponent.default { constructor(props) { super(props); this.state = { checked: false }; this.foundation = new _radioInnerFoundation.default(this.adapter); this.onChange = this.onChange.bind(this); } get adapter() { return Object.assign(Object.assign({}, super.adapter), { setNativeControlChecked: checked => { this.setState({ checked }); }, notifyChange: e => { this.props.onChange(e); } }); } componentDidMount() { this.foundation.init(); } componentDidUpdate(prevProps) { if (prevProps.checked !== this.props.checked) { this.foundation.setChecked(this.props.checked); } } componentWillUnmount() { this.foundation.destroy(); } blur() { this.inputEntity.blur(); } focus() { const { preventScroll } = this.props; this.inputEntity.focus({ preventScroll }); } onChange(e) { this.foundation.handleChange(e); } render() { const { disabled, mode, autoFocus, name, isButtonRadio, isPureCardRadioGroup, addonId, extraId, 'aria-label': ariaLabel, focusInner, onInputFocus, onInputBlur } = this.props; const { checked } = this.state; const prefix = this.props.prefixCls || _constants.radioClasses.PREFIX; const wrapper = (0, _classnames.default)({ [`${prefix}-inner`]: true, [`${prefix}-inner-checked`]: Boolean(checked), [`${prefix}-inner-buttonRadio`]: isButtonRadio, [`${prefix}-inner-pureCardRadio`]: isPureCardRadioGroup }); const inner = (0, _classnames.default)({ [`${prefix}-focus`]: focusInner, [`${prefix}-focus-border`]: focusInner && !checked, [`${prefix}-inner-display`]: !isButtonRadio }); return /*#__PURE__*/_react.default.createElement("span", { className: wrapper }, /*#__PURE__*/_react.default.createElement("input", { ref: ref => { this.inputEntity = ref; }, autoFocus: autoFocus, type: mode === 'advanced' ? 'checkbox' : 'radio', checked: Boolean(checked), disabled: disabled, onChange: this.onChange, name: name, "aria-label": ariaLabel, "aria-labelledby": addonId, "aria-describedby": extraId, onFocus: onInputFocus, onBlur: onInputBlur }), /*#__PURE__*/_react.default.createElement("span", { className: inner }, checked ? /*#__PURE__*/_react.default.createElement(_semiIcons.IconRadio, null) : null)); } } RadioInner.contextType = _context.default; RadioInner.propTypes = { checked: _propTypes.default.bool, disabled: _propTypes.default.bool, isButtonRadio: _propTypes.default.bool, onChange: _propTypes.default.func, mode: _propTypes.default.oneOf(['advanced', '']), 'aria-label': _propTypes.default.string, focusInner: _propTypes.default.bool, onInputFocus: _propTypes.default.func, onInputBlur: _propTypes.default.func, preventScroll: _propTypes.default.bool }; RadioInner.defaultProps = { onChange: _noop2.default, isButtonRadio: false }; var _default = exports.default = RadioInner;