index.vue 11 KB

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