React Table - useRowSelect 的单选输入

IT技术 javascript reactjs react-table
2021-05-22 09:30:41

如何在 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} />
    );
  });


它看起来正确,但没有将正确的选择状态传回:
在此处输入图片说明

3个回答

快速浏览了一下。试试这个:

  • 有一个 rowId 的状态
const [selectedRowId, setSelectedRowId] = useState(null);
  • 传递autoResetSelectedRows: false,useTable函数
  • 手动为收音机编写一个点击处理程序

    onClick={() => setSelectedRowId(row.id)}

在此处输入图片说明

我在这里整理了工作代码。 https://codesandbox.io/s/objective-faraday-gzf7y

笔记:

根据之前的答案,下面的解决方案有一些改进

  1. 将复选框更改为单选输入

    IndeterminateCheckbox 组件内部

    Remove  <input type="checkbox" ref={resolvedRef} {...rest} />*/}
    Add     <input type="radio" ref={resolvedRef} {...rest} />
    
  2. a) 如果要初始自动选择任何行,请设置初始状态

    b) 取消选择所有单选按钮

    c) row.getToggleRowSelectedProps().checked给出该行单选按钮的当前状态。因此相应地切换该值。IE

    如果选中-> 更改为未选中

    如果未选中-> 更改为选中

    // This selectedRowIds state gives the information about which row is selected currently.
    const{state: { selectedRowIds }}=useTable(
    {
     ....
     initialState: {
          columns,
          data,
          selectedRowIds: { 2: true },  //a) I want my second row to be autoselected initially
       },
     },
     useRowSelect,
         hooks => {
             hooks.visibleColumns.push(columns => [
                 {
                     id: "selection",                    
                     Cell: ({ row, toggleAllRowsSelected, toggleRowSelected }) => {                      
                        const currentState = row.getToggleRowSelectedProps();
                        return (
                           <IndeterminateCheckbox
                              {...currentState}
                              onClick={() => {
                                // b)
                                  toggleAllRowsSelected(false);
                                // c)
                                  toggleRowSelected(row.id, !currentState.checked);
                          }} />)
    
                        }},
                          ...columns
                        ]);
                        })
    

如果有人仍在为此苦苦挣扎,我找到了不同的解决方案。

IndeterminateCheckbox 与输入类型略有变化相同:

const IndeterminateCheckbox = React.forwardRef(
    ({ indeterminate, ...rest }, ref) => {
        const defaultRef = React.useRef();
        const resolvedRef = ref || defaultRef;

        React.useEffect(() => {
            resolvedRef.current.indeterminate = indeterminate;
        }, [resolvedRef, indeterminate]);

        return (
            <>
                {/*<input type="checkbox" ref={resolvedRef} {...rest} />*/}
                <input type="radio" ref={resolvedRef} {...rest} />
            </>
        );
    }
);

然后 onClick 我们取消选择所有行,并选择“单击”行。:)

 const {
            getTableProps,
            getTableBodyProps,
            headerGroups,
            rows,
            prepareRow,
            toggleAllRowsSelected,
            toggleRowSelected,
            state: { selectedRowIds },
        } = useTable(
            {
                columns,
                data,
            },
            useRowSelect,
            hooks => {
                hooks.visibleColumns.push(columns => [
                    // Let's make a column for selection
                    {
                        id: "selection",
                        // The cell can use the individual row's getToggleRowSelectedProps method
                        // to the render a checkbox
                        Cell: ({ row }) => {
                            return (
                                <div>
                                    <IndeterminateCheckbox
                                        {...row.getToggleRowSelectedProps()}
                                        onClick={() => {
                                            toggleAllRowsSelected(false);
                                            toggleRowSelected(row.id, true);
                                        }}
                                    />
                                </div>
                            );
                        }
                    },
                    ...columns
                ]);
            }
        );