index.taro.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. <template>
  2. <view :class="classes">
  3. <view v-if="leftIcon && leftIcon.length > 0" class="nut-input-left-icon" @click="onClickLeftIcon">
  4. <nut-icon :name="leftIcon" :size="leftIconSize"></nut-icon>
  5. </view>
  6. <view
  7. v-if="label"
  8. class="nut-input-label"
  9. :class="labelClass"
  10. :style="{
  11. width: `${labelWidth}px`,
  12. textAlign: labelAlign
  13. }"
  14. >
  15. <view class="label-string">
  16. {{ label }}
  17. {{ colon ? ':' : '' }}
  18. </view>
  19. </view>
  20. <template v-if="$slots.input">
  21. <view class="nut-input-value">
  22. <view class="nut-input-inner" @click="onClickInput">
  23. <slot name="input"></slot>
  24. </view>
  25. </view>
  26. </template>
  27. <template v-else>
  28. <view class="nut-input-value">
  29. <view class="nut-input-inner">
  30. <view class="nut-input-box">
  31. <textarea
  32. v-if="type == 'textarea'"
  33. class="input-text"
  34. v-bind="$attrs"
  35. ref="inputRef"
  36. :style="stylesTextarea"
  37. :maxlength="maxLength"
  38. :placeholder="placeholder"
  39. placeholder-class="nut-placeholder"
  40. :disabled="disabled"
  41. :readonly="readonly"
  42. :value="modelValue"
  43. :formatTrigger="formatTrigger"
  44. :adjust-position="adjustPosition"
  45. :always-system="alwaysSystem"
  46. @input="onInput"
  47. @focus="onFocus"
  48. @blur="onBlur"
  49. @click="onClickInput"
  50. />
  51. <input
  52. v-else
  53. class="input-text"
  54. v-bind="$attrs"
  55. ref="inputRef"
  56. :style="styles"
  57. :type="inputType(type)"
  58. :maxlength="maxLength"
  59. :placeholder="placeholder"
  60. placeholder-class="nut-placeholder"
  61. :disabled="disabled"
  62. :readonly="readonly"
  63. :value="modelValue"
  64. :formatTrigger="formatTrigger"
  65. :confirm-type="confirmType"
  66. :adjust-position="adjustPosition"
  67. :always-system="alwaysSystem"
  68. :autofocus="autofocus"
  69. :enterkeyhint="confirmType"
  70. @input="onInput"
  71. @focus="onFocus"
  72. @blur="onBlur"
  73. @click="onClickInput"
  74. />
  75. <view v-if="readonly" class="nut-input-disabled-mask" @click="onClickInput"></view>
  76. </view>
  77. <view class="nut-input-clear-box">
  78. <nut-icon
  79. class="nut-input-clear"
  80. v-if="clearable && !readonly"
  81. v-show="active && modelValue.length > 0"
  82. :name="clearIcon"
  83. :size="clearSize"
  84. @click="clear"
  85. >
  86. </nut-icon>
  87. </view>
  88. <view v-if="rightIcon && rightIcon.length > 0" class="nut-input-right-icon" @click="onClickRightIcon">
  89. <nut-icon :name="rightIcon" :size="rightIconSize"></nut-icon>
  90. </view>
  91. <slot v-if="$slots.button" name="button" class="nut-input-button"></slot>
  92. <slot v-if="$slots.rightExtra" name="rightExtra"></slot>
  93. </view>
  94. <view v-if="showWordLimit && maxLength" class="nut-input-word-limit">
  95. <span class="nut-input-word-num">{{ modelValue ? modelValue.length : 0 }}</span
  96. >/{{ maxLength }}
  97. </view>
  98. <view
  99. v-if="errorMessage"
  100. class="nut-input-error-message"
  101. :style="{
  102. textAlign: errorMessageAlign
  103. }"
  104. >
  105. {{ errorMessage }}
  106. </view>
  107. </view>
  108. </template>
  109. </view>
  110. </template>
  111. <script lang="ts">
  112. import { PropType, ref, reactive, computed, onMounted, watch, ComputedRef, InputHTMLAttributes } from 'vue';
  113. import { createComponent } from '@/packages/utils/create';
  114. import { formatNumber } from './util';
  115. const { componentName, create } = createComponent('input');
  116. export type InputType = InputHTMLAttributes['type'];
  117. export type InputAlignType = 'left' | 'center' | 'right'; // text-align
  118. export type InputFormatTrigger = 'onChange' | 'onBlur'; // onChange: 在输入时执行格式化 ; onBlur: 在失焦时执行格式化
  119. export type InputRule = {
  120. pattern?: RegExp;
  121. message?: string;
  122. required?: boolean;
  123. };
  124. export type ConfirmTextType = 'send' | 'search' | 'next' | 'go' | 'done';
  125. export default create({
  126. inheritAttrs: false,
  127. props: {
  128. ref: {
  129. type: String,
  130. default: ''
  131. },
  132. type: {
  133. type: String as PropType<InputType>,
  134. default: 'text'
  135. },
  136. modelValue: {
  137. type: String,
  138. default: ''
  139. },
  140. placeholder: {
  141. type: String,
  142. default: ''
  143. },
  144. label: {
  145. type: String,
  146. default: ''
  147. },
  148. labelClass: {
  149. type: String,
  150. default: ''
  151. },
  152. labelWidth: {
  153. type: [String, Number],
  154. default: '80'
  155. },
  156. labelAlign: {
  157. type: String as PropType<InputAlignType>,
  158. default: 'left'
  159. },
  160. colon: {
  161. type: Boolean,
  162. default: false
  163. },
  164. inputAlign: {
  165. type: String,
  166. default: 'left'
  167. },
  168. center: {
  169. type: Boolean,
  170. default: false
  171. },
  172. required: {
  173. type: Boolean,
  174. default: false
  175. },
  176. disabled: {
  177. type: Boolean,
  178. default: false
  179. },
  180. readonly: {
  181. type: Boolean,
  182. default: false
  183. },
  184. error: {
  185. type: Boolean,
  186. default: false
  187. },
  188. maxLength: {
  189. type: [String, Number],
  190. default: '9999'
  191. },
  192. leftIconSize: {
  193. type: [String, Number],
  194. default: ''
  195. },
  196. leftIcon: {
  197. type: String,
  198. default: ''
  199. },
  200. rightIcon: {
  201. type: String,
  202. default: ''
  203. },
  204. rightIconSize: {
  205. type: [String, Number],
  206. default: ''
  207. },
  208. clearable: {
  209. type: Boolean,
  210. default: false
  211. },
  212. clearIcon: {
  213. type: String,
  214. default: 'mask-close'
  215. },
  216. clearSize: {
  217. type: [String, Number],
  218. default: '14'
  219. },
  220. border: {
  221. type: Boolean,
  222. default: true
  223. },
  224. formatTrigger: {
  225. type: String as PropType<InputFormatTrigger>,
  226. default: 'onChange'
  227. },
  228. formatter: {
  229. type: Function as PropType<(value: string) => string>,
  230. default: null
  231. },
  232. rules: {
  233. type: Array as PropType<InputRule>,
  234. default: []
  235. },
  236. errorMessage: {
  237. type: String,
  238. default: ''
  239. },
  240. errorMessageAlign: {
  241. type: String as PropType<InputAlignType>,
  242. default: ''
  243. },
  244. rows: {
  245. type: [String, Number],
  246. default: null
  247. },
  248. showWordLimit: {
  249. type: Boolean,
  250. default: false
  251. },
  252. autofocus: {
  253. type: Boolean,
  254. default: false
  255. },
  256. confirmType: {
  257. type: String as PropType<ConfirmTextType>,
  258. default: 'done'
  259. },
  260. adjustPosition: {
  261. type: Boolean,
  262. default: true
  263. },
  264. alwaysSystem: {
  265. type: Boolean,
  266. default: false
  267. }
  268. },
  269. emits: [
  270. 'update:modelValue',
  271. 'change',
  272. 'blur',
  273. 'focus',
  274. 'clear',
  275. 'keypress',
  276. 'click-input',
  277. 'click-left-icon',
  278. 'click-right-icon'
  279. ],
  280. setup(props, { emit, slots }) {
  281. const active = ref(false);
  282. const inputRef = ref();
  283. const getModelValue = () => String(props.modelValue ?? '');
  284. const state = reactive({
  285. focused: false,
  286. validateFailed: false, // 校验失败
  287. validateMessage: '' // 校验信息
  288. });
  289. const classes = computed(() => {
  290. const prefixCls = componentName;
  291. return {
  292. [prefixCls]: true,
  293. center: props.center,
  294. [`${prefixCls}-readonly`]: props.readonly,
  295. [`${prefixCls}-disabled`]: props.disabled,
  296. [`${prefixCls}-required`]: props.required,
  297. [`${prefixCls}-error`]: props.error,
  298. [`${prefixCls}-border`]: props.border
  299. };
  300. });
  301. const styles: ComputedRef = computed(() => {
  302. return {
  303. textAlign: props.inputAlign
  304. };
  305. });
  306. const stylesTextarea: ComputedRef = computed(() => {
  307. return {
  308. textAlign: props.inputAlign,
  309. height: Number(props.rows) * 24 + 'px'
  310. };
  311. });
  312. const inputType = (type: InputType) => {
  313. if (type === 'number') {
  314. return 'text';
  315. } else if (type === 'digit') {
  316. return 'tel';
  317. } else {
  318. return type;
  319. }
  320. };
  321. const onInput = (event: Event) => {
  322. const input = event.target as HTMLInputElement;
  323. let value = input.value;
  324. if (props.maxLength && value.length > Number(props.maxLength)) {
  325. value = value.slice(0, Number(props.maxLength));
  326. }
  327. updateValue(value);
  328. };
  329. const updateValue = (value: string, trigger: InputFormatTrigger = 'onChange') => {
  330. if (props.type === 'digit') {
  331. value = formatNumber(value, false, false);
  332. }
  333. if (props.type === 'number') {
  334. value = formatNumber(value, true, true);
  335. }
  336. if (props.formatter && trigger === props.formatTrigger) {
  337. value = props.formatter(value);
  338. }
  339. if (inputRef?.value.value !== value) {
  340. inputRef.value.value = value;
  341. }
  342. if (value !== props.modelValue) {
  343. emit('update:modelValue', value);
  344. emit('change', value);
  345. }
  346. };
  347. const onFocus = (event: Event) => {
  348. if (props.disabled || props.readonly) {
  349. return;
  350. }
  351. const input = event.target as HTMLInputElement;
  352. let value = input.value;
  353. active.value = true;
  354. emit('focus', event);
  355. emit('update:modelValue', value);
  356. };
  357. const onBlur = (event: Event) => {
  358. if (props.disabled || props.readonly) {
  359. return;
  360. }
  361. setTimeout(() => {
  362. active.value = false;
  363. }, 200);
  364. const input = event.target as HTMLInputElement;
  365. let value = input.value;
  366. if (props.maxLength && value.length > Number(props.maxLength)) {
  367. value = value.slice(0, Number(props.maxLength));
  368. }
  369. updateValue(getModelValue(), 'onBlur');
  370. emit('blur', event);
  371. emit('update:modelValue', value);
  372. };
  373. const clear = (event: Event) => {
  374. event.stopPropagation();
  375. if (props.disabled) return;
  376. emit('clear', '', event);
  377. };
  378. const resetValidation = () => {
  379. if (state.validateFailed) {
  380. state.validateFailed = false;
  381. state.validateMessage = '';
  382. }
  383. };
  384. const onClickInput = (event: MouseEvent) => {
  385. if (props.disabled) {
  386. return;
  387. }
  388. emit('click-input', event);
  389. };
  390. const onClickLeftIcon = (event: MouseEvent) => {
  391. event.stopPropagation();
  392. if (props.disabled) {
  393. return;
  394. }
  395. emit('click-left-icon', event);
  396. };
  397. const onClickRightIcon = (event: MouseEvent) => {
  398. event.stopPropagation();
  399. if (props.disabled) {
  400. return;
  401. }
  402. emit('click-right-icon', event);
  403. };
  404. watch(
  405. () => props.modelValue,
  406. () => {
  407. if (!slots.input) {
  408. updateValue(getModelValue());
  409. resetValidation();
  410. }
  411. }
  412. );
  413. onMounted(() => {
  414. if (!slots.input) {
  415. if (props.autofocus) {
  416. inputRef.value.focus();
  417. }
  418. updateValue(getModelValue(), props.formatTrigger);
  419. }
  420. });
  421. return {
  422. inputRef,
  423. active,
  424. classes,
  425. styles,
  426. stylesTextarea,
  427. inputType,
  428. onInput,
  429. onFocus,
  430. onBlur,
  431. clear,
  432. onClickInput,
  433. onClickLeftIcon,
  434. onClickRightIcon
  435. };
  436. }
  437. });
  438. </script>