1

I am new to react. I have got a task to:

  • Display multiple checkboxes.
  • Checkboxes names can be shuffled in the future. (Client can decide which checkbox value to be displayed at the top and so on).
  • Display checkboxes based on order no.
  • There will be a submit button below the checkbox which will be disabled on load. Force the user to tick all the check boxes only then the button should be enabled.
  • On click of save button, send the data to backend. (For the time being I will log to console).

I am using react with typescript.
I have attached a snapshot for reference. Multiple checboxes example
So far I have done this:

import React, { useState } from "react";

interface Data {
  name: string;
  order: number;
}

const MyCheckBoxList: Data[] = [
  {
    order: 0,
    name: "Angular",
  },
  {
    order: 1,
    name: "React",
  },
  {
    order: 2,
    name: "Java",
  },
];

const MultipleCheckBoxComponent = () => {
  const [checked, setChecked] = useState(false);

  return (
    <div>
      <h3>Multiple checbox </h3> <br />
      <ul>
        {MyCheckBoxList.map(({ name, order }, index) => {
          return (
            <li key={index}>
              <div>
                <div>
                  <input
                    type="checkbox"
                    id={`custom-checkbox-${index}`}
                    name={name}
                    value={name}
                  />
                </div>
              </div>
            </li>
          );
        })}
      </ul>
      <button type="submit">Save</button>
    </div>
  );
};

export default MultipleCheckBoxComponent;

Can anybody tell me:
How do I display the check boxes based on order number (as I have mentioned in point 3) and how to achieve point 4?(the button should enable only when all the checkboxes are tick)
Glenn singh
  • 233
  • 1
  • 6
  • 18
  • How to do what? what its your problem? – yanir midler May 19 '22 at 09:51
  • Hey @yanirmidler I have edited my question. Here is what I am looking for: How do I display the check boxes based on order number (as I have mentioned in point 3) and how to achieve point 4?(the button should enable only when all the checkboxes are tick). – Glenn singh May 19 '22 at 09:58
  • turn your checkboxes into controlled inputs (manage value and onChange yourself) and on each change check if you should enable submit (all checked). For order, I'd say you can order the list before rendering it or use flex order property – Apolo May 19 '22 at 10:00

1 Answers1

3

You can sort your data based on your requirements while setting to state, i.e as a default value for the state.

const [data, setData] = useState(
  MyCheckBoxList.sort((a, b) => a.order - b.order)
);

You need to validate each and every checkbox value and then only you can enable your button. For you have to store the value of each and every item. For that, you can create a simple component that can render your check box, and there you can update the checkbox value along with the main data.

Finally, validate your updated data so that you can enable your submit button or not.

const isVerified = useMemo(() => {
    return data.every((d) => d.checked);
}, [data]);

return (
    <div className="App">
        {data.map((obj, index) => (
        <li key={index}>
            <Checkbox
            obj={obj}
            onChange={(item) => {
                setData(data.map((d) => (d.order === item.order ? item : d)));
            }}
            />
        </li>
    ))}
</div>)

And this is my checkbox component.

const Checkbox = ({ obj, onChange }) => {
  return (
    <>
      <input
        type="checkbox"
        id={`custom-checkbox-${obj.index}`}
        name={obj.name}
        value={obj.checked}
        onChange={() => onChange({ ...obj, checked: !obj.checked })}
      />
      {obj.name}
    </>
  );
};

Attaching the sandbox link so that you can update your logic based on that.

Edit dreamy-curran-4qggdc

mc-user
  • 1,769
  • 4
  • 14
  • 25
  • This works fine for me. Thanks a lot. Just a small question, what if "name" property in the MyCheckBoxList array has some html string like this: "

    This is some text.

    • Another text
    ". then what will be the best place to add "dangerouslySetInnerHTML" so that the string should render as html. As per current code, it will simply print the string with html tag. It is not rendering the tags. Any suggestion on this?
    – Glenn singh May 20 '22 at 12:34
  • https://stackoverflow.com/questions/29044518/safe-alternative-to-dangerouslysetinnerhtml/67688339#67688339 might help you – mc-user May 21 '22 at 01:32