baseui
Version:
A React Component library implementing the Base design language
112 lines (110 loc) • 5.33 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var React = _interopRequireWildcard(require("react"));
var _overrides = require("../helpers/overrides");
var _styledComponents = require("./styled-components");
var _focusVisible = require("../utils/focusVisible");
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } /*
Copyright (c) Uber Technologies, Inc.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/
class StatelessRadioGroup extends React.Component {
constructor(...args) {
super(...args);
_defineProperty(this, "state", {
isFocusVisible: false,
focusedRadioIndex: -1
});
_defineProperty(this, "handleFocus", (event, index) => {
if ((0, _focusVisible.isFocusVisible)(event)) {
this.setState({
isFocusVisible: true
});
}
this.setState({
focusedRadioIndex: index
});
this.props.onFocus && this.props.onFocus(event);
});
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_defineProperty(this, "handleBlur", (event, index) => {
if (this.state.isFocusVisible !== false) {
this.setState({
isFocusVisible: false
});
}
this.setState({
focusedRadioIndex: -1
});
this.props.onBlur && this.props.onBlur(event);
});
}
render() {
const {
overrides = {}
} = this.props;
const [RadioGroupRoot, radioGroupRootProps] = (0, _overrides.getOverrides)(overrides.RadioGroupRoot, _styledComponents.RadioGroupRoot);
return /*#__PURE__*/React.createElement(RadioGroupRoot, _extends({
id: this.props.id,
role: "radiogroup",
"aria-describedby": this.props['aria-describedby'],
"aria-errormessage": this.props['aria-errormessage'],
"aria-invalid": this.props.error || null,
"aria-label": this.props['aria-label'],
"aria-labelledby": this.props['aria-labelledby'],
$align: this.props.align,
$disabled: this.props.disabled,
$error: this.props.error,
$required: this.props.required
}, radioGroupRootProps), React.Children.map(this.props.children, (child, index) => {
if (! /*#__PURE__*/React.isValidElement(child)) {
return null;
}
const checked = this.props.value === child.props.value;
return /*#__PURE__*/React.cloneElement(child, {
//@ts-ignore
align: this.props.align,
autoFocus: this.props.autoFocus,
checked,
disabled: this.props.disabled || child.props.disabled,
error: this.props.error,
isFocused: this.state.focusedRadioIndex === index,
isFocusVisible: this.state.isFocusVisible,
tabIndex: index === 0 && !this.props.value || checked ? '0' : '-1',
labelPlacement: this.props.labelPlacement,
name: this.props.name,
onBlur: e => this.handleBlur(e, index),
onFocus: e => this.handleFocus(e, index),
onChange: this.props.onChange,
onMouseEnter: this.props.onMouseEnter,
onMouseLeave: this.props.onMouseLeave
});
}));
}
}
_defineProperty(StatelessRadioGroup, "defaultProps", {
name: '',
value: '',
disabled: false,
autoFocus: false,
labelPlacement: 'right',
align: 'vertical',
error: false,
required: false,
onChange: () => {},
onMouseEnter: () => {},
onMouseLeave: () => {},
onFocus: () => {},
onBlur: () => {},
overrides: {}
});
var _default = exports.default = StatelessRadioGroup;