baseui
Version:
A React Component library implementing the Base design language
148 lines (146 loc) • 6.61 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var React = _interopRequireWildcard(require("react"));
var _button = require("../button");
var _constants = require("./constants");
var _overrides = require("../helpers/overrides");
var _locale = require("../locale");
var _styledComponents = require("./styled-components");
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.
*/
// @ts-ignore
function isIndexSelected(selected, index) {
if (!Array.isArray(selected) && typeof selected !== 'number') {
return false;
}
if (Array.isArray(selected)) {
return selected.includes(index);
}
return selected === index;
}
class ButtonGroup extends React.Component {
constructor(...args) {
super(...args);
_defineProperty(this, "childRefs", {});
}
render() {
const {
overrides = {},
mode = _constants.MODE.checkbox,
children,
selected,
disabled,
onClick,
kind,
shape,
size
} = this.props;
const [Root, rootProps] = (0, _overrides.getOverrides)(overrides.Root, _styledComponents.StyledRoot);
const ariaLabel = this.props['aria-label'] || this.props.ariaLabel;
const isRadio = mode === _constants.MODE.radio;
const numItems = React.Children.count(children);
return /*#__PURE__*/React.createElement(_locale.LocaleContext.Consumer, null, locale => /*#__PURE__*/React.createElement(Root, _extends({
"aria-label": ariaLabel || locale.buttongroup.ariaLabel,
"data-baseweb": "button-group",
role: isRadio ? 'radiogroup' : 'group',
$shape: shape,
$length: children.length
}, rootProps), React.Children.map(children, (child, index) => {
if (! /*#__PURE__*/React.isValidElement(child)) {
return null;
}
const isSelected = child.props.isSelected ? child.props.isSelected : isIndexSelected(selected, index);
if (isRadio) {
this.childRefs[index] = /*#__PURE__*/React.createRef();
}
return /*#__PURE__*/React.cloneElement(child, {
// @ts-ignore
disabled: disabled || child.props.disabled,
isSelected,
ref: isRadio ? this.childRefs[index] : undefined,
tabIndex: !isRadio || isSelected || isRadio && (!selected || selected === -1) && index === 0 ? 0 : -1,
// @ts-ignore
onKeyDown: e => {
if (!isRadio) return;
const value = Number(selected) ? Number(selected) : 0;
if (e.key === 'ArrowUp' || e.key === 'ArrowLeft') {
e.preventDefault && e.preventDefault();
const prevIndex = value - 1 < 0 ? numItems - 1 : value - 1;
onClick && onClick(e, prevIndex);
this.childRefs[prevIndex].current && this.childRefs[prevIndex].current.focus();
}
if (e.key === 'ArrowDown' || e.key === 'ArrowRight') {
e.preventDefault && e.preventDefault();
const nextIndex = value + 1 > numItems - 1 ? 0 : value + 1;
onClick && onClick(e, nextIndex);
this.childRefs[nextIndex].current && this.childRefs[nextIndex].current.focus();
}
},
kind,
// @ts-ignore
onClick: event => {
if (disabled) {
return;
}
if (child.props.onClick) {
child.props.onClick(event);
}
if (onClick) {
onClick(event, index);
}
},
shape,
size,
overrides: {
BaseButton: {
// @ts-ignore
style: ({
$theme
}) => {
// Even though baseui's buttons have square corners, some applications override to
// rounded. Maintaining corner radius in this circumstance is ideal to avoid further
// customization.
if (children.length === 1) {
return {};
}
if (shape !== _button.SHAPE.default) {
return {
marginLeft: $theme.sizing.scale100,
marginRight: $theme.sizing.scale100
};
}
return {
marginLeft: '0.5px',
marginRight: '0.5px'
};
},
props: {
'aria-checked': isSelected,
role: isRadio ? 'radio' : 'checkbox'
}
},
...child.props.overrides
}
});
})));
}
}
exports.default = ButtonGroup;
_defineProperty(ButtonGroup, "defaultProps", {
disabled: false,
onClick: () => {},
shape: _button.SHAPE.default,
size: _button.SIZE.default,
kind: _button.KIND.secondary
});