cl-keyboard-car.uvue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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-car" :class="[pt.className]">
  19. <slot name="value" :value="value">
  20. <view
  21. v-if="showValue"
  22. class="cl-keyboard-car__value"
  23. :class="[pt.value?.className]"
  24. >
  25. <cl-text
  26. v-if="value != ''"
  27. :pt="{
  28. className: '!text-2xl'
  29. }"
  30. >{{ valueText }}</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-car__list">
  42. <view
  43. class="cl-keyboard-car__rows"
  44. v-for="(row, rowIndex) in list"
  45. :key="rowIndex"
  46. :class="[`is-mode-${mode}`]"
  47. >
  48. <view
  49. v-for="(item, index) in row"
  50. :key="item"
  51. class="cl-keyboard-car__item"
  52. :class="[
  53. `is-keycode-${item}`,
  54. {
  55. 'is-dark': isDark,
  56. 'is-empty': item == '',
  57. 'is-fill': rowIndex == 0 && mode == 'plate'
  58. },
  59. pt.item?.className
  60. ]"
  61. :style="{
  62. marginRight: index == row.length - 1 ? '0' : '10rpx'
  63. }"
  64. hover-class="opacity-50"
  65. :hover-stay-time="50"
  66. @touchstart.stop="onCommand(item)"
  67. >
  68. <slot name="item" :item="item">
  69. <cl-icon
  70. v-if="item == 'delete'"
  71. name="delete-back-2-line"
  72. :size="36"
  73. ></cl-icon>
  74. <cl-icon
  75. v-else-if="item == 'confirm'"
  76. name="check-line"
  77. :size="36"
  78. color="white"
  79. ></cl-icon>
  80. <cl-text v-else>{{ item }}</cl-text>
  81. </slot>
  82. </view>
  83. </view>
  84. </view>
  85. </view>
  86. </cl-popup>
  87. </template>
  88. <script setup lang="ts">
  89. import { useUi } from "../../hooks";
  90. import type { PassThroughProps } from "../../types";
  91. import type { ClPopupProps } from "../cl-popup/props";
  92. import { ref, computed, watch } from "vue";
  93. import { $t, t } from "@/locale";
  94. import { isDark, parseClass, parsePt } from "@/cool";
  95. import { vibrate } from "@/uni_modules/cool-vibrate";
  96. defineOptions({
  97. name: "cl-keyboard-car"
  98. });
  99. defineSlots<{
  100. value(props: { value: string }): any;
  101. item(props: { item: string }): any;
  102. }>();
  103. const props = defineProps({
  104. // 透传样式配置
  105. pt: {
  106. type: Object,
  107. default: () => ({})
  108. },
  109. // v-model绑定的值
  110. modelValue: {
  111. type: String,
  112. default: ""
  113. },
  114. // 弹窗标题
  115. title: {
  116. type: String,
  117. default: () => t("车牌键盘")
  118. },
  119. // 输入框占位符
  120. placeholder: {
  121. type: String,
  122. default: () => t("安全键盘,请放心输入")
  123. },
  124. // 最大输入长度
  125. maxlength: {
  126. type: Number,
  127. default: 8
  128. },
  129. // 是否显示输入值
  130. showValue: {
  131. type: Boolean,
  132. default: true
  133. },
  134. // 是否输入即绑定
  135. inputImmediate: {
  136. type: Boolean,
  137. default: false
  138. }
  139. });
  140. // 定义事件发射器,支持v-model和change事件
  141. const emit = defineEmits(["update:modelValue", "change"]);
  142. // 样式穿透类型
  143. type PassThrough = {
  144. className?: string;
  145. item?: PassThroughProps;
  146. value?: PassThroughProps;
  147. popup?: ClPopupProps;
  148. };
  149. // 样式穿透计算
  150. const pt = computed(() => parsePt<PassThrough>(props.pt));
  151. // 获取UI相关的工具方法
  152. const ui = useUi();
  153. // 控制弹窗显示/隐藏
  154. const visible = ref(false);
  155. // 输入框当前值,双向绑定
  156. const value = ref(props.modelValue);
  157. // 键盘模式,province: 省份简称区,plate: 字母数字及特殊类型区
  158. const mode = ref<"province" | "plate">("province");
  159. // 输入框显示值
  160. const valueText = computed(() => {
  161. if (value.value.length > 2) {
  162. return value.value.substring(0, 2) + " · " + value.value.substring(2);
  163. }
  164. return value.value;
  165. });
  166. // 数字键盘的按键列表,包含数字、删除、00和小数点
  167. const list = computed(() => {
  168. // 车牌键盘的省份简称区
  169. const province: string[][] = [
  170. ["京", "沪", "粤", "津", "冀", "豫", "云", "辽", "黑", "湘"],
  171. ["皖", "鲁", "新", "苏", "浙", "赣", "鄂", "桂", "甘"],
  172. ["晋", "蒙", "陕", "吉", "闽", "贵", "渝", "川"],
  173. ["青", "藏", "琼", "宁"]
  174. ];
  175. // 车牌键盘的字母数字及特殊类型区
  176. const plate: string[][] = [
  177. ["学", "警", "港", "澳", "领", "使", "电", "挂"],
  178. ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"],
  179. ["Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P"],
  180. ["A", "S", "D", "F", "G", "H", "J", "K", "L", "M"],
  181. ["Z", "X", "C", "V", "B", "N", "M", "delete", "confirm"]
  182. ];
  183. // 默认返回省份区,后续可根据输入位数切换键盘区
  184. return mode.value == "province" ? province : plate;
  185. });
  186. // 打开键盘弹窗
  187. function open() {
  188. visible.value = true;
  189. }
  190. // 关闭键盘弹窗
  191. function close() {
  192. visible.value = false;
  193. }
  194. // 处理键盘按键点击事件
  195. function onCommand(key: string) {
  196. // 震动
  197. vibrate(1);
  198. // 确认按钮逻辑
  199. if (key == "confirm") {
  200. if (value.value == "") {
  201. ui.showToast({
  202. message: t("请输入内容")
  203. });
  204. return;
  205. }
  206. // 校验车牌号
  207. if (value.value.length < 7) {
  208. ui.showToast({
  209. message: t("车牌号格式不正确")
  210. });
  211. return;
  212. }
  213. // 触发v-model和change事件
  214. emit("update:modelValue", value.value);
  215. emit("change", value.value);
  216. // 关闭弹窗
  217. close();
  218. return;
  219. }
  220. // 删除键,去掉最后一位
  221. if (key == "delete") {
  222. value.value = value.value.slice(0, -1);
  223. return;
  224. }
  225. // 超过最大输入长度,提示并返回
  226. if (value.value.length >= props.maxlength) {
  227. ui.showToast({
  228. message: $t("最多输入{maxlength}位", {
  229. maxlength: props.maxlength
  230. })
  231. });
  232. return;
  233. }
  234. // 其他按键直接拼接到value
  235. value.value += key;
  236. }
  237. watch(value, (val: string) => {
  238. // 如果输入即绑定,则立即更新绑定值
  239. if (props.inputImmediate) {
  240. emit("update:modelValue", val);
  241. emit("change", val);
  242. }
  243. // 根据输入位数切换键盘模式
  244. mode.value = val.length < 1 ? "province" : "plate";
  245. });
  246. // 监听外部v-model的变化,保持内部value同步
  247. watch(
  248. computed(() => props.modelValue),
  249. (val: string) => {
  250. value.value = val;
  251. }
  252. );
  253. defineExpose({
  254. open,
  255. close
  256. });
  257. </script>
  258. <style lang="scss" scoped>
  259. .cl-keyboard-car {
  260. padding: 0 20rpx 20rpx 20rpx;
  261. &__value {
  262. @apply flex flex-row items-center justify-center;
  263. height: 80rpx;
  264. margin-bottom: 20rpx;
  265. }
  266. &__list {
  267. @apply relative;
  268. }
  269. &__rows {
  270. @apply flex flex-row items-center;
  271. &.is-mode-province {
  272. @apply justify-center;
  273. }
  274. &.is-mode-plate {
  275. @apply justify-between;
  276. }
  277. }
  278. &__item {
  279. @apply flex items-center justify-center rounded-lg bg-white;
  280. height: 80rpx;
  281. width: 62rpx;
  282. margin-top: 10rpx;
  283. &.is-dark {
  284. @apply bg-surface-800;
  285. }
  286. &.is-fill {
  287. flex: 1;
  288. }
  289. &.is-keycode-delete {
  290. @apply bg-surface-200;
  291. flex: 1;
  292. &.is-dark {
  293. @apply bg-surface-600;
  294. }
  295. }
  296. &.is-keycode-confirm {
  297. @apply bg-primary-500;
  298. flex: 1;
  299. }
  300. &.is-empty {
  301. background-color: transparent !important;
  302. }
  303. }
  304. }
  305. </style>