(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 {
name?: string;
skin?: string;
value: string | number | object;
label?: string;
isIndeterminate?: boolean;
value: string | number | object;
modelValue?: boolean | Array<string | number | object>;
disabled?: boolean;
isIndeterminate?: boolean;
size?: CheckboxSize;
disabled?: boolean;
}
const props = withDefaults(defineProps<CheckboxProps>(), {
@ -113,6 +113,8 @@ const isDisabled = computed(() => {
}
return false;
});
defineExpose({ toggle: handleClick });
</script>
<template>

View File

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

View File

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

View File

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