|
1 | 1 | import React, { useState, useEffect, useRef } from 'react'; |
2 | 2 |
|
3 | | -import { Box, Input, InputAdornment } from '@mui/material'; |
| 3 | +import { |
| 4 | + Box, |
| 5 | + Input, |
| 6 | + InputAdornment, |
| 7 | + TextField, |
| 8 | + Select, |
| 9 | + MenuItem |
| 10 | +} from '@mui/material'; |
4 | 11 | import SearchIcon from '@mui/icons-material/Search'; |
5 | 12 |
|
6 | 13 | // root of this project |
@@ -1467,3 +1474,122 @@ export function LocalizationWithCustomComponents() { |
1467 | 1474 | /> |
1468 | 1475 | ); |
1469 | 1476 | } |
| 1477 | + |
| 1478 | +function CustomFilterWithOperatorSelection({ columnDef, onFilterChanged }) { |
| 1479 | + const [operator, setOperator] = React.useState('='); |
| 1480 | + const [value, setValue] = React.useState(undefined); |
| 1481 | + const operatorRef = React.useRef(operator); |
| 1482 | + const valueRef = React.useRef(value); |
| 1483 | + |
| 1484 | + React.useEffect(() => { |
| 1485 | + if (operatorRef.current !== operator || valueRef.current !== value) { |
| 1486 | + onFilterChanged(columnDef.tableData.id, value, operator); |
| 1487 | + operatorRef.current = operator; |
| 1488 | + valueRef.current = value; |
| 1489 | + } |
| 1490 | + }, [operator, value]); |
| 1491 | + |
| 1492 | + return ( |
| 1493 | + <span> |
| 1494 | + <Select |
| 1495 | + labelId="demo-simple-select-label" |
| 1496 | + id="demo-simple-select" |
| 1497 | + variant="standard" |
| 1498 | + value={operator} |
| 1499 | + onChange={(e) => setOperator(e.target.value)} |
| 1500 | + > |
| 1501 | + <MenuItem value={'='}>=</MenuItem> |
| 1502 | + <MenuItem value={'>'}>></MenuItem> |
| 1503 | + <MenuItem value={'<'}><</MenuItem> |
| 1504 | + </Select> |
| 1505 | + <TextField |
| 1506 | + variant="standard" |
| 1507 | + onChange={(e) => setValue(e.target.value)} |
| 1508 | + /> |
| 1509 | + </span> |
| 1510 | + ); |
| 1511 | +} |
| 1512 | + |
| 1513 | +const columns_with_custom_filter = [ |
| 1514 | + { title: 'Name', field: 'name', filtering: true }, |
| 1515 | + { |
| 1516 | + title: 'Some Number', |
| 1517 | + field: 'some_number', |
| 1518 | + filtering: true, |
| 1519 | + filterComponent: ({ columnDef, onFilterChanged }) => ( |
| 1520 | + <CustomFilterWithOperatorSelection |
| 1521 | + columnDef={columnDef} |
| 1522 | + onFilterChanged={onFilterChanged} |
| 1523 | + /> |
| 1524 | + ) |
| 1525 | + } |
| 1526 | +]; |
| 1527 | + |
| 1528 | +const data_with_custom_filter = [ |
| 1529 | + { name: 'Juan', some_number: 1 }, |
| 1530 | + { name: 'John', some_number: 4 }, |
| 1531 | + { name: 'Pedro', some_number: 8 }, |
| 1532 | + { name: 'Mary', some_number: 12 }, |
| 1533 | + { name: 'Oliver', some_number: 2 }, |
| 1534 | + { name: 'Ignacio', some_number: 4 } |
| 1535 | +]; |
| 1536 | + |
| 1537 | +export function FilterWithOperatorSelection() { |
| 1538 | + return ( |
| 1539 | + <MaterialTable |
| 1540 | + data={(query) => |
| 1541 | + new Promise((resolve, _reject) => { |
| 1542 | + if (query.filters.length > 0) { |
| 1543 | + query.filters.forEach((filter) => { |
| 1544 | + if ( |
| 1545 | + filter.value !== undefined && |
| 1546 | + filter.value !== null && |
| 1547 | + filter.value !== '' |
| 1548 | + ) { |
| 1549 | + switch (filter.operator) { |
| 1550 | + case '=': |
| 1551 | + resolve({ |
| 1552 | + data: data_with_custom_filter.filter( |
| 1553 | + (row) => row[filter.column.field] == filter.value |
| 1554 | + ), |
| 1555 | + page: 1, |
| 1556 | + totalCount: data_with_custom_filter.length |
| 1557 | + }); |
| 1558 | + break; |
| 1559 | + case '>': |
| 1560 | + resolve({ |
| 1561 | + data: data_with_custom_filter.filter( |
| 1562 | + (row) => row[filter.column.field] > filter.value |
| 1563 | + ), |
| 1564 | + page: 1, |
| 1565 | + totalCount: data_with_custom_filter.length |
| 1566 | + }); |
| 1567 | + break; |
| 1568 | + case '<': |
| 1569 | + resolve({ |
| 1570 | + data: data_with_custom_filter.filter( |
| 1571 | + (row) => row[filter.column.field] < filter.value |
| 1572 | + ), |
| 1573 | + page: 1, |
| 1574 | + totalCount: data_with_custom_filter.length |
| 1575 | + }); |
| 1576 | + break; |
| 1577 | + } |
| 1578 | + } |
| 1579 | + }); |
| 1580 | + } |
| 1581 | + resolve({ |
| 1582 | + data: data_with_custom_filter, |
| 1583 | + page: 1, |
| 1584 | + totalCount: data_with_custom_filter.length |
| 1585 | + }); |
| 1586 | + }) |
| 1587 | + } |
| 1588 | + columns={columns_with_custom_filter} |
| 1589 | + options={{ |
| 1590 | + search: false, |
| 1591 | + filtering: true |
| 1592 | + }} |
| 1593 | + /> |
| 1594 | + ); |
| 1595 | +} |
0 commit comments