cl-select.uvue 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. <template>
  2. <cl-select-trigger
  3. v-if="showTrigger"
  4. :pt="ptTrigger"
  5. :placeholder="placeholder"
  6. :disabled="disabled"
  7. :focus="popupRef?.isOpen"
  8. :text="text"
  9. @open="open()"
  10. @clear="clear"
  11. ></cl-select-trigger>
  12. <cl-popup ref="popupRef" v-model="visible" :title="title" :pt="ptPopup">
  13. <view class="cl-select-popup" @touchmove.stop>
  14. <slot name="prepend"></slot>
  15. <view class="cl-select-popup__picker">
  16. <cl-picker-view
  17. :value="indexes"
  18. :columns="columns"
  19. @change-index="onChange"
  20. ></cl-picker-view>
  21. </view>
  22. <slot name="append"></slot>
  23. <view class="cl-select-popup__op">
  24. <cl-button
  25. v-if="showCancel"
  26. size="large"
  27. text
  28. border
  29. type="light"
  30. :pt="{
  31. className: 'flex-1 !rounded-xl h-[80rpx]'
  32. }"
  33. @tap="close"
  34. >{{ cancelText }}</cl-button
  35. >
  36. <cl-button
  37. v-if="showConfirm"
  38. size="large"
  39. :pt="{
  40. className: 'flex-1 !rounded-xl h-[80rpx]'
  41. }"
  42. @tap="confirm"
  43. >{{ confirmText }}</cl-button
  44. >
  45. </view>
  46. </view>
  47. </cl-popup>
  48. </template>
  49. <script setup lang="ts">
  50. import { ref, computed, type PropType, watch } from "vue";
  51. import type { ClSelectOption, ClSelectValue } from "../../types";
  52. import { isEmpty, isNull, parsePt, parseToObject } from "@/cool";
  53. import type { ClSelectTriggerPassThrough } from "../cl-select-trigger/props";
  54. import type { ClPopupPassThrough } from "../cl-popup/props";
  55. import { t } from "@/locale";
  56. defineOptions({
  57. name: "cl-select"
  58. });
  59. defineSlots<{
  60. prepend(): any;
  61. append(): any;
  62. }>();
  63. // 组件属性定义
  64. const props = defineProps({
  65. // 透传样式配置
  66. pt: {
  67. type: Object,
  68. default: () => ({})
  69. },
  70. // 选择器的值
  71. modelValue: {
  72. type: [Array, Number, String] as PropType<ClSelectValue>,
  73. default: null
  74. },
  75. // 选择器标题
  76. title: {
  77. type: String,
  78. default: () => t("请选择")
  79. },
  80. // 选择器占位符
  81. placeholder: {
  82. type: String,
  83. default: () => t("请选择")
  84. },
  85. // 选项数据
  86. options: {
  87. type: Array as PropType<ClSelectOption[]>,
  88. default: () => []
  89. },
  90. // 是否显示选择器触发器
  91. showTrigger: {
  92. type: Boolean,
  93. default: true
  94. },
  95. // 是否禁用选择器
  96. disabled: {
  97. type: Boolean,
  98. default: false
  99. },
  100. // 列数
  101. columnCount: {
  102. type: Number as PropType<number>,
  103. default: 1
  104. },
  105. // 分隔符
  106. splitor: {
  107. type: String,
  108. default: " - "
  109. },
  110. // 确认按钮文本
  111. confirmText: {
  112. type: String,
  113. default: () => t("确定")
  114. },
  115. // 是否显示确认按钮
  116. showConfirm: {
  117. type: Boolean,
  118. default: true
  119. },
  120. // 取消按钮文本
  121. cancelText: {
  122. type: String,
  123. default: () => t("取消")
  124. },
  125. // 是否显示取消按钮
  126. showCancel: {
  127. type: Boolean,
  128. default: true
  129. }
  130. });
  131. // 定义事件
  132. const emit = defineEmits(["update:modelValue", "change", "changing"]);
  133. // 弹出层引用
  134. const popupRef = ref<ClPopupComponentPublicInstance | null>(null);
  135. // 透传样式类型定义
  136. type PassThrough = {
  137. trigger?: ClSelectTriggerPassThrough;
  138. popup?: ClPopupPassThrough;
  139. };
  140. // 解析透传样式配置
  141. const pt = computed(() => parsePt<PassThrough>(props.pt));
  142. // 解析触发器透传样式配置
  143. const ptTrigger = computed(() => parseToObject(pt.value.trigger));
  144. // 解析弹窗透传样式配置
  145. const ptPopup = computed(() => parseToObject(pt.value.popup));
  146. // 当前选中的值
  147. const value = ref<any[]>([]);
  148. // 当前选中项的索引
  149. const indexes = ref<number[]>([]);
  150. // 计算选择器列表数据
  151. const columns = computed<ClSelectOption[][]>(() => {
  152. // 获取原始选项数据
  153. let options = props.options;
  154. // 用于存储每一列的选项数据
  155. let columns = [] as ClSelectOption[][];
  156. // 根据当前选中值构建多列数据
  157. for (let i = 0; i < props.columnCount; i++) {
  158. // 复制当前层级的选项数据作为当前列的选项
  159. const column = [...options];
  160. // 获取当前列的选中值,如果value数组长度不足则为null
  161. const val = i >= value.value.length ? null : value.value[i];
  162. // 在当前列选项中查找选中值对应的选项
  163. let item = options.find((item) => item.value == val);
  164. // 如果未找到选中项且选项不为空,则默认选中第一项
  165. if (item == null && !isEmpty(options)) {
  166. item = options[0];
  167. }
  168. // 如果选中项有子选项,则更新options为子选项数据,用于构建下一列
  169. if (item?.children != null) {
  170. options = item.children as ClSelectOption[];
  171. }
  172. // 将当前列的选项数据添加到columns数组
  173. columns.push(column);
  174. }
  175. // 返回构建好的多列数据
  176. return columns;
  177. });
  178. // 显示文本
  179. const text = ref("");
  180. // 更新文本内容
  181. function updateText() {
  182. // 当前绑定的值
  183. const val = props.modelValue;
  184. // 如果值为null或空,直接返回空字符串
  185. if (val == null || isEmpty(val)) {
  186. text.value = "";
  187. } else {
  188. // 用于存储每列的选中值
  189. let arr: any[];
  190. if (props.columnCount == 1) {
  191. // 单列时将值包装为数组
  192. arr = [val];
  193. } else {
  194. // 多列时直接使用数组
  195. arr = val as any[];
  196. }
  197. // 遍历每列的选中值,查找对应label,找不到则用空字符串
  198. text.value = arr
  199. .map((e, i) => columns.value[i].find((a) => a.value == e)?.label ?? "")
  200. .join(props.splitor);
  201. }
  202. }
  203. // 获取当前选中值
  204. function getValue() {
  205. return props.columnCount == 1 ? value.value[0] : value.value;
  206. }
  207. // 解析值
  208. function setValue(val: ClSelectValue) {
  209. // 声明选中值数组
  210. let _value: any[];
  211. // 判断值是否为null
  212. if (val == null) {
  213. // 设置为空数组
  214. _value = [];
  215. }
  216. // 判断是否为数组类型
  217. else if (Array.isArray(val)) {
  218. // 使用该数组
  219. _value = [...(val as any[])];
  220. }
  221. // 其他类型
  222. else {
  223. // 转换为数组格式
  224. _value = [val];
  225. }
  226. // 存储每列选中项的索引值
  227. let _indexes = [] as number[];
  228. // 遍历所有列
  229. for (let i = 0; i < props.columnCount; i++) {
  230. // 获取当前列的选项数据
  231. const column = columns.value[i];
  232. // 判断是否超出选中值数组长度
  233. if (i >= _value.length) {
  234. // 添加默认索引0
  235. _indexes.push(0);
  236. // 添加默认值
  237. if (!isNull(column) && column.length > 0 && !isNull(column[0])) {
  238. _value.push(column[0].value);
  239. }
  240. }
  241. // 在范围内
  242. else {
  243. // 查找匹配的选项索引
  244. let index = column.findIndex((e) => e.value == _value[i]);
  245. // 索引无效时重置为0
  246. if (index < 0) {
  247. index = 0;
  248. }
  249. // 添加索引
  250. _indexes.push(index);
  251. }
  252. }
  253. // 更新选中值
  254. value.value = _value;
  255. // 更新索引值
  256. indexes.value = _indexes;
  257. // 更新显示文本
  258. updateText();
  259. }
  260. // 选择器值改变事件
  261. function onChange(a: number[]) {
  262. // 复制当前组件内部维护的索引数组
  263. const b = [...indexes.value];
  264. // 标记是否有列发生改变
  265. let changed = false;
  266. // 遍历所有列,处理联动逻辑
  267. for (let i = 0; i < a.length; i++) {
  268. if (changed) {
  269. // 如果前面的列发生改变,后续列重置为第一项(索引0)
  270. b[i] = 0;
  271. } else {
  272. // 检查当前列是否发生改变
  273. if (b[i] != a[i]) {
  274. // 更新当前列的索引
  275. b[i] = a[i];
  276. // 标记已发生改变,后续列需要重置
  277. changed = true;
  278. }
  279. }
  280. }
  281. // 更新组件内部维护的索引数组
  282. indexes.value = b;
  283. // 根据最新的索引数组,更新选中的值数组
  284. value.value = b.map((e, i) => (isNull(columns.value[i][e]) ? 0 : columns.value[i][e].value));
  285. // 触发changing事件
  286. emit("changing", getValue());
  287. }
  288. // 选择器显示状态
  289. const visible = ref(false);
  290. // 选择回调函数
  291. let callback: ((value: ClSelectValue) => void) | null = null;
  292. // 打开选择器
  293. function open(cb: ((value: ClSelectValue) => void) | null = null) {
  294. visible.value = true;
  295. // 设置值
  296. setValue(props.modelValue);
  297. // 回调
  298. callback = cb;
  299. }
  300. // 关闭选择器
  301. function close() {
  302. visible.value = false;
  303. }
  304. // 清空选择器
  305. function clear() {
  306. text.value = "";
  307. if (props.columnCount == 1) {
  308. emit("update:modelValue", null);
  309. emit("change", null);
  310. } else {
  311. emit("update:modelValue", [] as any[]);
  312. emit("change", [] as any[]);
  313. }
  314. }
  315. // 确认选择
  316. function confirm() {
  317. // 根据列数返回单个值或数组
  318. const val = getValue();
  319. // 触发更新事件
  320. emit("update:modelValue", val);
  321. emit("change", val);
  322. // 触发回调
  323. if (callback != null) {
  324. callback!(val);
  325. }
  326. // 关闭选择器
  327. close();
  328. }
  329. // 监听modelValue变化
  330. watch(
  331. computed(() => props.modelValue),
  332. (val: ClSelectValue) => {
  333. setValue(val);
  334. },
  335. {
  336. immediate: true
  337. }
  338. );
  339. defineExpose({
  340. open,
  341. close
  342. });
  343. </script>
  344. <style lang="scss" scoped>
  345. .cl-select {
  346. &-popup {
  347. &__op {
  348. @apply flex flex-row items-center justify-center;
  349. padding: 24rpx;
  350. }
  351. }
  352. }
  353. </style>