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

nselect动态创建,会允许创建和已有一模一样的label的选项 #4703

Closed
zz541843 opened this issue Apr 10, 2023 · 1 comment · Fixed by #4713
Closed

nselect动态创建,会允许创建和已有一模一样的label的选项 #4703

zz541843 opened this issue Apr 10, 2023 · 1 comment · Fixed by #4713
Labels
untriaged need to sort

Comments

@zz541843
Copy link

TuSimple/naive-ui version (版本)

2.34.3

Vue version (Vue 版本)

3.3.0-alpha.9

Browser and its version (浏览器及其版本)

100

System and its version (系统及其版本)

win11

Node version (Node 版本)

Reappearance link (重现链接)

https://www.naiveui.com/zh-CN/light/components/select#tag.vue

Reappearance steps (重现步骤)

官网的例子用下就行了,比如Girl这个选项,手动输入Girl,会有一个自定义的Girl和本身存在的Girl,手动输入和options里的某个label一样的情况的时候,应该不显示自定义创建的选项了吧

Expected results (期望的结果)

1

Actual results (实际的结果)

1

Remarks (补充说明)

@github-actions github-actions bot added the untriaged need to sort label Apr 10, 2023
@LanjianNUll
Copy link
Contributor

LanjianNUll commented Apr 12, 2023

个人理解: 这个应该不算bug,在实际业务中,可以通过 指定onCreate 来生成 输入 对应的value。 因为nselect 默认是通过比较 value来做搜索,而不是label。 当然也可以指定 value-field 的比较label,但是这也会影响到最总nselect的取值。

源码逻辑片段

if (tag && !remote) {
        if (!value) {
          beingCreatedOptionsRef.value = emptyArray
          return
        }
        const { onCreate } = props
        const optionBeingCreated = onCreate
          ? onCreate(value)
          : { [props.labelField]: value, [props.valueField]: value }
        const { valueField } = props
        if (
          compitableOptionsRef.value.some(
            (option) => option[valueField] === optionBeingCreated[valueField]
          ) ||
          createdOptionsRef.value.some(
            (option) => option[valueField] === optionBeingCreated[valueField]
          )
        ) {
          beingCreatedOptionsRef.value = emptyArray
        } else {
          beingCreatedOptionsRef.value = [optionBeingCreated]
        }
      }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
untriaged need to sort
Projects
None yet
2 participants