Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: handle non multiple selection #1377

Merged
merged 1 commit into from
Jun 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions frontend/taipy/src/CoreSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ interface CoreSelectorProps {
leafType: NodeType;
editComponent?: ComponentType<EditProps>;
showPins?: boolean;
onSelect?: (id: string | string[]) => void;
onSelect?: (id: string | string[] | null) => void;
updateCoreVars: string;
filter?: string;
sort?: string;
Expand Down Expand Up @@ -294,7 +294,7 @@ const filterTree = (entities: Entities, search: string, leafType: NodeType, coun
};

const localStoreSet = (val: string, ...ids: string[]) => {
const id = ids.filter(i => !!i).join(" ");
const id = ids.filter((i) => !!i).join(" ");
if (!id) {
return;
}
Expand All @@ -306,7 +306,7 @@ const localStoreSet = (val: string, ...ids: string[]) => {
};

const localStoreGet = (...ids: string[]) => {
const id = ids.filter(i => !!i).join(" ");
const id = ids.filter((i) => !!i).join(" ");
if (!id) {
return undefined;
}
Expand Down Expand Up @@ -364,23 +364,21 @@ const CoreSelector = (props: CoreSelectorProps) => {
}, []);

const onNodeSelect = useCallback(
(e: SyntheticEvent, nodeId: string, isSelected: boolean) => {
(e: SyntheticEvent, nodeId: string | string[] | null) => {
const { selectable = "false" } = e.currentTarget.parentElement?.dataset || {};
const isSelectable = selectable === "true";
if (!isSelectable && multiple) {
return;
}
setSelectedItems((old) => {
const res = isSelected ? [...old, nodeId] : old.filter((id) => id !== nodeId);
const scenariosVar = getUpdateVar(updateVars, lovPropertyName);
const val = multiple ? res : isSelectable ? nodeId : "";
setSelectedItems(() => {
const lovVar = getUpdateVar(updateVars, lovPropertyName);
const val = multiple ? nodeId : isSelectable ? nodeId : "";
setTimeout(
() =>
dispatch(createSendUpdateAction(updateVarName, val, module, onChange, propagate, scenariosVar)),
() => dispatch(createSendUpdateAction(updateVarName, val, module, onChange, propagate, lovVar)),
1
);
onSelect && isSelectable && onSelect(val);
return res;
return Array.isArray(nodeId) ? nodeId : nodeId ? [nodeId] : [];
});
},
[updateVarName, updateVars, onChange, onSelect, multiple, propagate, dispatch, module, lovPropertyName]
Expand Down Expand Up @@ -665,7 +663,7 @@ const CoreSelector = (props: CoreSelectorProps) => {
<SimpleTreeView
slots={treeSlots}
sx={treeViewSx}
onItemSelectionToggle={onNodeSelect}
onSelectedItemsChange={onNodeSelect}
selectedItems={selectedItems}
multiSelect={multiple}
expandedItems={expandedItems}
Expand Down
Loading