Fix collectable collection selecting initial items lazily
This commit is contained in:
@@ -6,7 +6,7 @@
|
|||||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { useCallback, useMemo, useRef, useState } from 'react';
|
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
||||||
|
|
||||||
export type SelectableCollectionReturnType<Id extends number | string, Key extends string = string> = {
|
export type SelectableCollectionReturnType<Id extends number | string, Key extends string = string> = {
|
||||||
selectedItemIds: Id[];
|
selectedItemIds: Id[];
|
||||||
@@ -134,6 +134,10 @@ export const useSelectableCollection = <Id extends number | string, Key extends
|
|||||||
setKeyToSelectedItemIds(initialState);
|
setKeyToSelectedItemIds(initialState);
|
||||||
}, [clearSelection, initialState]);
|
}, [clearSelection, initialState]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
setKeyToSelectedItemIds(initialState);
|
||||||
|
}, [initialState]);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
selectedItemIds,
|
selectedItemIds,
|
||||||
keySelectedItemIds,
|
keySelectedItemIds,
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ export function CheckboxListSetting<Item>({
|
|||||||
const { selectedItemIds, handleSelection, handleSelectAll, reset } = useSelectableCollection(items.length, {
|
const { selectedItemIds, handleSelection, handleSelectAll, reset } = useSelectableCollection(items.length, {
|
||||||
currentKey: 'default',
|
currentKey: 'default',
|
||||||
itemIds,
|
itemIds,
|
||||||
initialState: { default: currentSelectedItemIds },
|
initialState: useMemo(() => ({ default: currentSelectedItemIds }), [currentSelectedItemIds]),
|
||||||
});
|
});
|
||||||
|
|
||||||
const handleCancel = () => {
|
const handleCancel = () => {
|
||||||
|
|||||||
@@ -126,10 +126,13 @@ export function CategorySelect(props: CategorySelectProps) {
|
|||||||
'categoriesToAdd' | 'categoriesToRemove'
|
'categoriesToAdd' | 'categoriesToRemove'
|
||||||
>(allCategories.length, {
|
>(allCategories.length, {
|
||||||
currentKey: 'categoriesToAdd',
|
currentKey: 'categoriesToAdd',
|
||||||
initialState: {
|
initialState: useMemo(
|
||||||
categoriesToAdd: [...mangaCategoryIds, ...defaultCategoryIds],
|
() => ({
|
||||||
categoriesToRemove: [],
|
categoriesToAdd: [...mangaCategoryIds, ...defaultCategoryIds],
|
||||||
},
|
categoriesToRemove: [],
|
||||||
|
}),
|
||||||
|
[mangaCategoryIds, defaultCategoryIds],
|
||||||
|
),
|
||||||
});
|
});
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
|||||||
Reference in New Issue
Block a user