UNPKG

@procore/core-react

Version:
317 lines (306 loc) • 15.1 kB
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } var _excluded = ["aria-label", "aria-labelledby", "children", "block", "disabled", "onChange"], _excluded2 = ["active", "children", "placement", "tooltip", "disabled", "value", "internalIndex", "selected", "state"], _excluded3 = ["active", "children", "placement", "tooltip", "disabled", "value", "internalIndex", "selected"], _excluded4 = ["children", "overlay"], _excluded5 = ["children"]; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : 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); } function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } } function _arrayWithHoles(r) { if (Array.isArray(r)) return r; } function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); } function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; } function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; } import { useFocusRing } from '@react-aria/focus'; import { useRadio, useRadioGroup } from '@react-aria/radio'; import { mergeProps, useId } from '@react-aria/utils'; import { VisuallyHidden } from '@react-aria/visually-hidden'; import React from 'react'; import { useRadioGroupState } from 'react-stately'; import { Tooltip as TooltipBase } from '../Tooltip/Tooltip'; import { addSubcomponents } from '../_utils/addSubcomponents'; import { StyledContainer, StyledLabel, StyledSegment, StyledSegmentDisabledInteractiveWrapper } from './SegmentedController.styles'; function isSegment(node) { return /*#__PURE__*/React.isValidElement(node) && node.type === Segment; } export function determineSelectedState(_ref) { var active = _ref.active, selected = _ref.selected, isSelected = _ref.isSelected; // Selected is a prop, so it should be the source of truth if (selected !== undefined) { return selected; } // Active is a prop, so it should be the source of truth if (active !== undefined) { return active; } // If neither props are passed in, use the react-stately state return isSelected; } var RadioButtonContext = /*#__PURE__*/React.createContext(null); var SegmentedController_ = /*#__PURE__*/React.forwardRef(function SegmentedController(_ref2, ref) { var ariaLabel = _ref2['aria-label'], ariaLabelledby = _ref2['aria-labelledby'], children = _ref2.children, _ref2$block = _ref2.block, block = _ref2$block === void 0 ? false : _ref2$block, disabled = _ref2.disabled, _onChange = _ref2.onChange, props = _objectWithoutProperties(_ref2, _excluded); var name = useId(); var radioGroupState = useRadioGroupState({ name: name, isDisabled: disabled, onChange: function onChange(rawValue) { var num = Number(rawValue); var value = !isNaN(num) ? num : rawValue; _onChange === null || _onChange === void 0 ? void 0 : _onChange(value); } }); var _useRadioGroup = useRadioGroup({ 'aria-label': ariaLabel, 'aria-labelledby': ariaLabelledby, orientation: 'horizontal' }, radioGroupState), radioGroupProps = _useRadioGroup.radioGroupProps; return /*#__PURE__*/React.createElement(StyledContainer, _extends({ ref: ref, $block: block }, radioGroupProps, props), /*#__PURE__*/React.createElement(RadioButtonContext.Provider, { value: radioGroupState }, React.Children.toArray(children).filter(function (child) { return isSegment(child); }).map(function (child, index) { return /*#__PURE__*/React.cloneElement(child, { internalIndex: index }); }))); }); // Segment with RadioGroup context var SegmentWithGroup = /*#__PURE__*/React.forwardRef(function SegmentWithGroup(_ref3, ref) { var active = _ref3.active, children = _ref3.children, _ref3$placement = _ref3.placement, placement = _ref3$placement === void 0 ? 'top' : _ref3$placement, tooltip = _ref3.tooltip, disabled = _ref3.disabled, value = _ref3.value, internalIndex = _ref3.internalIndex, selected = _ref3.selected, state = _ref3.state, props = _objectWithoutProperties(_ref3, _excluded2); var adjustedValue = String(value !== null && value !== void 0 ? value : internalIndex); var innerRef = React.useRef(null); var actualRef = ref !== null && ref !== void 0 ? ref : innerRef; var _useRadio = useRadio({ isDisabled: disabled !== null && disabled !== void 0 ? disabled : state.isDisabled, value: adjustedValue, children: children }, state, actualRef), inputProps = _useRadio.inputProps, isSelected = _useRadio.isSelected, isDisabled = _useRadio.isDisabled; var _useFocusRing = useFocusRing(), focusProps = _useFocusRing.focusProps, isFocusVisible = _useFocusRing.isFocusVisible; React.useEffect(function () { if (active || selected) { state.setSelectedValue(adjustedValue); } }, [active, state, adjustedValue, selected]); return /*#__PURE__*/React.createElement(Tooltip, { overlay: tooltip, placement: placement }, /*#__PURE__*/React.createElement(StyledSegmentDisabledInteractiveWrapper, null, /*#__PURE__*/React.createElement(StyledSegment, { $focused: isFocusVisible, $disabled: isDisabled, $selected: determineSelectedState({ active: active, selected: selected, isSelected: isSelected }) }, /*#__PURE__*/React.createElement(VisuallyHidden, null, /*#__PURE__*/React.createElement("input", _extends({}, mergeProps(props, inputProps, focusProps), { ref: actualRef }))), /*#__PURE__*/React.createElement(StyledLabel, null, children)))); }); // Standalone segment var StandaloneSegment = /*#__PURE__*/React.forwardRef(function StandaloneSegment(_ref4, ref) { var _ref5, _ref6; var active = _ref4.active, children = _ref4.children, _ref4$placement = _ref4.placement, placement = _ref4$placement === void 0 ? 'top' : _ref4$placement, tooltip = _ref4.tooltip, disabled = _ref4.disabled, value = _ref4.value, internalIndex = _ref4.internalIndex, selected = _ref4.selected, props = _objectWithoutProperties(_ref4, _excluded3); var adjustedValue = String(value !== null && value !== void 0 ? value : internalIndex); var innerRef = React.useRef(null); var actualRef = ref !== null && ref !== void 0 ? ref : innerRef; var _useFocusRing2 = useFocusRing(), focusProps = _useFocusRing2.focusProps, isFocusVisible = _useFocusRing2.isFocusVisible; var isControlled = selected !== undefined || active !== undefined; var _React$useState = React.useState((_ref5 = selected !== null && selected !== void 0 ? selected : active) !== null && _ref5 !== void 0 ? _ref5 : false), _React$useState2 = _slicedToArray(_React$useState, 2), uncontrolledSelected = _React$useState2[0], setUncontrolledSelected = _React$useState2[1]; var isSelected = (_ref6 = selected !== null && selected !== void 0 ? selected : active) !== null && _ref6 !== void 0 ? _ref6 : uncontrolledSelected; var handleChange = function handleChange(e) { var _props$onChange; if (!isControlled) { setUncontrolledSelected(e.target.checked); } (_props$onChange = props.onChange) === null || _props$onChange === void 0 ? void 0 : _props$onChange.call(props, e); }; return /*#__PURE__*/React.createElement(Tooltip, { overlay: tooltip, placement: placement }, /*#__PURE__*/React.createElement(StyledSegmentDisabledInteractiveWrapper, null, /*#__PURE__*/React.createElement(StyledSegment, { $focused: isFocusVisible, $disabled: disabled !== null && disabled !== void 0 ? disabled : false, $selected: isSelected }, /*#__PURE__*/React.createElement(VisuallyHidden, null, /*#__PURE__*/React.createElement("input", _extends({}, mergeProps(props, focusProps), { ref: actualRef, type: "radio", value: adjustedValue, checked: isSelected, disabled: disabled, onChange: handleChange }))), /*#__PURE__*/React.createElement(StyledLabel, null, children)))); }); // Wrapper that chooses the right implementation export var Segment = /*#__PURE__*/React.forwardRef(function Segment(props, ref) { var state = React.useContext(RadioButtonContext); if (state) { return /*#__PURE__*/React.createElement(SegmentWithGroup, _extends({}, props, { state: state, ref: ref })); } return /*#__PURE__*/React.createElement(StandaloneSegment, _extends({}, props, { ref: ref })); }); /** * @deprecated * * The `SegmentedController.Tooltip` component has been deprecated will be * removed in a future version. * * Tooltips are now built into `SegmentedController.Segment`s, and can be added * by configuring the `tooltip` prop. * * **Before** * * ``` * <SegmentedController> * <SegmentedController.Tooltip tooltip="Item 1 is the first item"> * <SegmentedController.Segment> * Item 1 * </SegmentedController.Segment> * </SegmentedController.Tooltip> * </SegmentedController> * ``` * * **After** * * ``` * <SegmentedController> * <SegmentedController.Segment tooltip="Item 1 is the first item"> * Item 1 * </SegmentedController.Segment> * </SegmentedController> * ``` * * @deprecatedSince 10.20.0 */ export var Tooltip = /*#__PURE__*/React.forwardRef(function Tooltip(_ref7, ref) { var children = _ref7.children, overlay = _ref7.overlay, props = _objectWithoutProperties(_ref7, _excluded4); return overlay ? /*#__PURE__*/React.createElement(TooltipBase, _extends({}, props, { overlay: overlay, ref: ref }), children) : /*#__PURE__*/React.createElement(React.Fragment, null, children); }); var withState = function withState(config) { return function (Component) { return function (props) { var _React$useState3 = React.useState(0), _React$useState4 = _slicedToArray(_React$useState3, 2), selectedIndex = _React$useState4[0], setSelected = _React$useState4[1]; return /*#__PURE__*/React.createElement(Component, _objectSpread(_objectSpread({}, props), {}, { selectedIndex: selectedIndex, setSelected: setSelected })); }; }; }; /** * @deprecated * * This `SegmentedController.State` utility has been deprecated. Please just * use a simple state management hook instead. * * * **Before** * * ``` * <SegmentedController.State> * {({ selectedIndex, setSelected }) => ( * <SegmentedController> * <SegmentedController.Segment onClick={() => setSelected(0)} active={selectedIndex === 0}> * Item 1 * </SegmentedController.Segment> * </SegmentedController> * )} * </SegmentedController.State> * ``` * * **After** * * ``` * const [selected, setSelected] = React.useState(0) * * <SegmentedController onChange={setSelected}> * <SegmentedController.Segment active={selectedIndex === 0}> * Item 1 * </SegmentedController.Segment> * </SegmentedController> * ``` * * @deprecatedSince 10.20.0 */ export var State = withState({})(function (_ref8) { var children = _ref8.children, props = _objectWithoutProperties(_ref8, _excluded5); return children(props); }); SegmentedController_.displayName = 'SegmentedController'; Segment.displayName = 'SegmentedController.Segment'; Tooltip.displayName = 'SegmentedController.Tooltip'; /** Segmented controllers allow users to switch between related but mutually exclusive content or actions. @since 10.19.0 @see [Storybook](https://stories.core.procore.com/?path=/story/core-react_demos-segmentedcontroller--demo) @see [Design Guidelines](https://design.procore.com/segmented-controller) */ export var SegmentedController = addSubcomponents({ Segment: Segment, State: State, Tooltip: Tooltip }, SegmentedController_); //# sourceMappingURL=SegmentedController.js.map