react-component / react-component/select

filterOption for <OptGroup> may cause exception "TypeError: Cannot read property 'key' of null"

Open
#426 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
938
Forks
482
Avg merge
15h 8m
Merged PRs (30d)
2

Description

Actually I encountered this problem when using ant-design, here is the reproduction link:

https://codesandbox.io/s/antd-reproduction-template-t25es.

The cause condition:

  1. Use <OptGroup> in <Select>;
  2. In the <OptGroup>, add some conditional-generated <Option>, for example
const letters = [{ label: "a", value: "a" }, { label: "b", value: "b" }];

<Select.OptGroup label="L">
          {/** when no letters found, display a disabled message option */}
          {letters.length === 0 && (
            <Select.Option disabled key="_" value="_">
              No letters found.
            </Select.Option>
          )}
          {letters.map(item => (
            <Select.Option key={item.value} value={item.value}>
              {item.label}
            </Select.Option>
          ))}
        </Select.OptGroup>
  1. type letter L to fulfill the default filter for this <OptGroup>. It will immediately cause an exception as follow:
    image

It seems that when filtered the group, <Select> tries to iterate through each children of the <OptGroup> and get its key. But in this case, the children actually includes a false because of the conditional writing.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked CodeSandbox reproduction and trace the default filtering path for a Select containing an OptGroup with a conditional false child. Confirm the exception when typing “L”, then verify that filtering completes without error when the conditional child is absent.

Written by the indexing model from the issue text.

Assessment

Tech stack
react, typescript
Domain
frontend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.