【发布时间】:2020-07-06 01:27:27
【问题描述】:
如何在 React Table 中为可选表使用单选输入而不是复选框?
有一个复选框而不是单选按钮的示例:https://github.com/tannerlinsley/react-table/blob/master/examples/row-selection/src/App.js
将 IndeterminateCheckox 更改为使用单选输入不起作用,因为 React Table 中的选择状态未更新:
const IndeterminateCheckbox = React.forwardRef(({ indeterminate, ...rest }, ref) => {
const defaultRef = React.useRef();
const resolvedRef = ref || defaultRef;
useEffect(() => {
resolvedRef.current.indeterminate = indeterminate;
}, [resolvedRef, indeterminate]);
return <input name="select-radio" type="radio" ref={resolvedRef} {...rest} />
);
});
【问题讨论】:
-
当您选择不同的选项时会发生什么?您是否在开发工具中看到组件/组件状态的任何更改?此外,
...rest中包含什么——它是否包含您期望的所有内容?看起来很可能您的表实现很好,并且错误在您的状态实现中。 -
如果您仔细查看 userRowSelect 示例,React Table 为被点击的行存储一个“选择”状态。它假定使用复选框,因此它允许通过 ...rest 传递多个选择。我认为最简单的解决方案是在数据单元内实现自定义无线电输入,而不是使用 useRowSelect,我只是认为其他人之前会遇到这种情况。
标签: javascript reactjs react-table