!17 [修复] select组件中外部参数变更后组件内的数值变化不生效的问题

Merge pull request !17 from 鄢鹏权/develop
This commit is contained in:
就眠儀式 2022-01-08 04:47:16 +00:00 committed by Gitee
commit de3b058f07
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
5 changed files with 276 additions and 208 deletions

View File

@ -27,6 +27,40 @@ export default {
}
</script>
::: title 数据联动
:::
::: demo
<template>
<lay-select v-model="value">
<lay-select-option value="1" label="学习"></lay-select-option>
<lay-select-option value="2" label="编码"></lay-select-option>
<lay-select-option value="3" label="运动"></lay-select-option>
</lay-select>
<lay-button @click="change1">点击切换(当前值:{{value}})</lay-button>
</template>
<script>
import { ref } from 'vue'
export default {
name:'selectChange',
setup() {
const value = ref(null);
var i = 1;
function change1(){
value.value=i++%3+1
}
return {
value,
change1
}
}
}
</script>
:::
::: title 选择禁用
@ -74,6 +108,7 @@ export default {
<lay-select-option value="6" label="打篮球"></lay-select-option>
<lay-select-option value="7" label="rap"></lay-select-option>
</lay-select>
<lay-button @click="mvalue=[1,5,7]">点击切换(当前值:{{mvalue.join()}})</lay-button>
</template>
<script>

View File

@ -48,6 +48,7 @@
到第
<input
v-model="currentPageShow"
@keypress.enter="jumpPage()"
type="number"
class="layui-input layui-input-number"
/>

View File

@ -151,16 +151,21 @@ watch(
}
);
watch(
() => props.modelValue,
(value) => {
watch(props, () => {
let value = props.modelValue;
if (props.multiple) {
if (Array.isArray(value)) {
selectItem.value.value = value;
if (!value && value !== 0) {
props.multiple && (selectItem.value.value = []);
selectItem.value.label = props.multiple ? [] : null;
selectItem.value.label = value.map((o) => ItemsMap.value[o]);
} else {
console.error("多选时请传入数组值");
}
} else {
selectItem.value.value = value;
//@ts-ignore
selectItem.value.label = ItemsMap.value[value] || "";
}
);
});
//
const disabledItemMap: { [key: string | number]: boolean } = {};
@ -202,7 +207,14 @@ const removeItemHandle = function (e: MouseEvent, _selectItem: SelectItem) {
e.stopPropagation();
selectItemHandle(_selectItem, false);
};
const ItemsMap: Ref<{ [index: string]: string }> = ref({});
const selectItemPush = function (p: SelectItem) {
if (p.value !== null) {
//@ts-ignore
ItemsMap.value[p.value] = p.label;
}
};
provide("selectItemHandle", selectItemHandle);
provide("selectItem", selectItem);
provide("selectItemPush", selectItemPush);
</script>

View File

@ -24,7 +24,7 @@ export default {
<script setup lang="ts">
import LayCheckbox from "../checkbox";
import { SelectItem, SelectItemHandle } from "../type";
import { SelectItem, SelectItemHandle, SelectItemPush } from "../type";
import { computed, defineProps, inject, onMounted, Ref } from "vue";
const props = withDefaults(
@ -40,26 +40,42 @@ const props = withDefaults(
const selectItemHandle = inject("selectItemHandle") as SelectItemHandle;
const selectItem = inject("selectItem") as Ref<SelectItem>;
const selectItemPush = inject("selectItemPush") as Ref<SelectItemPush>;
const selectHandle = function () {
!props.disabled && callSelectItemHandle(!selected.value);
}
const callSelectItemHandle = function(isChecked ?: boolean){
selectItemHandle({
value : props.value,
label : props.label,
disabled : props.disabled
}, isChecked);
}
};
const callSelectItemHandle = function (isChecked?: boolean) {
// console.log("callSelectItemHandle");
selectItemHandle(
{
value: props.value,
label: props.label,
disabled: props.disabled,
},
isChecked
);
};
const selected = computed({
get(){
get() {
const selectValues = selectItem.value.value;
if (Array.isArray(selectValues)) {
return (selectValues as any[]).indexOf(props.value) > -1;
}
return selectItem.value.value === props.value
return selectItem.value.value === props.value;
},
set(val){}
})
onMounted(() => selected.value && callSelectItemHandle())
set(val) {},
});
const callSelectItemPush = function () {
let item = {
value: props.value,
label: props.label,
disabled: props.disabled,
};
selectItemPush(item);
};
onMounted(() => {
callSelectItemPush();
selected.value && callSelectItemHandle();
});
</script>

View File

@ -9,3 +9,7 @@ export interface SelectItem {
export interface SelectItemHandle {
(selectItem: SelectItem, isChecked?: boolean): void;
}
export interface SelectItemPush {
(selectItem: SelectItem): void
}