Skip to content

The select component is in uncontrolled mode. After selecting the value, modify the options, and the label of the value does not follow the modification #45734

Open
react-component/select
#1007

Description

@ouzhou

Reproduction link

Edit on CodeSandbox

Steps to reproduce

step1: choose any option

step2: click change language button

step3: click select to see options

What is expected?

  <p>
    I hope the selected value should automatically change the language, but
    currently only the option has changed the language
  </p>

What is actually happening?

  <p>
    I hope the selected value should automatically change the language, but
    currently only the option has changed the language
  </p>
Environment Info
antd 5.11.0
React 18.2
System macos
Browser chrome

Activity

  1. ouzhou commented on Nov 8, 2023

    @ouzhou
    Author

    find a solution, just remove undefined determine below

            if (rawLabel === undefined)
                  rawLabel = option?.[optionLabelProp || mergedFieldNames.label];
                if (rawKey === undefined) rawKey = option?.key ?? rawValue;
    

    to

         rawLabel = option?.[optionLabelProp || mergedFieldNames.label];
         rawKey = option?.key ?? rawValue;
    
    

    https://github.com/react-component/select/blob/ec0c6c748a24aa500ede15f1d2aacd0dacf4f63b/src/Select.tsx#L275
    https://github.com/react-component/select/blob/ec0c6c748a24aa500ede15f1d2aacd0dacf4f63b/src/Select.tsx#L277

  2. ouzhou commented on Nov 8, 2023

    @ouzhou
    Author

    because when component re-render, rawLabel and rawKey not be re-assign value

  3. chenmijiang commented on Nov 9, 2023

    @chenmijiang

    for controlled components, your usage of the "onChange" method is wrong. It is not a native method. Just pass the value directly, like this:
    image

  4. ouzhou commented on Nov 9, 2023

    @ouzhou
    Author

    i fixed the demo

  5. added
    🐛 BugAnt Design Team had proved that this is a bug.
    and removed on Nov 10, 2023
  6. bbb169 commented on Dec 22, 2023

    @bbb169
    Contributor

    image
    https://github.com/react-component/select/blob/ec0c6c748a24aa500ede15f1d2aacd0dacf4f63b/src/Select.tsx#L289
    本质还是internalValue这个值在select选择的时候会被triggerChange改变,当新的选项刷新试就会走到这个逻辑,要不要在这块增加一个逻辑,如果新旧label不匹配,就更新internalValue为新的值呢

  7. afc163 commented on Dec 22, 2023

    @afc163
    Member

    @bbb169 思路是可以的,但是最好不要在 convert2LabelValues 里做。convert2LabelValues 最好是个纯函数,不应该加副作用。

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

    Inactive🐛 BugAnt Design Team had proved that this is a bug.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions