如何在react-admin中管理可排序的迭代器状态?

问题描述 投票:-1回答:1

我正在编写一个自定义迭代器,它支持拖放以重新排序列表。我不知道如何更新admin / resources / xxx / data的顺序。

我试图从props设置组件状态,但由于react-admin在任何地方都使用cloneElement,因此它非常脆弱。

现在代码看起来像这样:

const Sortable = props => (
  <Datagrid {...props} body={<SortableBody />} />
)

const SortbaleBody = connect({null, {dispatchSortableUpdate}})((props) => (
  const sortable = useMemo(() => Object.values(props.data), [data]);
  const moveRecord = (newData) => {
    dispatchSortableUpdate({data: newData})
  }
  return (
    <DragDropContextProvider backend={HTML5Backend}>
      <TableBody>
        {sortable.map((record, index) => React.cloneElement(SortableRow, props, children))}
      </TableBody>
    </DragDropContextProvider>
  )
))

这是react-dnd可排序的例子:https://codesandbox.io/embed/github/react-dnd/react-dnd/tree/gh-pages/examples_js/04-sortable/simple?fontsize=14

我如何更新admin / resources / xxx / data的顺序?我在做Sortable吧?

react-admin
1个回答
0
投票

我最终用完了

const sortable = useMemo(() => Object.values(data).sort((a,b) => a.position - b.position))

const moveRecord = (dragIndex, hoverIndex) => {
  const dragRecord = sortable[dragIndex];
  const hoverRecord = sortable[hoverIndex];

  dragRecord.position = hoverIndex + 1;
  hoverRecord.position = dragIndex + 1;
  dispatchSortableUpdate({ id: dragRecord.id, data: dragRecord });
  dispatchSortableUpdate({ id: hoverRecord.id, data: hoverRecord });
}
const sortableUpdate = ({ id, data }) => ({
  type: SORTABLE_UPDATE,
  payload: {
    id,
    data,
  },
  meta: {
    resource: 'xxx',
    fetchResponse: UPDATE,
    fetchStatus: FETCH_END,
  },
});

© www.soinside.com 2019 - 2024. All rights reserved.