cl-keyboard-car.uvue 7.2 KB

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