@ecoding/components.antd 0.0.6 → 0.0.8

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.
@@ -0,0 +1,56 @@
1
+ import React from 'react';
2
+ export interface IDrawerProps {
3
+ key?: string;
4
+ id?: string;
5
+ className?: string;
6
+ bodyStyle?: React.CSSProperties;
7
+ closable?: boolean;
8
+ maskClosable?: boolean;
9
+ open?: boolean;
10
+ width?: string;
11
+ zIndex?: number;
12
+ title?: React.ReactNode;
13
+ okType?: "text" | "link" | "ghost" | "default" | "primary" | "dashed" | undefined;
14
+ okText?: string;
15
+ cancelText?: string;
16
+ mask?: boolean;
17
+ footer?: React.ReactNode;
18
+ onOk?: () => Promise<any>;
19
+ onOkAfter?: (args: any) => void | Promise<any>;
20
+ onCancel?: () => Promise<any>;
21
+ component?: React.ReactNode;
22
+ addonBefore?: React.ReactNode;
23
+ addonAfter?: React.ReactNode;
24
+ }
25
+ export declare class Drawers extends React.Component<IDrawerProps> {
26
+ static defaultProps: {
27
+ className: string;
28
+ open: boolean;
29
+ bodyStyle: {};
30
+ width: string;
31
+ zIndex: number;
32
+ mask: boolean;
33
+ title: string;
34
+ okType: string;
35
+ okText: string;
36
+ cancelText: string;
37
+ maskClosable: boolean;
38
+ closable: boolean;
39
+ footer: string;
40
+ onOk: () => Promise<void>;
41
+ onCancel: () => Promise<void>;
42
+ component: null;
43
+ };
44
+ state: {
45
+ loading: boolean;
46
+ open: boolean | undefined;
47
+ };
48
+ cancel: () => void;
49
+ ok: () => void;
50
+ render(): JSX.Element;
51
+ }
52
+ declare const _default: {
53
+ show(props?: IDrawerProps): void;
54
+ hide(props?: IDrawerProps): void;
55
+ };
56
+ export default _default;
@@ -0,0 +1,104 @@
1
+ import React from 'react';
2
+ import { Drawer, Button } from 'antd';
3
+ import InjectNotification from '../inject-notification';
4
+ const timeout = 300;
5
+ export class Drawers extends React.Component {
6
+ constructor() {
7
+ super(...arguments);
8
+ this.state = {
9
+ loading: false,
10
+ open: this.props.open
11
+ };
12
+ this.cancel = () => {
13
+ if (this.state.loading) {
14
+ return;
15
+ }
16
+ this.props.onCancel &&
17
+ this.props.onCancel().then(() => {
18
+ this.setState({
19
+ open: false
20
+ });
21
+ setTimeout(() => {
22
+ InjectNotification.removeNotice(this.props.id);
23
+ }, timeout);
24
+ });
25
+ };
26
+ this.ok = () => {
27
+ this.setState({
28
+ loading: true
29
+ });
30
+ this.props.onOk &&
31
+ this.props
32
+ .onOk()
33
+ .then((res) => {
34
+ this.setState({
35
+ loading: false,
36
+ open: false
37
+ });
38
+ setTimeout(() => {
39
+ InjectNotification.removeNotice(this.props.id);
40
+ this.props.onOkAfter && this.props.onOkAfter(res);
41
+ }, timeout);
42
+ })
43
+ .catch(() => {
44
+ this.setState({
45
+ loading: false
46
+ });
47
+ });
48
+ };
49
+ }
50
+ render() {
51
+ // if (!this.props.visible) return null;
52
+ return (React.createElement(Drawer, { className: this.props.className, bodyStyle: this.props.bodyStyle, open: this.props.open, destroyOnClose: true, maskClosable: this.props.maskClosable, keyboard: false, title: this.props.title, footer: this.props.footer === null ? null : this.props.footer ? (this.props.footer) : (React.createElement("div", { className: "tac" },
53
+ this.props.addonBefore ? this.props.addonBefore : null,
54
+ React.createElement(Button, { className: "mr10", key: "back", onClick: this.cancel }, this.props.cancelText),
55
+ React.createElement(Button, { key: "submit", type: this.props.okType, loading: this.state.loading, onClick: this.ok }, this.props.okText),
56
+ this.props.addonAfter ? this.props.addonAfter : null)), width: this.props.width, zIndex: this.props.zIndex, mask: this.props.mask, onClose: this.cancel, closable: this.props.closable }, this.props.component ? this.props.component : null));
57
+ }
58
+ }
59
+ Drawers.defaultProps = {
60
+ className: "g-drawer",
61
+ open: false,
62
+ bodyStyle: {},
63
+ width: "60vw",
64
+ zIndex: 900,
65
+ mask: true,
66
+ title: "标题",
67
+ okType: "primary",
68
+ okText: "保存",
69
+ cancelText: "取消",
70
+ maskClosable: true,
71
+ closable: true,
72
+ footer: "",
73
+ onOk: function () {
74
+ return Promise.resolve();
75
+ },
76
+ onCancel: function () {
77
+ return Promise.resolve();
78
+ },
79
+ component: null
80
+ };
81
+ export default {
82
+ show(props = {}) {
83
+ props.open = true;
84
+ props.id = props.id || props.key || "drawers";
85
+ if (props.footer !== null && !props.footer) {
86
+ props.footer = "";
87
+ }
88
+ if (!props.okText) {
89
+ props.okText = "保存";
90
+ }
91
+ if (!props.cancelText) {
92
+ props.cancelText = "取消";
93
+ }
94
+ InjectNotification.notice(React.createElement(Drawers, Object.assign({}, props, { key: props.id })));
95
+ },
96
+ hide(props = {}) {
97
+ props.open = false;
98
+ props.footer = "";
99
+ props.id = props.id || "drawers";
100
+ setTimeout(() => {
101
+ InjectNotification.removeNotice(props.id);
102
+ }, timeout);
103
+ }
104
+ };
@@ -0,0 +1,16 @@
1
+ import React from "react";
2
+ interface IState {
3
+ notices: React.Attributes[];
4
+ target: HTMLElement | null;
5
+ }
6
+ declare class InjectNotification extends React.Component<any, IState> {
7
+ static notice(component: any): void;
8
+ static removeNotice(key: any): void;
9
+ constructor(props: any);
10
+ add(notice: React.Attributes): void;
11
+ remove(key: string): void;
12
+ getNoticeList(): React.Attributes[];
13
+ componentDidMount(): void;
14
+ render(): React.ReactPortal | null;
15
+ }
16
+ export default InjectNotification;
@@ -0,0 +1,70 @@
1
+ import React from "react";
2
+ import { createPortal } from "react-dom";
3
+ import EventEmitter from "@ecoding/helper.event";
4
+ const instanceEvent = new EventEmitter();
5
+ // notification是父组件容器,通过其方法做额外渲染到 body 上的react节点
6
+ // 是动态插入和删除DOM节点的核心
7
+ class InjectNotification extends React.Component {
8
+ static notice(component) {
9
+ instanceEvent.emit("InjectNotification/SHOW", component);
10
+ }
11
+ static removeNotice(key) {
12
+ instanceEvent.emit("InjectNotification/HIDE", key);
13
+ }
14
+ constructor(props) {
15
+ super(props);
16
+ this.state = {
17
+ target: null,
18
+ notices: []
19
+ };
20
+ }
21
+ // 创建一个监听项
22
+ add(notice) {
23
+ // 添加notice
24
+ // 创造一个不重复的key
25
+ const { notices } = this.state;
26
+ const key = notice.key;
27
+ const index = notices.findIndex(item => item.key === key);
28
+ if (index === -1) {
29
+ // 不存在重复的 添加
30
+ notices.push(notice);
31
+ this.setState({
32
+ notices,
33
+ });
34
+ }
35
+ else {
36
+ const temp = notices.splice(index, 1);
37
+ console.log(notices.concat(temp));
38
+ this.setState(Object.assign({}, {
39
+ notices: notices.concat(temp),
40
+ }));
41
+ }
42
+ }
43
+ // 删除一个监听项
44
+ remove(key) {
45
+ this.setState(previousState => {
46
+ return {
47
+ notices: previousState.notices.filter(notice => notice.key !== key)
48
+ };
49
+ });
50
+ }
51
+ getNoticeList() {
52
+ const { notices } = this.state;
53
+ return notices.map((notice, i) => {
54
+ return notice;
55
+ });
56
+ }
57
+ componentDidMount() {
58
+ this.setState({
59
+ target: document.body,
60
+ notices: []
61
+ });
62
+ instanceEvent.on("InjectNotification/SHOW", this.add, this);
63
+ instanceEvent.on("InjectNotification/HIDE", this.remove, this);
64
+ }
65
+ render() {
66
+ const noticesDOM = this.getNoticeList();
67
+ return this.state.target ? createPortal(noticesDOM, this.state.target) : null;
68
+ }
69
+ }
70
+ export default InjectNotification;
@@ -1,5 +1,7 @@
1
1
  import React from 'react';
2
2
  export interface IModalProps {
3
+ key?: string;
4
+ id?: string;
3
5
  className?: string;
4
6
  open?: boolean;
5
7
  bodyStyle?: React.CSSProperties;
@@ -19,6 +21,32 @@ export interface IModalProps {
19
21
  addonBefore?: React.ReactNode;
20
22
  addonAfter?: React.ReactNode;
21
23
  }
24
+ export declare class Modals extends React.Component<IModalProps> {
25
+ static defaultProps: {
26
+ className: string;
27
+ open: boolean;
28
+ bodyStyle: {};
29
+ width: string;
30
+ zIndex: number;
31
+ mask: boolean;
32
+ title: string;
33
+ okType: string;
34
+ okText: string;
35
+ cancelText: string;
36
+ closable: boolean;
37
+ footer: string;
38
+ onOk: () => Promise<void>;
39
+ onCancel: () => Promise<void>;
40
+ component: null;
41
+ };
42
+ state: {
43
+ loading: boolean;
44
+ open: boolean | undefined;
45
+ };
46
+ cancel: () => void;
47
+ ok: () => void;
48
+ render(): JSX.Element;
49
+ }
22
50
  declare const _default: {
23
51
  show(props?: IModalProps): void;
24
52
  hide(props?: IModalProps): void;
@@ -1,13 +1,13 @@
1
1
  import React from 'react';
2
2
  import { Modal, Button } from 'antd';
3
- import Notification from '../../helpers/notification';
4
- const notification = Notification.newInstance();
5
- class Modals extends React.Component {
3
+ import InjectNotification from '../inject-notification';
4
+ const timeout = 300;
5
+ export class Modals extends React.Component {
6
6
  constructor() {
7
7
  super(...arguments);
8
8
  this.state = {
9
9
  loading: false,
10
- open: true
10
+ open: this.props.open
11
11
  };
12
12
  this.cancel = () => {
13
13
  if (this.state.loading) {
@@ -18,7 +18,9 @@ class Modals extends React.Component {
18
18
  this.setState({
19
19
  open: false
20
20
  });
21
- // hideModal();
21
+ setTimeout(() => {
22
+ InjectNotification.removeNotice(this.props.id);
23
+ }, timeout);
22
24
  });
23
25
  };
24
26
  this.ok = () => {
@@ -33,8 +35,10 @@ class Modals extends React.Component {
33
35
  loading: false,
34
36
  open: false
35
37
  });
36
- // hideModal();
37
- this.props.onOkAfter && this.props.onOkAfter(res);
38
+ setTimeout(() => {
39
+ InjectNotification.removeNotice(this.props.id);
40
+ this.props.onOkAfter && this.props.onOkAfter(res);
41
+ }, timeout);
38
42
  })
39
43
  .catch(() => {
40
44
  this.setState({
@@ -45,24 +49,55 @@ class Modals extends React.Component {
45
49
  }
46
50
  render() {
47
51
  // if (!this.props.visible) return null;
48
- return (React.createElement(Modal, { className: this.props.className, open: this.state.open, bodyStyle: this.props.bodyStyle, width: this.props.width, zIndex: this.props.zIndex, mask: this.props.mask, title: this.props.title, closable: this.props.closable, onCancel: this.cancel, footer: this.props.footer === null ? null : this.props.footer ? (this.props.footer) : (React.createElement(React.Fragment, null,
52
+ return (React.createElement(Modal, { className: this.props.className, open: this.state.open, bodyStyle: this.props.bodyStyle, width: this.props.width, zIndex: this.props.zIndex, mask: this.props.mask, title: this.props.title, closable: this.props.closable, onCancel: this.cancel, centered: true, destroyOnClose: true, keyboard: false, footer: this.props.footer === null ? null : this.props.footer ? (this.props.footer) : (React.createElement(React.Fragment, null,
49
53
  this.props.addonBefore ? this.props.addonBefore : null,
50
54
  React.createElement(Button, { key: "back", onClick: this.cancel }, this.props.cancelText),
51
55
  React.createElement(Button, { key: "submit", type: this.props.okType, loading: this.state.loading, onClick: this.ok }, this.props.okText),
52
- this.props.addonAfter ? this.props.addonAfter : null)), centered: true, destroyOnClose: true, keyboard: false }, this.props.component ? this.props.component : null));
56
+ this.props.addonAfter ? this.props.addonAfter : null)) }, this.props.component ? this.props.component : null));
53
57
  }
54
58
  }
55
59
  Modals.defaultProps = {
56
- tip: ''
60
+ className: "g-modals",
61
+ open: false,
62
+ bodyStyle: {},
63
+ width: "80vw",
64
+ zIndex: 950,
65
+ mask: true,
66
+ title: "标题",
67
+ okType: "primary",
68
+ okText: "保存",
69
+ cancelText: "取消",
70
+ closable: true,
71
+ footer: "",
72
+ onOk: function () {
73
+ return Promise.resolve();
74
+ },
75
+ onCancel: function () {
76
+ return Promise.resolve();
77
+ },
78
+ component: null
57
79
  };
58
80
  export default {
59
81
  show(props = {}) {
60
- if (notification) {
61
- notification.removeNotice('modals');
82
+ props.open = true;
83
+ props.id = props.id || props.key || "modals";
84
+ if (props.footer !== null && !props.footer) {
85
+ props.footer = "";
86
+ }
87
+ if (!props.okText) {
88
+ props.okText = "保存";
89
+ }
90
+ if (!props.cancelText) {
91
+ props.cancelText = "取消";
62
92
  }
63
- notification.notice(React.createElement(Modals, Object.assign({}, props, { key: "modals" })));
93
+ InjectNotification.notice(React.createElement(Modals, Object.assign({}, props, { key: props.id })));
64
94
  },
65
95
  hide(props = {}) {
66
- notification.removeNotice('modals');
96
+ props.open = false;
97
+ props.footer = "";
98
+ props.id = props.id || "modals";
99
+ setTimeout(() => {
100
+ InjectNotification.removeNotice(props.id);
101
+ }, timeout);
67
102
  }
68
103
  };
@@ -8,12 +8,12 @@ declare class Notification extends React.Component<any, IState> {
8
8
  notice(component: TNotices): void;
9
9
  removeNotice(key: string): void;
10
10
  destroy(): void;
11
- component: any;
11
+ component: React.RefObject<unknown>;
12
12
  };
13
13
  constructor(props: any);
14
14
  add(notice: TNotices): void;
15
15
  remove(key: string): void;
16
- getNoticeDOM(): React.Attributes[];
16
+ getNoticeList(): React.Attributes[];
17
17
  render(): JSX.Element;
18
18
  }
19
19
  export default Notification;
@@ -1,24 +1,25 @@
1
- import React from "react";
2
- import ReactDOM from "react-dom";
1
+ import React, { createElement, createRef } from "react";
2
+ import { createRoot } from "react-dom/client";
3
3
  // notification是父组件容器,通过其方法做额外渲染到 body 上的react节点
4
4
  // 是动态插入和删除DOM节点的核心
5
5
  class Notification extends React.Component {
6
6
  static newInstance() {
7
- const div = document.createElement("div");
8
- document.body.appendChild(div);
9
- const notification = ReactDOM.render(React.createElement(Notification, null), div);
7
+ const notification = createRef();
8
+ const div = document.createElement("Other");
9
+ const root = createRoot(div);
10
+ root.render(createElement(Notification, { ref: notification }));
10
11
  return {
11
12
  notice(component) {
12
- notification.add(component);
13
+ notification.current.add(component);
13
14
  },
14
15
  removeNotice(key) {
15
- notification.remove(key);
16
+ notification.current.remove(key);
16
17
  },
17
18
  destroy() {
18
- ReactDOM.unmountComponentAtNode(div);
19
- document.body.removeChild(div);
19
+ // unmountComponentAtNode(div);
20
+ root.unmount();
20
21
  },
21
- component: notification
22
+ component: notification,
22
23
  };
23
24
  }
24
25
  constructor(props) {
@@ -38,7 +39,7 @@ class Notification extends React.Component {
38
39
  // 不存在重复的 添加
39
40
  notices.push(notice);
40
41
  this.setState({
41
- notices
42
+ notices,
42
43
  });
43
44
  }
44
45
  }
@@ -50,14 +51,14 @@ class Notification extends React.Component {
50
51
  };
51
52
  });
52
53
  }
53
- getNoticeDOM() {
54
+ getNoticeList() {
54
55
  const { notices } = this.state;
55
56
  return notices.map((notice, i) => {
56
57
  return notice;
57
58
  });
58
59
  }
59
60
  render() {
60
- const noticesDOM = this.getNoticeDOM();
61
+ const noticesDOM = this.getNoticeList();
61
62
  return (React.createElement(React.Fragment, null, noticesDOM));
62
63
  }
63
64
  }
package/lib/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  export { default as Toast } from "./core/toast";
2
2
  export { default as Loading } from "./core/loading";
3
3
  export { default as Modal } from "./core/modal";
4
+ export { default as InjectNotification } from "./core/inject-notification";
4
5
  export { default as Confirm } from "./core/confirm";
5
6
  export { default as AsyncCascader } from "./core/async-cascader";
6
7
  export { default as LengthInput } from "./core/length-input";
package/lib/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  export { default as Toast } from "./core/toast";
2
2
  export { default as Loading } from "./core/loading";
3
3
  export { default as Modal } from "./core/modal";
4
+ export { default as InjectNotification } from "./core/inject-notification";
4
5
  export { default as Confirm } from "./core/confirm";
5
6
  export { default as AsyncCascader } from "./core/async-cascader";
6
7
  export { default as LengthInput } from "./core/length-input";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ecoding/components.antd",
3
- "version": "0.0.6",
3
+ "version": "0.0.8",
4
4
  "author": "cxc",
5
5
  "homepage": "",
6
6
  "license": "MIT",
@@ -24,6 +24,7 @@
24
24
  "build": "rm -rf lib && tsc"
25
25
  },
26
26
  "peerDependencies": {
27
+ "@ecoding/helper.event": ">=0.0.2",
27
28
  "@ecoding/helper.is": ">=0.0.4",
28
29
  "@ecoding/helper.json": ">=0.0.5",
29
30
  "@ecoding/helper.request.hook": ">=0.0.6",
@@ -31,6 +32,7 @@
31
32
  "axios": ">=1.1.2"
32
33
  },
33
34
  "devDependencies": {
35
+ "@ecoding/helper.event": "^0.0.2",
34
36
  "@ecoding/helper.is": "^0.0.4",
35
37
  "@ecoding/helper.json": "^0.0.5",
36
38
  "@ecoding/helper.request.hook": "^0.0.6",
@@ -40,5 +42,5 @@
40
42
  "dependencies": {
41
43
  "react-quill": "^2.0.0"
42
44
  },
43
- "gitHead": "05844c415468a69e3aa77f4e4748b4451b1d8a94"
45
+ "gitHead": "1efade9b77a4b03945a80f85c6e0fb69ad187e89"
44
46
  }