cl-keyboard-car.uvue 6.8 KB

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