UNPKG

rsuite

Version:

A suite of react components

275 lines (268 loc) 9 kB
'use client'; import _extends from "@babel/runtime/helpers/esm/extends"; import React, { useState } from 'react'; import pick from 'lodash/pick'; import isNil from 'lodash/isNil'; import isFunction from 'lodash/isFunction'; import SearchBox from "../internals/SearchBox/index.js"; import { useStyles, useCustom, useControlled, useEventCallback } from "../internals/hooks/index.js"; import { forwardRef, createChainedFunction, mergeRefs, shallowEqual, getDataGroupBy } from "../internals/utils/index.js"; import { Listbox, ListItem, PickerToggle, PickerToggleTrigger, PickerPopup, useFocusItemValue, useSearch, useToggleKeyDownEvent, usePickerRef, triggerPropKeys } from "../internals/Picker/index.js"; const emptyArray = []; /** * The `SelectPicker` component is used to select an item from a list of data. * @see https://rsuitejs.com/components/select-picker/ */ const SelectPicker = forwardRef((props, ref) => { const { propsWithDefaults } = useCustom('SelectPicker', props); const { appearance = 'default', as, block, className, cleanable = true, classPrefix = 'picker', data = emptyArray, defaultValue, disabled, disabledItemValues = emptyArray, groupBy, id, labelKey = 'label', listProps, listboxMaxHeight = 320, locale, placeholder, placement = 'bottomStart', popupAutoWidth = true, popupClassName, popupStyle, searchable = true, style, toggleAs, value: valueProp, valueKey = 'value', virtualized, sort, searchBy, renderValue, renderListbox, renderOptionGroup, renderOption, renderExtraFooter, onGroupTitleClick, onEnter, onExit, onClean, onChange, onSelect, onSearch, ...rest } = propsWithDefaults; const { trigger, root, target, overlay, list, searchInput } = usePickerRef(ref); const [value, setValue] = useControlled(valueProp, defaultValue); // Used to hover the focus item when trigger `onKeydown` const { focusItemValue, setFocusItemValue, onKeyDown: onFocusItem } = useFocusItemValue(value, { data, valueKey, target: () => overlay.current }); // Use search keywords to filter options. const { searchKeyword, filteredData, resetSearch, handleSearch } = useSearch(data, { labelKey, searchBy, callback: (searchKeyword, filteredData, event) => { var _filteredData$; // The first option after filtering is the focus. setFocusItemValue(filteredData === null || filteredData === void 0 || (_filteredData$ = filteredData[0]) === null || _filteredData$ === void 0 ? void 0 : _filteredData$[valueKey]); onSearch === null || onSearch === void 0 || onSearch(searchKeyword, event); } }); // Use component active state to support keyboard events. const [active, setActive] = useState(false); const handleClose = useEventCallback(() => { var _trigger$current, _trigger$current$clos; (_trigger$current = trigger.current) === null || _trigger$current === void 0 || (_trigger$current$clos = _trigger$current.close) === null || _trigger$current$clos === void 0 || _trigger$current$clos.call(_trigger$current); }); const handleSelect = useEventCallback((value, item, event) => { var _target$current; onSelect === null || onSelect === void 0 || onSelect(value, item, event); (_target$current = target.current) === null || _target$current === void 0 || _target$current.focus(); }); const handleChangeValue = useEventCallback((value, event) => { onChange === null || onChange === void 0 || onChange(value, event); }); const handleMenuPressEnter = useEventCallback(event => { if (!focusItemValue) { return; } // Find active `MenuItem` by `value` const focusItem = data.find(item => shallowEqual(item[valueKey], focusItemValue)); setValue(focusItemValue); handleSelect(focusItemValue, focusItem, event); handleChangeValue(focusItemValue, event); handleClose(); }); const handleItemSelect = useEventCallback((value, item, event) => { setValue(value); setFocusItemValue(value); handleSelect(value, item, event); handleChangeValue(value, event); handleClose(); }); const handleClean = useEventCallback(event => { if (disabled || !cleanable) { return; } setValue(null); setFocusItemValue(value); handleChangeValue(null, event); }); const onPickerKeyDown = useToggleKeyDownEvent({ toggle: !focusItemValue || !active, trigger, target, overlay, searchInput, active, onExit: handleClean, onMenuKeyDown: onFocusItem, onMenuPressEnter: handleMenuPressEnter, ...rest }); const handleExit = useEventCallback(() => { resetSearch(); setActive(false); onSearch === null || onSearch === void 0 || onSearch(''); setFocusItemValue(null); }); const handleEnter = useEventCallback(() => { setActive(true); setFocusItemValue(value); }); // Find active `MenuItem` by `value` const activeItem = data.find(item => shallowEqual(item[valueKey], value)); /** * 1.Have a value and the value is valid. * 2.Regardless of whether the value is valid, as long as renderValue is set, it is judged to have a value. */ let hasValue = !!activeItem || !isNil(value) && isFunction(renderValue); const { prefix, merge } = useStyles(classPrefix); let selectedElement = placeholder; if (activeItem !== null && activeItem !== void 0 && activeItem[labelKey]) { selectedElement = activeItem[labelKey]; } if (!isNil(value) && isFunction(renderValue)) { selectedElement = renderValue(value, activeItem, selectedElement); // If renderValue returns null or undefined, hasValue is false. if (isNil(selectedElement)) { hasValue = false; } } const renderPopup = (positionProps, speakerRef) => { const { className } = positionProps; const classes = merge(className, popupClassName, prefix('select-menu')); let items = filteredData; // Create a tree structure data when set `groupBy` if (groupBy) { items = getDataGroupBy(items, groupBy, sort); } else if (typeof sort === 'function') { items = items.sort(sort(false)); } const listbox = items.length ? /*#__PURE__*/React.createElement(Listbox, { listProps: listProps, listRef: list, disabledItemValues: disabledItemValues, valueKey: valueKey, labelKey: labelKey, renderOptionGroup: renderOptionGroup, renderOption: renderOption, maxHeight: listboxMaxHeight, classPrefix: 'picker-select-menu', listItemClassPrefix: 'picker-select-menu-item', listItemAs: ListItem, activeItemValues: [value], focusItemValue: focusItemValue, data: items, query: searchKeyword, groupBy: groupBy, onSelect: handleItemSelect, onGroupTitleClick: onGroupTitleClick, virtualized: virtualized }) : /*#__PURE__*/React.createElement("div", { className: prefix`none` }, locale === null || locale === void 0 ? void 0 : locale.noResultsText); return /*#__PURE__*/React.createElement(PickerPopup, { ref: mergeRefs(overlay, speakerRef), autoWidth: popupAutoWidth, className: classes, style: popupStyle, onKeyDown: onPickerKeyDown, target: trigger }, searchable && /*#__PURE__*/React.createElement(SearchBox, { placeholder: locale === null || locale === void 0 ? void 0 : locale.searchPlaceholder, onChange: handleSearch, value: searchKeyword, inputRef: searchInput }), renderListbox ? renderListbox(listbox) : listbox, renderExtraFooter === null || renderExtraFooter === void 0 ? void 0 : renderExtraFooter()); }; const triggerProps = { ...pick(props, triggerPropKeys), onEnter: createChainedFunction(handleEnter, onEnter), onExit: createChainedFunction(handleExit, onExit) }; return /*#__PURE__*/React.createElement(PickerToggleTrigger, { as: as, id: id, pickerType: "select", block: block, disabled: disabled, appearance: appearance, triggerProps: triggerProps, ref: trigger, placement: placement, speaker: renderPopup, rootRef: root, style: style, classPrefix: classPrefix, className: className }, /*#__PURE__*/React.createElement(PickerToggle, _extends({ ref: target, appearance: appearance, onClean: createChainedFunction(handleClean, onClean), onKeyDown: onPickerKeyDown, as: toggleAs, disabled: disabled, cleanable: cleanable && !disabled, hasValue: hasValue, inputValue: value !== null && value !== void 0 ? value : '', focusItemValue: focusItemValue, active: active, placement: placement }, rest), selectedElement || (locale === null || locale === void 0 ? void 0 : locale.placeholder))); }); SelectPicker.displayName = 'SelectPicker'; export default SelectPicker;