@procore/core-react
Version:
React library of Procore Design Guidelines
203 lines (197 loc) • 10.1 kB
JavaScript
var _excluded = ["afterHide", "afterShow", "beforeHide", "beforeShow", "block", "getGroupId", "getId", "getLabel", "getNextGroupId", "getValueString", "i18nScope", "onChange", "onClear", "onNavigate", "onQuickCreate", "onScrollBottom", "onSearch", "selectableTiers", "isTierDisabled", "tabIndex", "searchPlaceholder"];
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; }
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; }
import React from 'react';
import { LeafSelectionTieredSelect } from './LeafSelectionTieredSelect';
import { TierSelectionTieredSelect } from './TierSelectionTieredSelect';
function defaultGetGroupId(tier) {
return tier.groupId;
}
function defaultGetId(tier) {
return tier.id;
}
function defaultGetLabel(tier) {
return tier.label;
}
function defaultGetNextGroupId(tier) {
return tier.nextGroupId !== null ? tier.nextGroupId : undefined;
}
function getFullPath() {
var getLabel = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultGetLabel;
return function getValueString(value) {
return value.map(function (tier) {
return getLabel(tier);
}).join(' > ');
};
}
function getLeafOnly() {
var getLabel = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultGetLabel;
return function getValueString(value) {
return value.length ? getLabel(value[value.length - 1]) : '';
};
}
function getValueStringFn(valueStringResolution) {
var getLabel = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultGetLabel;
if (valueStringResolution === 'full-path') {
return getFullPath(getLabel);
}
if (valueStringResolution === 'leaf-only') {
return getLeafOnly(getLabel);
}
return valueStringResolution;
}
function noop() {}
function always() {
return true;
}
export var useHighlightItemEffects = function useHighlightItemEffects(_ref) {
var currentTier = _ref.currentTier,
searchValue = _ref.searchValue,
menuRef = _ref.menuRef,
loading = _ref.loading,
loadingMore = _ref.loadingMore;
var _React$useState = React.useState(),
_React$useState2 = _slicedToArray(_React$useState, 2),
previousTier = _React$useState2[0],
setPreviousTier = _React$useState2[1];
React.useEffect(function () {
setPreviousTier(currentTier);
}, [currentTier]);
var _React$useState3 = React.useState(''),
_React$useState4 = _slicedToArray(_React$useState3, 2),
previousSearch = _React$useState4[0],
setPreviousSearch = _React$useState4[1];
React.useEffect(function () {
setPreviousSearch(searchValue);
}, [searchValue]);
var currentMenuRef = menuRef.current;
var skipFirstSearchTierRef = React.useRef(true);
React.useEffect(function () {
if (previousSearch !== searchValue || previousTier !== currentTier) {
var _menuRef$current;
if (skipFirstSearchTierRef.current) {
skipFirstSearchTierRef.current = false;
return;
}
(_menuRef$current = menuRef.current) === null || _menuRef$current === void 0 ? void 0 : _menuRef$current.highlightFirst();
}
}, [currentTier, previousTier, searchValue, previousSearch, menuRef]);
React.useEffect(function () {
if (loadingMore) {
currentMenuRef === null || currentMenuRef === void 0 ? void 0 : currentMenuRef.highlightLast(false);
}
}, [currentMenuRef, loadingMore]);
var skipFirstLoadingRef = React.useRef(true);
React.useEffect(function () {
var _menuRef$current2;
if (!loading && !((_menuRef$current2 = menuRef.current) !== null && _menuRef$current2 !== void 0 && _menuRef$current2.highlighted())) {
var _menuRef$current3;
if (skipFirstLoadingRef.current) {
skipFirstLoadingRef.current = false;
return;
}
(_menuRef$current3 = menuRef.current) === null || _menuRef$current3 === void 0 ? void 0 : _menuRef$current3.highlightFirst();
}
}, [loading, menuRef]);
};
export var TieredSelectContext = /*#__PURE__*/React.createContext({
afterHide: noop,
afterShow: noop,
beforeHide: always,
beforeShow: always,
getGroupId: defaultGetGroupId,
getId: defaultGetId,
getLabel: defaultGetLabel,
getNextGroupId: defaultGetNextGroupId,
getValueString: getFullPath(defaultGetLabel),
i18nScope: 'core.tieredSelect',
onChange: noop
});
/**
We use tiered selects to allow users to choose a single parent or child option
that is nested within a tiered list. For example, choosing a location from
within a list of tiered locations. We typically see these selects on forms.
Other select components are group select, multi select, and select.
@since 10.19.0
@see [Storybook](https://stories.core.procore.com/?path=/story/core-react_demos-tieredselect--demo)
@see [Design Guidelines](https://design.procore.com/tiered-select)
*/
export var TieredSelect = /*#__PURE__*/React.forwardRef(function TieredSelect(_ref2, ref) {
var _ref2$afterHide = _ref2.afterHide,
afterHide = _ref2$afterHide === void 0 ? noop : _ref2$afterHide,
_ref2$afterShow = _ref2.afterShow,
afterShow = _ref2$afterShow === void 0 ? noop : _ref2$afterShow,
_ref2$beforeHide = _ref2.beforeHide,
beforeHide = _ref2$beforeHide === void 0 ? always : _ref2$beforeHide,
_ref2$beforeShow = _ref2.beforeShow,
beforeShow = _ref2$beforeShow === void 0 ? always : _ref2$beforeShow,
block = _ref2.block,
_ref2$getGroupId = _ref2.getGroupId,
getGroupId = _ref2$getGroupId === void 0 ? defaultGetGroupId : _ref2$getGroupId,
_ref2$getId = _ref2.getId,
getId = _ref2$getId === void 0 ? defaultGetId : _ref2$getId,
_ref2$getLabel = _ref2.getLabel,
getLabel = _ref2$getLabel === void 0 ? defaultGetLabel : _ref2$getLabel,
_ref2$getNextGroupId = _ref2.getNextGroupId,
getNextGroupId = _ref2$getNextGroupId === void 0 ? defaultGetNextGroupId : _ref2$getNextGroupId,
_ref2$getValueString = _ref2.getValueString,
getValueString = _ref2$getValueString === void 0 ? getFullPath(getLabel) : _ref2$getValueString,
_ref2$i18nScope = _ref2.i18nScope,
i18nScope = _ref2$i18nScope === void 0 ? 'core.tieredSelect' : _ref2$i18nScope,
_ref2$onChange = _ref2.onChange,
onChange = _ref2$onChange === void 0 ? noop : _ref2$onChange,
onClear = _ref2.onClear,
onNavigate = _ref2.onNavigate,
onQuickCreate = _ref2.onQuickCreate,
onScrollBottom = _ref2.onScrollBottom,
onSearch = _ref2.onSearch,
_ref2$selectableTiers = _ref2.selectableTiers,
selectableTiers = _ref2$selectableTiers === void 0 ? true : _ref2$selectableTiers,
_ref2$isTierDisabled = _ref2.isTierDisabled,
isTierDisabled = _ref2$isTierDisabled === void 0 ? function () {
return false;
} : _ref2$isTierDisabled,
tabIndex = _ref2.tabIndex,
searchPlaceholder = _ref2.searchPlaceholder,
props = _objectWithoutProperties(_ref2, _excluded);
var isLeaf = React.useCallback(function (tier) {
return !getNextGroupId(tier);
}, [getNextGroupId]);
return /*#__PURE__*/React.createElement(TieredSelectContext.Provider, {
value: {
afterHide: afterHide,
afterShow: afterShow,
beforeHide: beforeHide,
beforeShow: beforeShow,
block: block,
getGroupId: getGroupId,
getId: getId,
getLabel: getLabel,
getNextGroupId: getNextGroupId,
getValueString: getValueStringFn(getValueString),
i18nScope: i18nScope,
isLeaf: isLeaf,
isTierDisabled: isTierDisabled,
onChange: onChange,
onClear: onClear,
onNavigate: onNavigate,
onQuickCreate: onQuickCreate,
onScrollBottom: onScrollBottom,
onSearch: onSearch,
searchPlaceholder: searchPlaceholder,
tabIndex: tabIndex
}
}, selectableTiers ? /*#__PURE__*/React.createElement(TierSelectionTieredSelect, _extends({
ref: ref
}, props)) : /*#__PURE__*/React.createElement(LeafSelectionTieredSelect, _extends({
ref: ref
}, props)));
});
//# sourceMappingURL=TieredSelect.js.map