77 <div >
88 <el-text class =" mx-1" >属性值:</el-text >
99 <el-tag
10- v-for =" (value, valueIndex) in item.attributeValues "
10+ v-for =" (value, valueIndex) in item.values "
1111 :key =" value.name"
1212 :disable-transitions =" false"
1313 class =" mx-1"
1717 {{ value.name }}
1818 </el-tag >
1919 <el-input
20- v-if =" inputVisible"
20+ v-show =" inputVisible(index) "
2121 ref =" InputRef"
2222 v-model =" inputValue"
2323 class =" !w-20"
2424 size =" small"
2525 @blur =" handleInputConfirm(index)"
2626 @keyup.enter =" handleInputConfirm(index)"
2727 />
28- <el-button v-else class =" button-new-tag ml-1" size =" small" @click =" showInput(index)" >
28+ <el-button
29+ v-show =" !inputVisible(index)"
30+ class =" button-new-tag ml-1"
31+ size =" small"
32+ @click =" showInput(index)"
33+ >
2934 + 添加
3035 </el-button >
3136 </div >
3742import { ElInput } from ' element-plus'
3843
3944const inputValue = ref (' ' ) // 输入框值
40- const inputVisible = ref (false ) // 输入框显隐控制
41- const InputRef = ref <InstanceType <typeof ElInput >>() // 标签输入框Ref
45+ const attributeIndex = ref <number | null >(null ) // 获取焦点时记录当前属性项的index
46+ // 输入框显隐控制
47+ const inputVisible = computed (() => (index ) => {
48+ if (attributeIndex .value === null ) return false
49+ if (attributeIndex .value === index ) return true
50+ })
51+ const InputRef = ref () // 标签输入框Ref
4252const attributeList = ref ([])
4353const props = defineProps ({
4454 attributeData: {
@@ -60,23 +70,20 @@ watch(
6070)
6171/** 删除标签 tagValue 标签值*/
6272const handleClose = (index , valueIndex ) => {
63- const av = attributeList .value [index ].attributeValues
64- av .splice (valueIndex , 1 )
73+ attributeList .value [index ].values ?.splice (valueIndex , 1 )
6574}
6675/** 显示输入框并获取焦点 */
67- const showInput = (index ) => {
68- inputVisible .value = true
69- nextTick (() => {
70- InputRef .value [index ]! .input ! .focus ()
71- })
76+ const showInput = async (index ) => {
77+ attributeIndex .value = index
78+ // 因为组件在ref中所以需要用索引获取对应的Ref
79+ InputRef .value [index ]! .input ! .focus ()
7280}
7381/** 输入框失去焦点或点击回车时触发 */
7482const handleInputConfirm = (index ) => {
7583 if (inputValue .value ) {
76- // 因为ref再循环里,所以需要index获取对应的ref
77- attributeList .value [index ].attributeValues .push ({ name: inputValue .value })
84+ attributeList .value [index ].values .push ({ name: inputValue .value })
7885 }
79- inputVisible .value = false
86+ attributeIndex .value = null
8087 inputValue .value = ' '
8188}
8289 </script >
0 commit comments