react-component / react-component/select
filterOption for <OptGroup> may cause exception "TypeError: Cannot read property 'key' of null"
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:
- Use
<OptGroup>in<Select>; - 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>
- type letter
Lto fulfill the default filter for this<OptGroup>. It will immediately cause an exception as follow:

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
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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