@befe/brick-comp-badge 0.2.77

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.
package/README.md ADDED
@@ -0,0 +1 @@
1
+ ## FAQ
package/es/index.css ADDED
@@ -0,0 +1,63 @@
1
+ .brick-badge,
2
+ .brick-badge * {
3
+ box-sizing: border-box;
4
+ }
5
+ .brick-badge {
6
+ display: inline-block;
7
+ position: relative;
8
+ }
9
+ .brick-badge-mark {
10
+ display: inline-block;
11
+ text-align: center;
12
+ color: rgb(255, 255, 255);
13
+ background: #e64552;
14
+ border: 1px solid rgb(255, 255, 255);
15
+ }
16
+ .brick-badge-type-standard .brick-badge-mark, .brick-badge-type-dot .brick-badge-mark {
17
+ position: absolute;
18
+ top: 0;
19
+ right: 0;
20
+ transform: translateX(50%) translateY(-50%);
21
+ }
22
+ .brick-badge-type-standard .brick-badge-mark {
23
+ height: 20px;
24
+ min-width: 20px;
25
+ padding: 0 6px;
26
+ font-size: 12px;
27
+ line-height: 18px;
28
+ border-radius: 10px;
29
+ white-space: nowrap;
30
+ max-width: 120px;
31
+ overflow: hidden;
32
+ text-overflow: ellipsis;
33
+ }
34
+ .brick-badge-type-dot .brick-badge-mark, .brick-badge-type-status .brick-badge-mark {
35
+ width: 8px;
36
+ height: 8px;
37
+ }
38
+ .brick-badge-type-dot .brick-badge-mark, .brick-badge-type-status .brick-badge-mark {
39
+ border-radius: 4px;
40
+ }
41
+ .brick-badge-type-status .brick-badge-mark {
42
+ margin-right: 8px;
43
+ }
44
+
45
+ .brick-badge-color-primary .brick-badge-mark {
46
+ background: #4c84ff;
47
+ }
48
+
49
+ .brick-badge-color-success .brick-badge-mark {
50
+ background: #39bf45;
51
+ }
52
+
53
+ .brick-badge-color-danger .brick-badge-mark {
54
+ background: #e64552;
55
+ }
56
+
57
+ .brick-badge-color-warning .brick-badge-mark {
58
+ background: #f27c49;
59
+ }
60
+
61
+ .brick-badge-color-inactive .brick-badge-mark {
62
+ background: rgb(204, 204, 204);
63
+ }
package/es/index.js ADDED
@@ -0,0 +1,116 @@
1
+ import _Reflect$construct from "@babel/runtime-corejs3/core-js-stable/reflect/construct";
2
+ import _classCallCheck from "@babel/runtime-corejs3/helpers/classCallCheck";
3
+ import _createClass from "@babel/runtime-corejs3/helpers/createClass";
4
+ import _inherits from "@babel/runtime-corejs3/helpers/inherits";
5
+ import _possibleConstructorReturn from "@babel/runtime-corejs3/helpers/possibleConstructorReturn";
6
+ import _getPrototypeOf from "@babel/runtime-corejs3/helpers/getPrototypeOf";
7
+ import _defineProperty from "@babel/runtime-corejs3/helpers/defineProperty";
8
+ function _createSuper(t) { var r = _isNativeReflectConstruct(); return function () { var e, o = _getPrototypeOf(t); if (r) { var s = _getPrototypeOf(this).constructor; e = _Reflect$construct(o, arguments, s); } else e = o.apply(this, arguments); return _possibleConstructorReturn(this, e); }; }
9
+ function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(_Reflect$construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
10
+ import { Component, isValidElement } from 'react';
11
+ // import * as PropTypes from 'prop-types'
12
+ import c from 'classnames';
13
+ import { isString } from 'lodash-es';
14
+ import { codeWarning } from '@befe/brick-utils';
15
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
16
+ var BADGE_MAX_DEFAULT = 99;
17
+ export var Badge = /*#__PURE__*/function (_Component) {
18
+ _inherits(Badge, _Component);
19
+ var _super = _createSuper(Badge);
20
+ function Badge() {
21
+ _classCallCheck(this, Badge);
22
+ return _super.apply(this, arguments);
23
+ }
24
+ _createClass(Badge, [{
25
+ key: "color",
26
+ get: function get() {
27
+ var _ref = this.props,
28
+ type = _ref.type;
29
+ process.env.NODE_ENV !== "production" ? codeWarning(
30
+ // @ts-ignore 兼容已废弃的历史 color error
31
+ this.props.color !== 'error', '[Badge] color \'error\' has been deprecated, use color \'danger\' instead.') : void 0;
32
+
33
+ // istanbul ignore else
34
+ // @ts-ignore 兼容已废弃的历史 color error
35
+ var color = this.props.color === 'error' ? 'danger' : this.props.color;
36
+ if (color) {
37
+ return color;
38
+ }
39
+ if (type === 'status') {
40
+ return 'inactive';
41
+ }
42
+ return undefined;
43
+ }
44
+ }, {
45
+ key: "className",
46
+ get: function get() {
47
+ var _ref2 = this.props,
48
+ className = _ref2.className,
49
+ type = _ref2.type;
50
+ var color = this.color;
51
+ return c('brick-badge', color && "brick-badge-color-".concat(color), "brick-badge-type-".concat(type), className);
52
+ }
53
+ }, {
54
+ key: "titleTip",
55
+ get: function get() {
56
+ var _this$props = this.props,
57
+ title = _this$props.title,
58
+ content = _this$props.content;
59
+ if (isString(title)) {
60
+ return title;
61
+ }
62
+ if (typeof content === 'number') {
63
+ return content.toString();
64
+ }
65
+ return isString(content) ? content : undefined;
66
+ }
67
+ }, {
68
+ key: "markContent",
69
+ get: function get() {
70
+ var _this$props2 = this.props,
71
+ content = _this$props2.content,
72
+ max = _this$props2.max;
73
+ if (!content) {
74
+ return null;
75
+ }
76
+ if ( /*#__PURE__*/isValidElement(content) || isString(content)) {
77
+ return content;
78
+ }
79
+ if (content && typeof content === 'number') {
80
+ var count = Math.floor(Number(content));
81
+ var _max = max || BADGE_MAX_DEFAULT;
82
+ return count > _max ? "".concat(max, "+") : "".concat(count);
83
+ }
84
+ return null;
85
+ }
86
+ }, {
87
+ key: "renderMark",
88
+ value: function renderMark() {
89
+ var type = this.props.type;
90
+ var markContent = this.markContent;
91
+ var showMark = markContent || type === 'status';
92
+ var showContent = type !== 'dot' && type !== 'status';
93
+ return showMark && /*#__PURE__*/_jsx("div", {
94
+ className: 'brick-badge-mark',
95
+ children: showContent && markContent
96
+ });
97
+ }
98
+ }, {
99
+ key: "render",
100
+ value: function render() {
101
+ return /*#__PURE__*/_jsxs("div", {
102
+ className: this.className,
103
+ title: this.titleTip,
104
+ children: [this.renderMark(), this.props.children]
105
+ });
106
+ }
107
+ }]);
108
+ return Badge;
109
+ }(Component);
110
+ _defineProperty(Badge, "displayName", 'Badge');
111
+ // static propTypes = {}
112
+ _defineProperty(Badge, "defaultProps", {
113
+ className: '',
114
+ type: 'standard',
115
+ max: BADGE_MAX_DEFAULT
116
+ });
package/es/style.css ADDED
@@ -0,0 +1,63 @@
1
+ .brick-badge,
2
+ .brick-badge * {
3
+ box-sizing: border-box;
4
+ }
5
+ .brick-badge {
6
+ display: inline-block;
7
+ position: relative;
8
+ }
9
+ .brick-badge-mark {
10
+ display: inline-block;
11
+ text-align: center;
12
+ color: rgb(255, 255, 255);
13
+ background: #e64552;
14
+ border: 1px solid rgb(255, 255, 255);
15
+ }
16
+ .brick-badge-type-standard .brick-badge-mark, .brick-badge-type-dot .brick-badge-mark {
17
+ position: absolute;
18
+ top: 0;
19
+ right: 0;
20
+ transform: translateX(50%) translateY(-50%);
21
+ }
22
+ .brick-badge-type-standard .brick-badge-mark {
23
+ height: 20px;
24
+ min-width: 20px;
25
+ padding: 0 6px;
26
+ font-size: 12px;
27
+ line-height: 18px;
28
+ border-radius: 10px;
29
+ white-space: nowrap;
30
+ max-width: 120px;
31
+ overflow: hidden;
32
+ text-overflow: ellipsis;
33
+ }
34
+ .brick-badge-type-dot .brick-badge-mark, .brick-badge-type-status .brick-badge-mark {
35
+ width: 8px;
36
+ height: 8px;
37
+ }
38
+ .brick-badge-type-dot .brick-badge-mark, .brick-badge-type-status .brick-badge-mark {
39
+ border-radius: 4px;
40
+ }
41
+ .brick-badge-type-status .brick-badge-mark {
42
+ margin-right: 8px;
43
+ }
44
+
45
+ .brick-badge-color-primary .brick-badge-mark {
46
+ background: #4c84ff;
47
+ }
48
+
49
+ .brick-badge-color-success .brick-badge-mark {
50
+ background: #39bf45;
51
+ }
52
+
53
+ .brick-badge-color-danger .brick-badge-mark {
54
+ background: #e64552;
55
+ }
56
+
57
+ .brick-badge-color-warning .brick-badge-mark {
58
+ background: #f27c49;
59
+ }
60
+
61
+ .brick-badge-color-inactive .brick-badge-mark {
62
+ background: rgb(204, 204, 204);
63
+ }
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "@befe/brick-comp-badge",
3
+ "version": "0.2.77",
4
+ "description": "@befe/brick component Badge",
5
+ "main": "es",
6
+ "module": "es",
7
+ "types": "types",
8
+ "sideEffects": [
9
+ "*.css",
10
+ "*.scss"
11
+ ],
12
+ "scripts": {
13
+ "test": "node ../../scripts/test",
14
+ "clean": "rm -rf es lib mobx types",
15
+ "build": "npm run clean && npm run build:js && npm run build:dts && npm run build:style",
16
+ "build:dts": "node ../../scripts/build-dts",
17
+ "build:js": "node ../../scripts/build-js",
18
+ "build:style": "node ../../scripts/build-style-component"
19
+ },
20
+ "author": "wujun07",
21
+ "license": "ISC",
22
+ "dependencies": {
23
+ "@befe/brick-core": "^0.2.42",
24
+ "@befe/brick-style": "^0.2.54",
25
+ "@befe/brick-utils": "^0.2.17"
26
+ },
27
+ "peerDependencies": {
28
+ "classnames": "^2.0.0",
29
+ "lodash-es": "^4.0.0",
30
+ "react": "^16.9.0 || ^17.0.0",
31
+ "react-dom": "^16.9.0 || ^17.0.0"
32
+ },
33
+ "gitHead": "ed610b70dd5ccb910c0edaf32e1521f31d1aa397"
34
+ }
package/src/index.scss ADDED
@@ -0,0 +1,2 @@
1
+ @forward "./style";
2
+ // 依赖组件的样式写在下方,因为需要被 ./style 中的 base 覆盖 theme variable
package/src/index.tsx ADDED
@@ -0,0 +1,146 @@
1
+ import {Component, ReactNode, HTMLAttributes, isValidElement} from 'react'
2
+ // import * as PropTypes from 'prop-types'
3
+ import c from 'classnames'
4
+ import {isString} from 'lodash-es'
5
+ import {codeWarning} from '@befe/brick-utils'
6
+
7
+ export interface BadgeProps {
8
+ /**
9
+ * 自定义 class
10
+ */
11
+ className?: string
12
+
13
+ /**
14
+ * 标记类型
15
+ */
16
+ type?: 'standard' | 'dot' | 'status'
17
+
18
+ /**
19
+ * 标记内容
20
+ * - number: 数量
21
+ * - string: 内容
22
+ */
23
+ content?: ReactNode
24
+
25
+ /**
26
+ * content 为 number时 最大显示数量 n,超过则显示 n+
27
+ */
28
+ max?: number
29
+
30
+ /**
31
+ * 颜色
32
+ */
33
+ color?: 'primary' | 'success' | 'warning' | 'danger' | 'inactive'
34
+
35
+ /**
36
+ * 原生 hover title tip,如不同则以 content 为 tip,以便可以显示超出 max 的 count
37
+ */
38
+ title?: HTMLAttributes<HTMLDivElement>['title']
39
+ }
40
+
41
+ const BADGE_MAX_DEFAULT = 99
42
+
43
+ type BadgePropsWithDefaults = BadgeProps & Required<Pick<BadgeProps, keyof typeof Badge.defaultProps>>
44
+
45
+ export class Badge extends Component<BadgeProps> {
46
+ static displayName = 'Badge'
47
+ // static propTypes = {}
48
+ static defaultProps = {
49
+ className: '',
50
+ type: 'standard',
51
+ max: BADGE_MAX_DEFAULT,
52
+ }
53
+
54
+ get color() {
55
+ const {type} = this.props as BadgePropsWithDefaults
56
+
57
+ codeWarning(
58
+ // @ts-ignore 兼容已废弃的历史 color error
59
+ this.props.color !== 'error',
60
+ '[Badge] color \'error\' has been deprecated, use color \'danger\' instead.'
61
+ )
62
+
63
+ // istanbul ignore else
64
+ // @ts-ignore 兼容已废弃的历史 color error
65
+ const color = this.props.color === 'error' ? 'danger' : this.props.color
66
+
67
+ if (color) {
68
+ return color
69
+ }
70
+
71
+ if (type === 'status') {
72
+ return 'inactive'
73
+ }
74
+
75
+ return undefined
76
+ }
77
+
78
+ get className() {
79
+ const {className, type} = this.props as BadgePropsWithDefaults
80
+ const color = this.color
81
+
82
+ return c(
83
+ 'brick-badge',
84
+ color && `brick-badge-color-${color}`,
85
+ `brick-badge-type-${type}`,
86
+ className
87
+ )
88
+ }
89
+
90
+ get titleTip() {
91
+ const {title, content} = this.props
92
+
93
+ if (isString(title)) {
94
+ return title
95
+ }
96
+
97
+ if (typeof content === 'number') {
98
+ return content.toString()
99
+ }
100
+
101
+ return isString(content) ? content : undefined
102
+ }
103
+
104
+ get markContent() {
105
+ const {content, max} = this.props
106
+
107
+ if (!content) {
108
+ return null
109
+ }
110
+
111
+ if (isValidElement(content) || isString(content)) {
112
+ return content
113
+ }
114
+
115
+ if (content && typeof content === 'number') {
116
+ const count = Math.floor(Number(content))
117
+ const _max = max || BADGE_MAX_DEFAULT
118
+
119
+ return count > _max ? `${max as number}+` : `${count}`
120
+ }
121
+
122
+ return null
123
+ }
124
+
125
+ renderMark() {
126
+ const {type} = this.props
127
+ const markContent = this.markContent
128
+ const showMark = markContent || type === 'status'
129
+ const showContent = type !== 'dot' && type !== 'status'
130
+
131
+ return showMark && (
132
+ <div className={'brick-badge-mark'}>
133
+ {showContent && markContent}
134
+ </div>
135
+ )
136
+ }
137
+
138
+ render() {
139
+ return (
140
+ <div className={this.className} title={this.titleTip}>
141
+ {this.renderMark()}
142
+ {this.props.children}
143
+ </div>
144
+ )
145
+ }
146
+ }
package/src/style.scss ADDED
@@ -0,0 +1,72 @@
1
+ @use "sass:math";
2
+ @use "sass:map";
3
+ @use "@befe/brick-style/src/themes/base";
4
+ @use "@befe/brick-style/src/themes/badge";
5
+
6
+ .brick-badge {
7
+ @include base.reset();
8
+ & {
9
+ display: inline-block;
10
+ position: relative;
11
+ }
12
+
13
+ &-mark {
14
+ display: inline-block;
15
+ text-align: center;
16
+ color: base.$color-text-reverse;
17
+ background: badge.$badge-color-default;
18
+ border: badge.$badge-border-width solid badge.$badge-border-color;
19
+ }
20
+
21
+ &-type-standard,
22
+ &-type-dot {
23
+ .brick-badge-mark {
24
+ position: absolute;
25
+ top: 0;
26
+ right: 0;
27
+ transform: translateX(50%) translateY(-50%);
28
+ }
29
+ }
30
+
31
+ &-type-standard {
32
+ .brick-badge-mark {
33
+ height: badge.$badge-standard-height;
34
+ min-width: badge.$badge-standard-width;
35
+ padding: 0 badge.$badge-standard-padding-horizontal;
36
+ font-size: base.$font-size-sm;
37
+ line-height: badge.$badge-standard-size - badge.$badge-border-width * 2;
38
+ border-radius: math.div(badge.$badge-standard-height, 2);
39
+ // @info: 不让换行,不然遇到空格就自动换行了
40
+ white-space: nowrap;
41
+ max-width: badge.$badge-content-max-width;
42
+ overflow: hidden;
43
+ text-overflow: ellipsis;
44
+ }
45
+ }
46
+
47
+ &-type-dot,
48
+ &-type-status {
49
+ .brick-badge-mark {
50
+ @include base.size(badge.$badge-dot-size);
51
+ & {
52
+ border-radius: math.div(badge.$badge-dot-size, 2);
53
+ }
54
+ }
55
+ }
56
+
57
+ &-type-dot {
58
+ }
59
+ &-type-status {
60
+ .brick-badge-mark {
61
+ margin-right: badge.$badge-status-distance;
62
+ }
63
+ }
64
+ }
65
+
66
+ @each $color in badge.$badge-color-list {
67
+ .brick-badge-color-#{$color} {
68
+ .brick-badge-mark {
69
+ background: map.get(badge.$badge-color, $color);;
70
+ }
71
+ }
72
+ }
@@ -0,0 +1,43 @@
1
+ import { Component, ReactNode, HTMLAttributes } from 'react';
2
+ export interface BadgeProps {
3
+ /**
4
+ * 自定义 class
5
+ */
6
+ className?: string;
7
+ /**
8
+ * 标记类型
9
+ */
10
+ type?: 'standard' | 'dot' | 'status';
11
+ /**
12
+ * 标记内容
13
+ * - number: 数量
14
+ * - string: 内容
15
+ */
16
+ content?: ReactNode;
17
+ /**
18
+ * content 为 number时 最大显示数量 n,超过则显示 n+
19
+ */
20
+ max?: number;
21
+ /**
22
+ * 颜色
23
+ */
24
+ color?: 'primary' | 'success' | 'warning' | 'danger' | 'inactive';
25
+ /**
26
+ * 原生 hover title tip,如不同则以 content 为 tip,以便可以显示超出 max 的 count
27
+ */
28
+ title?: HTMLAttributes<HTMLDivElement>['title'];
29
+ }
30
+ export declare class Badge extends Component<BadgeProps> {
31
+ static displayName: string;
32
+ static defaultProps: {
33
+ className: string;
34
+ type: string;
35
+ max: number;
36
+ };
37
+ get color(): "primary" | "success" | "warning" | "danger" | "inactive" | undefined;
38
+ get className(): string;
39
+ get titleTip(): string | undefined;
40
+ get markContent(): string | import("react").ReactElement<unknown, string | import("react").JSXElementConstructor<any>> | null;
41
+ renderMark(): false | "" | JSX.Element;
42
+ render(): JSX.Element;
43
+ }