Fix collectable collection selecting initial items lazily

This commit is contained in:
schroda
2026-03-20 01:30:01 +01:00
parent 33c55f8b85
commit b63c8a548e
3 changed files with 13 additions and 6 deletions

View File

@@ -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,

View File

@@ -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 = () => {

View File

@@ -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], categoriesToAdd: [...mangaCategoryIds, ...defaultCategoryIds],
categoriesToRemove: [], categoriesToRemove: [],
}, }),
[mangaCategoryIds, defaultCategoryIds],
),
}); });
useEffect(() => { useEffect(() => {