-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(list): improve select performance for big lists (#378)
* fix(list): improve select performance for big lists * fix: fix test * refactor: move data out of useSelectedModels
- Loading branch information
Showing
7 changed files
with
174 additions
and
74 deletions.
There are no files selected for viewing
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
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
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
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,74 @@ | ||
// export interface useSelecto | ||
import { memoize } from 'lodash' | ||
import { useState, useMemo, useCallback } from 'react' | ||
import { BaseListModel, canEditModel } from '../../lib' | ||
|
||
export type UseSelectedModelsOptions = { intialSelected?: string[] } | ||
|
||
export function useSelectedModels(options?: UseSelectedModelsOptions) { | ||
const [selectedModels, setSelectedModels] = useState<Set<string>>( | ||
() => new Set(options?.intialSelected || []) | ||
) | ||
|
||
const add = useCallback( | ||
(ids: string | string[]) => { | ||
const addIds = Array.isArray(ids) ? ids : [ids] | ||
setSelectedModels((prevSelected) => { | ||
const newSelected = new Set(prevSelected) | ||
addIds.forEach((id) => newSelected.add(id)) | ||
return newSelected | ||
}) | ||
}, | ||
[setSelectedModels] | ||
) | ||
|
||
const remove = useCallback( | ||
(ids: string | string[]) => { | ||
const removeIds = Array.isArray(ids) ? ids : [ids] | ||
|
||
setSelectedModels((prevSelected) => { | ||
const newSelected = new Set(prevSelected) | ||
removeIds.forEach((id) => newSelected.delete(id)) | ||
return newSelected | ||
}) | ||
}, | ||
[setSelectedModels] | ||
) | ||
const toggle = useCallback( | ||
(ids: string | string[], checked: boolean) => { | ||
if (checked) { | ||
add(ids) | ||
} else { | ||
remove(ids) | ||
} | ||
}, | ||
[add, remove] | ||
) | ||
|
||
const clearAll = useCallback( | ||
() => setSelectedModels(new Set()), | ||
[setSelectedModels] | ||
) | ||
|
||
const checkAllSelected = useMemo( | ||
() => | ||
memoize((data: BaseListModel[]) => { | ||
if (!data) { | ||
return false | ||
} | ||
return data | ||
.filter((m) => canEditModel(m)) | ||
.every((model) => selectedModels.has(model.id)) | ||
}), | ||
[selectedModels] | ||
) | ||
|
||
return { | ||
selectedModels, | ||
checkAllSelected, | ||
add, | ||
remove, | ||
toggle, | ||
clearAll, | ||
} | ||
} |
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
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
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