(component): 优化 select-option 代码逻辑

This commit is contained in:
就眠儀式 2022-11-11 09:57:08 +08:00
parent 5a9aef0923
commit 6f95ba2fae
4 changed files with 33 additions and 42 deletions

View File

@ -13,12 +13,12 @@ import { CheckboxSize } from "./interface";
export interface CheckboxProps { export interface CheckboxProps {
name?: string; name?: string;
skin?: string; skin?: string;
value: string | number | object;
label?: string; label?: string;
isIndeterminate?: boolean; value: string | number | object;
modelValue?: boolean | Array<string | number | object>; modelValue?: boolean | Array<string | number | object>;
disabled?: boolean; isIndeterminate?: boolean;
size?: CheckboxSize; size?: CheckboxSize;
disabled?: boolean;
} }
const props = withDefaults(defineProps<CheckboxProps>(), { const props = withDefaults(defineProps<CheckboxProps>(), {
@ -113,6 +113,8 @@ const isDisabled = computed(() => {
} }
return false; return false;
}); });
defineExpose({ toggle: handleClick });
</script> </script>
<template> <template>

View File

@ -20,7 +20,7 @@ import {
cloneVNode, cloneVNode,
useAttrs, useAttrs,
StyleValue, StyleValue,
PropType, PropType,
} from "vue"; } from "vue";
import { import {
computed, computed,

View File

@ -12,7 +12,7 @@ import {
inject, inject,
WritableComputedRef, WritableComputedRef,
Ref, Ref,
onMounted, ref
} from "vue"; } from "vue";
export interface SelectOptionProps { export interface SelectOptionProps {
@ -30,17 +30,22 @@ const props = withDefaults(defineProps<SelectOptionProps>(), {
const searchValue: Ref<string> = inject("searchValue") as Ref<string>; const searchValue: Ref<string> = inject("searchValue") as Ref<string>;
const selectRef: Ref<HTMLElement> = inject("selectRef") as Ref<HTMLElement>; const selectRef: Ref<HTMLElement> = inject("selectRef") as Ref<HTMLElement>;
const selectedValue: WritableComputedRef<any> = inject( const selectedValue: WritableComputedRef<any> = inject("selectedValue") as WritableComputedRef<any>;
"selectedValue"
) as WritableComputedRef<any>;
const multiple: ComputedRef = inject("multiple") as ComputedRef; const multiple: ComputedRef = inject("multiple") as ComputedRef;
const checkboxRef = ref<HTMLElement>();
const handleSelect = () => { const handleSelect = () => {
if (!multiple.value && !props.disabled) { if(multiple.value) {
// @ts-ignore if(!props.disabled) {
selectRef.value.hide(); // @ts-ignore
selectedValue.value = props.value; checkboxRef.value?.toggle();
select(); }
} else {
if(!props.disabled) {
// @ts-ignore
selectRef.value.hide();
selectedValue.value = props.value;
}
} }
}; };
@ -52,18 +57,6 @@ const selected = computed(() => {
} }
}); });
const select = () => {
if (multiple.value) {
if (Array.isArray(selectedValue.value)) {
if (notChecked.value) selectedValue.value.push(props.value);
} else {
selectedValue.value = [props.value];
}
} else {
selectedValue.value = props.value;
}
};
const display = computed(() => { const display = computed(() => {
return ( return (
props.keyword?.toString().indexOf(searchValue.value) > -1 || props.keyword?.toString().indexOf(searchValue.value) > -1 ||
@ -71,34 +64,30 @@ const display = computed(() => {
); );
}); });
const notChecked = computed(() => { const classes = computed(() => {
return ( return [
selectedValue.value.find((item: any) => { 'layui-select-option',
return item === props.value; {
}) === undefined "layui-this": selected.value,
); "layui-disabled": props.disabled,
}); }
]
onMounted(() => { })
selected.value && select();
});
</script> </script>
<template> <template>
<dd <dd
v-show="display" v-show="display"
:class="[ :class="classes"
'layui-select-option',
{ 'layui-this': selected, 'layui-disabled': disabled },
]"
@click="handleSelect" @click="handleSelect"
> >
<template v-if="multiple"> <template v-if="multiple">
<lay-checkbox <lay-checkbox
skin="primary"
ref="checkboxRef"
v-model="selectedValue" v-model="selectedValue"
:disabled="disabled" :disabled="disabled"
:value="value" :value="value"
skin="primary"
></lay-checkbox> ></lay-checkbox>
</template> </template>
<slot>{{ label }}</slot> <slot>{{ label }}</slot>

View File

@ -18,9 +18,9 @@
<ul> <ul>
<li>[新增] upload 组件 text 属性, 设置上传描述。</li> <li>[新增] upload 组件 text 属性, 设置上传描述。</li>
<li>[新增] upload 组件 dragText 属性, 设置拖拽面板提示信息。</li> <li>[新增] upload 组件 dragText 属性, 设置拖拽面板提示信息。</li>
<li>[修复] select-option 组件 default 插槽内容为多层元素时, 使用 label 属性值作为回显。</li>
<li>[修复] input-number 组件 step 设置为小数时精度丢失的问题。</li> <li>[修复] input-number 组件 step 设置为小数时精度丢失的问题。</li>
<li>[修复] tooltip 组件临近屏幕边界, 三角位置显示错误。</li> <li>[修复] tooltip 组件临近屏幕边界, 三角位置显示错误。</li>
<li>[优化] select-option 组件 default 插槽。</li>
<li> <li>
</ul> </ul>
</li> </li>