Blame view

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