Blame view

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