@mui/x-data-grid
Version:
The Community plan edition of the Data Grid components (MUI X).
116 lines (115 loc) • 4.56 kB
JavaScript
import _extends from "@babel/runtime/helpers/esm/extends";
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
const _excluded = ["item", "applyValue", "type", "apiRef", "focusElementRef", "slotProps"];
import * as React from 'react';
import PropTypes from 'prop-types';
import { unstable_useId as useId } from '@mui/utils';
import { useGridRootProps } from "../../../hooks/utils/useGridRootProps.js";
import { getValueOptions, isSingleSelectColDef } from "./filterPanelUtils.js";
import { jsx as _jsx } from "react/jsx-runtime";
function GridFilterInputMultipleSingleSelect(props) {
const {
item,
applyValue,
type,
apiRef,
focusElementRef,
slotProps
} = props,
other = _objectWithoutPropertiesLoose(props, _excluded);
const id = useId();
const rootProps = useGridRootProps();
let resolvedColumn = null;
if (item.field) {
const column = apiRef.current.getColumn(item.field);
if (isSingleSelectColDef(column)) {
resolvedColumn = column;
}
}
const getOptionValue = resolvedColumn?.getOptionValue;
const getOptionLabel = resolvedColumn?.getOptionLabel;
const isOptionEqualToValue = React.useCallback((option, value) => getOptionValue(option) === getOptionValue(value), [getOptionValue]);
const resolvedValueOptions = React.useMemo(() => {
return getValueOptions(resolvedColumn) || [];
}, [resolvedColumn]);
// The value is computed from the item.value and used directly
// If it was done by a useEffect/useState, the Autocomplete could receive incoherent value and options
const filteredValues = React.useMemo(() => {
if (!Array.isArray(item.value)) {
return [];
}
return item.value.reduce((acc, value) => {
const resolvedValue = resolvedValueOptions.find(v => getOptionValue(v) === value);
if (resolvedValue != null) {
acc.push(resolvedValue);
}
return acc;
}, []);
}, [getOptionValue, item.value, resolvedValueOptions]);
const handleChange = React.useCallback((event, value) => {
applyValue(_extends({}, item, {
value: value.map(getOptionValue)
}));
}, [applyValue, item, getOptionValue]);
const BaseAutocomplete = rootProps.slots.baseAutocomplete;
return /*#__PURE__*/_jsx(BaseAutocomplete, _extends({
multiple: true,
options: resolvedValueOptions,
isOptionEqualToValue: isOptionEqualToValue,
id: id,
value: filteredValues,
onChange: handleChange,
getOptionLabel: getOptionLabel,
label: apiRef.current.getLocaleText('filterPanelInputLabel'),
placeholder: apiRef.current.getLocaleText('filterPanelInputPlaceholder'),
slotProps: {
textField: {
type: type || 'text',
inputRef: focusElementRef
}
}
}, other, slotProps?.root));
}
process.env.NODE_ENV !== "production" ? GridFilterInputMultipleSingleSelect.propTypes = {
// ----------------------------- Warning --------------------------------
// | These PropTypes are generated from the TypeScript type definitions |
// | To update them edit the TypeScript types and run "pnpm proptypes" |
// ----------------------------------------------------------------------
apiRef: PropTypes.shape({
current: PropTypes.object.isRequired
}).isRequired,
applyValue: PropTypes.func.isRequired,
className: PropTypes.string,
clearButton: PropTypes.node,
disabled: PropTypes.bool,
focusElementRef: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([PropTypes.func, PropTypes.object]),
headerFilterMenu: PropTypes.node,
inputRef: PropTypes.oneOfType([PropTypes.func, PropTypes.shape({
current: (props, propName) => {
if (props[propName] == null) {
return null;
}
if (typeof props[propName] !== 'object' || props[propName].nodeType !== 1) {
return new Error(`Expected prop '${propName}' to be of type Element`);
}
return null;
}
})]),
/**
* It is `true` if the filter either has a value or an operator with no value
* required is selected (for example `isEmpty`)
*/
isFilterActive: PropTypes.bool,
item: PropTypes.shape({
field: PropTypes.string.isRequired,
id: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
operator: PropTypes.string.isRequired,
value: PropTypes.any
}).isRequired,
onBlur: PropTypes.func,
onFocus: PropTypes.func,
slotProps: PropTypes.object,
tabIndex: PropTypes.number,
type: PropTypes.oneOf(['singleSelect'])
} : void 0;
export { GridFilterInputMultipleSingleSelect };