diginet-core-ui 1.3.33 → 1.3.34

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.
@@ -1,14 +1,14 @@
1
1
  /** @jsxRuntime classic */
2
2
 
3
3
  /** @jsx jsx */
4
- import { memo, useRef, forwardRef, useEffect, useMemo } from 'react';
4
+ import { memo, useRef, forwardRef, useEffect, useMemo, useImperativeHandle } from 'react';
5
5
  import PropTypes from 'prop-types';
6
6
  import { jsx, css } from '@emotion/core';
7
- import Icon from '../../icons';
8
- import { lighten } from '../../styles/color-helper';
7
+ import OptionWrapper from '../others/option-wrapper';
9
8
  import { color as colors } from '../../styles/colors';
10
9
  import { typography } from '../../styles/typography';
11
- import { alignCenter, borderBox, borderRadius4px, cursorPointer, flexRow, flexWrap, inlineFlex, justifyCenter, overflowHidden, positionAbsolute, positionRelative } from '../../styles/general';
10
+ import { alignCenter, borderBox, borderRadius4px, flexRow, flexWrap, inlineFlex, justifyCenter, pointerEventsNone, positionAbsolute, positionRelative } from '../../styles/general';
11
+ import Icon from '../../icons';
12
12
  const {
13
13
  paragraph1,
14
14
  paragraph2,
@@ -28,60 +28,96 @@ const {
28
28
  }
29
29
  } = colors;
30
30
  const colorMap = new Map([['default', rest], ['primary', active], ['success', success], ['warning', warning], ['danger', danger], ['info', info]]);
31
- const sizeMap = new Map([['medium', '40px'], ['large', '48px'], ['giant', '56px']]);
32
- const iconSizeMap = new Map([['medium', '24px'], ['large', '32px'], ['giant', '40px']]);
31
+ const iconSizeMap = new Map([['medium', 24], ['large', 32], ['giant', 40]]);
33
32
  const dotSizeMap = new Map([['medium', 8], ['large', 10], ['giant', 12]]);
34
- const dotPaddingSizeMap = new Map([['medium', 0], ['large', 1], ['giant', 2]]);
33
+ const dotIndentMap = new Map([['medium', 4], ['large', 2]]);
35
34
  const typographySizeMap = new Map([['medium', paragraph3], ['large', paragraph2], ['giant', paragraph1]]);
36
35
 
37
- const calDotPos = (dotSize, dotPaddingSize, iconSize, isText, content) => {
38
- return isText ? content || content === 0 ? iconSize - 4 * (dotPaddingSize + 1) : dotSize * 3 - dotPaddingSize : -(dotSize / 2);
36
+ const calDotPos = (anchorOrigin, contentDirection, dotSize, hasContent, size) => {
37
+ const halfDotSize = dotSize / 2;
38
+ const {
39
+ horizontal,
40
+ vertical
41
+ } = anchorOrigin;
42
+ const dotIndent = hasContent ? dotIndentMap.get(size) || 0 : 0;
43
+ const verticalCssRight = vertical === 'bottom' ? css`
44
+ bottom: ${-dotIndent}px;
45
+ ` : css`
46
+ top: ${-dotIndent}px;
47
+ `;
48
+ const verticalCssLeft = vertical === 'bottom' ? css`
49
+ bottom: ${-halfDotSize}px;
50
+ ` : css`
51
+ top: ${-halfDotSize}px;
52
+ `;
53
+
54
+ if (contentDirection === 'right') {
55
+ if (horizontal === 'left') {
56
+ return css`
57
+ left: ${-dotIndent}px;
58
+ ${verticalCssRight};
59
+ `;
60
+ } else {
61
+ return css`
62
+ left: ${`calc(100% - ${dotSize - dotIndent}px);`};
63
+ ${verticalCssRight};
64
+ `;
65
+ }
66
+ } else {
67
+ if (horizontal === 'left') {
68
+ return css`
69
+ right: ${`calc(100% - ${halfDotSize}px);`};
70
+ ${verticalCssLeft};
71
+ `;
72
+ } else {
73
+ return css`
74
+ right: ${`${-halfDotSize}px;`};
75
+ ${verticalCssLeft};
76
+ `;
77
+ }
78
+ }
39
79
  };
40
80
 
41
81
  const Badge = /*#__PURE__*/memo( /*#__PURE__*/forwardRef(({
82
+ anchorOrigin,
83
+ contentDirection,
42
84
  content,
43
85
  max,
44
- viewType,
45
86
  name,
46
87
  color,
47
88
  size,
48
89
  className,
49
90
  style,
50
- badgeNumStyle,
91
+ showZero,
92
+ invisible,
51
93
  refs,
52
- onClick,
53
94
  children,
95
+ iconProps,
54
96
  ...props
55
- }, ref) => {
56
- if (!ref) {
57
- ref = useRef(null);
58
- }
59
-
60
- const _size = sizeMap.get(size);
61
-
62
- const _color = colorMap.get(color);
63
-
97
+ }, reference) => {
98
+ const ref = useRef(null);
64
99
  const typographySize = typographySizeMap.get(size);
65
100
  const iconSize = iconSizeMap.get(size);
66
- const dotSize = content || content === 0 ? dotSizeMap.get(size) + 8 : dotSizeMap.get(size);
67
- const dotColor = colorMap.get(color);
68
- const dotPaddingSize = dotPaddingSizeMap.get(size);
69
- const dotPos = calDotPos(dotSize, dotPaddingSize, parseInt(iconSize), viewType === 'text', content);
101
+ const dotSize = content || content === 0 && showZero ? dotSizeMap.get(size) + 8 : dotSizeMap.get(size);
102
+ const dotColor = colorMap.get(color) || colors[color] || color;
103
+ const dotPos = calDotPos(anchorOrigin, !children && name ? 'right' : contentDirection, dotSize, content || content === 0, size);
70
104
  const BadgeNumber = css`
71
105
  ${typographySize};
72
106
  ${flexRow}
73
107
  ${alignCenter}
74
108
  ${justifyCenter}
75
109
  ${flexWrap}
76
- ${positionAbsolute}
77
110
  ${borderBox}
111
+ ${pointerEventsNone}
112
+ ${children || name ? positionAbsolute : positionRelative}
78
113
  color: ${white};
79
114
  min-height: ${dotSize}px;
80
115
  min-width: ${dotSize}px;
81
116
  background-color: ${dotColor};
82
117
  border-radius: ${dotSize / 2}px;
83
118
  padding: 0 4px;
84
- z-index: 1;
119
+ transition: transform 225ms cubic-bezier(0.4, 0, 0.2, 1) 0ms;
120
+ ${invisible && 'transform: scale(0) translate(50%, -50%);'};
85
121
  `;
86
122
  const BadgeRoot = css`
87
123
  ${inlineFlex}
@@ -90,39 +126,27 @@ const Badge = /*#__PURE__*/memo( /*#__PURE__*/forwardRef(({
90
126
  ${positionRelative}
91
127
  ${borderBox}
92
128
  ${borderRadius4px}
93
- ${onClick && cursorPointer}
94
- height: ${name && !children ? _size : 'max-content'};
95
- width: ${name && !children ? _size : 'max-content'};
96
- &.text {
97
- color: ${_color};
98
- background-color: ${lighten(_color, 0.9)};
99
- .css-${BadgeNumber.name} {
100
- bottom: ${dotPos}px;
101
- left: ${dotPos}px;
102
- }
103
- }
104
- &.filled {
105
- ${name && !children && `
106
- color: ${white};
107
- background-color: ${_color};
108
- `}
109
- .css-${BadgeNumber.name} {
110
- top: ${dotPos}px;
111
- right: ${dotPos}px;
112
- }
129
+ height: max-content;
130
+ min-height: max-content;
131
+ width: max-content;
132
+ min-width: max-content;
133
+ color: ${white};
134
+ .css-${BadgeNumber.name} {
135
+ ${(children || name) && dotPos}
113
136
  }
114
137
  `;
115
- const BadgeIcon = css`
116
- ${overflowHidden}
117
- `;
118
-
119
- const _onClick = event => {
120
- onClick && onClick(event);
121
- };
122
-
123
138
  useEffect(() => {
124
139
  if (refs) refs(ref);
125
140
  }, []);
141
+ useImperativeHandle(reference, () => {
142
+ const currentRef = ref.current || {};
143
+ const _instance = {}; // methods
144
+
145
+ _instance.__proto__ = {}; // hidden methods
146
+
147
+ currentRef.instance = _instance;
148
+ return currentRef;
149
+ });
126
150
  return useMemo(() => {
127
151
  let node = children;
128
152
 
@@ -131,39 +155,46 @@ const Badge = /*#__PURE__*/memo( /*#__PURE__*/forwardRef(({
131
155
  name: name,
132
156
  width: iconSize,
133
157
  height: iconSize,
134
- color: 'currentColor',
135
- viewBox: true
158
+ viewBox: true,
159
+ ...iconProps
136
160
  });
137
161
  }
138
162
 
139
163
  return jsx("div", {
140
164
  css: BadgeRoot,
141
165
  ref: ref,
142
- className: ['DGN-UI-Badge', viewType, className].join(' ').trim().replace(/\s+/g, ' '),
166
+ className: ['DGN-UI-Badge', className].join(' ').trim().replace(/\s+/g, ' '),
143
167
  style: style,
144
- onClick: _onClick,
145
168
  ...props
146
169
  }, jsx("span", {
147
- css: BadgeIcon,
148
- className: 'DGN-UI-Badge-Icon'
170
+ className: 'DGN-UI-Badge-Children'
149
171
  }, node), content !== undefined && jsx("span", {
150
172
  css: BadgeNumber,
151
- className: 'DGN-UI-Badge-Dot',
152
- style: badgeNumStyle
153
- }, max && content > max ? max + '+' : content));
154
- }, [content, name, size, children, max, viewType, color, style, className]);
173
+ className: 'DGN-UI-Badge-Dot'
174
+ }, max && content > max ? max + '+' : content === 0 && !showZero ? null : content));
175
+ }, [anchorOrigin, contentDirection, content, name, size, children, max, showZero, invisible, color, style, className]);
155
176
  }));
156
177
  Badge.defaultProps = {
157
- viewType: 'filled',
158
- // name: 'Notification',
159
178
  color: 'default',
160
179
  size: 'medium',
180
+ anchorOrigin: {
181
+ vertical: 'top',
182
+ horizontal: 'right'
183
+ },
161
184
  content: null,
162
185
  max: 99,
186
+ contentDirection: 'left',
187
+ showZero: false,
188
+ invisible: false,
163
189
  className: '',
164
190
  style: {}
165
191
  };
166
192
  Badge.propTypes = {
193
+ anchorOrigin: PropTypes.shape({
194
+ horizontal: PropTypes.oneOf(['left', 'right']),
195
+ vertical: PropTypes.oneOf(['top', 'bottom'])
196
+ }),
197
+
167
198
  /** the quantity is shown on the badge */
168
199
  content: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
169
200
 
@@ -171,36 +202,45 @@ Badge.propTypes = {
171
202
  max: PropTypes.number,
172
203
 
173
204
  /** used to fill background for button */
174
- viewType: PropTypes.oneOf(['text', 'filled']),
205
+ contentDirection: PropTypes.oneOf(['left', 'right']),
175
206
 
176
207
  /** background color of badge */
177
- color: PropTypes.oneOf(['default', 'primary', 'info', 'success', 'warning', 'danger']),
208
+ color: PropTypes.oneOfType([PropTypes.oneOf(['default', 'primary', 'info', 'success', 'warning', 'danger']), PropTypes.string]),
178
209
 
179
- /** name of icon in the icons store */
210
+ /** use name will replace children with ButtonIcon */
180
211
  name: PropTypes.string,
181
212
 
182
- /** size of Badge */
213
+ /** other icon props when use prop name */
214
+ iconProps: PropTypes.object,
215
+
216
+ /** size of Badge
217
+ *
218
+ * * medium (dot 8px, typography p3)
219
+ * * large (button 10px, typography p2)
220
+ * * giant (button 12px, typography p1)
221
+ * */
183
222
  size: PropTypes.oneOf(['medium', 'large', 'giant']),
184
223
 
185
- /** className of badge */
224
+ /** Controls whether the badge is hidden when content is zero */
225
+ showZero: PropTypes.bool,
226
+
227
+ /** If true, the badge is invisible */
228
+ invisible: PropTypes.bool,
229
+
230
+ /** className of component */
186
231
  className: PropTypes.string,
187
232
 
188
- /** style inline of badge */
233
+ /** style inline of component */
189
234
  style: PropTypes.object,
190
235
 
191
- /** style inline of badge number */
192
- badgeNumStyle: PropTypes.object,
193
-
194
236
  /** the function get ref */
195
237
  refs: PropTypes.func,
196
238
 
197
- /** the function will run when click on badge */
198
- onClick: PropTypes.func,
199
-
200
239
  /** the content displayed under the number of the badge */
201
240
  children: PropTypes.node,
202
241
 
203
242
  /** any props else */
204
243
  props: PropTypes.any
205
244
  };
206
- export default Badge;
245
+ export { Badge };
246
+ export default OptionWrapper(Badge);