cl-select.uvue 8.9 KB

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