英文:
Using Grid with SortableJS
问题
我正在使用 SortableJS 来处理 React,并尝试实现水平的 Grid,以使最终结果看起来像这样:
我已经成功实现了这一点,但排序函数根本不起作用。我认为这与 tag 属性有关。当我尝试使用 tag={Grid} 时,会出现以下错误:
Sortable: el 必须是一个 HTMLElement,而不是 [object Object]
有没有什么办法可以将 Grid 实现到 tag 属性中?以下是我的代码:
<Grid container>
    <Sortable options={{ fallbackOnBody: true, group: "items", handle: reorderHandle }} tag={Grid} onChange={handleChange}>
        {items.map((item, index) =>
            <Paper key={index} data-id={index} className={classes.item} elevation={0}>
                <ButtonHelper {...getHandleClass(editable)} key={index} icon={
                    <Badge badgeContent={index + 1} color="default">
                        {editable && <ReorderIcon />}
                    </Badge>}
                />
                <Grid item xs={4} key={index}>
                    <div className={classes.gridItem}>
                        <GalleryInput className={classes.image} style={{ width: '300px' }} label="image" source={`${source}[${index}].image`} />
                        <br></br>
                        <TextInput label="desc" source={`${source}[${index}].desc`} />
                        {editable && <ButtonHelper icon={<RemoveIcon />} onClick={handleRemove(index)} className={classes.left} />}
                    </div>
                </Grid>
            </Paper>
        )}
    </Sortable>
</Grid>
感谢您的任何帮助。
英文:
I am using SortableJS for React and I am trying to implement horizontal Grid, so that the final result would look like this:
I managed to do that, however the sort function does not work at all. I think it has something to do with the tag attribute. When I try tag={Grid} I get following error:
Sortable: el must be an HTMLElement, not [object Object]
Any ideas how can I implement Grid to the tag attribute? Here is my code:
     <Grid container>
            <Sortable options={{ fallbackOnBody: true, group: "items", handle: reorderHandle }} tag={Grid} onChange={handleChange}>
                {items.map((item, index) =>
                    <Paper key={index} data-id={index} className={classes.item} elevation={0}>
                        <ButtonHelper {...getHandleClass(editable)} key={index} icon={
                            <Badge badgeContent={index + 1} color="default">
                                {editable && <ReorderIcon />}
                            </Badge>}
                        />
                        <Grid item xs={4} key={index}>
                            <div className={classes.gridItem}>
                                <GalleryInput className={classes.image} style={{ width: '300px' }} label="image" source={`${source}[${index}].image`} />
                                <br></br>
                                <TextInput label="desc" source={`${source}[${index}].desc`} />
                                {editable && <ButtonHelper icon={<RemoveIcon />} onClick={handleRemove(index)} className={classes.left} />}
                            </div>
                        </Grid>
                    </Paper>
                )}
            </Sortable>
        </Grid>
Thank you for any help.
答案1
得分: 2
由于您正在使用自定义组件,tag 属性只接受它作为 forwardRef 组件。
因此,您需要以以下方式进行更新。
示例代码片段
// 这就像一个普通组件,但现在有一个 ref。
const CustomComponent = forwardRef<HTMLDivElement, any>((props, ref) => {
  return <div ref={ref}>{props.children}</div>;
});
export const BasicFunction: FC = props => {
  const [state, setState] = useState([
    { id: 1, name: "shrek" },
    { id: 2, name: "fiona" }
  ]);
  return (
    <ReactSortable tag={CustomComponent} list={state} setList={setState}>
      {state.map(item => (
        <div key={item.id}>{item.name}</div>
      ))}
    </ReactSortable>
  );
};
英文:
Since you are using a custom component, the tag prop accepts it as a forwarRef component only.
So you need to update in that way.
Sample Snippet
// This is just like a normal component, but now has a ref.
const CustomComponent = forwardRef<HTMLDivElement, any>((props, ref) => {
  return <div ref={ref}>{props.children}</div>;
});
export const BasicFunction: FC = props => {
  const [state, setState] = useState([
    { id: 1, name: "shrek" },
    { id: 2, name: "fiona" }
  ]);
  return (
    <ReactSortable tag={CustomComponent} list={state} setList={setState}>
      {state.map(item => (
        <div key={item.id}>{item.name}</div>
      ))}
    </ReactSortable>
  );
};
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。



评论