Skip to content

Commit

Permalink
handle non multiple selection
Browse files Browse the repository at this point in the history
resolves #1376
  • Loading branch information
Fred Lefévère-Laoide authored and Fred Lefévère-Laoide committed Jun 7, 2024
1 parent cfdb397 commit 1879b2c
Showing 1 changed file with 10 additions and 12 deletions.
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

0 comments on commit 1879b2c

Please sign in to comment.