Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

121
Visualizações
How to create multi checkbox for react application using antd

I am using antd checkbox . I am storing checked values by using an array of objects, but I need to get checkbox looks like image.

enter image description here

I have got array of objects for antd checkbox .

const plainOptions1 = [
  {
    name: 'parrent-1',
    key: 'Apple',
    child: [
      { name: 'Pear', key: 'pear' },
      { name: 'store', key: 'store' },
    ],
  },
  {
    name: 'parrent-2',
    key: 'blue',
    child: [
      { name: 'green', key: 'green' },
      { name: 'yellow', key: 'yellow' },
    ],
  },
  {
    name: 'Orange',
    key: 'Orange',
  },
];

here is defaultselectedcheckbox objects:

const defaultCheckedList = [
  { name: 'parrent-1', key: 'parrent-1' },
  {
    name: 'Orange',
    key: 'Orange',
  },
];

here is the method of my trying effort code

  const [checkedList, setCheckedList] = React.useState(
    defaultCheckedList.map((item) => item.name)
  );
  const [indeterminate, setIndeterminate] = React.useState(true);
  const [checkAll, setCheckAll] = React.useState(false);

when user click on the checkbox the value will be checked and unchecked

  const onChange = (list) => {
    setCheckedList(list);
    setIndeterminate(!!list.length && list.length < plainOptions1.length);
    setCheckAll(list.length === plainOptions1.length);
  };

when user click on partent -1 , value will check under parrent-1

  const onCheckAllChange = (e) => {
    setCheckedList(
      e.target.checked ? plainOptions1.map((item) => item.name) : []
    );
    setIndeterminate(false);
    setCheckAll(e.target.checked);
  };

  {plainOptions1.map((item) => (
    <div>
      <Checkbox
        indeterminate={indeterminate}
        onChange={onCheckAllChange}
        checked={checkAll}
      >
        {item.name}
      </Checkbox>
      <Divider />

      <CheckboxGroup
        options={item && item.child && item.child.map((item) => item.name)}
        value={checkedList}
        onChange={onChange}
      />
      <Divider />
    </div>

Here is my trying code : https://stackblitz.com/edit/react-xb1tkk

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda