Skip to content
This repository was archived by the owner on Nov 30, 2020. It is now read-only.

Commit e1221c5

Browse files
committed
feat(textfield): Use MutationObserver for reactive slot updates (#92)
1 parent ea731be commit e1221c5

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

components/textfield/Textfield.vue

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ export default {
7070
data () {
7171
return {
7272
mdcTextField: undefined,
73-
mdcRipple: undefined
73+
mdcRipple: undefined,
74+
slotObserver: undefined
7475
}
7576
},
7677
computed: {
@@ -90,28 +91,35 @@ export default {
9091
}
9192
},
9293
mounted () {
93-
if (this.$slots.leadingIcon) {
94-
this.$slots.leadingIcon.map(n => {
95-
n.elm.classList.add('mdc-text-field__icon')
96-
})
97-
}
98-
99-
if (this.$slots.trailingIcon) {
100-
this.$slots.trailingIcon.map(n => {
101-
n.elm.classList.add('mdc-text-field__icon')
102-
})
103-
}
104-
94+
this.updateSlots()
95+
this.slotObserver = new MutationObserver( () => this.updateSlots())
96+
this.slotObserver.observe(this.$el, {
97+
childList: true,
98+
subtree: true
99+
})
105100
this.mdcTextField = MDCTextField.attachTo(this.$el)
106101
},
107102
beforeDestroy () {
103+
this.slotObserver.disconnect()
108104
this.mdcTextField.destroy()
109105
110106
if (typeof this.mdcRipple !== 'undefined') {
111107
this.mdcRipple.destroy()
112108
}
113109
},
114110
methods: {
111+
updateSlots () {
112+
if (this.$slots.leadingIcon) {
113+
this.$slots.leadingIcon.map(n => {
114+
n.elm.classList.add('mdc-text-field__icon')
115+
})
116+
}
117+
if (this.$slots.trailingIcon) {
118+
this.$slots.trailingIcon.map(n => {
119+
n.elm.classList.add('mdc-text-field__icon')
120+
})
121+
}
122+
},
115123
onInput (event) {
116124
debounce(this.$emit('input', event.target.value))
117125
}

0 commit comments

Comments
 (0)