index.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. import Icon from '../icon';
  2. import Cell from '../cell';
  3. import { cellProps } from '../cell/shared';
  4. import { use, isObj, isDef, isIOS } from '../utils';
  5. import { preventDefault } from '../utils/event';
  6. import { getRootScrollTop } from '../utils/scroll';
  7. import { isNumber } from '../utils/validate/number';
  8. const [sfc, bem] = use('field');
  9. export default sfc({
  10. inheritAttrs: false,
  11. props: {
  12. ...cellProps,
  13. error: Boolean,
  14. leftIcon: String,
  15. rightIcon: String,
  16. readonly: Boolean,
  17. clearable: Boolean,
  18. labelWidth: [String, Number],
  19. labelAlign: String,
  20. inputAlign: String,
  21. onIconClick: Function,
  22. autosize: [Boolean, Object],
  23. errorMessage: String,
  24. errorMessageAlign: String,
  25. type: {
  26. type: String,
  27. default: 'text'
  28. }
  29. },
  30. data() {
  31. return {
  32. focused: false
  33. };
  34. },
  35. watch: {
  36. value() {
  37. this.$nextTick(this.adjustSize);
  38. }
  39. },
  40. mounted() {
  41. this.format();
  42. this.$nextTick(this.adjustSize);
  43. },
  44. computed: {
  45. showClear() {
  46. return (
  47. this.clearable && this.focused && this.value !== '' && isDef(this.value) && !this.readonly
  48. );
  49. },
  50. listeners() {
  51. return {
  52. ...this.$listeners,
  53. input: this.onInput,
  54. keypress: this.onKeypress,
  55. focus: this.onFocus,
  56. blur: this.onBlur
  57. };
  58. },
  59. labelStyle() {
  60. const { labelWidth } = this;
  61. if (labelWidth) {
  62. const width = isNumber(String(labelWidth)) ? `${labelWidth}px` : labelWidth;
  63. return {
  64. maxWidth: width,
  65. minWidth: width
  66. };
  67. }
  68. }
  69. },
  70. methods: {
  71. focus() {
  72. this.$refs.input && this.$refs.input.focus();
  73. },
  74. blur() {
  75. this.$refs.input && this.$refs.input.blur();
  76. },
  77. // native maxlength not work when type = number
  78. format(target = this.$refs.input) {
  79. let { value } = target;
  80. const { maxlength } = this.$attrs;
  81. if (this.type === 'number' && isDef(maxlength) && value.length > maxlength) {
  82. value = value.slice(0, maxlength);
  83. target.value = value;
  84. }
  85. return value;
  86. },
  87. onInput(event) {
  88. this.$emit('input', this.format(event.target));
  89. },
  90. onFocus(event) {
  91. this.focused = true;
  92. this.$emit('focus', event);
  93. // hack for safari
  94. /* istanbul ignore if */
  95. if (this.readonly) {
  96. this.blur();
  97. }
  98. },
  99. onBlur(event) {
  100. this.focused = false;
  101. this.$emit('blur', event);
  102. // Hack for iOS12 page scroll
  103. // https://developers.weixin.qq.com/community/develop/doc/00044ae90742f8c82fb78fcae56800
  104. /* istanbul ignore next */
  105. if (isIOS()) {
  106. window.scrollTo(0, getRootScrollTop());
  107. }
  108. },
  109. onClickLeftIcon() {
  110. this.$emit('click-left-icon');
  111. },
  112. onClickRightIcon() {
  113. // compatible old version
  114. this.$emit('click-icon');
  115. this.$emit('click-right-icon');
  116. this.onIconClick && this.onIconClick();
  117. },
  118. onClear(event) {
  119. preventDefault(event);
  120. this.$emit('input', '');
  121. this.$emit('clear');
  122. },
  123. onKeypress(event) {
  124. if (this.type === 'number') {
  125. const { keyCode } = event;
  126. const allowPoint = String(this.value).indexOf('.') === -1;
  127. const isValidKey =
  128. (keyCode >= 48 && keyCode <= 57) || (keyCode === 46 && allowPoint) || keyCode === 45;
  129. if (!isValidKey) {
  130. preventDefault(event);
  131. }
  132. }
  133. // trigger blur after click keyboard search button
  134. /* istanbul ignore next */
  135. if (this.type === 'search' && event.keyCode === 13) {
  136. this.blur();
  137. }
  138. this.$emit('keypress', event);
  139. },
  140. adjustSize() {
  141. const { input } = this.$refs;
  142. if (!(this.type === 'textarea' && this.autosize) || !input) {
  143. return;
  144. }
  145. input.style.height = 'auto';
  146. let height = input.scrollHeight;
  147. if (isObj(this.autosize)) {
  148. const { maxHeight, minHeight } = this.autosize;
  149. if (maxHeight) {
  150. height = Math.min(height, maxHeight);
  151. }
  152. if (minHeight) {
  153. height = Math.max(height, minHeight);
  154. }
  155. }
  156. if (height) {
  157. input.style.height = height + 'px';
  158. }
  159. },
  160. renderInput() {
  161. const inputProps = {
  162. ref: 'input',
  163. class: bem('control', this.inputAlign),
  164. domProps: {
  165. value: this.value
  166. },
  167. attrs: {
  168. ...this.$attrs,
  169. readonly: this.readonly
  170. },
  171. on: this.listeners
  172. };
  173. if (this.type === 'textarea') {
  174. return <textarea {...inputProps} />;
  175. }
  176. return <input type={this.type} {...inputProps} />;
  177. },
  178. renderLeftIcon() {
  179. const showLeftIcon = this.slots('left-icon') || this.leftIcon;
  180. if (showLeftIcon) {
  181. return (
  182. <div class={bem('left-icon')} onClick={this.onClickLeftIcon}>
  183. {this.slots('left-icon') || <Icon name={this.leftIcon} />}
  184. </div>
  185. );
  186. }
  187. },
  188. renderRightIcon() {
  189. const { slots } = this;
  190. const showRightIcon = slots('right-icon') || slots('icon') || this.rightIcon || this.icon;
  191. if (showRightIcon) {
  192. return (
  193. <div class={bem('right-icon')} onClick={this.onClickRightIcon}>
  194. {slots('right-icon') || slots('icon') || <Icon name={this.rightIcon || this.icon} />}
  195. </div>
  196. );
  197. }
  198. }
  199. },
  200. render(h) {
  201. const { slots, labelAlign } = this;
  202. const scopedSlots = {
  203. icon: this.renderLeftIcon
  204. };
  205. if (slots('label')) {
  206. scopedSlots.title = () => slots('label');
  207. }
  208. return (
  209. <Cell
  210. icon={this.leftIcon}
  211. size={this.size}
  212. title={this.label}
  213. center={this.center}
  214. border={this.border}
  215. isLink={this.isLink}
  216. required={this.required}
  217. titleStyle={this.labelStyle}
  218. titleClass={bem('label', labelAlign)}
  219. class={bem({
  220. error: this.error,
  221. disabled: this.$attrs.disabled,
  222. [`label-${labelAlign}`]: labelAlign,
  223. 'min-height': this.type === 'textarea' && !this.autosize
  224. })}
  225. scopedSlots={scopedSlots}
  226. >
  227. <div class={bem('body')}>
  228. {this.renderInput()}
  229. {this.showClear && <Icon name="clear" class={bem('clear')} onTouchstart={this.onClear} />}
  230. {this.renderRightIcon()}
  231. {slots('button') && <div class={bem('button')}>{slots('button')}</div>}
  232. </div>
  233. {this.errorMessage && <div class={bem('error-message', this.errorMessageAlign)}>{this.errorMessage}</div>}
  234. </Cell>
  235. );
  236. }
  237. });