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

select options 若存在 {value: 0, lable;'0'} 这项 即options 有一项 value等于数字0 会呈现失败 #1727

Closed
1 task done
SCWR opened this issue Jan 15, 2020 · 5 comments

Comments

@SCWR
Copy link

SCWR commented Jan 15, 2020

  • I have searched the issues of this repository and believe that this is not a duplicate.

Version

1.4.10

Environment

win10 chrome78 vue2.6.10

Reproduction link

Edit on CodeSandbox

Steps to reproduce

<template>
<div style="margin: 0 auto;">
<a-select :defaultValue="0" :options="options" style="width: 120px"/>
</div>
</template>
<script>
import { Select } from 'ant-design-vue';
export default {
components: {
ASelect: Select,
ASelectOption: Select.Option,
},
data () {
return {
options: [{
value: 0,
label: '0'
},{
value: 1,
label: '1'
},{
value: 2,
label: '2'
},]
}
}
}
</script>

点击下拉框 会发现 紧跟着的第二项也被高亮了
GIF 2020-1-15 11-42-51

What is expected?

第二项正确显示

What is actually happening?

第二项错误被高亮了

@SCWR
Copy link
Author

SCWR commented Jan 15, 2020

查看源码发现 select 组件会在展开下拉框 默认去寻找激活项

但是代码中忽略 对非false值即 '' 0 导致以上bug出现

若有选中项 则默认选中选项为激活项

DropdownMenu.jsx#L136 中以下代码导致非false值跳过

selectedKeys[0] || firstActiveValue;

if (selectedKeys.length || firstActiveValue) {
  if (props.visible && !this.lastVisible) {
-->    activeKeyProps.activeKey = selectedKeys[0] || firstActiveValue;
  } else if (!visible) {
    // Do not trigger auto active since we already have selectedKeys
    if (selectedKeys[0]) {
      defaultActiveFirst = false;
    }
    activeKeyProps.activeKey = undefined;
  }
}

选中值为 0时 被处理

若无选中项 则寻找第一个项为激活项

SubPopupMenu.jsx#L67 中以下代码导致非false值跳过
if (!activeKey && c && !propsData.disabled)

if (defaultActiveFirst) {
 loopMenuItem(children, (c, i) => {
   const propsData = c.componentOptions.propsData || {};
-->    if (!activeKey && c && !propsData.disabled) {
     activeKey = getKeyFromChildrenIndex(c, eventKey, i);
   }
 });
 return activeKey;
}

找到第一个为 0 时 再次判定被逻辑跳过了

@tangjinzhou
Copy link
Member

方便的话可以提交个pr and 建议先不要使用数字 0 隐藏风险挺高的

@nongzhenli
Copy link

查看源码发现 select 组件会在展开下拉框 默认去寻找激活项

但是代码中忽略 对非false值即 '' 0 导致以上bug出现

若有选中项 则默认选中选项为激活项

DropdownMenu.jsx#L136 中以下代码导致非false值跳过

selectedKeys[0] || firstActiveValue;

if (selectedKeys.length || firstActiveValue) {
  if (props.visible && !this.lastVisible) {
-->    activeKeyProps.activeKey = selectedKeys[0] || firstActiveValue;
  } else if (!visible) {
    // Do not trigger auto active since we already have selectedKeys
    if (selectedKeys[0]) {
      defaultActiveFirst = false;
    }
    activeKeyProps.activeKey = undefined;
  }
}

选中值为 0时 被处理

若无选中项 则寻找第一个项为激活项

SubPopupMenu.jsx#L67 中以下代码导致非false值跳过
if (!activeKey && c && !propsData.disabled)

if (defaultActiveFirst) {
 loopMenuItem(children, (c, i) => {
   const propsData = c.componentOptions.propsData || {};
-->    if (!activeKey && c && !propsData.disabled) {
     activeKey = getKeyFromChildrenIndex(c, eventKey, i);
   }
 });
 return activeKey;
}

找到第一个为 0 时 再次判定被逻辑跳过了

你好!我现在就遇到这么一个需求!
下拉搜索框中,必须为点击才是选中项,否则鼠标离开后,搜索框内容依然为选中值!
但是现在如您描述 默认选中第一项 这个问题困扰了!请问怎么解决呢?现有方法仅是改源码么?

@SCWR
Copy link
Author

SCWR commented Feb 20, 2020

查看源码发现 select 组件会在展开下拉框 默认去寻找激活项
但是代码中忽略 对非false值即 '' 0 导致以上bug出现

若有选中项 则默认选中选项为激活项

DropdownMenu.jsx#L136 中以下代码导致非false值跳过
selectedKeys[0] || firstActiveValue;

if (selectedKeys.length || firstActiveValue) {
  if (props.visible && !this.lastVisible) {
-->    activeKeyProps.activeKey = selectedKeys[0] || firstActiveValue;
  } else if (!visible) {
    // Do not trigger auto active since we already have selectedKeys
    if (selectedKeys[0]) {
      defaultActiveFirst = false;
    }
    activeKeyProps.activeKey = undefined;
  }
}

选中值为 0时 被处理

若无选中项 则寻找第一个项为激活项

SubPopupMenu.jsx#L67 中以下代码导致非false值跳过
if (!activeKey && c && !propsData.disabled)

if (defaultActiveFirst) {
 loopMenuItem(children, (c, i) => {
   const propsData = c.componentOptions.propsData || {};
-->    if (!activeKey && c && !propsData.disabled) {
     activeKey = getKeyFromChildrenIndex(c, eventKey, i);
   }
 });
 return activeKey;
}

找到第一个为 0 时 再次判定被逻辑跳过了

你好!我现在就遇到这么一个需求!
下拉搜索框中,必须为点击才是选中项,否则鼠标离开后,搜索框内容依然为选中值!
但是现在如您描述 默认选中第一项 这个问题困扰了!请问怎么解决呢?现有方法仅是改源码么?
@nongzhenli
不好意思
过年 现在才看到
官方组件 有隐藏的 prop
a-select的

v-bind:defaultActiveFirstOption="false" 

不去调用这部分逻辑就行

jy0529 added a commit to jy0529/ant-design-vue that referenced this issue Mar 29, 2020
tangjinzhou pushed a commit that referenced this issue Apr 4, 2020
#1979)

* fix: defaultActiveFirstOption is true and activeKey is Falsy will return the next child key #1727

* fix: defaultActiveFirstOption is true and activeKey is undefined #1727
@github-actions
Copy link

github-actions bot commented Aug 9, 2021

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 9, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants