UNPKG

@mui/base

Version:

A library of headless ('unstyled') React UI components and low-level hooks.

238 lines (236 loc) 7.79 kB
import _extends from "@babel/runtime/helpers/esm/extends"; import * as React from 'react'; import { unstable_useForkRef as useForkRef, unstable_useId as useId, unstable_useEnhancedEffect as useEnhancedEffect } from '@mui/utils'; import useButton from '../useButton'; import { SelectActionTypes } from './useSelect.types'; import useList from '../useList'; import defaultOptionStringifier from './defaultOptionStringifier'; import { useCompoundParent } from '../utils/useCompound'; import selectReducer from './selectReducer'; import combineHooksSlotProps from '../utils/combineHooksSlotProps'; function preventDefault(event) { event.preventDefault(); } /** * * Demos: * * - [Select](https://mui.com/base/react-select/#hooks) * * API: * * - [useSelect API](https://mui.com/base/react-select/hooks-api/#use-select) */ function useSelect(props) { const { buttonRef: buttonRefProp, defaultOpen = false, defaultValue: defaultValueProp, disabled = false, listboxId: listboxIdProp, listboxRef: listboxRefProp, multiple = false, onChange, onHighlightChange, onOpenChange, open: openProp, options: optionsParam, getOptionAsString = defaultOptionStringifier, value: valueProp } = props; const buttonRef = React.useRef(null); const handleButtonRef = useForkRef(buttonRefProp, buttonRef); const listboxRef = React.useRef(null); const listboxId = useId(listboxIdProp); let defaultValue; if (valueProp === undefined && defaultValueProp === undefined) { defaultValue = []; } else if (defaultValueProp !== undefined) { if (multiple) { defaultValue = defaultValueProp; } else { defaultValue = defaultValueProp == null ? [] : [defaultValueProp]; } } const value = React.useMemo(() => { if (valueProp !== undefined) { if (multiple) { return valueProp; } return valueProp == null ? [] : [valueProp]; } return undefined; }, [valueProp, multiple]); const { subitems, contextValue: compoundComponentContextValue } = useCompoundParent(); const options = React.useMemo(() => { if (optionsParam != null) { return new Map(optionsParam.map((option, index) => [option.value, { value: option.value, label: option.label, disabled: option.disabled, ref: /*#__PURE__*/React.createRef(), id: `${listboxId}_${index}` }])); } return subitems; }, [optionsParam, subitems, listboxId]); const handleListboxRef = useForkRef(listboxRefProp, listboxRef); const { getRootProps: getButtonRootProps, active: buttonActive, focusVisible: buttonFocusVisible, rootRef: mergedButtonRef } = useButton({ disabled, rootRef: handleButtonRef }); const optionValues = React.useMemo(() => Array.from(options.keys()), [options]); const isItemDisabled = React.useCallback(valueToCheck => { const option = options.get(valueToCheck); return option?.disabled ?? false; }, [options]); const stringifyOption = React.useCallback(valueToCheck => { const option = options.get(valueToCheck); if (!option) { return ''; } return getOptionAsString(option); }, [options, getOptionAsString]); const controlledState = React.useMemo(() => ({ selectedValues: value, open: openProp }), [value, openProp]); const getItemId = React.useCallback(itemValue => options.get(itemValue)?.id, [options]); const handleSelectionChange = React.useCallback((event, newValues) => { if (multiple) { onChange?.(event, newValues); } else { onChange?.(event, newValues[0] ?? null); } }, [multiple, onChange]); const handleHighlightChange = React.useCallback((event, newValue) => { onHighlightChange?.(event, newValue ?? null); }, [onHighlightChange]); const handleStateChange = React.useCallback((event, field, fieldValue) => { if (field === 'open') { onOpenChange?.(fieldValue); if (fieldValue === false && event?.type !== 'blur') { buttonRef.current?.focus(); } } }, [onOpenChange]); const useListParameters = { getInitialState: () => ({ highlightedValue: null, selectedValues: defaultValue ?? [], open: defaultOpen }), getItemId, controlledProps: controlledState, isItemDisabled, rootRef: mergedButtonRef, onChange: handleSelectionChange, onHighlightChange: handleHighlightChange, onStateChange: handleStateChange, reducerActionContext: React.useMemo(() => ({ multiple }), [multiple]), items: optionValues, getItemAsString: stringifyOption, selectionMode: multiple ? 'multiple' : 'single', stateReducer: selectReducer }; const { dispatch, getRootProps: getListboxRootProps, contextValue: listContextValue, state: { open, highlightedValue: highlightedOption, selectedValues: selectedOptions }, rootRef: mergedListRootRef } = useList(useListParameters); const createHandleButtonClick = otherHandlers => event => { otherHandlers?.onClick?.(event); if (!event.defaultMuiPrevented) { const action = { type: SelectActionTypes.buttonClick, event }; dispatch(action); } }; useEnhancedEffect(() => { // Scroll to the currently highlighted option. if (highlightedOption != null) { const optionRef = options.get(highlightedOption)?.ref; if (!listboxRef.current || !optionRef?.current) { return; } const listboxClientRect = listboxRef.current.getBoundingClientRect(); const optionClientRect = optionRef.current.getBoundingClientRect(); if (optionClientRect.top < listboxClientRect.top) { listboxRef.current.scrollTop -= listboxClientRect.top - optionClientRect.top; } else if (optionClientRect.bottom > listboxClientRect.bottom) { listboxRef.current.scrollTop += optionClientRect.bottom - listboxClientRect.bottom; } } }, [highlightedOption, options]); const getOptionMetadata = React.useCallback(optionValue => options.get(optionValue), [options]); const getSelectTriggerProps = (otherHandlers = {}) => { return _extends({}, otherHandlers, { onClick: createHandleButtonClick(otherHandlers), ref: mergedListRootRef, role: 'combobox', 'aria-expanded': open, 'aria-controls': listboxId }); }; const getButtonProps = (otherHandlers = {}) => { const listboxAndButtonProps = combineHooksSlotProps(getButtonRootProps, getListboxRootProps); const combinedProps = combineHooksSlotProps(listboxAndButtonProps, getSelectTriggerProps); return combinedProps(otherHandlers); }; const getListboxProps = (otherHandlers = {}) => { return _extends({}, otherHandlers, { id: listboxId, role: 'listbox', 'aria-multiselectable': multiple ? 'true' : undefined, ref: handleListboxRef, onMouseDown: preventDefault // to prevent the button from losing focus when interacting with the listbox }); }; React.useDebugValue({ selectedOptions, highlightedOption, open }); const contextValue = React.useMemo(() => _extends({}, listContextValue, compoundComponentContextValue), [listContextValue, compoundComponentContextValue]); let selectValue; if (props.multiple) { selectValue = selectedOptions; } else { selectValue = selectedOptions.length > 0 ? selectedOptions[0] : null; } return { buttonActive, buttonFocusVisible, buttonRef: mergedButtonRef, contextValue, disabled, dispatch, getButtonProps, getListboxProps, getOptionMetadata, listboxRef: mergedListRootRef, open, options: optionValues, value: selectValue, highlightedOption }; } export default useSelect;