Skip to content

Commit 7a64eb5

Browse files
author
puhui999
committed
商品管理: 修正添加属性和添加属性值时输入框显隐控制、输入框焦点获取错乱等bug
1 parent 61218ae commit 7a64eb5

File tree

3 files changed

+26
-22
lines changed

3 files changed

+26
-22
lines changed

src/views/mall/product/management/components/BasicInfoForm.vue

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ import ProductAttributes from './ProductAttributes.vue'
9898
import { copyValueToTarget } from '@/utils/object'
9999
// 业务Api
100100
import * as ProductCategoryApi from '@/api/mall/product/category'
101-
import * as PropertyApi from '@/api/mall/product/property'
102101
import { defaultProps, handleTree } from '@/utils/tree'
103102
import { ElInput } from 'element-plus'
104103
@@ -116,12 +115,11 @@ const attributeList = ref([
116115
{
117116
id: 1,
118117
name: '颜色',
119-
attributeValues: [{ id: 1, name: '白色' }]
118+
values: [{ id: 1, name: '白色' }]
120119
}
121120
])
122-
const addAttribute = async (propertyId: number) => {
123-
const data = await PropertyApi.getPropertyValuePage({ id: propertyId })
124-
console.log(data)
121+
const addAttribute = (property: any) => {
122+
attributeList.value.push(property)
125123
}
126124
const formData = reactive<SpuType>({
127125
name: '', // 商品名称
@@ -132,7 +130,6 @@ const formData = reactive<SpuType>({
132130
sliderPicUrls: [], // 商品轮播图
133131
introduction: '', // 商品简介
134132
deliveryTemplateId: 1, // 运费模版
135-
selectRule: '', // 选择规则 TODO 暂定
136133
specType: false, // 商品规格
137134
subCommissionType: false, // 分销类型
138135
skus: [

src/views/mall/product/management/components/ProductAttributes.vue

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
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"
@@ -17,15 +17,20 @@
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>
@@ -37,8 +42,13 @@
3742
import { ElInput } from 'element-plus'
3843
3944
const 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
4252
const attributeList = ref([])
4353
const props = defineProps({
4454
attributeData: {
@@ -60,23 +70,20 @@ watch(
6070
)
6171
/** 删除标签 tagValue 标签值*/
6272
const 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
/** 输入框失去焦点或点击回车时触发 */
7482
const 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>

src/views/mall/product/management/components/ProductAttributesAddForm.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ const submitForm = async () => {
6262
const propertyId = await PropertyApi.createProperty(data)
6363
emit('success', { id: propertyId, ...formData.value, values: [] })
6464
} else {
65-
emit(res[0]) // 因为只用一个
65+
emit('success', res[0]) // 因为只用一个
6666
}
6767
message.success(t('common.createSuccess'))
6868
dialogVisible.value = false

0 commit comments

Comments
 (0)