|
| 1 | + |
| 2 | +import { useState } from 'react'; |
| 3 | +import { DataGrid } from '@opencorestack/opengridx'; |
| 4 | +import type { GridColDef, GridSortModel } from '@opencorestack/opengridx'; |
| 5 | +import './MultiSortDemo.css'; |
| 6 | +import { DocsLayout } from '../../components/DocsLayout'; |
| 7 | +import sourceCode from './MultiSortDemo.tsx?raw'; |
| 8 | + |
| 9 | +// Focused dataset: 4 departments, distinct salaries — makes multi-sort result |
| 10 | +// immediately readable: dept ASC + salary DESC → highest-paid per dept appears first. |
| 11 | +const ROWS = [ |
| 12 | + { id: 1, name: 'Alice Chen', department: 'Engineering', salary: 120000, level: 'Senior' }, |
| 13 | + { id: 2, name: 'Bob Martin', department: 'Engineering', salary: 95000, level: 'Mid' }, |
| 14 | + { id: 3, name: 'Carol White', department: 'Engineering', salary: 145000, level: 'Staff' }, |
| 15 | + { id: 4, name: 'David Kim', department: 'Engineering', salary: 80000, level: 'Junior' }, |
| 16 | + { id: 5, name: 'Eva Patel', department: 'HR', salary: 72000, level: 'Senior' }, |
| 17 | + { id: 6, name: 'Frank Lee', department: 'HR', salary: 58000, level: 'Mid' }, |
| 18 | + { id: 7, name: 'Grace Nguyen', department: 'HR', salary: 65000, level: 'Senior' }, |
| 19 | + { id: 8, name: 'Henry Walsh', department: 'Marketing', salary: 88000, level: 'Staff' }, |
| 20 | + { id: 9, name: 'Iris Santos', department: 'Marketing', salary: 75000, level: 'Senior' }, |
| 21 | + { id: 10, name: 'Jack Okafor', department: 'Marketing', salary: 62000, level: 'Mid' }, |
| 22 | + { id: 11, name: 'Karen Zhou', department: 'Sales', salary: 105000, level: 'Staff' }, |
| 23 | + { id: 12, name: 'Liam Ross', department: 'Sales', salary: 91000, level: 'Senior' }, |
| 24 | + { id: 13, name: 'Mia Torres', department: 'Sales', salary: 77000, level: 'Mid' }, |
| 25 | + { id: 14, name: 'Noah Bakker', department: 'Sales', salary: 68000, level: 'Junior' }, |
| 26 | + { id: 15, name: 'Olivia Grant', department: 'Engineering', salary: 110000, level: 'Senior' }, |
| 27 | + { id: 16, name: 'Paul Dubois', department: 'Marketing', salary: 93000, level: 'Staff' }, |
| 28 | + { id: 17, name: 'Quinn Tran', department: 'HR', salary: 55000, level: 'Junior' }, |
| 29 | + { id: 18, name: 'Rosa Ferreira', department: 'Sales', salary: 115000, level: 'Staff' }, |
| 30 | + { id: 19, name: 'Sam Johansson', department: 'Engineering', salary: 70000, level: 'Junior' }, |
| 31 | + { id: 20, name: 'Tina Yamamoto', department: 'Marketing', salary: 85000, level: 'Senior' }, |
| 32 | +]; |
| 33 | + |
| 34 | +const columns: GridColDef[] = [ |
| 35 | + { field: 'name', headerName: 'Name', width: 170 }, |
| 36 | + { field: 'department', headerName: 'Department', width: 140 }, |
| 37 | + { field: 'level', headerName: 'Level', width: 110 }, |
| 38 | + { field: 'salary', headerName: 'Salary', width: 110, type: 'number' }, |
| 39 | + { field: 'id', headerName: 'ID', width: 60, sortable: false, |
| 40 | + description: 'Row ID — sortable: false, click should do nothing' }, |
| 41 | +]; |
| 42 | + |
| 43 | +export default function MultiSortDemo() { |
| 44 | + const [sortModel, setSortModel] = useState<GridSortModel>([ |
| 45 | + { field: 'department', sort: 'asc' }, |
| 46 | + { field: 'salary', sort: 'desc' }, |
| 47 | + ]); |
| 48 | + |
| 49 | + return ( |
| 50 | + <DocsLayout |
| 51 | + title="Multi-Column Sorting" |
| 52 | + description="Pass multiSort to enable single-click multi-column sorting — every click appends or cycles a column instead of replacing the sort. Numbered badges show sort priority. Clicking a column again cycles asc → desc → removed without touching other keys." |
| 53 | + sourceCode={sourceCode} |
| 54 | + > |
| 55 | + <div className="multi-sort-hint"> |
| 56 | + <strong>How to verify:</strong> The grid starts sorted by Department ① asc + Salary ② desc. |
| 57 | + Click <em>Level</em> to add a third sort key — note the existing two stay active. |
| 58 | + Click <em>Department</em> again to cycle it desc, then again to remove it. |
| 59 | + Clicking <em>ID</em> should do nothing (sortable: false). |
| 60 | + </div> |
| 61 | + <div className="multi-sort-state"> |
| 62 | + <span className="multi-sort-label">sortModel:</span> |
| 63 | + <code>{JSON.stringify(sortModel)}</code> |
| 64 | + </div> |
| 65 | + <DataGrid |
| 66 | + rows={ROWS} |
| 67 | + columns={columns} |
| 68 | + multiSort |
| 69 | + sortModel={sortModel} |
| 70 | + onSortModelChange={setSortModel} |
| 71 | + pagination |
| 72 | + pageSizeOptions={[10, 20]} |
| 73 | + initialState={{ pagination: { paginationModel: { pageSize: 20, page: 0 } } }} |
| 74 | + height={520} |
| 75 | + /> |
| 76 | + </DocsLayout> |
| 77 | + ); |
| 78 | +} |
0 commit comments