cl-keyboard-number.uvue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. <template>
  2. <cl-popup
  3. :title="title"
  4. :swipe-close-threshold="100"
  5. :pt="{
  6. inner: {
  7. className: parseClass([
  8. [isDark, '!bg-surface-700', '!bg-surface-100'],
  9. pt.popup?.className
  10. ])
  11. },
  12. mask: {
  13. className: '!bg-transparent'
  14. }
  15. }"
  16. v-model="visible"
  17. >
  18. <view class="cl-keyboard-number" :class="[pt.className]">
  19. <slot name="value" :value="value">
  20. <view
  21. v-if="showValue"
  22. class="cl-keyboard-number__value"
  23. :class="[pt.value?.className]"
  24. >
  25. <cl-text
  26. v-if="value != ''"
  27. :pt="{
  28. className: '!text-2xl'
  29. }"
  30. >{{ value }}</cl-text
  31. >
  32. <cl-text
  33. v-else
  34. :pt="{
  35. className: '!text-md !text-surface-400'
  36. }"
  37. >{{ placeholder }}</cl-text
  38. >
  39. </view>
  40. </slot>
  41. <view class="cl-keyboard-number__list">
  42. <cl-row :gutter="10">
  43. <cl-col :span="6" v-for="item in list" :key="item">
  44. <view
  45. class="cl-keyboard-number__item"
  46. :class="[
  47. `is-keycode-${item}`,
  48. {
  49. 'is-dark': isDark,
  50. 'is-empty': item == ''
  51. },
  52. pt.item?.className
  53. ]"
  54. hover-class="opacity-50"
  55. :hover-stay-time="50"
  56. @touchstart.stop="onCommand(item)"
  57. >
  58. <slot name="item" :item="item">
  59. <cl-icon
  60. v-if="item == 'delete'"
  61. name="delete-back-2-line"
  62. :size="36"
  63. ></cl-icon>
  64. <view
  65. v-else-if="item == 'confirm'"
  66. class="cl-keyboard-number__item-confirm"
  67. >
  68. <cl-text
  69. color="white"
  70. :pt="{
  71. className: '!text-lg'
  72. }"
  73. >{{ confirmText }}</cl-text
  74. >
  75. </view>
  76. <cl-text
  77. v-else
  78. :pt="{
  79. className: '!text-lg'
  80. }"
  81. >{{ item }}</cl-text
  82. >
  83. </slot>
  84. </view>
  85. </cl-col>
  86. </cl-row>
  87. </view>
  88. </view>
  89. </cl-popup>
  90. </template>
  91. <script setup lang="ts">
  92. import { useUi } from "../../hooks";
  93. import type { PassThroughProps } from "../../types";
  94. import type { ClPopupProps } from "../cl-popup/props";
  95. import { ref, computed, watch, type PropType } from "vue";
  96. import { $t, t } from "@/locale";
  97. import { isDark, parseClass, parsePt } from "@/cool";
  98. import { vibrate } from "@/uni_modules/cool-vibrate";
  99. defineOptions({
  100. name: "cl-keyboard-number"
  101. });
  102. defineSlots<{
  103. value(props: { value: string }): any;
  104. item(props: { item: string }): any;
  105. }>();
  106. const props = defineProps({
  107. // 透传样式配置
  108. pt: {
  109. type: Object,
  110. default: () => ({})
  111. },
  112. // v-model绑定的值
  113. modelValue: {
  114. type: String,
  115. default: ""
  116. },
  117. // 键盘类型,支持number、digit、idcard
  118. type: {
  119. type: String as PropType<"number" | "digit" | "idcard">,
  120. default: "digit"
  121. },
  122. // 弹窗标题
  123. title: {
  124. type: String,
  125. default: () => t("数字键盘")
  126. },
  127. // 输入框占位符
  128. placeholder: {
  129. type: String,
  130. default: () => t("安全键盘,请放心输入")
  131. },
  132. // 最大输入长度
  133. maxlength: {
  134. type: Number,
  135. default: 10
  136. },
  137. // 确认按钮文本
  138. confirmText: {
  139. type: String,
  140. default: () => t("确定")
  141. },
  142. // 是否显示输入值
  143. showValue: {
  144. type: Boolean,
  145. default: true
  146. },
  147. // 是否输入即绑定
  148. inputImmediate: {
  149. type: Boolean,
  150. default: false
  151. }
  152. });
  153. // 定义事件发射器,支持v-model和change事件
  154. const emit = defineEmits(["update:modelValue", "change"]);
  155. // 样式穿透类型
  156. type PassThrough = {
  157. className?: string;
  158. item?: PassThroughProps;
  159. value?: PassThroughProps;
  160. popup?: ClPopupProps;
  161. };
  162. // 样式穿透计算
  163. const pt = computed(() => parsePt<PassThrough>(props.pt));
  164. // 获取UI相关的工具方法
  165. const ui = useUi();
  166. // 控制弹窗显示/隐藏
  167. const visible = ref(false);
  168. // 输入框当前值,双向绑定
  169. const value = ref(props.modelValue);
  170. // 最大输入长度
  171. const maxlength = computed(() => {
  172. if (props.type == "idcard") {
  173. return 18;
  174. }
  175. return props.maxlength;
  176. });
  177. // 数字键盘的按键列表,包含数字、删除、00和小数点
  178. const list = computed(() => {
  179. const arr = [
  180. "1",
  181. "2",
  182. "3",
  183. "delete",
  184. "4",
  185. "5",
  186. "6",
  187. "",
  188. "7",
  189. "8",
  190. "9",
  191. "",
  192. "00",
  193. "0",
  194. "",
  195. "confirm"
  196. ];
  197. // 数字键盘显示为小数点 "."
  198. if (props.type == "digit") {
  199. arr[14] = ".";
  200. }
  201. // 身份证键盘显示为 "X"
  202. if (props.type == "idcard") {
  203. arr[14] = "X";
  204. }
  205. return arr;
  206. });
  207. // 打开键盘弹窗
  208. function open() {
  209. visible.value = true;
  210. }
  211. // 关闭键盘弹窗
  212. function close() {
  213. visible.value = false;
  214. }
  215. // 处理键盘按键点击事件
  216. function onCommand(key: string) {
  217. // 震动
  218. vibrate(1);
  219. // 确认按钮逻辑
  220. if (key == "confirm") {
  221. if (value.value == "") {
  222. ui.showToast({
  223. message: t("请输入内容")
  224. });
  225. return;
  226. }
  227. // 如果最后一位是小数点,去掉
  228. if (value.value.endsWith(".")) {
  229. value.value = value.value.slice(0, -1);
  230. }
  231. // 身份证号码正则校验(支持15位和18位,18位末尾可为X/x)
  232. if (props.type == "idcard") {
  233. if (
  234. !/^(^[1-9]\d{5}(18|19|20)?\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}(\d|X|x)?$)$/.test(
  235. value.value
  236. )
  237. ) {
  238. ui.showToast({
  239. message: t("身份证号码格式不正确")
  240. });
  241. return;
  242. }
  243. }
  244. // 触发v-model和change事件
  245. emit("update:modelValue", value.value);
  246. emit("change", value.value);
  247. // 关闭弹窗
  248. close();
  249. return;
  250. }
  251. // 删除键,去掉最后一位
  252. if (key == "delete") {
  253. value.value = value.value.slice(0, -1);
  254. return;
  255. }
  256. // 超过最大输入长度,提示并返回
  257. if (value.value.length >= maxlength.value) {
  258. ui.showToast({
  259. message: $t("最多输入{maxlength}位", {
  260. maxlength: maxlength.value
  261. })
  262. });
  263. return;
  264. }
  265. // 处理小数点输入,已存在则不再添加
  266. if (key == ".") {
  267. if (value.value.includes(".")) {
  268. return;
  269. }
  270. if (value.value == "") {
  271. value.value = "0.";
  272. return;
  273. }
  274. }
  275. // 处理00键,首位不能输入00,只能输入0
  276. if (key == "00") {
  277. if (value.value.length + 2 > maxlength.value) {
  278. value.value += "0";
  279. return;
  280. }
  281. if (value.value == "") {
  282. value.value = "0";
  283. return;
  284. }
  285. }
  286. if (key == "00" || key == "0") {
  287. if (value.value == "" || value.value == "0") {
  288. value.value = "0";
  289. return;
  290. }
  291. }
  292. // 其他按键直接拼接到value
  293. value.value += key;
  294. }
  295. watch(value, (val: string) => {
  296. // 如果输入即绑定,则立即更新绑定值
  297. if (props.inputImmediate) {
  298. emit("update:modelValue", val);
  299. emit("change", val);
  300. }
  301. });
  302. // 监听外部v-model的变化,保持内部value同步
  303. watch(
  304. computed(() => props.modelValue),
  305. (val: string) => {
  306. value.value = val;
  307. }
  308. );
  309. defineExpose({
  310. open,
  311. close
  312. });
  313. </script>
  314. <style lang="scss" scoped>
  315. .cl-keyboard-number {
  316. padding: 0 20rpx 20rpx 20rpx;
  317. &__value {
  318. @apply flex flex-row items-center justify-center;
  319. height: 80rpx;
  320. margin-bottom: 20rpx;
  321. }
  322. &__list {
  323. @apply relative;
  324. }
  325. &__item {
  326. @apply flex items-center justify-center rounded-xl bg-white;
  327. height: 100rpx;
  328. margin-top: 10rpx;
  329. overflow: visible;
  330. &.is-dark {
  331. @apply bg-surface-800;
  332. }
  333. &.is-keycode-delete {
  334. @apply bg-surface-200;
  335. &.is-dark {
  336. @apply bg-surface-800;
  337. }
  338. }
  339. &.is-keycode-confirm {
  340. @apply bg-transparent relative;
  341. }
  342. &-confirm {
  343. @apply flex flex-col items-center justify-center;
  344. @apply bg-primary-500 absolute w-full rounded-xl;
  345. height: 320rpx;
  346. top: -220rpx;
  347. }
  348. &.is-empty {
  349. background-color: transparent !important;
  350. }
  351. }
  352. }
  353. </style>