pre-installations and preparations
This commit is contained in:
1 parent
3e5a0329cb
commit
201d621ac3
6977 files changed
+742826
No files matched your search
+15
@@ -0,0 +1,15 @@
|
||||
import * as React from 'react';
|
||||
import { ListContextValue } from '../useList/ListContext';
|
||||
import { SelectOption } from '../useOption';
|
||||
import { CompoundComponentContextValue } from '../useCompound';
|
||||
export type SelectProviderValue<Value> = CompoundComponentContextValue<Value, SelectOption<Value>> & ListContextValue<Value>;
|
||||
export interface SelectProviderProps<Value> {
|
||||
value: SelectProviderValue<Value>;
|
||||
children: React.ReactNode;
|
||||
}
|
||||
/**
|
||||
* Sets up the contexts for the underlying Option components.
|
||||
*
|
||||
* @ignore - do not document.
|
||||
*/
|
||||
export declare function SelectProvider<Value>(props: SelectProviderProps<Value>): React.JSX.Element;
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
'use client';
|
||||
|
||||
import * as React from 'react';
|
||||
import { ListContext } from '../useList/ListContext';
|
||||
import { CompoundComponentContext } from '../useCompound';
|
||||
import { jsx as _jsx } from "react/jsx-runtime";
|
||||
/**
|
||||
* Sets up the contexts for the underlying Option components.
|
||||
*
|
||||
* @ignore - do not document.
|
||||
*/
|
||||
export function SelectProvider(props) {
|
||||
const {
|
||||
value,
|
||||
children
|
||||
} = props;
|
||||
const {
|
||||
dispatch,
|
||||
getItemIndex,
|
||||
getItemState,
|
||||
registerItem,
|
||||
totalSubitemCount
|
||||
} = value;
|
||||
const listContextValue = React.useMemo(() => ({
|
||||
dispatch,
|
||||
getItemState,
|
||||
getItemIndex
|
||||
}), [dispatch, getItemIndex, getItemState]);
|
||||
const compoundComponentContextValue = React.useMemo(() => ({
|
||||
getItemIndex,
|
||||
registerItem,
|
||||
totalSubitemCount
|
||||
}), [registerItem, getItemIndex, totalSubitemCount]);
|
||||
return /*#__PURE__*/_jsx(CompoundComponentContext.Provider, {
|
||||
value: compoundComponentContextValue,
|
||||
children: /*#__PURE__*/_jsx(ListContext.Provider, {
|
||||
value: listContextValue,
|
||||
children: children
|
||||
})
|
||||
});
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
import { SelectOption } from '../useOption';
|
||||
declare const defaultOptionStringifier: <OptionValue>(option: SelectOption<OptionValue>) => string;
|
||||
export { defaultOptionStringifier };
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
const defaultOptionStringifier = option => {
|
||||
const {
|
||||
label,
|
||||
value
|
||||
} = option;
|
||||
if (typeof label === 'string') {
|
||||
return label;
|
||||
}
|
||||
if (typeof value === 'string') {
|
||||
return value;
|
||||
}
|
||||
|
||||
// Fallback string representation
|
||||
return String(option);
|
||||
};
|
||||
export { defaultOptionStringifier };
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
export { useSelect } from './useSelect';
|
||||
export * from './useSelect.types';
|
||||
export * from './SelectProvider';
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
'use client';
|
||||
|
||||
export { useSelect } from './useSelect';
|
||||
export * from './useSelect.types';
|
||||
export * from './SelectProvider';
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"sideEffects": false,
|
||||
"module": "./index.js",
|
||||
"main": "../node/useSelect/index.js",
|
||||
"types": "./index.d.ts"
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
import { ListAction, ListActionContext } from '../useList';
|
||||
import { ActionWithContext } from '../utils/useControllableReducer.types';
|
||||
import { SelectAction, SelectInternalState } from './useSelect.types';
|
||||
export declare function selectReducer<OptionValue>(state: SelectInternalState<OptionValue>, action: ActionWithContext<ListAction<OptionValue> | SelectAction<OptionValue>, ListActionContext<OptionValue>>): SelectInternalState<OptionValue>;
|
||||
+65
@@ -0,0 +1,65 @@
|
||||
import _extends from "@babel/runtime/helpers/esm/extends";
|
||||
import { moveHighlight, listReducer, ListActionTypes, handleItemSelection } from '../useList';
|
||||
import { SelectActionTypes } from './useSelect.types';
|
||||
export function selectReducer(state, action) {
|
||||
const {
|
||||
open
|
||||
} = state;
|
||||
const {
|
||||
context: {
|
||||
selectionMode
|
||||
}
|
||||
} = action;
|
||||
if (action.type === SelectActionTypes.buttonClick) {
|
||||
var _state$selectedValues;
|
||||
const itemToHighlight = (_state$selectedValues = state.selectedValues[0]) != null ? _state$selectedValues : moveHighlight(null, 'start', action.context);
|
||||
return _extends({}, state, {
|
||||
open: !open,
|
||||
highlightedValue: !open ? itemToHighlight : null
|
||||
});
|
||||
}
|
||||
if (action.type === SelectActionTypes.browserAutoFill) {
|
||||
return handleItemSelection(action.item, state, action.context);
|
||||
}
|
||||
const newState = listReducer(state, action);
|
||||
switch (action.type) {
|
||||
case ListActionTypes.keyDown:
|
||||
if (state.open) {
|
||||
if (action.event.key === 'Escape') {
|
||||
return _extends({}, newState, {
|
||||
open: false
|
||||
});
|
||||
}
|
||||
} else {
|
||||
if (action.event.key === 'ArrowDown') {
|
||||
var _state$selectedValues2;
|
||||
return _extends({}, state, {
|
||||
open: true,
|
||||
highlightedValue: (_state$selectedValues2 = state.selectedValues[0]) != null ? _state$selectedValues2 : moveHighlight(null, 'start', action.context)
|
||||
});
|
||||
}
|
||||
if (action.event.key === 'ArrowUp') {
|
||||
var _state$selectedValues3;
|
||||
return _extends({}, state, {
|
||||
open: true,
|
||||
highlightedValue: (_state$selectedValues3 = state.selectedValues[0]) != null ? _state$selectedValues3 : moveHighlight(null, 'end', action.context)
|
||||
});
|
||||
}
|
||||
}
|
||||
break;
|
||||
case ListActionTypes.itemClick:
|
||||
if (selectionMode === 'single') {
|
||||
return _extends({}, newState, {
|
||||
open: false
|
||||
});
|
||||
}
|
||||
break;
|
||||
case ListActionTypes.blur:
|
||||
return _extends({}, newState, {
|
||||
open: false
|
||||
});
|
||||
default:
|
||||
return newState;
|
||||
}
|
||||
return newState;
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
import { UseSelectParameters, UseSelectReturnValue } from './useSelect.types';
|
||||
/**
|
||||
*
|
||||
* Demos:
|
||||
*
|
||||
* - [Select](https://mui.com/base-ui/react-select/#hooks)
|
||||
*
|
||||
* API:
|
||||
*
|
||||
* - [useSelect API](https://mui.com/base-ui/react-select/hooks-api/#use-select)
|
||||
*/
|
||||
declare function useSelect<OptionValue, Multiple extends boolean = false>(props: UseSelectParameters<OptionValue, Multiple>): UseSelectReturnValue<OptionValue, Multiple>;
|
||||
export { useSelect };
|
||||
+368
@@ -0,0 +1,368 @@
|
||||
'use client';
|
||||
|
||||
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, visuallyHidden as visuallyHiddenStyle } from '@mui/utils';
|
||||
import { useButton } from '../useButton';
|
||||
import { SelectActionTypes } from './useSelect.types';
|
||||
import { ListActionTypes, useList } from '../useList';
|
||||
import { defaultOptionStringifier } from './defaultOptionStringifier';
|
||||
import { useCompoundParent } from '../useCompound';
|
||||
import { extractEventHandlers } from '../utils/extractEventHandlers';
|
||||
import { selectReducer } from './selectReducer';
|
||||
import { combineHooksSlotProps } from '../utils/combineHooksSlotProps';
|
||||
function defaultFormValueProvider(selectedOption) {
|
||||
if (Array.isArray(selectedOption)) {
|
||||
if (selectedOption.length === 0) {
|
||||
return '';
|
||||
}
|
||||
return JSON.stringify(selectedOption.map(o => o.value));
|
||||
}
|
||||
if ((selectedOption == null ? void 0 : selectedOption.value) == null) {
|
||||
return '';
|
||||
}
|
||||
if (typeof selectedOption.value === 'string' || typeof selectedOption.value === 'number') {
|
||||
return selectedOption.value;
|
||||
}
|
||||
return JSON.stringify(selectedOption.value);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Demos:
|
||||
*
|
||||
* - [Select](https://mui.com/base-ui/react-select/#hooks)
|
||||
*
|
||||
* API:
|
||||
*
|
||||
* - [useSelect API](https://mui.com/base-ui/react-select/hooks-api/#use-select)
|
||||
*/
|
||||
function useSelect(props) {
|
||||
const {
|
||||
areOptionsEqual,
|
||||
buttonRef: buttonRefProp,
|
||||
defaultOpen = false,
|
||||
defaultValue: defaultValueProp,
|
||||
disabled = false,
|
||||
listboxId: listboxIdProp,
|
||||
listboxRef: listboxRefProp,
|
||||
multiple = false,
|
||||
name,
|
||||
required,
|
||||
onChange,
|
||||
onHighlightChange,
|
||||
onOpenChange,
|
||||
open: openProp,
|
||||
options: optionsParam,
|
||||
getOptionAsString = defaultOptionStringifier,
|
||||
getSerializedValue = defaultFormValueProvider,
|
||||
value: valueProp,
|
||||
componentName = 'useSelect'
|
||||
} = 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 getOptionByValue = React.useCallback(valueToGet => {
|
||||
// This can't be simply `options.get(valueToGet)` because of the `areOptionsEqual` prop.
|
||||
// If it's provided, we assume that the user wants to compare the options by value.
|
||||
if (areOptionsEqual !== undefined) {
|
||||
const similarValue = optionValues.find(optionValue => areOptionsEqual(optionValue, valueToGet));
|
||||
return options.get(similarValue);
|
||||
}
|
||||
return options.get(valueToGet);
|
||||
}, [options, areOptionsEqual, optionValues]);
|
||||
const isItemDisabled = React.useCallback(valueToCheck => {
|
||||
var _option$disabled;
|
||||
const option = getOptionByValue(valueToCheck);
|
||||
return (_option$disabled = option == null ? void 0 : option.disabled) != null ? _option$disabled : false;
|
||||
}, [getOptionByValue]);
|
||||
const stringifyOption = React.useCallback(valueToCheck => {
|
||||
const option = getOptionByValue(valueToCheck);
|
||||
if (!option) {
|
||||
return '';
|
||||
}
|
||||
return getOptionAsString(option);
|
||||
}, [getOptionByValue, getOptionAsString]);
|
||||
const controlledState = React.useMemo(() => ({
|
||||
selectedValues: value,
|
||||
open: openProp
|
||||
}), [value, openProp]);
|
||||
const getItemId = React.useCallback(itemValue => {
|
||||
var _options$get;
|
||||
return (_options$get = options.get(itemValue)) == null ? void 0 : _options$get.id;
|
||||
}, [options]);
|
||||
const handleSelectionChange = React.useCallback((event, newValues) => {
|
||||
if (multiple) {
|
||||
onChange == null || onChange(event, newValues);
|
||||
} else {
|
||||
var _newValues$;
|
||||
onChange == null || onChange(event, (_newValues$ = newValues[0]) != null ? _newValues$ : null);
|
||||
}
|
||||
}, [multiple, onChange]);
|
||||
const handleHighlightChange = React.useCallback((event, newValue) => {
|
||||
onHighlightChange == null || onHighlightChange(event, newValue != null ? newValue : null);
|
||||
}, [onHighlightChange]);
|
||||
const handleStateChange = React.useCallback((event, field, fieldValue) => {
|
||||
if (field === 'open') {
|
||||
onOpenChange == null || onOpenChange(fieldValue);
|
||||
if (fieldValue === false && (event == null ? void 0 : event.type) !== 'blur') {
|
||||
var _buttonRef$current;
|
||||
(_buttonRef$current = buttonRef.current) == null || _buttonRef$current.focus();
|
||||
}
|
||||
}
|
||||
}, [onOpenChange]);
|
||||
const getItemDomElement = React.useCallback(itemId => {
|
||||
var _subitems$get$ref$cur, _subitems$get;
|
||||
if (itemId == null) {
|
||||
return null;
|
||||
}
|
||||
return (_subitems$get$ref$cur = (_subitems$get = subitems.get(itemId)) == null ? void 0 : _subitems$get.ref.current) != null ? _subitems$get$ref$cur : null;
|
||||
}, [subitems]);
|
||||
const useListParameters = {
|
||||
getInitialState: () => {
|
||||
var _defaultValue;
|
||||
return {
|
||||
highlightedValue: null,
|
||||
selectedValues: (_defaultValue = defaultValue) != null ? _defaultValue : [],
|
||||
open: defaultOpen
|
||||
};
|
||||
},
|
||||
getItemId,
|
||||
controlledProps: controlledState,
|
||||
focusManagement: 'DOM',
|
||||
getItemDomElement,
|
||||
itemComparer: areOptionsEqual,
|
||||
isItemDisabled,
|
||||
rootRef: handleListboxRef,
|
||||
onChange: handleSelectionChange,
|
||||
onHighlightChange: handleHighlightChange,
|
||||
onStateChange: handleStateChange,
|
||||
reducerActionContext: React.useMemo(() => ({
|
||||
multiple
|
||||
}), [multiple]),
|
||||
items: optionValues,
|
||||
getItemAsString: stringifyOption,
|
||||
selectionMode: multiple ? 'multiple' : 'single',
|
||||
stateReducer: selectReducer,
|
||||
componentName
|
||||
};
|
||||
const {
|
||||
dispatch,
|
||||
getRootProps: getListboxRootProps,
|
||||
contextValue: listContextValue,
|
||||
state: {
|
||||
open,
|
||||
highlightedValue: highlightedOption,
|
||||
selectedValues: selectedOptions
|
||||
},
|
||||
rootRef: mergedListRootRef
|
||||
} = useList(useListParameters);
|
||||
|
||||
// store the initial open state to prevent focus stealing
|
||||
// (the first option gets focused only when the select is opened by the user)
|
||||
const isInitiallyOpen = React.useRef(open);
|
||||
useEnhancedEffect(() => {
|
||||
if (open && highlightedOption !== null) {
|
||||
var _getOptionByValue;
|
||||
const optionRef = (_getOptionByValue = getOptionByValue(highlightedOption)) == null ? void 0 : _getOptionByValue.ref;
|
||||
if (!listboxRef.current || !(optionRef != null && optionRef.current)) {
|
||||
return;
|
||||
}
|
||||
if (!isInitiallyOpen.current) {
|
||||
optionRef.current.focus({
|
||||
preventScroll: true
|
||||
});
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
}, [open, highlightedOption, getOptionByValue]);
|
||||
const getOptionMetadata = React.useCallback(optionValue => getOptionByValue(optionValue), [getOptionByValue]);
|
||||
const createHandleButtonClick = externalEventHandlers => event => {
|
||||
var _externalEventHandler;
|
||||
externalEventHandlers == null || (_externalEventHandler = externalEventHandlers.onClick) == null || _externalEventHandler.call(externalEventHandlers, event);
|
||||
if (!event.defaultMuiPrevented) {
|
||||
const action = {
|
||||
type: SelectActionTypes.buttonClick,
|
||||
event
|
||||
};
|
||||
dispatch(action);
|
||||
}
|
||||
};
|
||||
const createHandleButtonKeyDown = otherHandlers => event => {
|
||||
var _otherHandlers$onKeyD;
|
||||
(_otherHandlers$onKeyD = otherHandlers.onKeyDown) == null || _otherHandlers$onKeyD.call(otherHandlers, event);
|
||||
if (event.defaultMuiPrevented) {
|
||||
return;
|
||||
}
|
||||
if (event.key === 'ArrowDown' || event.key === 'ArrowUp') {
|
||||
event.preventDefault();
|
||||
dispatch({
|
||||
type: ListActionTypes.keyDown,
|
||||
key: event.key,
|
||||
event
|
||||
});
|
||||
}
|
||||
};
|
||||
const getButtonOwnRootProps = (otherHandlers = {}) => ({
|
||||
onClick: createHandleButtonClick(otherHandlers),
|
||||
onKeyDown: createHandleButtonKeyDown(otherHandlers)
|
||||
});
|
||||
const getSelectTriggerProps = (otherHandlers = {}) => {
|
||||
return _extends({}, otherHandlers, getButtonOwnRootProps(otherHandlers), {
|
||||
role: 'combobox',
|
||||
'aria-expanded': open,
|
||||
'aria-controls': listboxId
|
||||
});
|
||||
};
|
||||
const getButtonProps = (externalProps = {}) => {
|
||||
const externalEventHandlers = extractEventHandlers(externalProps);
|
||||
const combinedProps = combineHooksSlotProps(getSelectTriggerProps, getButtonRootProps);
|
||||
return _extends({}, externalProps, combinedProps(externalEventHandlers));
|
||||
};
|
||||
const createListboxHandleBlur = otherHandlers => event => {
|
||||
var _otherHandlers$onBlur, _listboxRef$current;
|
||||
(_otherHandlers$onBlur = otherHandlers.onBlur) == null || _otherHandlers$onBlur.call(otherHandlers, event);
|
||||
if (event.defaultMuiPrevented) {
|
||||
return;
|
||||
}
|
||||
if ((_listboxRef$current = listboxRef.current) != null && _listboxRef$current.contains(event.relatedTarget) || event.relatedTarget === buttonRef.current) {
|
||||
event.defaultMuiPrevented = true;
|
||||
}
|
||||
};
|
||||
const getOwnListboxHandlers = (otherHandlers = {}) => ({
|
||||
onBlur: createListboxHandleBlur(otherHandlers)
|
||||
});
|
||||
const getListboxProps = (externalProps = {}) => {
|
||||
const externalEventHandlers = extractEventHandlers(externalProps);
|
||||
const getCombinedRootProps = combineHooksSlotProps(getOwnListboxHandlers, getListboxRootProps);
|
||||
return _extends({
|
||||
id: listboxId,
|
||||
role: 'listbox',
|
||||
'aria-multiselectable': multiple ? 'true' : undefined
|
||||
}, externalProps, getCombinedRootProps(externalEventHandlers));
|
||||
};
|
||||
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;
|
||||
}
|
||||
let selectedOptionsMetadata;
|
||||
if (multiple) {
|
||||
selectedOptionsMetadata = selectValue.map(v => getOptionMetadata(v)).filter(o => o !== undefined);
|
||||
} else {
|
||||
var _getOptionMetadata;
|
||||
selectedOptionsMetadata = (_getOptionMetadata = getOptionMetadata(selectValue)) != null ? _getOptionMetadata : null;
|
||||
}
|
||||
const createHandleHiddenInputChange = externalEventHandlers => event => {
|
||||
var _externalEventHandler2;
|
||||
externalEventHandlers == null || (_externalEventHandler2 = externalEventHandlers.onChange) == null || _externalEventHandler2.call(externalEventHandlers, event);
|
||||
if (event.defaultMuiPrevented) {
|
||||
return;
|
||||
}
|
||||
const option = options.get(event.target.value);
|
||||
|
||||
// support autofill
|
||||
if (event.target.value === '') {
|
||||
dispatch({
|
||||
type: ListActionTypes.clearSelection
|
||||
});
|
||||
} else if (option !== undefined) {
|
||||
dispatch({
|
||||
type: SelectActionTypes.browserAutoFill,
|
||||
item: option.value,
|
||||
event
|
||||
});
|
||||
}
|
||||
};
|
||||
const getHiddenInputProps = (externalProps = {}) => {
|
||||
const externalEventHandlers = extractEventHandlers(externalProps);
|
||||
return _extends({
|
||||
name,
|
||||
tabIndex: -1,
|
||||
'aria-hidden': true,
|
||||
required: required ? true : undefined,
|
||||
value: getSerializedValue(selectedOptionsMetadata),
|
||||
style: visuallyHiddenStyle
|
||||
}, externalProps, {
|
||||
onChange: createHandleHiddenInputChange(externalEventHandlers)
|
||||
});
|
||||
};
|
||||
return {
|
||||
buttonActive,
|
||||
buttonFocusVisible,
|
||||
buttonRef: mergedButtonRef,
|
||||
contextValue,
|
||||
disabled,
|
||||
dispatch,
|
||||
getButtonProps,
|
||||
getHiddenInputProps,
|
||||
getListboxProps,
|
||||
getOptionMetadata,
|
||||
listboxRef: mergedListRootRef,
|
||||
open,
|
||||
options: optionValues,
|
||||
value: selectValue,
|
||||
highlightedOption
|
||||
};
|
||||
}
|
||||
export { useSelect };
|
||||
+224
@@ -0,0 +1,224 @@
|
||||
import * as React from 'react';
|
||||
import { ListAction, ListState, UseListRootSlotProps } from '../useList';
|
||||
import { SelectOption } from '../useOption/useOption.types';
|
||||
import { SelectProviderValue } from './SelectProvider';
|
||||
import { MuiCancellableEventHandler } from '../utils/MuiCancellableEvent';
|
||||
import { UseButtonRootSlotProps } from '../useButton';
|
||||
export type SelectChangeEventType = React.MouseEvent<Element, MouseEvent> | React.KeyboardEvent<Element> | React.FocusEvent<Element, Element> | null;
|
||||
export type SelectValue<Value, Multiple> = Multiple extends true ? Value[] : Value | null;
|
||||
export interface SelectOptionDefinition<Value> {
|
||||
value: Value;
|
||||
disabled?: boolean;
|
||||
label: string;
|
||||
}
|
||||
export interface UseSelectParameters<OptionValue, Multiple extends boolean = false> {
|
||||
/**
|
||||
* A function used to determine if two options' values are equal.
|
||||
* By default, reference equality is used.
|
||||
*
|
||||
* There is a performance impact when using the `areOptionsEqual` prop (proportional to the number of options).
|
||||
* Therefore, it's recommented to use the default reference equality comparison whenever possible.
|
||||
*/
|
||||
areOptionsEqual?: (a: OptionValue, b: OptionValue) => boolean;
|
||||
/**
|
||||
* If `true`, the select will be open by default.
|
||||
* @default false
|
||||
*/
|
||||
defaultOpen?: boolean;
|
||||
/**
|
||||
* The default selected value. Use when the component is not controlled.
|
||||
*/
|
||||
defaultValue?: SelectValue<OptionValue, Multiple>;
|
||||
/**
|
||||
* If `true`, the select is disabled.
|
||||
* @default false
|
||||
*/
|
||||
disabled?: boolean;
|
||||
/**
|
||||
* The ref of the trigger button element.
|
||||
*/
|
||||
buttonRef?: React.Ref<Element>;
|
||||
/**
|
||||
* The `id` attribute of the listbox element.
|
||||
*/
|
||||
listboxId?: string;
|
||||
/**
|
||||
* The ref of the listbox element.
|
||||
*/
|
||||
listboxRef?: React.Ref<Element>;
|
||||
/**
|
||||
* If `true`, the end user can select multiple values.
|
||||
* This affects the type of the `value`, `defaultValue`, and `onChange` props.
|
||||
*
|
||||
* @default false
|
||||
*/
|
||||
multiple?: Multiple;
|
||||
/**
|
||||
* The `name` attribute of the hidden input element.
|
||||
* This is useful when the select is embedded in a form and you want to access the selected value in the form data.
|
||||
*/
|
||||
name?: string;
|
||||
/**
|
||||
* If `true`, the select embedded in a form must have a selected value.
|
||||
* Otherwise, the form submission will fail.
|
||||
*/
|
||||
required?: boolean;
|
||||
/**
|
||||
* Callback fired when an option is selected.
|
||||
*/
|
||||
onChange?: (event: React.MouseEvent | React.KeyboardEvent | React.FocusEvent | null, value: SelectValue<OptionValue, Multiple>) => void;
|
||||
/**
|
||||
* Callback fired when an option is highlighted.
|
||||
*/
|
||||
onHighlightChange?: (event: React.MouseEvent<Element, MouseEvent> | React.KeyboardEvent<Element> | React.FocusEvent<Element, Element> | null, highlighted: OptionValue | null) => void;
|
||||
/**
|
||||
* Callback fired when the listbox is opened or closed.
|
||||
*/
|
||||
onOpenChange?: (open: boolean) => void;
|
||||
/**
|
||||
* Controls the open state of the select's listbox.
|
||||
* This is the controlled equivalent of the `defaultOpen` prop.
|
||||
*/
|
||||
open?: boolean;
|
||||
/**
|
||||
* An alternative way to specify the options.
|
||||
* If this parameter is set, options defined as JSX children are ignored.
|
||||
*/
|
||||
options?: ReadonlyArray<SelectOptionDefinition<OptionValue>>;
|
||||
/**
|
||||
* A function to convert the currently selected value to a string.
|
||||
* Used to set a value of a hidden input associated with the select,
|
||||
* so that the selected value can be posted with a form.
|
||||
*/
|
||||
getSerializedValue?: (option: SelectValue<SelectOption<OptionValue>, Multiple>) => React.InputHTMLAttributes<HTMLInputElement>['value'];
|
||||
/**
|
||||
* A function used to convert the option label to a string.
|
||||
* This is useful when labels are elements and need to be converted to plain text
|
||||
* to enable keyboard navigation with character keys.
|
||||
*
|
||||
* @default defaultOptionStringifier
|
||||
*/
|
||||
getOptionAsString?: (option: SelectOption<OptionValue>) => string;
|
||||
/**
|
||||
* The selected value.
|
||||
* Set to `null` to deselect all options.
|
||||
*/
|
||||
value?: SelectValue<OptionValue, Multiple>;
|
||||
/**
|
||||
* The name of the component using useSelect.
|
||||
* For debugging purposes.
|
||||
* @default 'useSelect'
|
||||
*/
|
||||
componentName?: string;
|
||||
}
|
||||
interface UseSelectButtonSlotEventHandlers {
|
||||
onMouseDown: MuiCancellableEventHandler<React.MouseEvent>;
|
||||
}
|
||||
export type UseSelectButtonSlotProps<TOther = {}> = UseButtonRootSlotProps<Omit<TOther, keyof UseSelectButtonSlotEventHandlers>> & UseSelectButtonSlotEventHandlers & {
|
||||
'aria-expanded': React.AriaAttributes['aria-expanded'];
|
||||
'aria-controls': React.AriaAttributes['aria-controls'];
|
||||
role: React.HTMLAttributes<Element>['role'];
|
||||
ref: React.RefCallback<Element> | null;
|
||||
};
|
||||
interface UseSelectHiddenInputSlotEventHandlers {
|
||||
onChange: MuiCancellableEventHandler<React.ChangeEvent<HTMLInputElement>>;
|
||||
}
|
||||
export type UseSelectHiddenInputSlotProps<TOther = {}> = UseSelectHiddenInputSlotEventHandlers & React.InputHTMLAttributes<HTMLInputElement> & TOther;
|
||||
interface UseSelectListboxSlotEventHandlers {
|
||||
onBlur: MuiCancellableEventHandler<React.FocusEvent<HTMLElement>>;
|
||||
}
|
||||
export type UseSelectListboxSlotProps<TOther = {}> = UseListRootSlotProps<Omit<TOther, keyof UseSelectListboxSlotEventHandlers>> & UseSelectListboxSlotEventHandlers & {
|
||||
'aria-multiselectable': React.AriaAttributes['aria-multiselectable'];
|
||||
id: string | undefined;
|
||||
role: React.HTMLAttributes<Element>['role'];
|
||||
};
|
||||
export interface UseSelectReturnValue<Value, Multiple> {
|
||||
/**
|
||||
* If `true`, the trigger button is active (pressed).
|
||||
*/
|
||||
buttonActive: boolean;
|
||||
/**
|
||||
* If `true`, the trigger button has a visible focus.
|
||||
*/
|
||||
buttonFocusVisible: boolean;
|
||||
/**
|
||||
* Ref to the button slot DOM node.
|
||||
*/
|
||||
buttonRef: React.RefCallback<Element> | null;
|
||||
/**
|
||||
* If `true`, the select is disabled.
|
||||
*/
|
||||
disabled: boolean;
|
||||
/**
|
||||
* Action dispatcher for the select component.
|
||||
* Allows to programmatically control the select.
|
||||
*/
|
||||
dispatch: (action: ListAction<Value> | SelectAction<Value>) => void;
|
||||
/**
|
||||
* Resolver for the button slot's props.
|
||||
* @param externalProps event handlers for the button slot
|
||||
* @returns props that should be spread on the button slot
|
||||
*/
|
||||
getButtonProps: <ExternalProps extends Record<string, unknown> = {}>(externalProps?: ExternalProps) => UseSelectButtonSlotProps<ExternalProps>;
|
||||
/**
|
||||
* Resolver for the hidden input slot's props.
|
||||
* @param externalProps event handlers for the hidden input slot
|
||||
* @returns HTML input attributes that should be spread on the hidden input slot
|
||||
*/
|
||||
getHiddenInputProps: <ExternalProps extends Record<string, unknown> = {}>(externalProps?: ExternalProps) => UseSelectHiddenInputSlotProps<ExternalProps>;
|
||||
/**
|
||||
* Resolver for the listbox slot's props.
|
||||
* @param externalProps event handlers for the listbox slot
|
||||
* @returns props that should be spread on the listbox slot
|
||||
*/
|
||||
getListboxProps: <ExternalProps extends Record<string, unknown> = {}>(externalProps?: ExternalProps) => UseSelectListboxSlotProps<ExternalProps>;
|
||||
/**
|
||||
* A function that returns the metadata of an option with a given value.
|
||||
*
|
||||
* @param optionValue the value of the option
|
||||
* @returns
|
||||
*/
|
||||
getOptionMetadata: (optionValue: Value) => SelectOption<Value> | undefined;
|
||||
/**
|
||||
* A value to be passed to the `SelectProvider` component.
|
||||
*/
|
||||
contextValue: SelectProviderValue<Value>;
|
||||
/**
|
||||
* The value of the highlighted option.
|
||||
*/
|
||||
highlightedOption: Value | null;
|
||||
/**
|
||||
* Ref to the listbox slot DOM node.
|
||||
*/
|
||||
listboxRef: React.RefCallback<Element> | null;
|
||||
/**
|
||||
* If `true`, the listbox is open.
|
||||
*/
|
||||
open: boolean;
|
||||
/**
|
||||
* Values of all the registered options.
|
||||
*/
|
||||
options: Value[];
|
||||
/**
|
||||
* The value of the selected option(s).
|
||||
*/
|
||||
value: SelectValue<Value, Multiple>;
|
||||
}
|
||||
export declare const SelectActionTypes: {
|
||||
readonly buttonClick: "buttonClick";
|
||||
readonly browserAutoFill: "browserAutoFill";
|
||||
};
|
||||
export interface ButtonClickAction {
|
||||
type: typeof SelectActionTypes.buttonClick;
|
||||
event: React.MouseEvent;
|
||||
}
|
||||
export interface BrowserAutofillAction<OptionValue> {
|
||||
type: typeof SelectActionTypes.browserAutoFill;
|
||||
item: OptionValue;
|
||||
event: React.ChangeEvent;
|
||||
}
|
||||
export type SelectAction<OptionValue> = ButtonClickAction | BrowserAutofillAction<OptionValue>;
|
||||
export interface SelectInternalState<OptionValue> extends ListState<OptionValue> {
|
||||
open: boolean;
|
||||
}
|
||||
export {};
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
export const SelectActionTypes = {
|
||||
buttonClick: 'buttonClick',
|
||||
browserAutoFill: 'browserAutoFill'
|
||||
};
|
||||
Reference in new issue
Block a user