NumberInput.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <template>
  2. <FlexCol>
  3. <view :style="{
  4. ...themeStyles.view.value,
  5. ...props.innerStyle,
  6. }">
  7. <NumberInputBox
  8. v-for="i of numberCount"
  9. :key="i"
  10. :index="i"
  11. :value="modelValue.charAt(i - 1)"
  12. :active="(isFocus && (i === 1)) || Boolean(modelValue.charAt(i - 1) || Boolean(modelValue.charAt(i - 2)))"
  13. :autoSize="autoSize"
  14. :isPassword="isPassword"
  15. :disableKeyPad="disableKeyPad"
  16. :showCursur="showCursur"
  17. :gutter="gutter"
  18. :boxStyle="boxStyle"
  19. :textStyle="textStyle"
  20. :borderWidth="borderWidth"
  21. :borderType="borderType"
  22. :borderColor="borderColor"
  23. :backgroundColor="backgroundColor"
  24. :activeBorderColor="activeBorderColor"
  25. :activeBackgroundColor="activeBackgroundColor"
  26. @click="onBoxClicked(i - 1)"
  27. />
  28. </view>
  29. <input
  30. v-if="useSystemInput"
  31. :style="themeStyles.invisibleInput.value"
  32. ref="inputRef"
  33. :value="textShadow"
  34. :focus="isFocus"
  35. :password="isPassword"
  36. type="number"
  37. @input="onInputChangeText(($event as any).detail.value)"
  38. @blur="onBlurInput"
  39. />
  40. <text v-if="info" :style="themeStyles.info.value">{{info}}</text>
  41. <text v-if="errorMessage" :style="themeStyles.errorMessage.value">{{errorMessage}}</text>
  42. <NumberKeyBoard
  43. v-model:show="showInput"
  44. @input="onInput"
  45. @delete="onDelete"
  46. />
  47. </FlexCol>
  48. </template>
  49. <script setup lang="ts">
  50. import { onMounted, ref, watch } from 'vue';
  51. import NumberKeyBoard from '../keyboard/NumberKeyBoard.vue';
  52. import NumberInputBox from './NumberInputBox.vue';
  53. import { propGetThemeVar, useTheme, type TextStyle, type ViewStyle } from '../theme/ThemeDefine';
  54. import { DynamicColor, DynamicSize } from '../theme/ThemeTools';
  55. import FlexCol from '../layout/FlexCol.vue';
  56. /**
  57. * * underline 下划线
  58. * * box 外边框样式
  59. * * flat 仅有背景颜色样式
  60. * * activeBorderBox 外边框样式(激活状态)和背景颜色样式
  61. */
  62. export type NumberInputBorderType = 'underline'|'box'|'flat'|'activeBorderBox';
  63. export interface NumberInputProps {
  64. /**
  65. * 数值
  66. */
  67. modelValue: string;
  68. /**
  69. * 底部错误提示文案,为空时不展示
  70. */
  71. errorMessage?: string;
  72. /**
  73. * 数字位数
  74. * @default 6
  75. */
  76. numberCount?: number;
  77. /**
  78. * 是否是密码
  79. * @default false
  80. */
  81. isPassword?: boolean;
  82. /**
  83. * 是否在组件初始化时激活键盘
  84. * @default false
  85. */
  86. startFocus?: boolean;
  87. /**
  88. * 使用系统输入键盘,否则使用 NumberKeyBoard 作为输入键盘,默认是
  89. * @default true
  90. */
  91. useSystemInput?: boolean;
  92. /**
  93. * 输入框下方文字提示
  94. */
  95. info?: string;
  96. /**
  97. * 输入框格子之间的间距
  98. * @default 0
  99. */
  100. gutter?: number;
  101. /**
  102. * 是否自动调整宽度
  103. * @default false
  104. */
  105. autoSize?: boolean;
  106. /**
  107. * 格子的边框
  108. * @default 'activeBorderBox'
  109. */
  110. borderType?: NumberInputBorderType;
  111. /**
  112. * 格子的边框颜色
  113. * @default 'border.default'
  114. */
  115. borderColor?: string;
  116. /**
  117. * 格子的背景颜色
  118. * @default 'background.input'
  119. */
  120. backgroundColor?: string;
  121. /**
  122. * 格子的边框宽度
  123. * @default 1.5
  124. */
  125. borderWidth?: number;
  126. /**
  127. * 已输入格子的边框颜色
  128. * @default 'primary'
  129. */
  130. activeBorderColor?: string;
  131. /**
  132. * 已输入格子的背景颜色
  133. * @default 'background.primary'
  134. */
  135. activeBackgroundColor?: string;
  136. /**
  137. * 格子样式
  138. */
  139. boxStyle?: ViewStyle;
  140. /**
  141. * 是否禁用点击打开键盘
  142. * @default false
  143. */
  144. disableKeyPad?: boolean;
  145. /**
  146. * 是显示输入闪烁光标
  147. * @default true
  148. */
  149. showCursur?: boolean;
  150. /**
  151. * 是否在输入完成后自动收起键盘
  152. * @default true
  153. */
  154. finishHideKeyPad?: boolean;
  155. /**
  156. * 文字样式
  157. */
  158. textStyle?: TextStyle;
  159. /**
  160. * 外层样式
  161. */
  162. innerStyle?: ViewStyle;
  163. }
  164. const emit = defineEmits([
  165. /**
  166. * 文字更改回调
  167. */
  168. 'update:modelValue',
  169. /**
  170. * 当输入完成(全部位数都已经输入)时返回事件
  171. */
  172. 'enterFinish',
  173. ]);
  174. const props = withDefaults(defineProps<NumberInputProps>(), {
  175. useSystemInput: true,
  176. numberCount: 6,
  177. isPassword: false,
  178. autoSize: false,
  179. disableKeyPad: false,
  180. startFocus: false,
  181. finishHideKeyPad: true,
  182. showCursur: true,
  183. borderType: () => propGetThemeVar('NumberInputBorderType', 'activeBorderBox'),
  184. borderWidth: () => propGetThemeVar('NumberInputBorderWidth', 4),
  185. borderColor: () => propGetThemeVar('NumberInputBorderColor', 'border.default'),
  186. backgroundColor: () => propGetThemeVar('NumberInputBackgroundColor', 'background.input'),
  187. gutter: () => propGetThemeVar('NumberInputGutter', 4),
  188. activeBorderColor: () => propGetThemeVar('NumberInputActiveBorderColor', 'primary'),
  189. activeBackgroundColor: () => propGetThemeVar('NumberInputActiveBackgroundColor', 'background.primary'),
  190. });
  191. const themeContext = useTheme();
  192. const themeStyles = themeContext.useThemeStyles({
  193. view: {
  194. display: 'flex',
  195. position: 'relative',
  196. flexDirection: 'row',
  197. alignItems: 'center',
  198. justifyContent: 'center',
  199. },
  200. info: {
  201. marginTop: DynamicSize('NumberInputInfoMarginTop', 10),
  202. textAlign: 'center',
  203. color: DynamicColor('NumberInputInfoColor', 'text.second'),
  204. },
  205. errorMessage: {
  206. marginTop: DynamicSize('NumberInputErrorMessageMarginTop', 10),
  207. textAlign: 'center',
  208. color: DynamicColor('NumberInputErrorMessageColor', 'danger'),
  209. },
  210. invisibleInput: {
  211. opacity: 0,
  212. position: 'absolute',
  213. height: 1,
  214. left: -300,
  215. },
  216. });
  217. let lastClickBoxRet = -1;
  218. const textShadow = ref(props.modelValue);
  219. const inputRef = ref();
  220. const showInput = ref(props.startFocus);
  221. const isFocus = ref(props.startFocus);
  222. function focusInput() {
  223. if (props.useSystemInput) {
  224. if (typeof inputRef.value?.focus === 'function')
  225. inputRef.value.focus();
  226. } else
  227. showInput.value = true;
  228. isFocus.value = true;
  229. }
  230. function blurInput() {
  231. if (props.useSystemInput) {
  232. if (typeof inputRef.value?.blur === 'function')
  233. inputRef.value.blur();
  234. } else
  235. showInput.value = false;
  236. isFocus.value = false;
  237. }
  238. function onBlurInput() {
  239. isFocus.value = false;
  240. }
  241. function onInputChangeText(text: string) {
  242. //点击了前面的方格输入,需要清除方格后面的文字
  243. if (lastClickBoxRet >= 0 && text.length > lastClickBoxRet) {
  244. text = (text.substring(0, lastClickBoxRet) + text.charAt(text.length - 1));
  245. lastClickBoxRet = -1;
  246. }
  247. if (text.length > props.numberCount)
  248. return;
  249. textShadow.value = text;
  250. if (text.length === props.numberCount) {
  251. emit('enterFinish', text);
  252. if (props.finishHideKeyPad)
  253. blurInput();
  254. }
  255. }
  256. function onBoxClicked(i: number) {
  257. lastClickBoxRet = i;
  258. focusInput();
  259. }
  260. function onInput(str: string) {
  261. onInputChangeText(textShadow.value + str);
  262. }
  263. function onDelete() {
  264. onInputChangeText(textShadow.value.substring(0, textShadow.value.length - 1));
  265. }
  266. watch(textShadow, (val) => {
  267. emit('update:modelValue', val);
  268. });
  269. onMounted(() => {
  270. if (props.startFocus)
  271. focusInput();
  272. });
  273. </script>