cl-select.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <template>
  2. <view class="cl-select" :class="[classList]">
  3. <picker
  4. :mode="mode"
  5. :value="index"
  6. :range="options"
  7. :range-key="rangeKey"
  8. :disabled="isDisabled"
  9. :end="end"
  10. :start="start"
  11. :fields="fields"
  12. @change="onChange"
  13. @columnchange="onColumnChange"
  14. @cancel="onCancel"
  15. >
  16. <view class="cl-select__inner">
  17. <text class="cl-select__value"
  18. >{{ text
  19. }}<text class="cl-select__placeholder" v-if="showPlaceholder">{{
  20. placeholder
  21. }}</text></text
  22. >
  23. </view>
  24. </picker>
  25. <text class="cl-select__icon cl-icon-arrow-bottom"></text>
  26. </view>
  27. </template>
  28. <script>
  29. import { isArray, isEmpty, compareValue } from "../../utils";
  30. import Form from "../../mixins/form";
  31. /**
  32. * select 下拉选择
  33. * @description 下拉选择,基于 picker 组件的封装
  34. * @tutorial https://docs.cool-js.com/uni/components/form/select.html
  35. * @property {null} value 绑定值
  36. * @property {String, Number} mode 选择器模式,selector | multiSelector
  37. * @property {String} placeholder 占位内容
  38. * @property {Array<Object>} options 数据列表
  39. * @property {String} labelKey 内容关键字,默认label
  40. * @property {String} valueKey 值关键字,默认value
  41. * @property {String} separator 分隔符,默认 /
  42. * @property {Boolean} disabled 是否禁用
  43. * @property {Boolean} border 是否带有边框
  44. * @property {Boolean} fields 日期字段,有效值 year | month | day
  45. * @property {Boolean} start 有效日期的开始
  46. * @property {Boolean} end 有效日期的结束
  47. * @event {Function} change 绑定值改变时触发
  48. * @event {Function} confirm 绑定值改变时触发,返回完整数据
  49. * @event {Function} cancel 取消时触发
  50. * @event {Function} column-change 列发生改变时触发
  51. * @example <cl-select :options="[{label: '内容1', value: 1},{label: '内容2', value: 2}]" />
  52. */
  53. export default {
  54. name: "cl-select",
  55. props: {
  56. // 绑定值
  57. value: null,
  58. // 选择器模式
  59. mode: {
  60. type: String,
  61. default: "selector",
  62. },
  63. // 占位内容
  64. placeholder: {
  65. type: String,
  66. default: "请选择",
  67. },
  68. // 数据列表
  69. options: {
  70. type: Array,
  71. default: () => [],
  72. },
  73. // 内容关键字
  74. labelKey: {
  75. type: String,
  76. default: "label",
  77. },
  78. // 值关键字
  79. valueKey: {
  80. type: String,
  81. default: "value",
  82. },
  83. // 分隔符
  84. separator: {
  85. type: String,
  86. default: "/",
  87. },
  88. // 是否禁用
  89. disabled: Boolean,
  90. // 是否带有边框
  91. border: Boolean,
  92. // 日期字段,有效值 year | month | day
  93. fields: {
  94. type: String,
  95. default: "day",
  96. },
  97. // 有效日期的开始
  98. start: String,
  99. // 有效日期的结束
  100. end: String,
  101. // 是否默认返回第一个
  102. defaultFirstOption: {
  103. type: Boolean,
  104. default: true,
  105. },
  106. // 设置 options 时是否重新解析 value
  107. setOptionsIsParseValue: Boolean,
  108. },
  109. mixins: [Form],
  110. data() {
  111. return {
  112. index: "",
  113. text: "",
  114. };
  115. },
  116. watch: {
  117. value: {
  118. immediate: true,
  119. handler(val) {
  120. this.parse(val);
  121. },
  122. },
  123. options: {
  124. immediate: true,
  125. handler(arr) {
  126. // 避免重复设置 options 异常问题
  127. if (!this.setOptionsIsParseValue) {
  128. this.parse(this.value);
  129. }
  130. // 为空时,默认返回列表第一个
  131. if (arr && arr.length > 0 && this.defaultFirstOption) {
  132. if (this.value === undefined) {
  133. this.$emit("input", arr[0][this.valueKey]);
  134. }
  135. }
  136. },
  137. },
  138. },
  139. computed: {
  140. rangeKey() {
  141. return this.mode == "region" ? "" : this.labelKey;
  142. },
  143. showPlaceholder() {
  144. return isEmpty(this.text);
  145. },
  146. classList() {
  147. let list = [];
  148. if (this.isDisabled) {
  149. list.push("is-disabled");
  150. }
  151. if (this.border) {
  152. list.push("is-border");
  153. }
  154. return list.join(" ");
  155. },
  156. },
  157. methods: {
  158. parse(val) {
  159. // 取下标
  160. this.index = (() => {
  161. switch (this.mode) {
  162. case "selector":
  163. return this.options.findIndex((e) => compareValue(e[this.valueKey], val));
  164. case "multiSelector":
  165. return (isArray(val) ? val : [val]).map((v, i) => {
  166. return this.options[i].findIndex((e) =>
  167. compareValue(e[this.valueKey], v)
  168. );
  169. });
  170. default:
  171. return val;
  172. }
  173. })();
  174. // 取文本值
  175. this.text = (() => {
  176. switch (this.mode) {
  177. case "selector":
  178. return this.options[this.index]
  179. ? this.options[this.index][this.labelKey]
  180. : "";
  181. case "multiSelector":
  182. return this.index
  183. .filter((v) => v >= 0)
  184. .map((v, i) => this.options[i][v][this.labelKey])
  185. .join(this.separator);
  186. case "region":
  187. console.warn("请使用 cl-select-region 代替");
  188. default:
  189. return this.index;
  190. }
  191. })();
  192. },
  193. onChange({ detail }) {
  194. if (detail.value < 0 || detail.value === undefined) {
  195. return false;
  196. }
  197. // 返回的完整数据
  198. let data = null;
  199. // 返回的唯一数据
  200. let value = null;
  201. switch (this.mode) {
  202. case "selector":
  203. data = this.options[detail.value];
  204. value = data ? data[this.valueKey] : null;
  205. break;
  206. case "multiSelector":
  207. data = detail.value
  208. .map((v) => (v < 0 ? 0 : v))
  209. .map((v, i) => this.options[i][v]);
  210. value = data.map((e) => e[this.valueKey]);
  211. break;
  212. default:
  213. value = detail.value;
  214. }
  215. this.$emit("confirm", data);
  216. this.$emit("change", value);
  217. this.$emit("input", value);
  218. },
  219. onColumnChange({ detail }) {
  220. this.index = this.index.map((v, i) =>
  221. i < detail.column ? v : i === detail.column ? detail.value : 0
  222. );
  223. this.$emit("column-change", { ...detail, selects: this.index });
  224. },
  225. onCancel() {
  226. this.$emit("cancel");
  227. },
  228. },
  229. };
  230. </script>