Skip to content

Commit 056da7b

Browse files
committed
改善React代码
1 parent 5d5838e commit 056da7b

File tree

1 file changed

+59
-68
lines changed

1 file changed

+59
-68
lines changed

src/pages/options/routes/ScriptList.tsx

Lines changed: 59 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -658,88 +658,79 @@ function ScriptList() {
658658
})
659659
);
660660

661-
useEffect(() => {
662-
if (!newColumns.length) {
663-
return;
664-
}
665-
const SortableWrapper = (props: any, ref: any) => {
666-
return (
667-
<DndContext
668-
sensors={sensors}
669-
collisionDetection={closestCenter}
670-
onDragEnd={(event: DragEndEvent) => {
671-
const { active, over } = event;
672-
if (!over) {
673-
return;
674-
}
675-
if (active.id !== over.id) {
676-
dispatch(sortScript({ active: active.id as string, over: over.id as string }));
677-
}
678-
}}
661+
const SortableWrapper = (props: any, ref: any) => {
662+
return (
663+
<DndContext
664+
sensors={sensors}
665+
collisionDetection={closestCenter}
666+
onDragEnd={(event: DragEndEvent) => {
667+
const { active, over } = event;
668+
if (!over) {
669+
return;
670+
}
671+
if (active.id !== over.id) {
672+
dispatch(sortScript({ active: active.id as string, over: over.id as string }));
673+
}
674+
}}
675+
>
676+
<SortableContext
677+
items={store.getState().script.scripts.map((s) => ({ ...s, id: s.uuid }))}
678+
strategy={verticalListSortingStrategy}
679679
>
680-
<SortableContext
681-
items={store.getState().script.scripts.map((s) => ({ ...s, id: s.uuid }))}
682-
strategy={verticalListSortingStrategy}
683-
>
684-
<table ref={ref} {...props} />
685-
</SortableContext>
686-
</DndContext>
687-
);
688-
};
689-
const dealColumns: ColumnProps[] = [];
680+
<table ref={ref} {...props} />
681+
</SortableContext>
682+
</DndContext>
683+
);
684+
};
690685

691-
newColumns.forEach((item) => {
692-
switch (item.width) {
693-
case -1:
694-
break;
695-
default:
696-
dealColumns.push(item);
697-
break;
698-
}
699-
});
686+
const [sortIndexState, setSortIndexState] = useState(-1);
700687

701-
const sortIndex = dealColumns.findIndex((item) => item.key === "sort");
702-
let SortableItem;
703-
if (sortIndex !== -1) {
704-
const SortableItemComponent = (props: any) => {
705-
const { attributes, listeners, setNodeRef, transform, transition } = useSortable({ id: props!.record.uuid });
688+
const SortableItemComponent = (props: any) => {
689+
const { attributes, listeners, setNodeRef, transform, transition } = useSortable({ id: props!.record.uuid });
706690

707-
const style = {
708-
transform: CSS.Transform.toString(transform),
709-
transition,
710-
};
691+
const style = {
692+
transform: CSS.Transform.toString(transform),
693+
transition,
694+
};
711695

712-
// 替换排序列,使其可以拖拽
713-
props.children[sortIndex + 1] = (
714-
<td
715-
className="arco-table-td"
696+
// 替换排序列,使其可以拖拽
697+
props.children[sortIndexState + 1] = (
698+
<td
699+
className="arco-table-td"
700+
style={{
701+
textAlign: "center",
702+
}}
703+
key="drag"
704+
>
705+
<div className="arco-table-cell">
706+
<IconMenu
716707
style={{
717-
textAlign: "center",
708+
cursor: "move",
718709
}}
719-
key="drag"
720-
>
721-
<div className="arco-table-cell">
722-
<IconMenu
723-
style={{
724-
cursor: "move",
725-
}}
726-
{...listeners}
727-
/>
728-
</div>
729-
</td>
730-
);
710+
{...listeners}
711+
/>
712+
</div>
713+
</td>
714+
);
715+
716+
return <tr ref={setNodeRef} style={style} {...attributes} {...props} />;
717+
};
718+
SortableItemComponent.displayName = "SortableItem";
731719

732-
return <tr ref={setNodeRef} style={style} {...attributes} {...props} />;
733-
};
734-
SortableItemComponent.displayName = "SortableItem";
735-
SortableItem = SortableItemComponent;
720+
useEffect(() => {
721+
if (!newColumns.length) {
722+
return;
736723
}
724+
const dealColumns: ColumnProps[] = newColumns.filter((item) => item.width !== -1);
725+
726+
const sortIndex = dealColumns.findIndex((item) => item.key === "sort");
727+
setSortIndexState(sortIndex);
737728

738729
setComponents({
739730
table: React.forwardRef(SortableWrapper),
740731
body: {
741732
// tbody: SortableWrapper,
742-
row: SortableItem,
733+
row: sortIndex !== -1 ? SortableItemComponent : undefined,
743734
},
744735
});
745736
setDealColumns(dealColumns);

0 commit comments

Comments
 (0)