cl-keyboard-number.uvue 7.5 KB

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