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

fix: select multiple 支持undefined #345

Closed
wants to merge 1 commit into from
Closed

Conversation

winixt
Copy link
Collaborator

@winixt winixt commented Jul 14, 2023

What kind of change does this PR introduce? (check at least one)

  • Bugfix

Does this PR introduce a breaking change? (check one)

  • No

@@ -78,7 +78,7 @@ export const useArrayModel = (
updateCurrentValue(value);
return;
}
const val = computedValue.value;
const val = computedValue.value || [];
const index = val.indexOf(value);
if (index !== -1) {
val.splice(index, 1);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

以下是您提供的代码补丁,请允许我对其进行简要的代码审查。欢迎指出任何潜在的错误风险和改进建议:

@@ -78,7 +78,7 @@ export const useArrayModel = (
updateCurrentValue(value);
return;
}
-const val = computedValue.value;
+const val = computedValue.value || [];
const index = val.indexOf(value);
if (index !== -1) {
val.splice(index, 1);

代码审查意见:

  • 在原始代码中,const val = computedValue.value;将没有值的情况留给了后面的逻辑处理。通过将其修改为const val = computedValue.value || [];,如果computedValue.value为假值(如nullundefined),则会为val提供一个默认值[]。这样做可以防止报错或异常行为,并确保代码正常运行。
  • 除此之外,该代码补丁没有其他明显的错误或问题。根据提供的内容,无法确认更多的细节或上下文,因此无法提供更多改进建议。

请注意,以上反馈基于我能够理解的代码片段,并且只能提供相对概括的回答。对于准确的评估,建议您查看完整的代码并运行测试。

@@ -417,7 +417,7 @@ export default defineComponent({
watch(isOpenedRef, () => {
if (isOpenedRef.value) {
if (props.multiple) {
if (currentValue.value.length > 0) {
if (currentValue.value?.length > 0) {
hoverOptionValue.value = currentValue.value[0];
}
} else if (!isNil(currentValue.value)) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这段代码补丁主要涉及到一个 Vue 组件的更新。下面是对每个修改处的问题和可能的改进建议:

  • 第 151 行:当前的修复是调整了条件判断中 currentValue.value 的访问方式,从 currentValue.value.length 变成了 currentValue.value?.length。它在处理可选类型(null 或者数组)时更加安全,避免了潜在的空指针异常。这是一个良好的改进。

  • 第 239 行:原始代码使用严格相等运算符 (===) 比较 selectVal === null,而修复后的代码使用了相等运算符 (==) 进行比较。相等运算符会进行隐式类型转换,所以 selectVal 的值为 null 或者 undefined 时都可以通过条件。这是一种有效的改进。

  • 第 252 行:类似于第 151 行的改进,使用了可选链操作符 (?.) 来访问 selectVallength 属性。这是一个合理的改进。

  • 第 329 行:修复后的代码使用了括号将赋值表达式 (newValue || []) 包裹起来,确保即使 newValuenull 或者 undefined 也能返回一个空数组。这样做可以避免在调用 map 函数时出现空指针异常。这是一个良好的改进。

  • 第 417 行:修复后的代码使用了可选链操作符 (?.) 来访问 currentValue.valuelength 属性。这样处理可以避免可能发生的空指针异常。这也是一种有效的改进。

总体而言,这些修改都有助于代码的健壮性和可读性。操作符的调整以及对可能为空的值进行处理都是很好的实践。

@ocean-gao ocean-gao closed this Aug 14, 2023
@ocean-gao ocean-gao deleted the fix-select-undefined branch August 15, 2023 03:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants