Skip to content

Commit

Permalink
fix: input-item slot render error (didi#368)
Browse files Browse the repository at this point in the history
* fix(input-item): remove prop key when use error slot

* fix: input-item slot render error
  • Loading branch information
gebilaoxiong authored and moyus committed Mar 21, 2019
1 parent c0b1895 commit 83ea19c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 14 deletions.
3 changes: 0 additions & 3 deletions components/input-item/demo/cases/demo5.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
type="bankCard"
title="储蓄卡号"
v-model="bankCardNo"
:key="bankCardKey"
clearable
@blur="checkBankCard"
>
Expand Down Expand Up @@ -43,7 +42,6 @@ export default {
data() {
return {
bankCardNo: '',
bankCardKey: Date.now(),
isError: false,
}
},
Expand All @@ -54,7 +52,6 @@ export default {
} else {
this.isError = false
}
this.bankCardKey = Date.now()
},
bankCardTip() {
Dialog.alert({
Expand Down
21 changes: 10 additions & 11 deletions components/input-item/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
isTitleLatent ? 'is-title-latent' : '',
isInputActive ? 'is-active' : '',
isInputFocus ? 'is-focus' : '',
isInputError ? 'is-error' : '',
isInputBrief ? 'with-brief' : '',
isInputError() ? 'is-error' : '',
isInputBrief() && !isInputError() ? 'with-brief' : '',
isDisabled ? 'is-disabled': '',
isAmount ? 'is-amount': '',
clearable ? 'is-clear' : '',
Expand Down Expand Up @@ -86,14 +86,14 @@
<!-- BRIEF/ERROR TIP -->
<!-- -------------------- -->
<div
v-if="isInputError"
v-if="isInputError()"
class="md-input-item-msg"
>
<p v-if="error !== ''" v-text="error"></p>
<slot name="error" v-else></slot>
</div>
<div
v-if="isInputBrief"
v-if="isInputBrief() && !isInputError()"
class="md-input-item-brief"
>
<p v-if="brief !== ''" v-text="brief"></p>
Expand Down Expand Up @@ -279,12 +279,6 @@ export default {
isInputEmpty() {
return !this.inputValue.length
},
isInputError() {
return this.$slots.error || this.error !== ''
},
isInputBrief() {
return (this.$slots.brief || this.brief !== '') && !this.isInputError
},
isDisabled() {
return this.rootField.disabled || this.disabled
},
Expand Down Expand Up @@ -316,7 +310,6 @@ export default {
}
},
},
created() {
this.inputValue = this.$_formateValue(this.$_subValue(this.value + '')).value
},
Expand Down Expand Up @@ -397,6 +390,12 @@ export default {
return formateValue
},
isInputError() {
return this.$slots.error || this.error !== ''
},
isInputBrief() {
return this.$slots.brief || this.brief !== ''
},
$_trimValue(val) {
return trimValue(val, '\\s|,')
},
Expand Down
37 changes: 37 additions & 0 deletions components/input-item/test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,41 @@ describe('InputItem - Operation', () => {

input.trigger('click')
})

test('should show error slot', async () => {
wrapper = mount({
template: `
<md-input-item
type="bankCard"
title="label">
<p
v-if="isError"
class="error"
slot="error">
errorMsg
</p>
</md-input-item>
`,
components: {
[InputItem.name]: InputItem,
},
data() {
return {
isError: false,
}
},
})

expect(wrapper.contains('.md-input-item-msg')).toBe(false)
wrapper.vm.isError = true
await wrapper.vm.$nextTick(() => {
expect(wrapper.contains('.md-input-item-msg')).toBe(true)
expect(wrapper.find('.md-input-item-msg').text()).toBe('errorMsg')
})

wrapper.vm.isError = false
await wrapper.vm.$nextTick(() => {
expect(wrapper.contains('.md-input-item-msg')).toBe(false)
})
})
})

0 comments on commit 83ea19c

Please sign in to comment.