123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360 |
- <template>
- <div :class="computedClasses" class="material-input__component">
- <div :class="{iconClass:icon}">
- <i v-if="icon" :class="['el-icon-' + icon]" class="el-input__icon material-input__icon" />
- <input
- v-if="type === 'email'"
- v-model="currentValue"
- :name="name"
- :placeholder="fillPlaceHolder"
- :readonly="readonly"
- :disabled="disabled"
- :autocomplete="autoComplete"
- :required="required"
- type="email"
- class="material-input"
- @focus="handleMdFocus"
- @blur="handleMdBlur"
- @input="handleModelInput"
- >
- <input
- v-if="type === 'url'"
- v-model="currentValue"
- :name="name"
- :placeholder="fillPlaceHolder"
- :readonly="readonly"
- :disabled="disabled"
- :autocomplete="autoComplete"
- :required="required"
- type="url"
- class="material-input"
- @focus="handleMdFocus"
- @blur="handleMdBlur"
- @input="handleModelInput"
- >
- <input
- v-if="type === 'number'"
- v-model="currentValue"
- :name="name"
- :placeholder="fillPlaceHolder"
- :step="step"
- :readonly="readonly"
- :disabled="disabled"
- :autocomplete="autoComplete"
- :max="max"
- :min="min"
- :minlength="minlength"
- :maxlength="maxlength"
- :required="required"
- type="number"
- class="material-input"
- @focus="handleMdFocus"
- @blur="handleMdBlur"
- @input="handleModelInput"
- >
- <input
- v-if="type === 'password'"
- v-model="currentValue"
- :name="name"
- :placeholder="fillPlaceHolder"
- :readonly="readonly"
- :disabled="disabled"
- :autocomplete="autoComplete"
- :max="max"
- :min="min"
- :required="required"
- type="password"
- class="material-input"
- @focus="handleMdFocus"
- @blur="handleMdBlur"
- @input="handleModelInput"
- >
- <input
- v-if="type === 'tel'"
- v-model="currentValue"
- :name="name"
- :placeholder="fillPlaceHolder"
- :readonly="readonly"
- :disabled="disabled"
- :autocomplete="autoComplete"
- :required="required"
- type="tel"
- class="material-input"
- @focus="handleMdFocus"
- @blur="handleMdBlur"
- @input="handleModelInput"
- >
- <input
- v-if="type === 'text'"
- v-model="currentValue"
- :name="name"
- :placeholder="fillPlaceHolder"
- :readonly="readonly"
- :disabled="disabled"
- :autocomplete="autoComplete"
- :minlength="minlength"
- :maxlength="maxlength"
- :required="required"
- type="text"
- class="material-input"
- @focus="handleMdFocus"
- @blur="handleMdBlur"
- @input="handleModelInput"
- >
- <span class="material-input-bar" />
- <label class="material-label">
- <slot />
- </label>
- </div>
- </div>
- </template>
- <script>
- // source:https://github.com/wemake-services/vue-material-input/blob/master/src/components/MaterialInput.vue
- export default {
- name: 'MdInput',
- props: {
- /* eslint-disable */
- icon: String,
- name: String,
- type: {
- type: String,
- default: 'text'
- },
- value: [String, Number],
- placeholder: String,
- readonly: Boolean,
- disabled: Boolean,
- min: String,
- max: String,
- step: String,
- minlength: Number,
- maxlength: Number,
- required: {
- type: Boolean,
- default: true
- },
- autoComplete: {
- type: String,
- default: 'off'
- },
- validateEvent: {
- type: Boolean,
- default: true
- }
- },
- data() {
- return {
- currentValue: this.value,
- focus: false,
- fillPlaceHolder: null
- }
- },
- computed: {
- computedClasses() {
- return {
- 'material--active': this.focus,
- 'material--disabled': this.disabled,
- 'material--raised': Boolean(this.focus || this.currentValue) // has value
- }
- }
- },
- watch: {
- value(newValue) {
- this.currentValue = newValue
- }
- },
- methods: {
- handleModelInput(event) {
- const value = event.target.value
- this.$emit('input', value)
- if (this.$parent.$options.componentName === 'ElFormItem') {
- if (this.validateEvent) {
- this.$parent.$emit('el.form.change', [value])
- }
- }
- this.$emit('change', value)
- },
- handleMdFocus(event) {
- this.focus = true
- this.$emit('focus', event)
- if (this.placeholder && this.placeholder !== '') {
- this.fillPlaceHolder = this.placeholder
- }
- },
- handleMdBlur(event) {
- this.focus = false
- this.$emit('blur', event)
- this.fillPlaceHolder = null
- if (this.$parent.$options.componentName === 'ElFormItem') {
- if (this.validateEvent) {
- this.$parent.$emit('el.form.blur', [this.currentValue])
- }
- }
- }
- }
- }
- </script>
- <style lang="scss" scoped>
- // Fonts:
- $font-size-base: 16px;
- $font-size-small: 18px;
- $font-size-smallest: 12px;
- $font-weight-normal: normal;
- $font-weight-bold: bold;
- $apixel: 1px;
- // Utils
- $spacer: 12px;
- $transition: 0.2s ease all;
- $index: 0px;
- $index-has-icon: 30px;
- // Theme:
- $color-white: white;
- $color-grey: #9E9E9E;
- $color-grey-light: #E0E0E0;
- $color-blue: #2196F3;
- $color-red: #F44336;
- $color-black: black;
- // Base clases:
- %base-bar-pseudo {
- content: '';
- height: 1px;
- width: 0;
- bottom: 0;
- position: absolute;
- transition: $transition;
- }
- // Mixins:
- @mixin slided-top() {
- top: - ($font-size-base + $spacer);
- left: 0;
- font-size: $font-size-base;
- font-weight: $font-weight-bold;
- }
- // Component:
- .material-input__component {
- margin-top: 36px;
- position: relative;
- * {
- box-sizing: border-box;
- }
- .iconClass {
- .material-input__icon {
- position: absolute;
- left: 0;
- line-height: $font-size-base;
- color: $color-blue;
- top: $spacer;
- width: $index-has-icon;
- height: $font-size-base;
- font-size: $font-size-base;
- font-weight: $font-weight-normal;
- pointer-events: none;
- }
- .material-label {
- left: $index-has-icon;
- }
- .material-input {
- text-indent: $index-has-icon;
- }
- }
- .material-input {
- font-size: $font-size-base;
- padding: 12px;
- display: block;
- width: 100%;
- border: none;
- line-height: 1;
- border-radius: 0;
- &:focus {
- outline: none;
- border: none;
- border-bottom: 1px solid transparent; // fixes the height issue
- }
- }
- .material-label {
- font-weight: $font-weight-normal;
- position: absolute;
- pointer-events: none;
- left: $index;
- top: 0;
- transition: $transition;
- font-size: $font-size-small;
- }
- .material-input-bar {
- position: relative;
- display: block;
- width: 100%;
- &:before {
- @extend %base-bar-pseudo;
- left: 50%;
- }
- &:after {
- @extend %base-bar-pseudo;
- right: 50%;
- }
- }
- // Disabled state:
- &.material--disabled {
- .material-input {
- border-bottom-style: dashed;
- }
- }
- // Raised state:
- &.material--raised {
- .material-label {
- @include slided-top();
- }
- }
- // Active state:
- &.material--active {
- .material-input-bar {
- &:before,
- &:after {
- width: 50%;
- }
- }
- }
- }
- .material-input__component {
- background: $color-white;
- .material-input {
- background: none;
- color: $color-black;
- text-indent: $index;
- border-bottom: 1px solid $color-grey-light;
- }
- .material-label {
- color: $color-grey;
- }
- .material-input-bar {
- &:before,
- &:after {
- background: $color-blue;
- }
- }
- // Active state:
- &.material--active {
- .material-label {
- color: $color-blue;
- }
- }
- // Errors:
- &.material--has-errors {
- &.material--active .material-label {
- color: $color-red;
- }
- .material-input-bar {
- &:before,
- &:after {
- background: transparent;
- }
- }
- }
- }
- </style>
|