Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] - Select not working correctly with numbers #2926

Open
jegork opened this issue May 1, 2024 · 3 comments · May be fixed by #2937
Open

[BUG] - Select not working correctly with numbers #2926

jegork opened this issue May 1, 2024 · 3 comments · May be fixed by #2937
Assignees
Labels
🐛 Type: Bug Something isn't working

Comments

@jegork
Copy link

jegork commented May 1, 2024

NextUI Version

2.3.6

Describe the bug

Select component seems to accept numbers, and given this type definitions

export type Key = string | number;
export type Selection = 'all' | Set<Key>;
onSelectionChange?: (keys: Selection) => any,

it indeed seems possible. However, when you provide numbers as items of Select, in onSelectionChange, keys is always Set<string>

Your Example Website or App

No response

Steps to Reproduce the Bug or Issue

<Select
          label="Test"
          items={[{ label: "2024", value: 2024 }]}
          onSelectionChange={(v) => {
            console.log(v);
          }}
        >
          {(item) => (
            <SelectItem key={item.value} value={item.value}>
              {item.label}
            </SelectItem>
          )}
        </Select>

when selecting a item, log statement prints {'2024'} (but should {2024})

Expected behavior

value in onSelectionChange should be Set<number>

Screenshots or Videos

No response

Operating System Version

macOS

Browser

Chrome

Copy link

linear bot commented May 1, 2024

@jegork
Copy link
Author

jegork commented May 1, 2024

Additionally, when you provide string[] to selectedKeys, e.g. [2020, 2021], then it cannot find these keys, therefore the Select dropdown does not show ticks next to selected items.

image

@ryo-manba ryo-manba self-assigned this May 3, 2024
@ryo-manba
Copy link
Member

ryo-manba commented May 3, 2024

Thanks for the issue!

This issue stems from React converting the key to a string.

Note that if you do specify a custom key on each Item using the key prop, React will convert the provided key to a string. As a result, all the collection component's key-related event handlers and props (e.g. onSelectionChange, selectedKey(s)) will expect and return strings.

See: https://react-spectrum.adobe.com/react-stately/collections.html#unique-keys

While it can be resolved by assigning an id to items as shown below, but the current situation requires a key, so some adjustments need to be made. I'm currently working on this fix.

let [years, setYears] = useState([
  {id: 1, value: 2024},
  {id: 2, value: 2025},
  {id: 3, value: 2026}
]);

<Select items={years}>
  {item => <SelectItem>{item.value}</SelectItem>}
</Select>

@ryo-manba ryo-manba added the 🐛 Type: Bug Something isn't working label May 3, 2024
@ryo-manba ryo-manba linked a pull request May 3, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛 Type: Bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants