UNPKG

rsuite

Version:

A suite of react components

272 lines (265 loc) 8.04 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) => { // The first option after filtering is the focus. setFocusItemValue(filteredData?.[0]?.[valueKey]); onSearch?.(searchKeyword, event); } }); // Use component active state to support keyboard events. const [active, setActive] = useState(false); const handleClose = useEventCallback(() => { trigger.current?.close?.(); }); const handleSelect = useEventCallback((value, item, event) => { onSelect?.(value, item, event); target.current?.focus(); }); const handleChangeValue = useEventCallback((value, event) => { 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?.(''); 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?.[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?.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?.searchPlaceholder, onChange: handleSearch, value: searchKeyword, inputRef: searchInput }), renderListbox ? renderListbox(listbox) : listbox, 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 ?? '', focusItemValue: focusItemValue, active: active, placement: placement }, rest), selectedElement || locale?.placeholder)); }); SelectPicker.displayName = 'SelectPicker'; export default SelectPicker;