1

I want to mark the checkbox checked inside a subComponent with the help of forwardref but i am not getting the result. I have tried defaultChecked = {true} defaultValue = {true} inside input field but didn't succeed.

Here is the checkbox component

import { forwardRef, useEffect, useRef } from "react";

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

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

  return (
    <>
      <div class="flex items-center">
        <input
          type="checkbox"
          ref={resolvedRef}
          {...rest}
          id="A3-yes"
          name="A3-confirmation"
          class="opacity-0 absolute h-8 w-8"
        />
      </div>
    </>
  );
});

This is how I called the checkbox Component.

= useTable(
    {
      columns,
      data,
      state : {expanded},
    },
    useExpanded,
    useRowSelect,
    (hooks) => {
      hooks.visibleColumns.push((columns) => {
        return [
          ...columns,
          {
            Header: "Choose Items",
            id: "selection",
            Cell: ({ row }) => (
              (details.isSelected) ? ( 
              <div>
                <SubRowsCheckBox  {...row.getToggleRowSelectedProps() }  />
              </div>
            ) : ( null 
            )
            ),
          },
        ];
      });
    }
    
 
  )

The component is rendered only if row has got some subRows. I have also tried resolvedRef.current.checked = true. It marks the checkbox checked but it doesn't works for the all rows. Here are the results First Image with subRow

These are the results of resolvedRef.current.checked = true. The defaultChecked prop isn't changing anything. Second Image

Any kind of help will be highly appreciated.

I want to mark all the subrows checkbox checked for the first render and rest of it works fine.

1 Answers1

1

React Table v7 has a lot of nice configurations. You can handle the initialState of your rows and subrows next to where you pass in columns and data.

To prevent unchecking boxes when deselecting a row, you'll need to set the autoResetHiddenColumns flag to `false.

To set some initial row and sub-rows as checked, you'll need to use the initialState object. It contains some optional properties: expanded and selectedRowIds. You'll notice the child-rows have decimal values for their keys.

{
  columns,
  data,
  autoResetHiddenColumns: false,
  initialState: {
    expanded: {
      0: true
    },
    selectedRowIds: {
      0: true,
      "0.0": true,
      0.1: true,
      0.2: true
    }
  }
},

Now you should see the 1st row and it's 3 child-rows selected by default. When you expand the 2nd row and check the row-box, all 3 child-rows are expanded - the 1st row and it's 3 child-rows should remain unaffected.

Wesley LeMahieu
  • 2,296
  • 1
  • 12
  • 8
  • Indeed you are right. I want the initial State to be checked The `detail.isSelected` is checking if the main row is selected and the data is an array of objects and each object has got nested object which are the subrows. – Muhammad Misbah Ul Haq Feb 07 '23 at 18:42
  • 1
    Yes the example link is best matched but it has got the same issue what I have mentioned. Do expand the first mainRow all the subRows are checked thats fine till here. Now when you expand the second main Row it subRows are Checked but it "unChecked" the first SubRows. Do expand the Main Rows directly when you open the example. – Muhammad Misbah Ul Haq Feb 08 '23 at 11:30
  • Ah, I see that. I revised the sandbox with a built-in strategy for React Tables. I think this should get you closer to where you want to go. If you want the child rows to be selected when expanding a row, that is possible (but might not be a good UX). I hope that helps! – Wesley LeMahieu Feb 08 '23 at 15:54
  • 1
    Thankyou Yes indeed. I will discuss. and the method isn't good for dynamic subRows. – Muhammad Misbah Ul Haq Feb 09 '23 at 17:25