pre-installations and preparations

This commit is contained in:
MuhammadKhalilzadeh committed 2024-05-05 11:21:00 +03:30
1 parent 3e5a0329cb
commit 201d621ac3
6977 files changed
+742826

No files matched your search

+3
View File
@@ -0,0 +1,3 @@
export { useOption } from './useOption';
export * from './useOption.types';
export * from './useOptionContextStabilizer';
+5
View File
@@ -0,0 +1,5 @@
'use client';
export { useOption } from './useOption';
export * from './useOption.types';
export * from './useOptionContextStabilizer';
+6
View File
@@ -0,0 +1,6 @@
{
"sideEffects": false,
"module": "./index.js",
"main": "../node/useOption/index.js",
"types": "./index.d.ts"
}
+12
View File
@@ -0,0 +1,12 @@
import { UseOptionParameters, UseOptionReturnValue } from './useOption.types';
/**
*
* Demos:
*
* - [Select](https://mui.com/base-ui/react-select/#hooks)
*
* API:
*
* - [useOption API](https://mui.com/base-ui/react-select/hooks-api/#use-option)
*/
export declare function useOption<Value>(params: UseOptionParameters<Value>): UseOptionReturnValue;
+85
View File
@@ -0,0 +1,85 @@
'use client';
import _extends from "@babel/runtime/helpers/esm/extends";
import * as React from 'react';
import { unstable_useForkRef as useForkRef, unstable_useId as useId } from '@mui/utils';
import { extractEventHandlers } from '../utils/extractEventHandlers';
import { useListItem } from '../useList';
import { useCompoundItem } from '../useCompound';
import { useButton } from '../useButton';
import { combineHooksSlotProps } from '../utils/combineHooksSlotProps';
/**
*
* Demos:
*
* - [Select](https://mui.com/base-ui/react-select/#hooks)
*
* API:
*
* - [useOption API](https://mui.com/base-ui/react-select/hooks-api/#use-option)
*/
export function useOption(params) {
const {
value,
label,
disabled,
rootRef: optionRefParam,
id: idParam
} = params;
const {
getRootProps: getListItemProps,
highlighted,
selected
} = useListItem({
item: value
});
const {
getRootProps: getButtonProps,
rootRef: buttonRefHandler
} = useButton({
disabled,
focusableWhenDisabled: true
});
const id = useId(idParam);
const optionRef = React.useRef(null);
const selectOption = React.useMemo(() => ({
disabled,
label,
value,
ref: optionRef,
id
}), [disabled, label, value, id]);
const {
index
} = useCompoundItem(value, selectOption);
const handleRef = useForkRef(optionRefParam, optionRef, buttonRefHandler);
const createHandleKeyDown = otherHandlers => event => {
var _otherHandlers$onKeyD;
(_otherHandlers$onKeyD = otherHandlers.onKeyDown) == null || _otherHandlers$onKeyD.call(otherHandlers, event);
if (event.defaultMuiPrevented) {
return;
}
if ([' ', 'Enter'].includes(event.key)) {
event.defaultMuiPrevented = true; // prevent listbox onKeyDown
}
};
const getOwnHandlers = (otherHandlers = {}) => ({
onKeyDown: createHandleKeyDown(otherHandlers)
});
return {
getRootProps: (externalProps = {}) => {
const externalEventHandlers = extractEventHandlers(externalProps);
const getCombinedRootProps = combineHooksSlotProps(getListItemProps, combineHooksSlotProps(getButtonProps, getOwnHandlers));
return _extends({}, externalProps, externalEventHandlers, getCombinedRootProps(externalEventHandlers), {
id,
ref: handleRef,
role: 'option',
'aria-selected': selected
});
},
highlighted,
index,
selected,
rootRef: handleRef
};
}
+40
View File
@@ -0,0 +1,40 @@
/// <reference types="react" />
import { UseListItemRootSlotProps } from '../useList';
export interface SelectOption<Value> {
value: Value;
label: React.ReactNode;
disabled?: boolean;
ref: React.RefObject<HTMLElement>;
id?: string;
}
export interface UseOptionParameters<Value> {
disabled: boolean;
id?: string;
label: string | React.ReactNode;
rootRef?: React.Ref<Element>;
value: Value;
}
export interface UseOptionReturnValue {
/**
* If `true`, the option is selected.
*/
selected: boolean;
/**
* If `true`, the option is highlighted.
*/
highlighted: boolean;
index: number;
/**
* Resolver for the root slot's props.
* @param externalProps props for the root slot
* @returns props that should be spread on the root slot
*/
getRootProps: <ExternalProps extends Record<string, unknown>>(externalProps?: ExternalProps) => UseOptionRootSlotProps<ExternalProps>;
/**
* Ref to the root slot DOM node.
*/
rootRef: React.RefCallback<Element> | null;
}
export type UseOptionRootSlotProps<ExternalProps extends Record<string, unknown> = {}> = UseListItemRootSlotProps<ExternalProps> & {
ref?: React.RefCallback<Element> | null;
} & ExternalProps;
+1
View File
@@ -0,0 +1 @@
export {};
+24
View File
@@ -0,0 +1,24 @@
/**
* Stabilizes the ListContext value for the Option component, so it doesn't change when sibling Options update.
*
* @param value The value of the Option.
* @returns The stable ListContext value.
*
* Demos:
*
* - [Select](https://mui.com/base-ui/react-select/#hooks)
*
* API:
*
* - [useOptionContextStabilizer API](https://mui.com/base-ui/react-select/hooks-api/#use-option-context-stabilizer)
*/
export declare function useOptionContextStabilizer<OptionValue>(value: OptionValue): {
contextValue: {
dispatch: (action: import("../useList").ListAction<OptionValue>) => void;
getItemState: (itemValue: OptionValue) => {
highlighted: boolean;
selected: boolean;
focusable: boolean;
};
};
};
+58
View File
@@ -0,0 +1,58 @@
'use client';
import * as React from 'react';
import { ListContext } from '../useList';
/**
* Stabilizes the ListContext value for the Option component, so it doesn't change when sibling Options update.
*
* @param value The value of the Option.
* @returns The stable ListContext value.
*
* Demos:
*
* - [Select](https://mui.com/base-ui/react-select/#hooks)
*
* API:
*
* - [useOptionContextStabilizer API](https://mui.com/base-ui/react-select/hooks-api/#use-option-context-stabilizer)
*/
export function useOptionContextStabilizer(value) {
const listContext = React.useContext(ListContext);
if (!listContext) {
throw new Error('Option: ListContext was not found.');
}
const {
getItemState,
dispatch
} = listContext;
const {
highlighted,
selected,
focusable
} = getItemState(value);
// The local version of getItemState can be only called with the current Option's value.
// It doesn't make much sense to render an Option depending on other Options' state anyway.
const localGetItemState = React.useCallback(itemValue => {
if (itemValue !== value) {
throw new Error(['Base UI Option: Tried to access the state of another Option.', 'This is unsupported when the Option uses the OptionContextStabilizer as a performance optimization.'].join('/n'));
}
return {
highlighted,
selected,
focusable
};
}, [highlighted, selected, focusable, value]);
// Create a local (per Option) instance of the ListContext that changes only when
// the getItemState's return value changes.
// This makes Options re-render only when their state actually change, not when any Option's state changes.
const localContextValue = React.useMemo(() => ({
dispatch,
getItemState: localGetItemState
}), [dispatch, localGetItemState]);
return {
contextValue: localContextValue
};
}