使用Grid与SortableJS

huangapple go评论90阅读模式
英文:

Using Grid with SortableJS

问题

我正在使用 SortableJS 来处理 React,并尝试实现水平的 Grid,以使最终结果看起来像这样:

使用Grid与SortableJS

我已经成功实现了这一点,但排序函数根本不起作用。我认为这与 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:

使用Grid与SortableJS

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:

     &lt;Grid container&gt;
            &lt;Sortable options={{ fallbackOnBody: true, group: &quot;items&quot;, handle: reorderHandle }} tag={Grid} onChange={handleChange}&gt;
                {items.map((item, index) =&gt;
                    &lt;Paper key={index} data-id={index} className={classes.item} elevation={0}&gt;
                        &lt;ButtonHelper {...getHandleClass(editable)} key={index} icon={
                            &lt;Badge badgeContent={index + 1} color=&quot;default&quot;&gt;
                                {editable &amp;&amp; &lt;ReorderIcon /&gt;}
                            &lt;/Badge&gt;}
                        /&gt;
                        &lt;Grid item xs={4} key={index}&gt;
                            &lt;div className={classes.gridItem}&gt;
                                &lt;GalleryInput className={classes.image} style={{ width: &#39;300px&#39; }} label=&quot;image&quot; source={`${source}[${index}].image`} /&gt;
                                &lt;br&gt;&lt;/br&gt;
                                &lt;TextInput label=&quot;desc&quot; source={`${source}[${index}].desc`} /&gt;
                                {editable &amp;&amp; &lt;ButtonHelper icon={&lt;RemoveIcon /&gt;} onClick={handleRemove(index)} className={classes.left} /&gt;}
                            &lt;/div&gt;
                        &lt;/Grid&gt;
                    &lt;/Paper&gt;
                )}
            &lt;/Sortable&gt;
        &lt;/Grid&gt;

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&lt;HTMLDivElement, any&gt;((props, ref) =&gt; {
  return &lt;div ref={ref}&gt;{props.children}&lt;/div&gt;;
});

export const BasicFunction: FC = props =&gt; {
  const [state, setState] = useState([
    { id: 1, name: &quot;shrek&quot; },
    { id: 2, name: &quot;fiona&quot; }
  ]);

  return (
    &lt;ReactSortable tag={CustomComponent} list={state} setList={setState}&gt;
      {state.map(item =&gt; (
        &lt;div key={item.id}&gt;{item.name}&lt;/div&gt;
      ))}
    &lt;/ReactSortable&gt;
  );
};

huangapple
  • 本文由 发表于 2020年1月6日 17:57:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/59609973.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定