UNPKG

@mui/x-data-grid

Version:

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

166 lines (137 loc) 7.71 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.GridToolbarQuickFilter = GridToolbarQuickFilter; var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose")); var React = _interopRequireWildcard(require("react")); var _propTypes = _interopRequireDefault(require("prop-types")); var _TextField = _interopRequireDefault(require("@mui/material/TextField")); var _IconButton = _interopRequireDefault(require("@mui/material/IconButton")); var _styles = require("@mui/material/styles"); var _utils = require("@mui/material/utils"); var _useGridApiContext = require("../../hooks/utils/useGridApiContext"); var _useGridRootProps = require("../../hooks/utils/useGridRootProps"); var _useGridSelector = require("../../hooks/utils/useGridSelector"); var _filter = require("../../hooks/features/filter"); var _utils2 = require("../../utils/utils"); var _jsxRuntime = require("react/jsx-runtime"); const _excluded = ["quickFilterParser", "quickFilterFormatter", "debounceMs"]; function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } const GridToolbarQuickFilterRoot = (0, _styles.styled)(_TextField.default, { name: 'MuiDataGrid', slot: 'ToolbarQuickFilter', overridesResolver: (props, styles) => styles.toolbarQuickFilter })(({ theme }) => ({ width: 'auto', paddingBottom: theme.spacing(0.5), '& input': { marginLeft: theme.spacing(0.5) }, '& .MuiInput-underline:before': { borderBottom: `1px solid ${theme.palette.divider}` }, [`& input[type=search]::-ms-clear, & input[type=search]::-ms-reveal`]: { /* clears the 'X' icon from IE */ display: 'none', width: 0, height: 0 }, [`& input[type="search"]::-webkit-search-decoration, & input[type="search"]::-webkit-search-cancel-button, & input[type="search"]::-webkit-search-results-button, & input[type="search"]::-webkit-search-results-decoration`]: { /* clears the 'X' icon from Chrome */ display: 'none' } })); const defaultSearchValueParser = searchText => searchText.split(' ').filter(word => word !== ''); const defaultSearchValueFormatter = values => values.join(' '); function GridToolbarQuickFilter(props) { var _rootProps$components; const { quickFilterParser = defaultSearchValueParser, quickFilterFormatter = defaultSearchValueFormatter, debounceMs = 500 } = props, other = (0, _objectWithoutPropertiesLoose2.default)(props, _excluded); const apiRef = (0, _useGridApiContext.useGridApiContext)(); const rootProps = (0, _useGridRootProps.useGridRootProps)(); const quickFilterValues = (0, _useGridSelector.useGridSelector)(apiRef, _filter.gridQuickFilterValuesSelector); const [searchValue, setSearchValue] = React.useState(() => quickFilterFormatter(quickFilterValues != null ? quickFilterValues : [])); const [prevQuickFilterValues, setPrevQuickFilterValues] = React.useState(quickFilterValues); React.useEffect(() => { if (!(0, _utils2.isDeepEqual)(prevQuickFilterValues, quickFilterValues)) { // The model of quick filter value has been updated setPrevQuickFilterValues(quickFilterValues); // Update the input value if needed to match the new model setSearchValue(prevSearchValue => (0, _utils2.isDeepEqual)(quickFilterParser(prevSearchValue), quickFilterValues) ? prevSearchValue : quickFilterFormatter(quickFilterValues != null ? quickFilterValues : [])); } }, [prevQuickFilterValues, quickFilterValues, quickFilterFormatter, quickFilterParser]); const updateSearchValue = React.useCallback(newSearchValue => { apiRef.current.setQuickFilterValues(quickFilterParser(newSearchValue)); }, [apiRef, quickFilterParser]); const debouncedUpdateSearchValue = React.useMemo(() => (0, _utils.debounce)(updateSearchValue, debounceMs), [updateSearchValue, debounceMs]); const handleSearchValueChange = React.useCallback(event => { const newSearchValue = event.target.value; setSearchValue(newSearchValue); debouncedUpdateSearchValue(newSearchValue); }, [debouncedUpdateSearchValue]); const handleSearchReset = React.useCallback(() => { setSearchValue(''); updateSearchValue(''); }, [updateSearchValue]); return /*#__PURE__*/(0, _jsxRuntime.jsx)(GridToolbarQuickFilterRoot, (0, _extends2.default)({ as: rootProps.components.BaseTextField, variant: "standard", value: searchValue, onChange: handleSearchValueChange, placeholder: apiRef.current.getLocaleText('toolbarQuickFilterPlaceholder'), "aria-label": apiRef.current.getLocaleText('toolbarQuickFilterLabel'), type: "search", InputProps: { startAdornment: /*#__PURE__*/(0, _jsxRuntime.jsx)(rootProps.components.QuickFilterIcon, { fontSize: "small" }), endAdornment: /*#__PURE__*/(0, _jsxRuntime.jsx)(_IconButton.default, { "aria-label": apiRef.current.getLocaleText('toolbarQuickFilterDeleteIconLabel'), size: "small", sx: { visibility: searchValue ? 'visible' : 'hidden' }, onClick: handleSearchReset, children: /*#__PURE__*/(0, _jsxRuntime.jsx)(rootProps.components.QuickFilterClearIcon, { fontSize: "small" }) }) } }, other, (_rootProps$components = rootProps.componentsProps) == null ? void 0 : _rootProps$components.baseTextField)); } process.env.NODE_ENV !== "production" ? GridToolbarQuickFilter.propTypes = { // ----------------------------- Warning -------------------------------- // | These PropTypes are generated from the TypeScript type definitions | // | To update them edit the TypeScript types and run "yarn proptypes" | // ---------------------------------------------------------------------- /** * The debounce time in milliseconds. * @default 500 */ debounceMs: _propTypes.default.number, /** * Function responsible for formatting values of quick filter in a string when the model is modified * @param {any[]} values The new values passed to the quick filter model * @returns {string} The string to display in the text field */ quickFilterFormatter: _propTypes.default.func, /** * Function responsible for parsing text input in an array of independent values for quick filtering. * @param {string} input The value entered by the user * @returns {any[]} The array of value on which quick filter is applied */ quickFilterParser: _propTypes.default.func } : void 0;