@spaced-out/ui-design-system
Version:
Sense UI components library
222 lines (221 loc) • 7.92 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Typeahead = void 0;
var React = _interopRequireWildcard(require("react"));
var _react2 = require("@floating-ui/react");
var _hooks = require("../../hooks");
var _space = require("../../styles/variables/_space");
var _classify = require("../../utils/classify");
var _clickAway = require("../../utils/click-away");
var _mergeRefs = require("../../utils/merge-refs");
var _qa = require("../../utils/qa");
var _Menu = require("../Menu");
var _SearchInput = require("../SearchInput");
var _Tooltip = require("../Tooltip");
var _TypeaheadModule = _interopRequireDefault(require("./Typeahead.module.css"));
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
const BaseTypeahead = /*#__PURE__*/React.forwardRef((_ref, ref) => {
let {
size = 'medium',
classNames,
placeholder = 'Select...',
onSelect,
onSearch,
onClear,
menu,
onMenuOpen,
onMenuClose,
typeaheadInputText = '',
isLoading,
menuOpenOffset = 1,
onFocus,
clickAwayRef,
elevation = 'modal',
testId,
rightSlot,
...inputProps
} = _ref;
const menuOptions = menu?.options;
const {
x,
y,
refs,
strategy,
context
} = (0, _react2.useFloating)({
open: true,
strategy: 'absolute',
placement: 'bottom-start',
whileElementsMounted: _react2.autoUpdate,
middleware: [(0, _react2.flip)(), (0, _react2.offset)(parseInt(_space.spaceXXSmall))]
});
const dropdownWidth = (0, _hooks.useReferenceElementWidth)(refs.reference?.current);
const onMenuToggle = isOpen => {
if (isOpen) {
onMenuOpen?.();
} else {
onMenuClose?.();
}
};
return /*#__PURE__*/(0, _jsxRuntime.jsx)(_clickAway.ClickAway, {
onChange: onMenuToggle,
clickAwayRef: clickAwayRef,
children: _ref2 => {
let {
isOpen,
onOpen,
clickAway,
boundaryRef,
triggerRef
} = _ref2;
return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
"data-testid": (0, _qa.generateTestId)({
base: testId,
slot: 'root'
}),
className: (0, _classify.classify)(_TypeaheadModule.default.typeaheadContainer, classNames?.wrapper),
children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_SearchInput.SearchInput, {
...inputProps,
ref: ref,
boxRef: (0, _mergeRefs.mergeRefs)([refs.setReference, triggerRef]),
size: size,
placeholder: placeholder,
value: typeaheadInputText,
classNames: {
box: classNames?.box
},
isLoading: isLoading,
rightSlot: rightSlot,
onChange: e => {
e.stopPropagation();
onSearch?.(e);
if (e.target.value.length >= menuOpenOffset) {
if (!isOpen) {
onOpen();
}
} else {
clickAway();
}
},
onFocus: _e => {
if (typeaheadInputText.length >= menuOpenOffset) {
if (!isOpen) {
onOpen();
}
} else {
clickAway();
}
onFocus?.(_e);
},
onClear: () => {
onClear?.();
},
testId: (0, _qa.generateTestId)({
base: testId,
slot: 'search-input'
})
}), isOpen && !isLoading && menu && menuOptions && !!menuOptions.length && /*#__PURE__*/(0, _jsxRuntime.jsx)(_react2.FloatingPortal, {
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_react2.FloatingFocusManager, {
modal: false,
context: context,
returnFocus: false,
initialFocus: refs.reference,
children: /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
ref: (0, _mergeRefs.mergeRefs)([refs.setFloating, boundaryRef]),
className: _TypeaheadModule.default.menuWrapper,
"data-testid": (0, _qa.generateTestId)({
base: testId,
slot: 'menu-wrapper'
}),
style: {
position: strategy,
top: y ?? _space.spaceNone,
left: x ?? _space.spaceNone,
/* NOTE(Sharad): The FloatingPortal renders the menu outside the normal DOM structure,
so its parent is effectively the <body> element. This means the menu
would otherwise default to the body's width. To support fluid width,
we must manually set the dropdown width here; otherwise, it uses a fixed width.
Also, Only treat menu as non-fluid if isFluid is strictly false, since default is true in menu and undefined means fluid. */
...(menu.isFluid !== false && {
'--dropdown-width': dropdownWidth
}),
'--menu-elevation': (0, _Tooltip.getElevationValue)(elevation)
},
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Menu.Menu, {
...menu,
options: menuOptions,
testId: (0, _qa.generateTestId)({
base: testId,
slot: 'menu'
}),
onSelect: (option, e) => {
onSelect?.(option, e);
if (
// option.keepMenuOpenOnOptionSelect - to allow the menu persist its open stat upon option selection in normal variant
!option.keepMenuOpenOnOptionSelect && (!menu.optionsVariant || menu.optionsVariant === 'normal')) {
clickAway();
}
},
size: menu.size || size,
onTabOut: clickAway
})
})
})
})]
});
}
});
});
const StatefulTypeahead = /*#__PURE__*/React.forwardRef((_ref3, ref) => {
let {
menu,
...props
} = _ref3;
const {
typeaheadInputText = ''
} = props;
const [filteredOptions, setFilteredOptions] = React.useState(menu?.options);
React.useEffect(() => {
const optionsFiltered = menu?.options && menu.options.filter(option => {
if (!option.label || !typeaheadInputText) {
return true;
} else {
return option.label.toLowerCase().indexOf(typeaheadInputText.toLowerCase()) !== -1;
}
});
setFilteredOptions(optionsFiltered || []);
}, [typeaheadInputText, menu?.options]);
return /*#__PURE__*/(0, _jsxRuntime.jsx)(BaseTypeahead, {
...props,
menu: {
...menu,
options: filteredOptions
},
ref: ref
});
});
const StatelessTypeahead = /*#__PURE__*/React.forwardRef((props, ref) => /*#__PURE__*/(0, _jsxRuntime.jsx)(BaseTypeahead, {
...props,
ref: ref
}));
const Typeahead = exports.Typeahead = /*#__PURE__*/React.forwardRef((_ref4, ref) => {
let {
allowInternalFilter = true,
...props
} = _ref4;
if (allowInternalFilter) {
return /*#__PURE__*/(0, _jsxRuntime.jsx)(StatefulTypeahead, {
...props,
ref: ref
});
} else {
return /*#__PURE__*/(0, _jsxRuntime.jsx)(StatelessTypeahead, {
...props,
ref: ref
});
}
});