Blame view

src/components/input/input.vue 6.76 KB
7fa943eb   梁灏   init
1
  <template>
7d5431d8   梁灏   update some style
2
      <div :class="wrapClasses">
0f822c9b   梁灏   add Input component
3
          <template v-if="type !== 'textarea'">
fc7ef072   梁灏   support Input
4
              <div :class="[prefixCls + '-group-prepend']" v-if="prepend" v-show="slotReady" ref="prepend"><slot name="prepend"></slot></div>
0f822c9b   梁灏   add Input component
5
              <i class="ivu-icon" :class="['ivu-icon-' + icon, prefixCls + '-icon']" v-if="icon" @click="handleIconClick"></i>
fc7ef072   梁灏   support Input
6
7
8
              <transition name="fade">
                  <i class="ivu-icon ivu-icon-load-c ivu-load-loop" :class="[prefixCls + '-icon', prefixCls + '-icon-validate']" v-if="!icon"></i>
              </transition>
0f822c9b   梁灏   add Input component
9
              <input
6c145a04   梁灏   fixed #77
10
                  :type="type"
0f822c9b   梁灏   add Input component
11
12
13
14
                  :class="inputClasses"
                  :placeholder="placeholder"
                  :disabled="disabled"
                  :maxlength="maxlength"
0a48ac45   梁灏   Input add readonl...
15
                  :readonly="readonly"
731d69a2   梁灏   fixed #101
16
                  :name="name"
fc7ef072   梁灏   support Input
17
                  :value="currentValue"
c3a9f389   梁灏   update Input
18
                  :number="number"
0a48ac45   梁灏   Input add readonl...
19
20
                  @keyup.enter="handleEnter"
                  @focus="handleFocus"
c46f385a   梁灏   update DatePicker
21
                  @blur="handleBlur"
531cd165   梁灏   support DatePicke...
22
23
                  @input="handleInput"
                  @change="handleChange">
fc7ef072   梁灏   support Input
24
              <div :class="[prefixCls + '-group-append']" v-if="append" v-show="slotReady" ref="append"><slot name="append"></slot></div>
0f822c9b   梁灏   add Input component
25
26
27
          </template>
          <textarea
              v-else
fc7ef072   梁灏   support Input
28
              ref="textarea"
0f822c9b   梁灏   add Input component
29
30
              :class="textareaClasses"
              :style="textareaStyles"
7d5431d8   梁灏   update some style
31
              :placeholder="placeholder"
0f822c9b   梁灏   add Input component
32
33
34
              :disabled="disabled"
              :rows="rows"
              :maxlength="maxlength"
0a48ac45   梁灏   Input add readonl...
35
              :readonly="readonly"
731d69a2   梁灏   fixed #101
36
              :name="name"
fc7ef072   梁灏   support Input
37
              :value="value"
0a48ac45   梁灏   Input add readonl...
38
39
              @keyup.enter="handleEnter"
              @focus="handleFocus"
c46f385a   梁灏   update DatePicker
40
              @blur="handleBlur"
fc7ef072   梁灏   support Input
41
              @input="handleInput">
0f822c9b   梁灏   add Input component
42
          </textarea>
7d5431d8   梁灏   update some style
43
      </div>
7fa943eb   梁灏   init
44
45
  </template>
  <script>
21dad188   梁灏   prevent dispatch ...
46
      import { oneOf, findComponentUpward } from '../../utils/assist';
0f822c9b   梁灏   add Input component
47
      import calcTextareaHeight from '../../utils/calcTextareaHeight';
cd78c9c4   梁灏   some comps suppor...
48
      import Emitter from '../../mixins/emitter';
7fa943eb   梁灏   init
49
50
51
52
  
      const prefixCls = 'ivu-input';
  
      export default {
06322514   梁灏   support Radio
53
          name: 'Input',
cd78c9c4   梁灏   some comps suppor...
54
          mixins: [ Emitter ],
7fa943eb   梁灏   init
55
56
          props: {
              type: {
0f822c9b   梁灏   add Input component
57
                  validator (value) {
6c145a04   梁灏   fixed #77
58
                      return oneOf(value, ['text', 'textarea', 'password']);
0f822c9b   梁灏   add Input component
59
                  },
7fa943eb   梁灏   init
60
61
62
63
                  default: 'text'
              },
              value: {
                  type: [String, Number],
fc7ef072   梁灏   support Input
64
                  default: ''
7fa943eb   梁灏   init
65
              },
7fa943eb   梁灏   init
66
67
68
69
              size: {
                  validator (value) {
                      return oneOf(value, ['small', 'large']);
                  }
0f822c9b   梁灏   add Input component
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
              },
              placeholder: {
                  type: String,
                  default: ''
              },
              maxlength: {
                  type: Number
              },
              disabled: {
                  type: Boolean,
                  default: false
              },
              icon: String,
              autosize: {
                  type: [Boolean, Object],
                  default: false
              },
              rows: {
                  type: Number,
                  default: 2
0a48ac45   梁灏   Input add readonl...
90
91
92
93
              },
              readonly: {
                  type: Boolean,
                  default: false
731d69a2   梁灏   fixed #101
94
95
96
              },
              name: {
                  type: String
c3a9f389   梁灏   update Input
97
98
99
100
              },
              number: {
                  type: Boolean,
                  default: false
7fa943eb   梁灏   init
101
102
103
104
              }
          },
          data () {
              return {
fc7ef072   梁灏   support Input
105
                  currentValue: this.value,
0f822c9b   梁灏   add Input component
106
107
108
                  prefixCls: prefixCls,
                  prepend: true,
                  append: true,
6ff31952   梁灏   optimize Input sh...
109
                  slotReady: false,
0f822c9b   梁灏   add Input component
110
                  textareaStyles: {}
b0893113   jingsam   :art: add eslint
111
              };
7fa943eb   梁灏   init
112
113
          },
          computed: {
7d5431d8   梁灏   update some style
114
              wrapClasses () {
0f822c9b   梁灏   add Input component
115
116
117
                  return [
                      `${prefixCls}-wrapper`,
                      {
12418c6a   梁灏   fixed #74
118
                          [`${prefixCls}-wrapper-${this.size}`]: !!this.size,
0f822c9b   梁灏   add Input component
119
120
121
122
                          [`${prefixCls}-type`]: this.type,
                          [`${prefixCls}-group`]: this.prepend || this.append,
                          [`${prefixCls}-group-${this.size}`]: (this.prepend || this.append) && !!this.size
                      }
b0893113   jingsam   :art: add eslint
123
                  ];
0f822c9b   梁灏   add Input component
124
125
126
127
128
129
130
131
              },
              inputClasses () {
                  return [
                      `${prefixCls}`,
                      {
                          [`${prefixCls}-${this.size}`]: !!this.size,
                          [`${prefixCls}-disabled`]: this.disabled
                      }
b0893113   jingsam   :art: add eslint
132
                  ];
7d5431d8   梁灏   update some style
133
              },
0f822c9b   梁灏   add Input component
134
              textareaClasses () {
7fa943eb   梁灏   init
135
136
137
                  return [
                      `${prefixCls}`,
                      {
0f822c9b   梁灏   add Input component
138
                          [`${prefixCls}-disabled`]: this.disabled
7fa943eb   梁灏   init
139
                      }
b0893113   jingsam   :art: add eslint
140
                  ];
7fa943eb   梁灏   init
141
              }
0f822c9b   梁灏   add Input component
142
143
144
145
146
147
148
149
          },
          methods: {
              handleEnter () {
                  this.$emit('on-enter');
              },
              handleIconClick () {
                  this.$emit('on-click');
              },
0a48ac45   梁灏   Input add readonl...
150
151
152
153
154
              handleFocus () {
                  this.$emit('on-focus');
              },
              handleBlur () {
                  this.$emit('on-blur');
04e5e3cd   梁灏   prevent dispatch ...
155
                  if (!findComponentUpward(this, ['DatePicker', 'TimePicker', 'Cascader'])) {
21dad188   梁灏   prevent dispatch ...
156
157
                      this.dispatch('FormItem', 'on-form-blur', this.currentValue);
                  }
0a48ac45   梁灏   Input add readonl...
158
              },
fc7ef072   梁灏   support Input
159
160
161
162
              handleInput (event) {
                  const value = event.target.value;
                  this.$emit('input', value);
                  this.setCurrentValue(value);
e1874103   梁灏   update DatePicker
163
                  this.$emit('on-change', event);
c46f385a   梁灏   update DatePicker
164
              },
531cd165   梁灏   support DatePicke...
165
166
167
              handleChange (event) {
                  this.$emit('on-input-change', event);
              },
fc7ef072   梁灏   support Input
168
169
170
171
172
173
              setCurrentValue (value) {
                  if (value === this.currentValue) return;
                  this.$nextTick(() => {
                      this.resizeTextarea();
                  });
                  this.currentValue = value;
04e5e3cd   梁灏   prevent dispatch ...
174
                  if (!findComponentUpward(this, ['DatePicker', 'TimePicker', 'Cascader'])) {
21dad188   梁灏   prevent dispatch ...
175
176
                      this.dispatch('FormItem', 'on-form-change', value);
                  }
fc7ef072   梁灏   support Input
177
              },
0f822c9b   梁灏   add Input component
178
179
180
181
182
183
184
185
186
              resizeTextarea () {
                  const autosize = this.autosize;
                  if (!autosize || this.type !== 'textarea') {
                      return false;
                  }
  
                  const minRows = autosize.minRows;
                  const maxRows = autosize.maxRows;
  
fc7ef072   梁灏   support Input
187
                  this.textareaStyles = calcTextareaHeight(this.$refs.textarea, minRows, maxRows);
0f822c9b   梁灏   add Input component
188
189
190
              }
          },
          watch: {
fc7ef072   梁灏   support Input
191
192
              value (val) {
                  this.setCurrentValue(val);
0f822c9b   梁灏   add Input component
193
194
              }
          },
fc7ef072   梁灏   support Input
195
196
197
198
199
200
201
202
203
204
          mounted () {
              if (this.type !== 'textarea') {
                  this.prepend = this.$slots.prepend !== undefined;
                  this.append = this.$slots.append !== undefined;
              } else {
                  this.prepend = false;
                  this.append = false;
              }
              this.slotReady = true;
              this.resizeTextarea();
7fa943eb   梁灏   init
205
          }
b0893113   jingsam   :art: add eslint
206
207
      };
  </script>