UNPKG

@mui/x-data-grid

Version:

The community edition of the data grid component (MUI X).

147 lines (135 loc) 5.87 kB
import _extends from "@babel/runtime/helpers/esm/extends"; import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose"; const _excluded = ["item", "applyValue", "type", "apiRef", "focusElementRef"]; import * as React from 'react'; import PropTypes from 'prop-types'; import { unstable_useId as useId } from '@mui/material/utils'; import MenuItem from '@mui/material/MenuItem'; import { GridLoadIcon } from '../../icons'; import { useGridRootProps } from '../../../hooks/utils/useGridRootProps'; import { getValueFromValueOptions } from './filterPanelUtils'; import { jsx as _jsx } from "react/jsx-runtime"; const warnedOnce = {}; function warnDeprecatedTypeSupport(type) { console.warn([`MUI: Using GridFilterInputValue with a "${type}" column is deprecated.`, 'Use GridFilterInputSingleSelect instead.'].join('\n')); warnedOnce[type] = true; } const renderSingleSelectOptions = ({ valueOptions, valueFormatter, field }, api, OptionComponent) => { const iterableColumnValues = typeof valueOptions === 'function' ? ['', ...valueOptions({ field })] : ['', ...(valueOptions || [])]; return iterableColumnValues.map(option => { const isOptionTypeObject = typeof option === 'object'; const key = isOptionTypeObject ? option.value : option; const value = isOptionTypeObject ? option.value : option; const formattedValue = valueFormatter && option !== '' ? valueFormatter({ value: option, field, api }) : option; const content = isOptionTypeObject ? option.label : formattedValue; return /*#__PURE__*/_jsx(OptionComponent, { value: value, children: content }, key); }); }; export const SUBMIT_FILTER_STROKE_TIME = 500; function GridFilterInputValue(props) { var _item$value, _rootProps$components, _baseSelectProps$nati, _rootProps$components2, _rootProps$components3; const { item, applyValue, type, apiRef, focusElementRef } = props, others = _objectWithoutPropertiesLoose(props, _excluded); if (process.env.NODE_ENV !== 'production' && ['date', 'datetime-local', 'singleSelect'].includes(type) && !warnedOnce[type]) { warnDeprecatedTypeSupport(type); } const filterTimeout = React.useRef(); const [filterValueState, setFilterValueState] = React.useState((_item$value = item.value) != null ? _item$value : ''); const [applying, setIsApplying] = React.useState(false); const id = useId(); const rootProps = useGridRootProps(); const baseSelectProps = ((_rootProps$components = rootProps.componentsProps) == null ? void 0 : _rootProps$components.baseSelect) || {}; const isSelectNative = (_baseSelectProps$nati = baseSelectProps.native) != null ? _baseSelectProps$nati : true; const singleSelectProps = type === 'singleSelect' ? { select: true, SelectProps: _extends({ native: isSelectNative }, (_rootProps$components2 = rootProps.componentsProps) == null ? void 0 : _rootProps$components2.baseSelect), children: renderSingleSelectOptions(apiRef.current.getColumn(item.columnField), apiRef.current, isSelectNative ? 'option' : MenuItem) } : {}; const onFilterChange = React.useCallback(event => { let value = event.target.value; // NativeSelect casts the value to a string. if (type === 'singleSelect') { const column = apiRef.current.getColumn(item.columnField); const columnValueOptions = typeof column.valueOptions === 'function' ? column.valueOptions({ field: column.field }) : column.valueOptions; value = getValueFromValueOptions(value, columnValueOptions); } clearTimeout(filterTimeout.current); setFilterValueState(String(value)); setIsApplying(true); // TODO singleSelect doesn't debounce filterTimeout.current = setTimeout(() => { applyValue(_extends({}, item, { value })); setIsApplying(false); }, SUBMIT_FILTER_STROKE_TIME); }, [apiRef, applyValue, item, type]); React.useEffect(() => { return () => { clearTimeout(filterTimeout.current); }; }, []); React.useEffect(() => { var _item$value2; const itemValue = (_item$value2 = item.value) != null ? _item$value2 : ''; setFilterValueState(String(itemValue)); }, [item.value]); const InputProps = applying ? { endAdornment: /*#__PURE__*/_jsx(GridLoadIcon, {}) } : others.InputProps; return /*#__PURE__*/_jsx(rootProps.components.BaseTextField, _extends({ id: id, label: apiRef.current.getLocaleText('filterPanelInputLabel'), placeholder: apiRef.current.getLocaleText('filterPanelInputPlaceholder'), value: filterValueState, onChange: onFilterChange, variant: "standard", type: type || 'text', InputProps: InputProps, InputLabelProps: { shrink: true }, inputRef: focusElementRef }, singleSelectProps, others, (_rootProps$components3 = rootProps.componentsProps) == null ? void 0 : _rootProps$components3.baseTextField)); } process.env.NODE_ENV !== "production" ? GridFilterInputValue.propTypes = { // ----------------------------- Warning -------------------------------- // | These PropTypes are generated from the TypeScript type definitions | // | To update them edit the TypeScript types and run "yarn proptypes" | // ---------------------------------------------------------------------- apiRef: PropTypes.shape({ current: PropTypes.object.isRequired }).isRequired, applyValue: PropTypes.func.isRequired, focusElementRef: PropTypes /* @typescript-to-proptypes-ignore */ .oneOfType([PropTypes.func, PropTypes.object]), item: PropTypes.shape({ columnField: PropTypes.string.isRequired, id: PropTypes.oneOfType([PropTypes.number, PropTypes.string]), operatorValue: PropTypes.string, value: PropTypes.any }).isRequired } : void 0; export { GridFilterInputValue };