From 387da872915b494cf2247ccc9ba7fa7777775155 Mon Sep 17 00:00:00 2001 From: Marcos Moura Date: Mon, 14 Nov 2016 03:33:04 -0200 Subject: [PATCH] Make inputs and textareas more reactive --- src/components/mdInputContainer/common.js | 54 +++++++++ src/components/mdInputContainer/mdInput.vue | 109 +++-------------- .../mdInputContainer/mdInputContainer.vue | 16 ++- .../mdInputContainer/mdTextarea.vue | 111 +++--------------- 4 files changed, 96 insertions(+), 194 deletions(-) create mode 100644 src/components/mdInputContainer/common.js diff --git a/src/components/mdInputContainer/common.js b/src/components/mdInputContainer/common.js new file mode 100644 index 0000000..513258f --- /dev/null +++ b/src/components/mdInputContainer/common.js @@ -0,0 +1,54 @@ +export default { + props: { + value: [String, Number], + disabled: Boolean, + required: Boolean, + maxlength: [String, Number], + placeholder: [String, Number] + }, + watch: { + disabled() { + this.setParentDisabled(); + }, + required() { + this.setParentRequired(); + }, + placeholder() { + this.setParentPlaceholder(); + }, + maxlength() { + this.handleMaxLength(); + } + }, + methods: { + handleMaxLength() { + this.parentContainer.enableCounter = this.maxlength > 0; + this.parentContainer.counterLength = this.maxlength; + }, + setParentValue() { + this.parentContainer.setValue(this.$el.value); + }, + setParentDisabled() { + this.parentContainer.isDisabled = this.disabled; + }, + setParentRequired() { + this.parentContainer.isRequired = this.required; + }, + setParentPlaceholder() { + this.parentContainer.hasPlaceholder = !!this.placeholder; + }, + onFocus() { + this.parentContainer.isFocused = true; + }, + onBlur() { + this.parentContainer.isFocused = false; + this.setParentValue(); + }, + onInput() { + this.setParentValue(); + this.parentContainer.inputLength = this.$el.value.length; + this.$emit('change', this.$el.value); + this.$emit('input', this.$el.value); + } + } +}; diff --git a/src/components/mdInputContainer/mdInput.vue b/src/components/mdInputContainer/mdInput.vue index 2c313e1..11d5a79 100644 --- a/src/components/mdInputContainer/mdInput.vue +++ b/src/components/mdInputContainer/mdInput.vue @@ -1,14 +1,12 @@