Skip to content

fix(MdInput): Avoid InputEvent object from @input event #1196

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Nov 27, 2017
23 changes: 16 additions & 7 deletions src/components/MdField/MdFieldMixin.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export default {
props: {
value: {
required: true
default: null
Copy link
Member

@marcosmoura marcosmoura Nov 23, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a better approach to this is value: {}
This will allow any type.

},
placeholder: String,
maxlength: [String, Number],
Expand All @@ -10,18 +10,21 @@ export default {
disabled: Boolean,
mdCounter: [String, Number]
},
data: () => ({
textareaHeight: false
}),
data () {
return {
localValue: this.value,
textareaHeight: false
}
},
computed: {
model: {
get () {
return this.value
return this.localValue
},
set (value) {
if (value.constructor.name.toLowerCase() !== 'inputevent') {
this.$nextTick(() => {
this.$emit('input', value)
this.localValue = value
})
}
}
Expand Down Expand Up @@ -66,6 +69,12 @@ export default {
},
mdCounter () {
this.setMaxlength()
},
localValue (val) {
this.$emit('input', val)
},
value (val) {
this.localValue = val
}
},
methods: {
Expand All @@ -87,7 +96,7 @@ export default {
}
}
},
setFieldValue (value) {
setFieldValue () {
this.MdField.value = this.model
},
setPlaceholder () {
Expand Down
8 changes: 7 additions & 1 deletion src/components/MdField/MdInput/MdInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
class="md-input"
v-model="model"
v-bind="attributes"
v-on="$listeners"
v-on="listeners"
@focus="onFocus"
@blur="onBlur">
</template>
Expand Down Expand Up @@ -33,6 +33,12 @@
},
isPassword () {
return this.type === 'password'
},
listeners () {
return {
...this.$listeners,
input: event => this.$emit('input', event.target.value)
}
}
},
watch: {
Expand Down
3 changes: 2 additions & 1 deletion src/components/MdField/MdSelect/MdOption.vue
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
return this.MdOptgroup.disabled || this.disabled
},
key () {
return this.value || this.uniqueId
let isSet = (this.value || this.value === 0)
return isSet ? this.value : this.uniqueId
},
inputLabel () {
return this.MdSelect.label
Expand Down
95 changes: 62 additions & 33 deletions src/components/MdField/MdSelect/MdSelect.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
:disabled="disabled"
:required="required"
:placeholder="placeholder"
v-on="$listeners"
v-on="inputListeners"
v-bind="$attrs"
@focus.prevent="onFocus"
@blur.prevent="removeHighlight"
Expand Down Expand Up @@ -75,35 +75,43 @@
name: String
},
inject: ['MdField'],
data: () => ({
uniqueId: 'md-select-menu-' + MdUuid(),
menuStyles: {},
offset: {
x: defaultOffset.x,
y: 0
},
showSelect: true,
didMount: false,
MdSelect: {
items: {},
label: null,
multiple: false,
modelValue: null
data () {
return {
uniqueId: 'md-select-menu-' + MdUuid(),
menuStyles: {},
offset: {
x: defaultOffset.x,
y: 0
},
showSelect: true,
didMount: false,
MdSelect: {
items: {},
label: null,
multiple: false,
modelValue: this.model,
setValue: this.setValue,
setContent: this.setContent,
setMultipleValue: this.setMultipleValue,
setMultipleContent: this.setMultipleContent
}
}
}),
},
provide () {
const MdSelect = this.MdSelect

MdSelect.setValue = this.setValue
MdSelect.setContent = this.setContent
MdSelect.setMultipleValue = this.setMultipleValue
MdSelect.setMultipleContent = this.setMultipleContent
MdSelect.modelValue = this.model

return { MdSelect }
},
computed: {
inputListeners () {
return {
...this.$listeners,
input: undefined
}
}
},
watch: {
value: {
localValue: {
immediate: true,
handler () {
this.setFieldContent()
Expand All @@ -113,7 +121,11 @@
immediate: true,
handler (isMultiple) {
this.MdSelect.multiple = isMultiple
this.$nextTick(() => this.initialLocalValueByDefault())
}
},
model () {
this.MdSelect.modelValue = this.model
}
},
methods: {
Expand Down Expand Up @@ -180,14 +192,19 @@
this.showSelect = true
}
},
toggleArrayValue (array, value) {
if (array.includes(value)) {
const index = array.indexOf(value)

array.splice(index, 1)
} else {
array.push(value)
arrayAccessorRemove (arr, index) {
let before = arr.slice(0, index)
let after = arr.slice(index + 1, arr.length)
return before.concat(after)
},
toggleArrayValue (value) {
let index = this.localValue.indexOf(value)
let includes = index > -1
if (!includes) {
this.localValue = this.localValue.concat([value])
return
}
this.localValue = this.arrayAccessorRemove(this.localValue, index)
},
setValue (newValue) {
this.model = newValue
Expand All @@ -198,7 +215,7 @@
this.MdSelect.label = newLabel
},
setContentByValue () {
const textContent = this.MdSelect.items[this.value]
const textContent = this.MdSelect.items[this.localValue]

if (textContent) {
this.setContent(textContent)
Expand All @@ -209,13 +226,14 @@
setMultipleValue (value) {
const newValue = value

this.toggleArrayValue(this.model, newValue)
this.toggleArrayValue(newValue)
this.setFieldValue()
},
setMultipleContentByValue () {
if (!this.localValue) this.initialLocalValueByDefault()
Copy link
Member

@marcosmoura marcosmoura Nov 23, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To improve code style, change this if to be inside brackets:
if ( ... ) {
...
}

let content = []

this.value.forEach(item => {
this.localValue.forEach(item => {
const textContent = this.MdSelect.items[item]

if (textContent) {
Expand All @@ -231,6 +249,17 @@
} else {
this.setContentByValue()
}
},
initialLocalValueByDefault () {
let isArray = Array.isArray(this.localValue)
if (this.multiple && !isArray) {
let isSet = this.localValue !== undefined && this.localValue !== null
this.localValue = isSet ? [this.localValue] : []
return
}
if (!this.multiple && isArray) {
this.localValue = this.localValue.length > 0 ? this.localValue[0] : null
}
}
},
async mounted () {
Expand Down