cl-select.uvue 8.8 KB

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