react-native-ui-lib icon indicating copy to clipboard operation
react-native-ui-lib copied to clipboard

Adding Picker.Items after initial render fails to retrieve labels for selected items

Open siteassist-benjamin opened this issue 2 years ago • 12 comments

Description

Related to

Unfortunately there is no mention of this online, I tried very hard to find it.

Steps to reproduce

const [templates, setTemplates] = useState([])

API.getTemplates().then(res => setTemplates(res))

return (
      <Picker
        value={value}
        getLabel={(item) => {
          // console.log("templates", templates);
          const name = templates.find((template) => template.id === item)?.name;
          return name;
        }}
        templates={templates}
      >
        {templates?.map(({ id, name }) => {
          return <PickerItem key={id} value={id} label={name} />;
        })}
      </Picker>

Expected behavior

Expected behavior would be for the input storing the currently selected value to show the selected label

Actual behavior

When an item is selected from the modal picker list - which has all the correct labels, the input field showing the selected item after the modal is closed is blank

Screenshots/Video

Screenshot 2023-01-19 at 19 48 54

Environment

  • React Native version: 0.69.7
  • React Native UI Lib version: 6.21.0

Affected platforms

  • [x ] Android
  • [x ] iOS
  • [ n/a] Web

siteassist-benjamin avatar Jan 19 '23 18:01 siteassist-benjamin

same to me, any solution for this ?

oottoohh avatar Jan 31 '23 05:01 oottoohh

after update to 7.0.0, the picker become strange. any solution for this?

illank86 avatar Feb 22 '23 02:02 illank86

It looks like Picker items get set as state only on initial creation: src/components/picker/index.tsx. In our app, we've been able to work around this by updating the key prop on the Picker when the items change to force a state reset.

brandon-hass avatar Mar 14 '23 13:03 brandon-hass

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar May 21 '23 08:05 stale[bot]

Any updates?

jiteshgolecha avatar May 23 '23 10:05 jiteshgolecha

I have found a workaround, add renderPicker to show the label.

<Picker topBarProps={{ useSafeArea: true }} showSearch={true} value={selectedProduct} placeholder="Products" onSearchChange={query => fetchProducts(query)} floatingPlaceholder fieldStyle={styles.withUnderline2} renderPicker={selectedItem => { console.log("selectedItem", selectedItem); var itm = _.find(productList, {id: selectedItem}) console.log("itm", itm); return ( <TextField placeholder="Product" floatingPlaceholder fieldStyle={styles.withUnderline} value={itm?.name} /> ) }} onChange={item => { console.log("productList", productList);console.log("item", item); setSelectedProduct(item); }} mode={Picker.modes.SINGLE} > { productList.map(option => ( <Picker.Item key={option.id} value={option.id} label={option.name} labelStyle={{paddingVertical: 10, paddingHorizontal: 10 }} /> )) } </Picker>

jiteshgolecha avatar May 23 '23 11:05 jiteshgolecha

@siteassist-benjamin @trofima I actually couldn't reproduce the issue. I tried with a similar example to the one you shared and everything works as expected. Can you please explain again what is the issue and share a video of the bug? Thanks!

lidord-wix avatar Jul 04 '23 08:07 lidord-wix

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Sep 17 '23 18:09 stale[bot]

I have the same problem with the newest version 7.8.0

DrZoidberg09 avatar Sep 28 '23 18:09 DrZoidberg09

The issue still persists on the latest version.

muratulashozturk avatar Oct 04 '23 07:10 muratulashozturk

I have the same issue. For me it occurs when I change the options that I supply to the picker after initially generated. So I have 2 pickers, and the options of the second picker are based on the value of the first picker. When I click on the second picker I can choose a new value, and that value is correctly selected, but it does not display as correctly selected. I am using the solution provided by @jiteshgolecha above

chris-hud avatar Oct 07 '23 22:10 chris-hud

Can you please try passing the items array prop to Picker component instead of rendering Picker.Item as children

ethanshar avatar Oct 09 '23 08:10 ethanshar

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Feb 01 '25 00:02 stale[bot]