Skip to content

itemKey 0 is ignored when selecting an item #762

Open

Description

@dajiaohuang

Describe the bug

Numeric item keys are allowed, but an item whose key is 0 is not matched by itemKey={0}. getSelectedItem checks if (item.key && key), so it falls back to matching value; when values are duplicated, it selects the first matching item instead of the item identified by key 0.

To Reproduce

  1. Render the component with the items and props shown below.
  2. Read the displayed input label.

Expected behavior

The input should display Zero key, because itemKey is 0 and numeric keys are part of the declared API.

Screenshots

n/a (the selected label is sufficient to observe this behavior).

Additional details

  • Device: Not verified on a device; this follows from the platform-independent item lookup in src/index.js.
  • OS: Not applicable to the lookup; runtime reproduction not performed.
  • react-native-picker-select version: 9.3.1 (audited source)
  • react-native version: Not run
  • expo sdk version: n/a

Reproduction and/or code sample

<RNPickerSelect
  items={[
    { label: 'First', value: 'same', key: 'first' },
    { label: 'Zero key', value: 'same', key: 0 },
  ]}
  itemKey={0}
  value="same"
  onValueChange={() => {}}
/>

The source-level example is self-contained. A hosted Snack was not prepared because runtime checks and installs were outside the assigned authorization.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions