Picker.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. var __create = Object.create;
  2. var __defProp = Object.defineProperty;
  3. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  4. var __getOwnPropNames = Object.getOwnPropertyNames;
  5. var __getProtoOf = Object.getPrototypeOf;
  6. var __hasOwnProp = Object.prototype.hasOwnProperty;
  7. var __export = (target, all) => {
  8. for (var name2 in all)
  9. __defProp(target, name2, { get: all[name2], enumerable: true });
  10. };
  11. var __copyProps = (to, from, except, desc) => {
  12. if (from && typeof from === "object" || typeof from === "function") {
  13. for (let key of __getOwnPropNames(from))
  14. if (!__hasOwnProp.call(to, key) && key !== except)
  15. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  16. }
  17. return to;
  18. };
  19. var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  20. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  21. mod
  22. ));
  23. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  24. var stdin_exports = {};
  25. __export(stdin_exports, {
  26. default: () => stdin_default,
  27. pickerSharedProps: () => pickerSharedProps
  28. });
  29. module.exports = __toCommonJS(stdin_exports);
  30. var import_vue = require("vue");
  31. var import_vue2 = require("vue");
  32. var import_utils = require("../utils");
  33. var import_use = require("@vant/use");
  34. var import_use_expose = require("../composables/use-expose");
  35. var import_loading = require("../loading");
  36. var import_PickerColumn = __toESM(require("./PickerColumn"));
  37. const [name, bem, t] = (0, import_utils.createNamespace)("picker");
  38. const pickerSharedProps = {
  39. title: String,
  40. loading: Boolean,
  41. readonly: Boolean,
  42. allowHtml: Boolean,
  43. itemHeight: (0, import_utils.makeNumericProp)(44),
  44. showToolbar: import_utils.truthProp,
  45. swipeDuration: (0, import_utils.makeNumericProp)(1e3),
  46. visibleItemCount: (0, import_utils.makeNumericProp)(6),
  47. cancelButtonText: String,
  48. confirmButtonText: String
  49. };
  50. const pickerProps = (0, import_utils.extend)({}, pickerSharedProps, {
  51. columns: (0, import_utils.makeArrayProp)(),
  52. valueKey: String,
  53. defaultIndex: (0, import_utils.makeNumericProp)(0),
  54. toolbarPosition: (0, import_utils.makeStringProp)("top"),
  55. columnsFieldNames: Object
  56. });
  57. var stdin_default = (0, import_vue2.defineComponent)({
  58. name,
  59. props: pickerProps,
  60. emits: ["confirm", "cancel", "change"],
  61. setup(props, {
  62. emit,
  63. slots
  64. }) {
  65. if (process.env.NODE_ENV !== "production") {
  66. if (slots.default) {
  67. console.warn('[Vant] Picker: "default" slot is deprecated, please use "toolbar" slot instead.');
  68. }
  69. if (props.valueKey) {
  70. console.warn('[Vant] Picker: "valueKey" prop is deprecated, please use "columnsFieldNames" prop instead.');
  71. }
  72. }
  73. const hasOptions = (0, import_vue2.ref)(false);
  74. const columnsRef = (0, import_vue2.ref)();
  75. const formattedColumns = (0, import_vue2.ref)([]);
  76. const columnsFieldNames = (0, import_vue2.computed)(() => {
  77. const {
  78. columnsFieldNames: columnsFieldNames2
  79. } = props;
  80. return {
  81. text: (columnsFieldNames2 == null ? void 0 : columnsFieldNames2.text) || props.valueKey || "text",
  82. values: (columnsFieldNames2 == null ? void 0 : columnsFieldNames2.values) || "values",
  83. children: (columnsFieldNames2 == null ? void 0 : columnsFieldNames2.children) || "children"
  84. };
  85. });
  86. const {
  87. children,
  88. linkChildren
  89. } = (0, import_use.useChildren)(import_PickerColumn.PICKER_KEY);
  90. linkChildren();
  91. const itemHeight = (0, import_vue2.computed)(() => (0, import_utils.unitToPx)(props.itemHeight));
  92. const dataType = (0, import_vue2.computed)(() => {
  93. const firstColumn = props.columns[0];
  94. if (typeof firstColumn === "object") {
  95. if (columnsFieldNames.value.children in firstColumn) {
  96. return "cascade";
  97. }
  98. if (columnsFieldNames.value.values in firstColumn) {
  99. return "object";
  100. }
  101. }
  102. return "plain";
  103. });
  104. const formatCascade = () => {
  105. var _a;
  106. const formatted = [];
  107. let cursor = {
  108. [columnsFieldNames.value.children]: props.columns
  109. };
  110. while (cursor && cursor[columnsFieldNames.value.children]) {
  111. const children2 = cursor[columnsFieldNames.value.children];
  112. let defaultIndex = (_a = cursor.defaultIndex) != null ? _a : +props.defaultIndex;
  113. while (children2[defaultIndex] && children2[defaultIndex].disabled) {
  114. if (defaultIndex < children2.length - 1) {
  115. defaultIndex++;
  116. } else {
  117. defaultIndex = 0;
  118. break;
  119. }
  120. }
  121. formatted.push({
  122. [columnsFieldNames.value.values]: cursor[columnsFieldNames.value.children],
  123. className: cursor.className,
  124. defaultIndex
  125. });
  126. cursor = children2[defaultIndex];
  127. }
  128. formattedColumns.value = formatted;
  129. };
  130. const format = () => {
  131. const {
  132. columns
  133. } = props;
  134. if (dataType.value === "plain") {
  135. formattedColumns.value = [{
  136. [columnsFieldNames.value.values]: columns
  137. }];
  138. } else if (dataType.value === "cascade") {
  139. formatCascade();
  140. } else {
  141. formattedColumns.value = columns;
  142. }
  143. hasOptions.value = formattedColumns.value.some((item) => item[columnsFieldNames.value.values] && item[columnsFieldNames.value.values].length !== 0) || children.some((item) => item.hasOptions);
  144. };
  145. const getIndexes = () => children.map((child) => child.state.index);
  146. const setColumnValues = (index, options) => {
  147. const column = children[index];
  148. if (column) {
  149. column.setOptions(options);
  150. hasOptions.value = true;
  151. }
  152. };
  153. const onCascadeChange = (columnIndex) => {
  154. let cursor = {
  155. [columnsFieldNames.value.children]: props.columns
  156. };
  157. const indexes = getIndexes();
  158. for (let i = 0; i <= columnIndex; i++) {
  159. cursor = cursor[columnsFieldNames.value.children][indexes[i]];
  160. }
  161. while (cursor && cursor[columnsFieldNames.value.children]) {
  162. columnIndex++;
  163. setColumnValues(columnIndex, cursor[columnsFieldNames.value.children]);
  164. cursor = cursor[columnsFieldNames.value.children][cursor.defaultIndex || 0];
  165. }
  166. };
  167. const getChild = (index) => children[index];
  168. const getColumnValue = (index) => {
  169. const column = getChild(index);
  170. if (column) {
  171. return column.getValue();
  172. }
  173. };
  174. const setColumnValue = (index, value) => {
  175. const column = getChild(index);
  176. if (column) {
  177. column.setValue(value);
  178. if (dataType.value === "cascade") {
  179. onCascadeChange(index);
  180. }
  181. }
  182. };
  183. const getColumnIndex = (index) => {
  184. const column = getChild(index);
  185. if (column) {
  186. return column.state.index;
  187. }
  188. };
  189. const setColumnIndex = (columnIndex, optionIndex) => {
  190. const column = getChild(columnIndex);
  191. if (column) {
  192. column.setIndex(optionIndex);
  193. if (dataType.value === "cascade") {
  194. onCascadeChange(columnIndex);
  195. }
  196. }
  197. };
  198. const getColumnValues = (index) => {
  199. const column = getChild(index);
  200. if (column) {
  201. return column.state.options;
  202. }
  203. };
  204. const getValues = () => children.map((child) => child.getValue());
  205. const setValues = (values) => {
  206. values.forEach((value, index) => {
  207. setColumnValue(index, value);
  208. });
  209. };
  210. const setIndexes = (indexes) => {
  211. indexes.forEach((optionIndex, columnIndex) => {
  212. setColumnIndex(columnIndex, optionIndex);
  213. });
  214. };
  215. const emitAction = (event) => {
  216. if (dataType.value === "plain") {
  217. emit(event, getColumnValue(0), getColumnIndex(0));
  218. } else {
  219. emit(event, getValues(), getIndexes());
  220. }
  221. };
  222. const onChange = (columnIndex) => {
  223. if (dataType.value === "cascade") {
  224. onCascadeChange(columnIndex);
  225. }
  226. if (dataType.value === "plain") {
  227. emit("change", getColumnValue(0), getColumnIndex(0));
  228. } else {
  229. emit("change", getValues(), columnIndex);
  230. }
  231. };
  232. const confirm = () => {
  233. children.forEach((child) => child.stopMomentum());
  234. emitAction("confirm");
  235. };
  236. const cancel = () => emitAction("cancel");
  237. const renderTitle = () => {
  238. if (slots.title) {
  239. return slots.title();
  240. }
  241. if (props.title) {
  242. return (0, import_vue.createVNode)("div", {
  243. "class": [bem("title"), "van-ellipsis"]
  244. }, [props.title]);
  245. }
  246. };
  247. const renderCancel = () => {
  248. const text = props.cancelButtonText || t("cancel");
  249. return (0, import_vue.createVNode)("button", {
  250. "type": "button",
  251. "class": [bem("cancel"), import_utils.HAPTICS_FEEDBACK],
  252. "onClick": cancel
  253. }, [slots.cancel ? slots.cancel() : text]);
  254. };
  255. const renderConfirm = () => {
  256. const text = props.confirmButtonText || t("confirm");
  257. return (0, import_vue.createVNode)("button", {
  258. "type": "button",
  259. "class": [bem("confirm"), import_utils.HAPTICS_FEEDBACK],
  260. "onClick": confirm
  261. }, [slots.confirm ? slots.confirm() : text]);
  262. };
  263. const renderToolbar = () => {
  264. if (props.showToolbar) {
  265. const slot = slots.toolbar || slots.default;
  266. return (0, import_vue.createVNode)("div", {
  267. "class": bem("toolbar")
  268. }, [slot ? slot() : [renderCancel(), renderTitle(), renderConfirm()]]);
  269. }
  270. };
  271. const renderColumnItems = () => formattedColumns.value.map((item, columnIndex) => {
  272. var _a;
  273. return (0, import_vue.createVNode)(import_PickerColumn.default, {
  274. "textKey": columnsFieldNames.value.text,
  275. "readonly": props.readonly,
  276. "allowHtml": props.allowHtml,
  277. "className": item.className,
  278. "itemHeight": itemHeight.value,
  279. "defaultIndex": (_a = item.defaultIndex) != null ? _a : +props.defaultIndex,
  280. "swipeDuration": props.swipeDuration,
  281. "initialOptions": item[columnsFieldNames.value.values],
  282. "visibleItemCount": props.visibleItemCount,
  283. "onChange": () => onChange(columnIndex)
  284. }, {
  285. option: slots.option
  286. });
  287. });
  288. const renderMask = (wrapHeight) => {
  289. if (hasOptions.value) {
  290. const frameStyle = {
  291. height: `${itemHeight.value}px`
  292. };
  293. const maskStyle = {
  294. backgroundSize: `100% ${(wrapHeight - itemHeight.value) / 2}px`
  295. };
  296. return [(0, import_vue.createVNode)("div", {
  297. "class": bem("mask"),
  298. "style": maskStyle
  299. }, null), (0, import_vue.createVNode)("div", {
  300. "class": [import_utils.BORDER_UNSET_TOP_BOTTOM, bem("frame")],
  301. "style": frameStyle
  302. }, null)];
  303. }
  304. };
  305. const renderColumns = () => {
  306. const wrapHeight = itemHeight.value * +props.visibleItemCount;
  307. const columnsStyle = {
  308. height: `${wrapHeight}px`
  309. };
  310. return (0, import_vue.createVNode)("div", {
  311. "ref": columnsRef,
  312. "class": bem("columns"),
  313. "style": columnsStyle
  314. }, [renderColumnItems(), renderMask(wrapHeight)]);
  315. };
  316. (0, import_vue2.watch)(() => props.columns, format, {
  317. immediate: true
  318. });
  319. (0, import_use.useEventListener)("touchmove", import_utils.preventDefault, {
  320. target: columnsRef
  321. });
  322. (0, import_use_expose.useExpose)({
  323. confirm,
  324. getValues,
  325. setValues,
  326. getIndexes,
  327. setIndexes,
  328. getColumnIndex,
  329. setColumnIndex,
  330. getColumnValue,
  331. setColumnValue,
  332. getColumnValues,
  333. setColumnValues
  334. });
  335. return () => {
  336. var _a, _b;
  337. return (0, import_vue.createVNode)("div", {
  338. "class": bem()
  339. }, [props.toolbarPosition === "top" ? renderToolbar() : null, props.loading ? (0, import_vue.createVNode)(import_loading.Loading, {
  340. "class": bem("loading")
  341. }, null) : null, (_a = slots["columns-top"]) == null ? void 0 : _a.call(slots), renderColumns(), (_b = slots["columns-bottom"]) == null ? void 0 : _b.call(slots), props.toolbarPosition === "bottom" ? renderToolbar() : null]);
  342. };
  343. }
  344. });