-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DataGrid] Add recipe for persisting filters in local storage (#14208)
- Loading branch information
1 parent
f964fef
commit 3e2d3d0
Showing
4 changed files
with
193 additions
and
0 deletions.
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
docs/data/data-grid/filtering-recipes/FilteringLocalStorage.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import * as React from 'react'; | ||
import { DataGrid, GridToolbar } from '@mui/x-data-grid'; | ||
import { useDemoData } from '@mui/x-data-grid-generator'; | ||
|
||
const VISIBLE_FIELDS = ['name', 'rating', 'country', 'dateCreated', 'isAdmin']; | ||
|
||
const createFilterModelStore = () => { | ||
let listeners = []; | ||
const lsKey = 'gridFilterModel'; | ||
const emptyModel = 'null'; | ||
|
||
return { | ||
subscribe: (callback) => { | ||
listeners.push(callback); | ||
return () => { | ||
listeners = listeners.filter((listener) => listener !== callback); | ||
}; | ||
}, | ||
getSnapshot: () => { | ||
try { | ||
return localStorage.getItem(lsKey) || emptyModel; | ||
} catch (error) { | ||
return emptyModel; | ||
} | ||
}, | ||
getServerSnapshot: () => { | ||
return emptyModel; | ||
}, | ||
update: (filterModel) => { | ||
localStorage.setItem(lsKey, JSON.stringify(filterModel)); | ||
listeners.forEach((listener) => listener()); | ||
}, | ||
}; | ||
}; | ||
|
||
const usePersistedFilterModel = () => { | ||
const [filterModelStore] = React.useState(createFilterModelStore); | ||
|
||
const filterModelString = React.useSyncExternalStore( | ||
filterModelStore.subscribe, | ||
filterModelStore.getSnapshot, | ||
filterModelStore.getServerSnapshot, | ||
); | ||
|
||
const filterModel = React.useMemo(() => { | ||
try { | ||
return JSON.parse(filterModelString) || undefined; | ||
} catch (error) { | ||
return undefined; | ||
} | ||
}, [filterModelString]); | ||
|
||
return React.useMemo( | ||
() => [filterModel, filterModelStore.update], | ||
[filterModel, filterModelStore.update], | ||
); | ||
}; | ||
|
||
export default function FilteringLocalStorage() { | ||
const { data } = useDemoData({ | ||
dataSet: 'Employee', | ||
visibleFields: VISIBLE_FIELDS, | ||
rowLength: 100, | ||
}); | ||
|
||
const [filterModel, setFilterModel] = usePersistedFilterModel(); | ||
|
||
const onFilterModelChange = React.useCallback( | ||
(newFilterModel) => { | ||
setFilterModel(newFilterModel); | ||
}, | ||
[setFilterModel], | ||
); | ||
|
||
return ( | ||
<div style={{ height: 400, width: '100%' }}> | ||
<DataGrid | ||
{...data} | ||
slots={{ toolbar: GridToolbar }} | ||
filterModel={filterModel} | ||
onFilterModelChange={onFilterModelChange} | ||
/> | ||
</div> | ||
); | ||
} |
94 changes: 94 additions & 0 deletions
94
docs/data/data-grid/filtering-recipes/FilteringLocalStorage.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import * as React from 'react'; | ||
import { | ||
DataGrid, | ||
DataGridProps, | ||
GridFilterModel, | ||
GridToolbar, | ||
} from '@mui/x-data-grid'; | ||
import { useDemoData } from '@mui/x-data-grid-generator'; | ||
|
||
const VISIBLE_FIELDS = ['name', 'rating', 'country', 'dateCreated', 'isAdmin']; | ||
|
||
const createFilterModelStore = () => { | ||
let listeners: Array<() => void> = []; | ||
const lsKey = 'gridFilterModel'; | ||
const emptyModel = 'null'; | ||
|
||
return { | ||
subscribe: (callback: () => void) => { | ||
listeners.push(callback); | ||
return () => { | ||
listeners = listeners.filter((listener) => listener !== callback); | ||
}; | ||
}, | ||
getSnapshot: () => { | ||
try { | ||
return localStorage.getItem(lsKey) || emptyModel; | ||
} catch (error) { | ||
return emptyModel; | ||
} | ||
}, | ||
|
||
getServerSnapshot: () => { | ||
return emptyModel; | ||
}, | ||
|
||
update: (filterModel: GridFilterModel) => { | ||
localStorage.setItem(lsKey, JSON.stringify(filterModel)); | ||
listeners.forEach((listener) => listener()); | ||
}, | ||
}; | ||
}; | ||
|
||
const usePersistedFilterModel = () => { | ||
const [filterModelStore] = React.useState(createFilterModelStore); | ||
|
||
const filterModelString = React.useSyncExternalStore( | ||
filterModelStore.subscribe, | ||
filterModelStore.getSnapshot, | ||
filterModelStore.getServerSnapshot, | ||
); | ||
|
||
const filterModel = React.useMemo(() => { | ||
try { | ||
return (JSON.parse(filterModelString) as GridFilterModel) || undefined; | ||
} catch (error) { | ||
return undefined; | ||
} | ||
}, [filterModelString]); | ||
|
||
return React.useMemo( | ||
() => [filterModel, filterModelStore.update] as const, | ||
[filterModel, filterModelStore.update], | ||
); | ||
}; | ||
|
||
export default function FilteringLocalStorage() { | ||
const { data } = useDemoData({ | ||
dataSet: 'Employee', | ||
visibleFields: VISIBLE_FIELDS, | ||
rowLength: 100, | ||
}); | ||
|
||
const [filterModel, setFilterModel] = usePersistedFilterModel(); | ||
|
||
const onFilterModelChange = React.useCallback< | ||
NonNullable<DataGridProps['onFilterModelChange']> | ||
>( | ||
(newFilterModel) => { | ||
setFilterModel(newFilterModel); | ||
}, | ||
[setFilterModel], | ||
); | ||
|
||
return ( | ||
<div style={{ height: 400, width: '100%' }}> | ||
<DataGrid | ||
{...data} | ||
slots={{ toolbar: GridToolbar }} | ||
filterModel={filterModel} | ||
onFilterModelChange={onFilterModelChange} | ||
/> | ||
</div> | ||
); | ||
} |
6 changes: 6 additions & 0 deletions
6
docs/data/data-grid/filtering-recipes/FilteringLocalStorage.tsx.preview
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<DataGrid | ||
{...data} | ||
slots={{ toolbar: GridToolbar }} | ||
filterModel={filterModel} | ||
onFilterModelChange={onFilterModelChange} | ||
/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters