clxx 2.0.5 → 2.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. package/build/Ago/index.d.ts +3 -5
  2. package/build/Ago/index.js +6 -7
  3. package/build/Alert/Wrapper.d.ts +3 -4
  4. package/build/Alert/Wrapper.js +10 -17
  5. package/build/Alert/index.d.ts +2 -2
  6. package/build/Alert/index.js +9 -10
  7. package/build/AutoGrid/index.d.ts +4 -5
  8. package/build/AutoGrid/index.js +19 -21
  9. package/build/CarouselNotice/index.d.ts +4 -5
  10. package/build/CarouselNotice/index.js +20 -21
  11. package/build/Clickable/index.d.ts +1 -1
  12. package/build/Clickable/index.js +3 -2
  13. package/build/Container/index.d.ts +3 -4
  14. package/build/Container/index.js +37 -39
  15. package/build/Countdowner/index.d.ts +4 -5
  16. package/build/Countdowner/index.js +13 -14
  17. package/build/Dialog/Wrapper.d.ts +2 -2
  18. package/build/Dialog/Wrapper.js +6 -9
  19. package/build/Dialog/index.d.ts +3 -3
  20. package/build/Dialog/index.js +17 -12
  21. package/build/Flex/Col.d.ts +7 -9
  22. package/build/Flex/Col.js +14 -15
  23. package/build/Flex/Row.d.ts +7 -9
  24. package/build/Flex/Row.js +14 -15
  25. package/build/Flex/index.d.ts +2 -4
  26. package/build/Flex/index.js +5 -6
  27. package/build/Indicator/index.d.ts +4 -5
  28. package/build/Indicator/index.js +14 -15
  29. package/build/Loading/Wrapper.d.ts +6 -7
  30. package/build/Loading/Wrapper.js +14 -18
  31. package/build/Loading/index.d.ts +1 -1
  32. package/build/Loading/index.js +10 -11
  33. package/build/Overlay/index.d.ts +2 -4
  34. package/build/Overlay/index.js +15 -17
  35. package/build/SafeArea/index.d.ts +2 -4
  36. package/build/SafeArea/index.js +15 -10
  37. package/build/ScrollView/index.d.ts +2 -2
  38. package/build/ScrollView/index.js +3 -8
  39. package/build/Toast/Toast.d.ts +2 -2
  40. package/build/Toast/Toast.js +4 -5
  41. package/build/Toast/index.d.ts +2 -2
  42. package/build/Toast/index.js +13 -11
  43. package/build/Toast/style.d.ts +1 -1
  44. package/build/index.d.ts +0 -2
  45. package/build/index.js +0 -2
  46. package/build/utils/createApp.d.ts +7 -7
  47. package/build/utils/createApp.js +28 -22
  48. package/build/utils/dom.d.ts +5 -5
  49. package/build/utils/dom.js +14 -27
  50. package/package.json +15 -15
  51. package/build/Cascader/Column.d.ts +0 -13
  52. package/build/Cascader/Column.js +0 -65
  53. package/build/Cascader/Wrapper.d.ts +0 -21
  54. package/build/Cascader/Wrapper.js +0 -105
  55. package/build/Cascader/index.d.ts +0 -21
  56. package/build/Cascader/index.js +0 -63
  57. package/build/Cascader/style.d.ts +0 -4
  58. package/build/Cascader/style.js +0 -111
  59. package/build/Picker/Wrapper.d.ts +0 -20
  60. package/build/Picker/Wrapper.js +0 -92
  61. package/build/Picker/index.d.ts +0 -21
  62. package/build/Picker/index.js +0 -64
  63. package/build/Picker/style.d.ts +0 -3
  64. package/build/Picker/style.js +0 -122
  65. package/build/Picker/swiperStyle.d.ts +0 -1
  66. package/build/Picker/swiperStyle.js +0 -66
@@ -9,8 +9,7 @@ var __rest = (this && this.__rest) || function (s, e) {
9
9
  }
10
10
  return t;
11
11
  };
12
- /** @jsx jsx */
13
- import { jsx } from "@emotion/react";
12
+ import { jsx as _jsx, jsxs as _jsxs } from "@emotion/react/jsx-runtime";
14
13
  import { useEffect, useRef, useState } from "react";
15
14
  import { Indicator } from "../Indicator";
16
15
  import { RowCenter } from "../Flex/Row";
@@ -65,9 +64,7 @@ export function ScrollView(props) {
65
64
  let showLoadingContent = null;
66
65
  if (showLoading) {
67
66
  if (!loadingContent) {
68
- showLoadingContent = (jsx(RowCenter, { css: [style.loading, loadingStyle] },
69
- jsx(Indicator, { barColor: "#333", barCount: 12 }),
70
- jsx("p", null, "\u6570\u636E\u52A0\u8F7D\u4E2D...")));
67
+ showLoadingContent = (_jsxs(RowCenter, Object.assign({ css: [style.loading, loadingStyle] }, { children: [_jsx(Indicator, { barColor: "#333", barCount: 12 }), _jsx("p", { children: "\u6570\u636E\u52A0\u8F7D\u4E2D..." })] })));
71
68
  }
72
69
  else {
73
70
  showLoadingContent = loadingContent;
@@ -79,7 +76,5 @@ export function ScrollView(props) {
79
76
  let hasScrollBar = container.current.scrollHeight > container.current.clientHeight;
80
77
  setHasScrollBar(hasScrollBar);
81
78
  });
82
- return (jsx("div", Object.assign({ css: [style.container, heightStyle, containerStyle], onScroll: scrollCallback, ref: container }, attrs),
83
- jsx("div", { css: wrapperStyle, ref: child }, children),
84
- hasScrollBar && showLoadingContent));
79
+ return (_jsxs("div", Object.assign({ css: [style.container, heightStyle, containerStyle], onScroll: scrollCallback, ref: container }, attrs, { children: [_jsx("div", Object.assign({ css: wrapperStyle, ref: child }, { children: children })), hasScrollBar && showLoadingContent] })));
85
80
  }
@@ -1,5 +1,5 @@
1
1
  /** @jsx jsx */
2
- import { Interpolation, jsx, Theme } from "@emotion/react";
2
+ import { Interpolation, Theme } from "@emotion/react";
3
3
  import React from "react";
4
4
  export interface ToastProps extends Omit<React.HTMLProps<HTMLDivElement>, "content"> {
5
5
  onHide?: () => void;
@@ -12,4 +12,4 @@ export interface ToastProps extends Omit<React.HTMLProps<HTMLDivElement>, "conte
12
12
  containerStyle?: Interpolation<Theme>;
13
13
  contentStyle?: Interpolation<Theme>;
14
14
  }
15
- export declare function Toast(props: ToastProps): jsx.JSX.Element;
15
+ export declare function Toast(props: ToastProps): import("@emotion/react/jsx-runtime").JSX.Element;
@@ -9,8 +9,7 @@ var __rest = (this && this.__rest) || function (s, e) {
9
9
  }
10
10
  return t;
11
11
  };
12
- /** @jsx jsx */
13
- import { jsx } from "@emotion/react";
12
+ import { jsx as _jsx } from "@emotion/react/jsx-runtime";
14
13
  import React, { useState, useEffect } from "react";
15
14
  import { style, getAnimation } from "./style";
16
15
  export function Toast(props) {
@@ -30,10 +29,10 @@ export function Toast(props) {
30
29
  let showContent;
31
30
  const middleStyle = position === "middle" ? style.contentMiddle : undefined;
32
31
  if (React.isValidElement(content)) {
33
- showContent = jsx("div", { css: [middleStyle, contentStyle] }, content);
32
+ showContent = _jsx("div", Object.assign({ css: [middleStyle, contentStyle] }, { children: content }));
34
33
  }
35
34
  else {
36
- showContent = (jsx("p", { css: [style.content(radius), middleStyle, contentStyle] }, content));
35
+ showContent = (_jsx("p", Object.assign({ css: [style.content(radius), middleStyle, contentStyle] }, { children: content })));
37
36
  }
38
37
  // toast消失动画结束触发
39
38
  const animationEnd = (event) => {
@@ -52,5 +51,5 @@ export function Toast(props) {
52
51
  else {
53
52
  positionStyle = style.middle;
54
53
  }
55
- return (jsx("div", Object.assign({ css: [style.container(), positionStyle, animation, containerStyle], onAnimationEnd: animationEnd }, attributes), showContent));
54
+ return (_jsx("div", Object.assign({ css: [style.container(), positionStyle, animation, containerStyle], onAnimationEnd: animationEnd }, attributes, { children: showContent })));
56
55
  }
@@ -1,5 +1,5 @@
1
- import React from "react";
2
- import { ToastProps } from "./Toast";
1
+ import React from 'react';
2
+ import { ToastProps } from './Toast';
3
3
  /**
4
4
  * 显示一个全局的轻提示,这个toast不是唯一的
5
5
  * @param option 可以是一个字符串,也可以是一个React组件
@@ -1,22 +1,24 @@
1
- import React from "react";
2
- import { uniqKey } from "../utils/uniqKey";
3
- import { createPortalDOM } from "../utils/dom";
4
- import { Toast as ToastComponent } from "./Toast";
1
+ import { createElement as _createElement } from "@emotion/react";
2
+ import { jsx as _jsx } from "@emotion/react/jsx-runtime";
3
+ import React from 'react';
4
+ import { uniqKey } from '../utils/uniqKey';
5
+ import { createPortalDOM } from '../utils/dom';
6
+ import { Toast as ToastComponent } from './Toast';
5
7
  /**
6
8
  * 显示一个全局的轻提示,这个toast不是唯一的
7
9
  * @param option 可以是一个字符串,也可以是一个React组件
8
10
  */
9
11
  export function showToast(option) {
10
- const { mount, destroy } = createPortalDOM();
12
+ const { mount, unmount } = createPortalDOM();
11
13
  let props = {};
12
- if (React.isValidElement(option) || typeof option !== "object") {
14
+ if (React.isValidElement(option) || typeof option !== 'object') {
13
15
  props.content = option;
14
16
  }
15
17
  else {
16
18
  props = option;
17
19
  }
18
- props.onHide = destroy;
19
- mount(React.createElement(ToastComponent, Object.assign({}, props)));
20
+ props.onHide = unmount;
21
+ mount(_jsx(ToastComponent, Object.assign({}, props)));
20
22
  }
21
23
  /**
22
24
  * 生成一个全局唯一的Toast
@@ -29,16 +31,16 @@ export function showUniqToast(option) {
29
31
  }
30
32
  let props = {};
31
33
  // 默认Toast是唯一的
32
- if (React.isValidElement(option) || typeof option !== "object") {
34
+ if (React.isValidElement(option) || typeof option !== 'object') {
33
35
  props.content = option;
34
36
  }
35
37
  else {
36
38
  props = option;
37
39
  }
38
40
  const onHide = () => {
39
- portalDOM === null || portalDOM === void 0 ? void 0 : portalDOM.destroy();
41
+ portalDOM === null || portalDOM === void 0 ? void 0 : portalDOM.unmount();
40
42
  portalDOM = null;
41
43
  };
42
44
  props.onHide = onHide;
43
- portalDOM.mount(React.createElement(ToastComponent, Object.assign({}, props, { key: uniqKey() })));
45
+ portalDOM.mount(_createElement(ToastComponent, Object.assign({}, props, { key: uniqKey() })));
44
46
  }
@@ -20,7 +20,7 @@ export declare const style: {
20
20
  top(offset: number): import("@emotion/utils").SerializedStyles;
21
21
  middle: import("@emotion/utils").SerializedStyles;
22
22
  bottom(offset: number): import("@emotion/utils").SerializedStyles;
23
- content: (radius?: number | undefined) => import("@emotion/utils").SerializedStyles;
23
+ content: (radius?: number) => import("@emotion/utils").SerializedStyles;
24
24
  contentMiddle: {
25
25
  transform: string;
26
26
  };
package/build/index.d.ts CHANGED
@@ -30,5 +30,3 @@ export { AutoGrid } from "./AutoGrid";
30
30
  export { showAlert } from "./Alert";
31
31
  export { ScrollView } from "./ScrollView";
32
32
  export { CarouselNotice } from "./CarouselNotice";
33
- export { showPicker, Picker } from "./Picker";
34
- export { showCascader, Cascader } from "./Cascader";
package/build/index.js CHANGED
@@ -30,5 +30,3 @@ export { AutoGrid } from "./AutoGrid";
30
30
  export { showAlert } from "./Alert";
31
31
  export { ScrollView } from "./ScrollView";
32
32
  export { CarouselNotice } from "./CarouselNotice";
33
- export { showPicker, Picker } from "./Picker";
34
- export { showCascader, Cascader } from "./Cascader";
@@ -1,10 +1,10 @@
1
- import React from "react";
2
- import { History } from "history";
3
- import { ContainerProps } from "../Container";
4
- import { ContextValue } from "../context";
5
- export declare type RouteMethod = "browser" | "hash" | "memory";
1
+ import React from 'react';
2
+ import { History } from 'history';
3
+ import { ContainerProps } from '../Container';
4
+ import { ContextValue } from '../context';
5
+ export declare type RouteMethod = 'browser' | 'hash' | 'memory';
6
6
  export declare type AwaitValue<T> = T | Promise<T>;
7
- export interface CreateAppOption extends Omit<ContainerProps, "children">, ContextValue {
7
+ export interface CreateAppOption extends Omit<ContainerProps, 'children'>, ContextValue {
8
8
  onBeforeRenderPage?: (pathname?: string) => AwaitValue<void>;
9
9
  onAfterRenderPage?: (pathname?: string) => AwaitValue<void>;
10
10
  renderLoading?: (pathname?: string) => AwaitValue<React.ReactNode>;
@@ -14,7 +14,7 @@ export interface CreateAppOption extends Omit<ContainerProps, "children">, Conte
14
14
  target: string | HTMLElement;
15
15
  }
16
16
  export declare let history: null | History;
17
- export declare function getHistory(routeMethod?: RouteMethod): History<import("history").State>;
17
+ export declare function getHistory(routeMethod?: RouteMethod): History;
18
18
  /**
19
19
  * 创建带路由的APP对象,全局对象,绝大部分情况下只需要调用一次
20
20
  * @param option CreateAppOption
@@ -7,16 +7,17 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
7
7
  step((generator = generator.apply(thisArg, _arguments || [])).next());
8
8
  });
9
9
  };
10
- import React, { useCallback, useEffect, useState } from "react";
11
- import ReactDOM from "react-dom";
12
- import { createBrowserHistory, createHashHistory, createMemoryHistory } from "history";
13
- import { Container } from "../Container";
14
- import { setContextValue } from "../context";
15
- import pick from "lodash/pick";
10
+ import { jsx as _jsx } from "@emotion/react/jsx-runtime";
11
+ import { useCallback, useEffect, useState } from 'react';
12
+ import { createRoot } from 'react-dom/client';
13
+ import { createBrowserHistory, createHashHistory, createMemoryHistory, } from 'history';
14
+ import { Container } from '../Container';
15
+ import { setContextValue } from '../context';
16
+ import pick from 'lodash/pick';
16
17
  // 存储历史记录对象
17
18
  export let history = null;
18
19
  // 获取历史记录对象
19
- export function getHistory(routeMethod = "browser") {
20
+ export function getHistory(routeMethod = 'browser') {
20
21
  if (history === null) {
21
22
  const createMap = {
22
23
  browser: createBrowserHistory,
@@ -34,18 +35,22 @@ export function getHistory(routeMethod = "browser") {
34
35
  export function createApp(option) {
35
36
  return __awaiter(this, void 0, void 0, function* () {
36
37
  // 设置默认的路由方式
37
- if (!option.routeMethod || ["browser", "hash", "memory"].indexOf(option.routeMethod) === -1) {
38
- option.routeMethod = "browser";
38
+ if (!option.routeMethod ||
39
+ ['browser', 'hash', 'memory'].indexOf(option.routeMethod) === -1) {
40
+ option.routeMethod = 'browser';
39
41
  }
40
42
  // 设置默认路由路径
41
43
  if (!option.defaultRoute) {
42
- option.defaultRoute = "/index";
44
+ option.defaultRoute = '/index';
43
45
  }
44
46
  // 这里是为了确保历史记录对象在组件渲染之前一定存在
45
47
  history = getHistory(option.routeMethod);
46
48
  // 提取关键数据
47
- const context = pick(option, ["minDocWidth", "maxDocWidth"]);
48
- const containerProps = pick(option, ["designWidth", "globalStyle"]);
49
+ const context = pick(option, ['minDocWidth', 'maxDocWidth']);
50
+ const containerProps = pick(option, [
51
+ 'designWidth',
52
+ 'globalStyle',
53
+ ]);
49
54
  const { onBeforeRenderPage, onAfterRenderPage, renderLoading, renderPage } = option;
50
55
  // 设置上下文属性
51
56
  setContextValue(context);
@@ -60,13 +65,13 @@ export function createApp(option) {
60
65
  */
61
66
  const showPage = useCallback((pathname) => __awaiter(this, void 0, void 0, function* () {
62
67
  const pathReg = /^\/*|\/*$/g;
63
- pathname = pathname.replace(pathReg, "");
68
+ pathname = pathname.replace(pathReg, '');
64
69
  if (!pathname) {
65
- pathname = option.defaultRoute.replace(pathReg, "");
70
+ pathname = option.defaultRoute.replace(pathReg, '');
66
71
  }
67
72
  // 如果有loading,要先显示loading
68
- if (typeof renderLoading === "function") {
69
- setPage(renderLoading === null || renderLoading === void 0 ? void 0 : renderLoading(pathname));
73
+ if (typeof renderLoading === 'function') {
74
+ setPage(yield (renderLoading === null || renderLoading === void 0 ? void 0 : renderLoading(pathname)));
70
75
  }
71
76
  // 加载页面之前可能会存在的逻辑
72
77
  yield (onBeforeRenderPage === null || onBeforeRenderPage === void 0 ? void 0 : onBeforeRenderPage(pathname));
@@ -88,19 +93,20 @@ export function createApp(option) {
88
93
  // 卸载时,取消监听
89
94
  return unlisten;
90
95
  }, []);
91
- return React.createElement(Container, Object.assign({}, containerProps), page);
96
+ return _jsx(Container, Object.assign({}, containerProps, { children: page }));
92
97
  };
93
98
  // 获取挂载对象
94
99
  let mount = null;
95
- if (typeof option.target === "string") {
100
+ if (typeof option.target === 'string') {
96
101
  mount = document.querySelector(option.target);
97
102
  }
98
103
  else if (option.target instanceof HTMLElement) {
99
104
  mount = option.target;
100
105
  }
101
- // 挂载组件
102
- yield new Promise((resolve) => {
103
- ReactDOM.render(React.createElement(App, null), mount, resolve);
104
- });
106
+ else {
107
+ throw new Error('No mounted object is specified');
108
+ }
109
+ const root = createRoot(mount);
110
+ root.render(_jsx(App, {}));
105
111
  });
106
112
  }
@@ -1,14 +1,14 @@
1
- import React from "react";
1
+ import React from 'react';
2
2
  export interface PortalDOM {
3
3
  element: HTMLDivElement;
4
- mount: (component: React.ReactNode) => Promise<void>;
5
- destroy: () => void;
4
+ mount: (component: React.ReactNode) => void;
5
+ unmount: () => void;
6
6
  }
7
7
  /**
8
- * 类似ReactDOM.createPortal,这个函数将会在body下创建一个组件的挂载点
8
+ *
9
9
  * 组件可以通过函数的第一个参数传递进去
10
10
  *
11
- * @param point HTMLElement 挂载点
11
+ * @param point HTMLElement 挂载点,如果未指定,则挂载点为body
12
12
  * @returns CreatePortalDOMResult
13
13
  */
14
14
  export declare function createPortalDOM(point?: HTMLElement): PortalDOM;
@@ -1,45 +1,32 @@
1
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
- return new (P || (P = Promise))(function (resolve, reject) {
4
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
- step((generator = generator.apply(thisArg, _arguments || [])).next());
8
- });
9
- };
10
- import React from "react";
11
- import ReactDOM from "react-dom";
1
+ import { createRoot } from 'react-dom/client';
12
2
  /**
13
- * 类似ReactDOM.createPortal,这个函数将会在body下创建一个组件的挂载点
3
+ *
14
4
  * 组件可以通过函数的第一个参数传递进去
15
5
  *
16
- * @param point HTMLElement 挂载点
6
+ * @param point HTMLElement 挂载点,如果未指定,则挂载点为body
17
7
  * @returns CreatePortalDOMResult
18
8
  */
19
9
  export function createPortalDOM(point) {
20
- const element = document.createElement("div");
10
+ const container = document.createElement('div');
21
11
  let mountPoint = document.body;
22
12
  if (point instanceof HTMLElement) {
23
13
  mountPoint = point;
24
14
  }
25
- mountPoint.appendChild(element);
15
+ mountPoint.appendChild(container);
16
+ const root = createRoot(container);
26
17
  return {
27
- element,
18
+ element: container,
28
19
  mount(component) {
29
- return __awaiter(this, void 0, void 0, function* () {
30
- return new Promise((resolve) => {
31
- ReactDOM.render(React.createElement(React.Fragment, null, component), element, resolve);
32
- });
33
- });
20
+ root.render(component);
34
21
  },
35
- destroy() {
36
- if (element instanceof HTMLDivElement) {
37
- ReactDOM.unmountComponentAtNode(element);
38
- if (typeof element.remove === "function") {
39
- element.remove();
22
+ unmount() {
23
+ root.unmount();
24
+ if (container instanceof HTMLDivElement) {
25
+ if (typeof container.remove === 'function') {
26
+ container.remove();
40
27
  }
41
28
  else {
42
- mountPoint.removeChild(element);
29
+ mountPoint.removeChild(container);
43
30
  }
44
31
  }
45
32
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clxx",
3
- "version": "2.0.5",
3
+ "version": "2.0.6",
4
4
  "description": "Basic JS library for mobile devices",
5
5
  "main": "./build/index.js",
6
6
  "module": "./build/index.js",
@@ -32,21 +32,21 @@
32
32
  "registry": "https://registry.npmjs.org",
33
33
  "access": "public"
34
34
  },
35
- "devDependencies": {
36
- "@types/lodash": "^4.14.175",
37
- "@types/react": "^17.0.27",
38
- "@types/react-dom": "^17.0.9",
39
- "@types/swiper": "^5.4.3",
40
- "csstype": "^3.0.9",
41
- "typescript": "^4.4.3"
42
- },
43
35
  "dependencies": {
44
- "@emotion/react": "^11.4.1",
45
- "dayjs": "^1.10.7",
46
- "history": "^5.1.0",
36
+ "@emotion/react": "^11.10.0",
37
+ "dayjs": "^1.11.5",
38
+ "history": "^5.3.0",
47
39
  "lodash": "^4.17.21",
48
- "react": "^17.0.2",
49
- "react-dom": "^17.0.2",
50
- "swiper": "^6.8.4"
40
+ "react": "^18.2.0",
41
+ "react-dom": "^18.2.0",
42
+ "swiper": "^8.3.2"
43
+ },
44
+ "devDependencies": {
45
+ "@types/lodash": "^4.14.182",
46
+ "@types/react": "^18.0.17",
47
+ "@types/react-dom": "^18.0.6",
48
+ "@types/swiper": "^6.0.0",
49
+ "csstype": "^3.1.0",
50
+ "typescript": "^4.7.4"
51
51
  }
52
52
  }
@@ -1,13 +0,0 @@
1
- /** @jsx jsx */
2
- import { jsx } from "@emotion/react";
3
- import React from "react";
4
- export interface ColumnItem {
5
- label: React.ReactNode;
6
- value: string | number;
7
- }
8
- export interface ColumnProps {
9
- columnOptions: ColumnItem[];
10
- defaultValue?: string | number;
11
- updateColumns: (value: string | number) => void;
12
- }
13
- export declare function Column(props: ColumnProps): jsx.JSX.Element;
@@ -1,65 +0,0 @@
1
- /** @jsx jsx */
2
- import { jsx } from "@emotion/react";
3
- import { useCallback, useEffect, useRef, useLayoutEffect } from "react";
4
- import { style } from "./style";
5
- import Swiper from "swiper";
6
- import { RowCenter } from "../Flex/Row";
7
- import { swiperStyle } from "../Picker/swiperStyle";
8
- export function Column(props) {
9
- const { columnOptions, defaultValue, updateColumns } = props;
10
- const swiperRef = useRef(null);
11
- const usedColumnOptions = useRef(columnOptions);
12
- // usedColumnOptions.current在组件在内存中更新完毕之时更新,此时onSlideChange(真实渲染完毕之后触发)内部的usedColumnOptions.current会拿到正确的值
13
- useLayoutEffect(() => {
14
- usedColumnOptions.current = columnOptions;
15
- }, [columnOptions]);
16
- // 获取初始索引,只在初始化时被执行一次
17
- const getInitialSlide = useCallback(() => {
18
- let initialSlide = 0;
19
- if (Array.isArray(columnOptions)) {
20
- let findIndex = columnOptions.findIndex((option) => option.value === defaultValue);
21
- if (findIndex >= 0) {
22
- initialSlide = findIndex;
23
- }
24
- }
25
- return initialSlide;
26
- }, [columnOptions, defaultValue]);
27
- // 避免observerUpdate内的slideTo触发onSlideChange
28
- const transitionEnd = useRef(true);
29
- useEffect(() => {
30
- var swiper = new Swiper(swiperRef.current, {
31
- initialSlide: getInitialSlide(),
32
- direction: "vertical",
33
- slidesPerView: 5,
34
- centeredSlides: true,
35
- freeMode: true,
36
- freeModeSticky: true,
37
- // 监听子组件内容变化
38
- observer: true,
39
- });
40
- swiper.on("slideChange", () => {
41
- var _a;
42
- if (!transitionEnd.current) {
43
- return;
44
- }
45
- const selectedValue = (_a = usedColumnOptions.current[swiper.realIndex]) === null || _a === void 0 ? void 0 : _a.value;
46
- updateColumns(selectedValue);
47
- });
48
- // 内容更新时滚回到第一项
49
- swiper.on("observerUpdate", () => {
50
- transitionEnd.current = false;
51
- swiper.slideTo(0, 0, false);
52
- transitionEnd.current = true;
53
- });
54
- return () => {
55
- swiper.destroy();
56
- };
57
- }, []);
58
- return (jsx("div", { css: [swiperStyle, style.body] },
59
- jsx("div", { className: "swiper", ref: swiperRef, css: style.swiper },
60
- jsx("div", { className: "swiper-wrapper" }, columnOptions.map((item) => {
61
- return (jsx(RowCenter, { className: "swiper-slide", key: item.value }, item.label));
62
- })),
63
- jsx("div", { css: [style.mask, style.maskTop] }),
64
- jsx("div", { css: [style.mask, style.maskBottom] }))));
65
- }
@@ -1,21 +0,0 @@
1
- /** @jsx jsx */
2
- import { jsx } from "@emotion/react";
3
- import React from "react";
4
- export interface CascaderOption {
5
- label: React.ReactNode;
6
- value: string | number;
7
- children?: CascaderOption[];
8
- }
9
- declare type defaultValue = (string | number)[];
10
- export interface WrapperProps {
11
- options: CascaderOption[];
12
- defaultValue: defaultValue;
13
- onSelect: (selectValue: defaultValue) => void;
14
- renderCancel?: React.ReactNode | (() => React.ReactNode);
15
- renderConfirm?: React.ReactNode | (() => React.ReactNode);
16
- renderTitle?: React.ReactNode | (() => React.ReactNode);
17
- onConfirm?: (selectValue: defaultValue) => void;
18
- onCancel?: () => void;
19
- }
20
- export declare function Wrapper(props: WrapperProps): jsx.JSX.Element;
21
- export {};
@@ -1,105 +0,0 @@
1
- /** @jsx jsx */
2
- import { jsx } from "@emotion/react";
3
- import { useCallback, useEffect, useMemo, useRef } from "react";
4
- import { RowBetween } from "../Flex/Row";
5
- import { style } from "./style";
6
- import { Column } from "./Column";
7
- import useUpdate from "../Effect/useUpdate";
8
- export function Wrapper(props) {
9
- const { options, defaultValue, onSelect, renderTitle, renderCancel, renderConfirm } = props;
10
- const update = useUpdate();
11
- // 当前选中的值
12
- let selectedValueArray = useRef(Array.isArray(defaultValue) ? defaultValue : []);
13
- // 待渲染项,每一项渲染一个Column组件(Swiper组件)
14
- const currentColumns = useRef([]);
15
- // 获取options的最大深度以及所有子树
16
- const { maxDepth, subOptionsRecord } = useMemo(() => {
17
- let subOptionsRecord = {};
18
- let maxDepth = 1;
19
- const traverse = (options, currentDepth) => {
20
- options.forEach((option) => {
21
- if (Array.isArray(option.children) && option.children.length >= 1) {
22
- subOptionsRecord[option.value] = option.children;
23
- maxDepth = Math.max(currentDepth + 1, maxDepth);
24
- traverse(option.children, currentDepth + 1);
25
- }
26
- });
27
- };
28
- traverse(options, 1);
29
- return { maxDepth, subOptionsRecord };
30
- }, [options]);
31
- // 常量函数,根据options以及初始值获取初始渲染列
32
- const getInitColumns = useCallback((options, selectedValue) => {
33
- var _a;
34
- // 待渲染的Columns项,每一项最终渲染一个Column组件(独立的Swiper组件)
35
- let initColumns = [options.map((item) => ({ value: item.value, label: item.label }))];
36
- // 当前待搜索的子项
37
- let currentSubOption = options;
38
- // 根据初始值获取下一次的子项
39
- for (let i = 0; i < maxDepth - 1; i++) {
40
- let currentValue = selectedValue[i];
41
- let nextIndex = currentSubOption.findIndex((item) => item.value === currentValue);
42
- // 没有找到就取第一项作为下一次的子项
43
- let nextSubOption = ((_a = currentSubOption[nextIndex === -1 ? 0 : nextIndex]) === null || _a === void 0 ? void 0 : _a.children) || [];
44
- initColumns.push(nextSubOption.map((item) => ({
45
- value: item.value,
46
- label: item.label,
47
- })));
48
- currentSubOption = nextSubOption;
49
- }
50
- return initColumns;
51
- }, []);
52
- // 选项列表或初始值更新时,重新计算当前渲染项
53
- useEffect(() => {
54
- selectedValueArray.current = Array.isArray(defaultValue) ? defaultValue : [];
55
- currentColumns.current = getInitColumns(options, selectedValueArray.current);
56
- update();
57
- }, [options, defaultValue]);
58
- // 更新columns项,触发onSelect回调
59
- const updateColumnsAndSelectedValues = useCallback((value, index) => {
60
- let usedTree = subOptionsRecord[value];
61
- // index之前的默认值不变
62
- let updateValueArray = selectedValueArray.current.slice(0, index);
63
- updateValueArray.push(value);
64
- // index+1之前的columns不变
65
- let updateCurrentColumns = currentColumns.current.slice(0, index + 1);
66
- for (let i = 0; i < maxDepth - index - 1; i++) {
67
- if (!usedTree) {
68
- updateCurrentColumns.push([]);
69
- continue;
70
- }
71
- updateValueArray.push(usedTree[0].value);
72
- updateCurrentColumns.push(usedTree.map((item) => ({ value: item.value, label: item.label })));
73
- usedTree = subOptionsRecord[usedTree[0].value];
74
- }
75
- // 更新当前选中值
76
- selectedValueArray.current = updateValueArray;
77
- // 触发onSelect回调
78
- onSelect === null || onSelect === void 0 ? void 0 : onSelect(updateValueArray);
79
- // 更新渲染项
80
- currentColumns.current = updateCurrentColumns;
81
- update();
82
- }, []);
83
- const showRenderContent = (render, defaultContent) => {
84
- var _a;
85
- return typeof render === "function" ? (_a = render()) !== null && _a !== void 0 ? _a : defaultContent : render !== null && render !== void 0 ? render : defaultContent;
86
- };
87
- return (jsx("div", { css: [style.container] },
88
- jsx(RowBetween, { css: style.header },
89
- jsx("div", { css: style.title }, showRenderContent(renderTitle, "")),
90
- jsx("div", { css: [style.btn, style.btnCancel], onClick: () => {
91
- var _a;
92
- (_a = props.onCancel) === null || _a === void 0 ? void 0 : _a.call(props);
93
- } }, showRenderContent(renderCancel, "取消")),
94
- jsx("div", { css: [style.btn, style.btnConfirm], onClick: () => {
95
- var _a;
96
- (_a = props.onConfirm) === null || _a === void 0 ? void 0 : _a.call(props, selectedValueArray.current);
97
- } }, showRenderContent(renderConfirm, "确认"))),
98
- jsx(RowBetween, null, currentColumns.current.map((column, index) => {
99
- return (jsx(Column, { updateColumns: (value) => {
100
- updateColumnsAndSelectedValues(value, index);
101
- }, columnOptions: column,
102
- // defaultValue实际上只有初始化时有效
103
- defaultValue: selectedValueArray.current[index], key: index }));
104
- }))));
105
- }
@@ -1,21 +0,0 @@
1
- import React from "react";
2
- import { WrapperProps } from "./Wrapper";
3
- export interface ShowCascaderProps extends WrapperProps {
4
- blankClosable: boolean;
5
- showMask?: boolean;
6
- maskColor?: string;
7
- }
8
- export interface CascaderProps extends ShowCascaderProps, Omit<React.HTMLProps<HTMLDivElement>, "defaultValue" | "onSelect"> {
9
- children?: React.ReactNode;
10
- }
11
- /**
12
- * 显示Cascader
13
- * @param option
14
- */
15
- export declare function showCascader(props: ShowCascaderProps): () => Promise<void>;
16
- /**
17
- * Cascader组件
18
- * @param props
19
- * @returns
20
- */
21
- export declare function Cascader(props: CascaderProps): JSX.Element;