cl-keyboard-number.uvue 7.4 KB

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