input.vue 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. <template>
  2. <div :class="[
  3. type === 'textarea' ? 'el-textarea' : 'el-input',
  4. inputSize ? 'el-input--' + inputSize : '',
  5. {
  6. 'is-disabled': inputDisabled,
  7. 'el-input-group': $slots.prepend || $slots.append,
  8. 'el-input-group--append': $slots.append,
  9. 'el-input-group--prepend': $slots.prepend,
  10. 'el-input--prefix': $slots.prefix || prefixIcon,
  11. 'el-input--suffix': $slots.suffix || suffixIcon
  12. }
  13. ]"
  14. @mouseenter="hovering = true"
  15. @mouseleave="hovering = false"
  16. >
  17. <template v-if="type !== 'textarea'">
  18. <!-- 前置元素 -->
  19. <div class="el-input-group__prepend" v-if="$slots.prepend">
  20. <slot name="prepend"></slot>
  21. </div>
  22. <input
  23. :tabindex="tabindex"
  24. v-if="type !== 'textarea'"
  25. class="el-input__inner"
  26. v-bind="$props"
  27. :disabled="inputDisabled"
  28. :autocomplete="autoComplete"
  29. :value="currentValue"
  30. ref="input"
  31. @input="handleInput"
  32. @focus="handleFocus"
  33. @blur="handleBlur"
  34. @change="handleChange"
  35. :aria-label="label"
  36. >
  37. <!-- 前置内容 -->
  38. <span class="el-input__prefix" v-if="$slots.prefix || prefixIcon" :style="prefixOffset">
  39. <slot name="prefix"></slot>
  40. <i class="el-input__icon"
  41. v-if="prefixIcon"
  42. :class="prefixIcon">
  43. </i>
  44. </span>
  45. <!-- 后置内容 -->
  46. <span
  47. class="el-input__suffix"
  48. v-if="$slots.suffix || suffixIcon || showClear || validateState && needStatusIcon"
  49. :style="suffixOffset">
  50. <span class="el-input__suffix-inner">
  51. <template v-if="!showClear">
  52. <slot name="suffix"></slot>
  53. <i class="el-input__icon"
  54. v-if="suffixIcon"
  55. :class="suffixIcon">
  56. </i>
  57. </template>
  58. <i v-else
  59. class="el-input__icon el-icon-circle-close el-input__clear"
  60. @click="clear"
  61. ></i>
  62. </span>
  63. <i class="el-input__icon"
  64. v-if="validateState"
  65. :class="['el-input__validateIcon', validateIcon]">
  66. </i>
  67. </span>
  68. <!-- 后置元素 -->
  69. <div class="el-input-group__append" v-if="$slots.append">
  70. <slot name="append"></slot>
  71. </div>
  72. </template>
  73. <textarea
  74. v-else
  75. :tabindex="tabindex"
  76. class="el-textarea__inner"
  77. :value="currentValue"
  78. @input="handleInput"
  79. ref="textarea"
  80. v-bind="$props"
  81. :disabled="inputDisabled"
  82. :style="textareaStyle"
  83. @focus="handleFocus"
  84. @blur="handleBlur"
  85. @change="handleChange"
  86. :aria-label="label"
  87. >
  88. </textarea>
  89. </div>
  90. </template>
  91. <script>
  92. import emitter from 'element-ui/src/mixins/emitter';
  93. import Migrating from 'element-ui/src/mixins/migrating';
  94. import calcTextareaHeight from './calcTextareaHeight';
  95. import merge from 'element-ui/src/utils/merge';
  96. export default {
  97. name: 'ElInput',
  98. componentName: 'ElInput',
  99. mixins: [emitter, Migrating],
  100. inject: {
  101. elForm: {
  102. default: ''
  103. },
  104. elFormItem: {
  105. default: ''
  106. }
  107. },
  108. data() {
  109. return {
  110. currentValue: this.value,
  111. textareaCalcStyle: {},
  112. prefixOffset: null,
  113. suffixOffset: null,
  114. hovering: false,
  115. focused: false
  116. };
  117. },
  118. props: {
  119. value: [String, Number],
  120. placeholder: String,
  121. size: String,
  122. resize: String,
  123. name: String,
  124. form: String,
  125. id: String,
  126. maxlength: Number,
  127. minlength: Number,
  128. readonly: Boolean,
  129. autofocus: Boolean,
  130. disabled: Boolean,
  131. type: {
  132. type: String,
  133. default: 'text'
  134. },
  135. autosize: {
  136. type: [Boolean, Object],
  137. default: false
  138. },
  139. rows: {
  140. type: Number,
  141. default: 2
  142. },
  143. autoComplete: {
  144. type: String,
  145. default: 'off'
  146. },
  147. max: {},
  148. min: {},
  149. step: {},
  150. validateEvent: {
  151. type: Boolean,
  152. default: true
  153. },
  154. suffixIcon: String,
  155. prefixIcon: String,
  156. label: String,
  157. clearable: {
  158. type: Boolean,
  159. default: false
  160. },
  161. tabindex: String
  162. },
  163. computed: {
  164. _elFormItemSize() {
  165. return (this.elFormItem || {}).elFormItemSize;
  166. },
  167. validateState() {
  168. return this.elFormItem ? this.elFormItem.validateState : '';
  169. },
  170. needStatusIcon() {
  171. return this.elForm ? this.elForm.statusIcon : false;
  172. },
  173. validateIcon() {
  174. return {
  175. validating: 'el-icon-loading',
  176. success: 'el-icon-circle-check',
  177. error: 'el-icon-circle-close'
  178. }[this.validateState];
  179. },
  180. textareaStyle() {
  181. return merge({}, this.textareaCalcStyle, { resize: this.resize });
  182. },
  183. inputSize() {
  184. return this.size || this._elFormItemSize || (this.$ELEMENT || {}).size;
  185. },
  186. inputDisabled() {
  187. return this.disabled || (this.elForm || {}).disabled;
  188. },
  189. isGroup() {
  190. return this.$slots.prepend || this.$slots.append;
  191. },
  192. showClear() {
  193. return this.clearable && this.currentValue !== '' && (this.focused || this.hovering);
  194. }
  195. },
  196. watch: {
  197. 'value'(val, oldValue) {
  198. this.setCurrentValue(val);
  199. }
  200. },
  201. methods: {
  202. focus() {
  203. (this.$refs.input || this.$refs.textarea).focus();
  204. },
  205. getMigratingConfig() {
  206. return {
  207. props: {
  208. 'icon': 'icon is removed, use suffix-icon / prefix-icon instead.',
  209. 'on-icon-click': 'on-icon-click is removed.'
  210. },
  211. events: {
  212. 'click': 'click is removed.'
  213. }
  214. };
  215. },
  216. handleBlur(event) {
  217. this.focused = false;
  218. this.$emit('blur', event);
  219. if (this.validateEvent) {
  220. this.dispatch('ElFormItem', 'el.form.blur', [this.currentValue]);
  221. }
  222. },
  223. inputSelect() {
  224. (this.$refs.input || this.$refs.textarea).select();
  225. },
  226. resizeTextarea() {
  227. if (this.$isServer) return;
  228. const { autosize, type } = this;
  229. if (type !== 'textarea') return;
  230. if (!autosize) {
  231. this.textareaCalcStyle = {
  232. minHeight: calcTextareaHeight(this.$refs.textarea).minHeight
  233. };
  234. return;
  235. }
  236. const minRows = autosize.minRows;
  237. const maxRows = autosize.maxRows;
  238. this.textareaCalcStyle = calcTextareaHeight(this.$refs.textarea, minRows, maxRows);
  239. },
  240. handleFocus(event) {
  241. this.focused = true;
  242. this.$emit('focus', event);
  243. },
  244. handleInput(event) {
  245. const value = event.target.value;
  246. this.$emit('input', value);
  247. this.setCurrentValue(value);
  248. },
  249. handleChange(event) {
  250. this.$emit('change', event.target.value);
  251. },
  252. setCurrentValue(value) {
  253. if (value === this.currentValue) return;
  254. this.$nextTick(_ => {
  255. this.resizeTextarea();
  256. });
  257. this.currentValue = value;
  258. if (this.validateEvent) {
  259. this.dispatch('ElFormItem', 'el.form.change', [value]);
  260. }
  261. },
  262. calcIconOffset(place) {
  263. const pendantMap = {
  264. 'suf': 'append',
  265. 'pre': 'prepend'
  266. };
  267. const pendant = pendantMap[place];
  268. if (this.$slots[pendant]) {
  269. return { transform: `translateX(${place === 'suf' ? '-' : ''}${this.$el.querySelector(`.el-input-group__${pendant}`).offsetWidth}px)` };
  270. }
  271. },
  272. clear() {
  273. this.$emit('input', '');
  274. this.$emit('change', '');
  275. this.$emit('clear');
  276. this.setCurrentValue('');
  277. this.focus();
  278. }
  279. },
  280. created() {
  281. this.$on('inputSelect', this.inputSelect);
  282. },
  283. mounted() {
  284. this.resizeTextarea();
  285. if (this.isGroup) {
  286. this.prefixOffset = this.calcIconOffset('pre');
  287. this.suffixOffset = this.calcIconOffset('suf');
  288. }
  289. }
  290. };
  291. </script>