Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F99837471
SelectUnstyled.js
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Sun, Jan 26, 21:36
Size
9 KB
Mime Type
text/x-java
Expires
Tue, Jan 28, 21:36 (1 d, 23 h)
Engine
blob
Format
Raw Data
Handle
23831541
Attached To
rOACCT Open Access Compliance Check Tool (OACCT)
SelectUnstyled.js
View Options
import _extends from "@babel/runtime/helpers/esm/extends";
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
const _excluded = ["autoFocus", "children", "component", "defaultValue", "defaultListboxOpen", "disabled", "getSerializedValue", "listboxId", "listboxOpen", "name", "onChange", "onListboxOpenChange", "optionStringifier", "renderValue", "slotProps", "slots", "value"];
import * as React from 'react';
import PropTypes from 'prop-types';
import { unstable_useForkRef as useForkRef, unstable_useControlled as useControlled } from '@mui/utils';
import { flattenOptionGroups, getOptionsFromChildren } from './utils';
import useSelect from './useSelect';
import { useSlotProps } from '../utils';
import PopperUnstyled from '../PopperUnstyled';
import { SelectUnstyledContext } from './SelectUnstyledContext';
import composeClasses from '../composeClasses';
import { getSelectUnstyledUtilityClass } from './selectUnstyledClasses';
import defaultOptionStringifier from './defaultOptionStringifier';
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
function defaultRenderSingleValue(selectedOption) {
return selectedOption?.label ?? '';
}
function defaultFormValueProvider(selectedOption) {
if (selectedOption?.value == null) {
return '';
}
if (typeof selectedOption.value === 'string' || typeof selectedOption.value === 'number') {
return selectedOption.value;
}
return JSON.stringify(selectedOption.value);
}
function useUtilityClasses(ownerState) {
const {
active,
disabled,
open,
focusVisible
} = ownerState;
const slots = {
root: ['root', disabled && 'disabled', focusVisible && 'focusVisible', active && 'active', open && 'expanded'],
listbox: ['listbox', disabled && 'disabled'],
popper: ['popper']
};
return composeClasses(slots, getSelectUnstyledUtilityClass, {});
}
/**
* The foundation for building custom-styled select components.
*
* Demos:
*
* - [Unstyled Select](https://mui.com/base/react-select/)
*
* API:
*
* - [SelectUnstyled API](https://mui.com/base/api/select-unstyled/)
*/
const SelectUnstyled = /*#__PURE__*/React.forwardRef(function SelectUnstyled(props, forwardedRef) {
const {
autoFocus,
children,
component,
defaultValue,
defaultListboxOpen = false,
disabled: disabledProp,
getSerializedValue = defaultFormValueProvider,
listboxId,
listboxOpen: listboxOpenProp,
name,
onChange,
onListboxOpenChange,
optionStringifier = defaultOptionStringifier,
renderValue: renderValueProp,
slotProps = {},
slots = {},
value: valueProp
} = props,
other = _objectWithoutPropertiesLoose(props, _excluded);
const renderValue = renderValueProp ?? defaultRenderSingleValue;
const [groupedOptions, setGroupedOptions] = React.useState([]);
const options = React.useMemo(() => flattenOptionGroups(groupedOptions), [groupedOptions]);
const [listboxOpen, setListboxOpen] = useControlled({
controlled: listboxOpenProp,
default: defaultListboxOpen,
name: 'SelectUnstyled',
state: 'listboxOpen'
});
React.useEffect(() => {
setGroupedOptions(getOptionsFromChildren(children));
}, [children]);
const [buttonDefined, setButtonDefined] = React.useState(false);
const buttonRef = React.useRef(null);
const listboxRef = React.useRef(null);
const Button = component ?? slots.root ?? 'button';
const ListboxRoot = slots.listbox ?? 'ul';
const Popper = slots.popper ?? PopperUnstyled;
const handleButtonRefChange = React.useCallback(element => {
setButtonDefined(element != null);
}, []);
const handleButtonRef = useForkRef(forwardedRef, buttonRef, handleButtonRefChange);
React.useEffect(() => {
if (autoFocus) {
buttonRef.current.focus();
}
}, [autoFocus]);
const handleOpenChange = isOpen => {
setListboxOpen(isOpen);
onListboxOpenChange?.(isOpen);
};
const {
buttonActive,
buttonFocusVisible,
disabled,
getButtonProps,
getListboxProps,
getOptionProps,
getOptionState,
value
} = useSelect({
buttonRef: handleButtonRef,
defaultValue,
disabled: disabledProp,
listboxId,
multiple: false,
onChange,
onOpenChange: handleOpenChange,
open: listboxOpen,
options,
optionStringifier,
value: valueProp
});
const ownerState = _extends({}, props, {
active: buttonActive,
defaultListboxOpen,
disabled,
focusVisible: buttonFocusVisible,
open: listboxOpen,
renderValue,
value
});
const classes = useUtilityClasses(ownerState);
const selectedOption = React.useMemo(() => {
return options.find(o => value === o.value) ?? null;
}, [options, value]);
const buttonProps = useSlotProps({
elementType: Button,
getSlotProps: getButtonProps,
externalSlotProps: slotProps.root,
externalForwardedProps: other,
ownerState,
className: classes.root
});
const listboxProps = useSlotProps({
elementType: ListboxRoot,
getSlotProps: getListboxProps,
externalSlotProps: slotProps.listbox,
additionalProps: {
ref: listboxRef
},
ownerState,
className: classes.listbox
});
const popperProps = useSlotProps({
elementType: Popper,
externalSlotProps: slotProps.popper,
additionalProps: {
anchorEl: buttonRef.current,
disablePortal: true,
open: listboxOpen,
placement: 'bottom-start',
role: undefined
},
ownerState,
className: classes.popper
});
const context = React.useMemo(() => ({
getOptionProps,
getOptionState,
listboxRef
}), [getOptionProps, getOptionState]);
return /*#__PURE__*/_jsxs(React.Fragment, {
children: [/*#__PURE__*/_jsx(Button, _extends({}, buttonProps, {
children: renderValue(selectedOption)
})), buttonDefined && /*#__PURE__*/_jsx(Popper, _extends({}, popperProps, {
children: /*#__PURE__*/_jsx(ListboxRoot, _extends({}, listboxProps, {
children: /*#__PURE__*/_jsx(SelectUnstyledContext.Provider, {
value: context,
children: children
})
}))
})), name && /*#__PURE__*/_jsx("input", {
type: "hidden",
name: name,
value: getSerializedValue(selectedOption)
})]
});
});
process.env.NODE_ENV !== "production" ? SelectUnstyled.propTypes /* remove-proptypes */ = {
// ----------------------------- Warning --------------------------------
// | These PropTypes are generated from the TypeScript type definitions |
// | To update them edit TypeScript types and run "yarn proptypes" |
// ----------------------------------------------------------------------
/**
* If `true`, the select element is focused during the first mount
* @default false
*/
autoFocus: PropTypes.bool,
/**
* @ignore
*/
children: PropTypes.node,
/**
* The component used for the root node.
* Either a string to use a HTML element or a component.
*/
component: PropTypes.elementType,
/**
* If `true`, the select will be initially open.
* @default false
*/
defaultListboxOpen: PropTypes.bool,
/**
* The default selected value. Use when the component is not controlled.
*/
defaultValue: PropTypes.any,
/**
* If `true`, the select is disabled.
* @default false
*/
disabled: PropTypes.bool,
/**
* 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: PropTypes.func,
/**
* `id` attribute of the listbox element.
* Also used to derive the `id` attributes of options.
*/
listboxId: PropTypes.string,
/**
* Controls the open state of the select's listbox.
* @default undefined
*/
listboxOpen: PropTypes.bool,
/**
* Name of the element. For example used by the server to identify the fields in form submits.
* If the name is provided, the component will render a hidden input element that can be submitted to a server.
*/
name: PropTypes.string,
/**
* Callback fired when an option is selected.
*/
onChange: PropTypes.func,
/**
* Callback fired when the component requests to be opened.
* Use in controlled mode (see listboxOpen).
*/
onListboxOpenChange: PropTypes.func,
/**
* A function used to convert the option label to a string.
* It's useful when labels are elements and need to be converted to plain text
* to enable navigation using character keys on a keyboard.
*
* @default defaultOptionStringifier
*/
optionStringifier: PropTypes.func,
/**
* Function that customizes the rendering of the selected value.
*/
renderValue: PropTypes.func,
/**
* The props used for each slot inside the Input.
* @default {}
*/
slotProps: PropTypes.shape({
listbox: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
popper: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
root: PropTypes.oneOfType([PropTypes.func, PropTypes.object])
}),
/**
* The components used for each slot inside the Select.
* Either a string to use a HTML element or a component.
* @default {}
*/
slots: PropTypes /* @typescript-to-proptypes-ignore */.shape({
listbox: PropTypes.elementType,
popper: PropTypes.elementType,
root: PropTypes.elementType
}),
/**
* The selected value.
* Set to `null` to deselect all options.
*/
value: PropTypes.any
} : void 0;
export default SelectUnstyled;
Event Timeline
Log In to Comment