Skip to content

Commit

Permalink
chore: prepareItems after review
Browse files Browse the repository at this point in the history
  • Loading branch information
GermanVor committed Feb 13, 2024
1 parent 4a59966 commit de3d679
Showing 1 changed file with 26 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const reorderArray = <T extends unknown>(list: T[], startIndex: number, endIndex
return result;
};

const prepareItems = (tableColumnItems: TableColumnSetupItem[]) => {
const prepareDndItems = (tableColumnItems: TableColumnSetupItem[]) => {
return tableColumnItems.map<Item>((tableColumnItem) => {
const hasSelectionIcon = tableColumnItem.isRequired === false;

Expand All @@ -52,6 +52,18 @@ const prepareItems = (tableColumnItems: TableColumnSetupItem[]) => {
});
};

const prepareValue = (tableColumnItems: TableColumnSetupItem[]) => {
const selectedIds: string[] = [];

tableColumnItems.forEach(({id, isSelected}) => {
if (isSelected) {
selectedIds.push(id);
}
});

return selectedIds;
};

interface SwitcherProps {
onKeyDown: React.KeyboardEventHandler<HTMLElement>;
onClick: React.MouseEventHandler<HTMLElement>;
Expand Down Expand Up @@ -88,13 +100,12 @@ export const TableColumnSetup = (props: TableColumnSetupProps) => {

const [open, setOpen] = React.useState(false);

const [items, setItems] = React.useState(prepareItems(propsItems));

const [items, setItems] = React.useState(propsItems);
const [prevPropsItems, setPrevPropsItems] = React.useState(propsItems);
if (propsItems !== prevPropsItems) {
setPrevPropsItems(propsItems);

const newItems = prepareItems(propsItems);
const newItems = propsItems;
setItems(newItems);
}

Expand All @@ -121,8 +132,7 @@ export const TableColumnSetup = (props: TableColumnSetupProps) => {
setOpen(open);

if (open === false) {
const initialItems = prepareItems(propsItems);
setItems(initialItems);
setItems(propsItems);
}
};

Expand Down Expand Up @@ -225,33 +235,31 @@ export const TableColumnSetup = (props: TableColumnSetupProps) => {
);
};

const value = React.useMemo(() => {
const selectedIds: string[] = [];

items.forEach(({id, isSelected}) => {
if (isSelected) {
selectedIds.push(id);
}
});
const valueRef = React.useRef<string[]>();
if (valueRef.current === undefined || items !== prevPropsItems) {
valueRef.current = prepareValue(items);
}

return selectedIds;
}, [items]);
const dndItemsRef = React.useRef<Item[]>();
if (dndItemsRef.current === undefined || items !== prevPropsItems) {
dndItemsRef.current = prepareDndItems(items);
}

return (
<TreeSelect
className={tableColumnSetupCn}
multiple
size="l"
open={open}
value={value}
value={valueRef.current}
items={dndItemsRef.current}
onUpdate={onUpdate}
popupWidth={popupWidth}
onOpenChange={onOpenChange}
placement={popupPlacement}
renderContainer={renderContainer}
renderControl={renderControl}
renderItem={renderItem}
items={items}
/>
);
};

0 comments on commit de3d679

Please sign in to comment.