@spaced-out/ui-design-system 0.1.124 → 0.1.125
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/CHANGELOG.md +7 -0
- package/lib/components/Chip/index.js.flow +1 -1
- package/lib/components/Rating/Rating.js +90 -0
- package/lib/components/Rating/Rating.js.flow +133 -0
- package/lib/components/Rating/Rating.module.css +41 -0
- package/lib/components/Rating/index.js +16 -0
- package/lib/components/Rating/index.js.flow +3 -0
- package/lib/utils/index.js +11 -0
- package/lib/utils/index.js.flow +1 -0
- package/lib/utils/rating/index.js +16 -0
- package/lib/utils/rating/index.js.flow +3 -0
- package/lib/utils/rating/rating.js +30 -0
- package/lib/utils/rating/rating.js.flow +35 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
|
4
4
|
|
|
5
|
+
### [0.1.125](https://github.com/spaced-out/ui-design-system/compare/v0.1.124...v0.1.125) (2024-08-29)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
### Features
|
|
9
|
+
|
|
10
|
+
* rating component ([#258](https://github.com/spaced-out/ui-design-system/issues/258)) ([450caa5](https://github.com/spaced-out/ui-design-system/commit/450caa523c55b5cf3e4635064f63b3e4eec19a8e))
|
|
11
|
+
|
|
5
12
|
### [0.1.124](https://github.com/spaced-out/ui-design-system/compare/v0.1.123...v0.1.124) (2024-08-27)
|
|
6
13
|
|
|
7
14
|
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.Rating = exports.RATING_SIZE = void 0;
|
|
7
|
+
var React = _interopRequireWildcard(require("react"));
|
|
8
|
+
var _invariant = _interopRequireDefault(require("invariant"));
|
|
9
|
+
var _lodash = require("lodash");
|
|
10
|
+
var _classify = require("../../utils/classify");
|
|
11
|
+
var _rating = require("../../utils/rating");
|
|
12
|
+
var _Button = require("../Button");
|
|
13
|
+
var _Chip = require("../Chip");
|
|
14
|
+
var _ConditionalWrapper = require("../ConditionalWrapper");
|
|
15
|
+
var _Icon = require("../Icon");
|
|
16
|
+
var _Text = require("../Text");
|
|
17
|
+
var _RatingModule = _interopRequireDefault(require("./Rating.module.css"));
|
|
18
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
19
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
20
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
21
|
+
|
|
22
|
+
const RATING_SIZE = Object.freeze({
|
|
23
|
+
small: 'small',
|
|
24
|
+
medium: 'medium'
|
|
25
|
+
});
|
|
26
|
+
exports.RATING_SIZE = RATING_SIZE;
|
|
27
|
+
const Rating = /*#__PURE__*/React.forwardRef((_ref, ref) => {
|
|
28
|
+
let {
|
|
29
|
+
size = 'medium',
|
|
30
|
+
start = 0,
|
|
31
|
+
end = 5,
|
|
32
|
+
quiet,
|
|
33
|
+
rating = 0,
|
|
34
|
+
labels = _rating.RATINGS,
|
|
35
|
+
onChange,
|
|
36
|
+
readOnly,
|
|
37
|
+
hideLabel,
|
|
38
|
+
iconName = 'star',
|
|
39
|
+
iconColor = _Text.TEXT_COLORS.favorite,
|
|
40
|
+
chipSemantic = _Chip.CHIP_SEMANTIC.warning,
|
|
41
|
+
classNames
|
|
42
|
+
} = _ref;
|
|
43
|
+
const [hoverIcon, setHoverIcon] = React.useState(-1);
|
|
44
|
+
const chipText = (0, _rating.getRatingLabel)(rating - start, labels);
|
|
45
|
+
const totalIcons = end - start;
|
|
46
|
+
(0, _invariant.default)(totalIcons, JSON.stringify(_rating.RATING_ERRORS.INVALID_RANGE));
|
|
47
|
+
(0, _invariant.default)((0, _lodash.isUndefined)(labels) || (0, _lodash.isArray)(labels) && labels.length, JSON.stringify(_rating.RATING_ERRORS.INVALID_RATING_LABELS));
|
|
48
|
+
return /*#__PURE__*/React.createElement("div", {
|
|
49
|
+
"data-testid": "Rating",
|
|
50
|
+
ref: ref,
|
|
51
|
+
className: (0, _classify.classify)(_RatingModule.default.ratingContainer, classNames?.wrapper)
|
|
52
|
+
}, /*#__PURE__*/React.createElement("div", {
|
|
53
|
+
className: _RatingModule.default.stars
|
|
54
|
+
}, Array.from({
|
|
55
|
+
length: totalIcons
|
|
56
|
+
}).map((_, index) => {
|
|
57
|
+
const isHovering = hoverIcon > -1;
|
|
58
|
+
const iconCount = index + 1;
|
|
59
|
+
const iconType = (isHovering ? iconCount <= hoverIcon : iconCount <= rating - start) ? 'solid' : 'regular';
|
|
60
|
+
return /*#__PURE__*/React.createElement(_ConditionalWrapper.ConditionalWrapper, {
|
|
61
|
+
key: iconCount,
|
|
62
|
+
condition: !readOnly,
|
|
63
|
+
wrapper: children => /*#__PURE__*/React.createElement(_Button.UnstyledButton, {
|
|
64
|
+
onKeyDown: e => {
|
|
65
|
+
if (e.key === 'Enter') {
|
|
66
|
+
onChange?.(start + iconCount, e);
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
onFocus: () => !quiet && setHoverIcon(iconCount),
|
|
70
|
+
className: (0, _classify.classify)(_RatingModule.default.button, classNames?.button),
|
|
71
|
+
onClick: e => onChange?.(start + iconCount, e),
|
|
72
|
+
onMouseEnter: () => !quiet && setHoverIcon(iconCount),
|
|
73
|
+
onMouseLeave: () => !quiet && setHoverIcon(-1)
|
|
74
|
+
}, children)
|
|
75
|
+
}, /*#__PURE__*/React.createElement(_Icon.Icon, {
|
|
76
|
+
size: size,
|
|
77
|
+
name: iconName,
|
|
78
|
+
type: iconType,
|
|
79
|
+
color: iconColor,
|
|
80
|
+
className: (0, _classify.classify)(_RatingModule.default.icon, classNames?.icon)
|
|
81
|
+
}));
|
|
82
|
+
})), !hideLabel &&
|
|
83
|
+
/*#__PURE__*/
|
|
84
|
+
// $FlowFixMe[incompatible-type]
|
|
85
|
+
React.createElement(_Chip.Chip, {
|
|
86
|
+
size: size,
|
|
87
|
+
semantic: chipSemantic
|
|
88
|
+
}, chipText));
|
|
89
|
+
});
|
|
90
|
+
exports.Rating = Rating;
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
// @flow strict
|
|
2
|
+
|
|
3
|
+
import * as React from 'react';
|
|
4
|
+
import invariant from 'invariant';
|
|
5
|
+
import {isArray, isUndefined} from 'lodash';
|
|
6
|
+
|
|
7
|
+
import type {ColorTypes} from '../../types/typography';
|
|
8
|
+
import {classify} from '../../utils/classify';
|
|
9
|
+
import {getRatingLabel, RATING_ERRORS, RATINGS} from '../../utils/rating';
|
|
10
|
+
import {UnstyledButton} from '../Button';
|
|
11
|
+
import type {ChipSemanticType} from '../Chip';
|
|
12
|
+
import {Chip, CHIP_SEMANTIC} from '../Chip';
|
|
13
|
+
import {ConditionalWrapper} from '../ConditionalWrapper';
|
|
14
|
+
import {Icon} from '../Icon';
|
|
15
|
+
import {TEXT_COLORS} from '../Text';
|
|
16
|
+
|
|
17
|
+
import css from './Rating.module.css';
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
export const RATING_SIZE = Object.freeze({
|
|
21
|
+
small: 'small',
|
|
22
|
+
medium: 'medium',
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
type ClassNames = $ReadOnly<{wrapper?: string, icon?: string, button?: string}>;
|
|
26
|
+
type RatingSize = $Values<typeof RATING_SIZE>;
|
|
27
|
+
|
|
28
|
+
export type RatingProps = {
|
|
29
|
+
size?: RatingSize,
|
|
30
|
+
start?: number,
|
|
31
|
+
end?: number,
|
|
32
|
+
quiet?: boolean,
|
|
33
|
+
rating?: number,
|
|
34
|
+
labels?: Array<string>,
|
|
35
|
+
iconColor?: ColorTypes,
|
|
36
|
+
onChange?: (rating: number, ?SyntheticEvent<HTMLElement>) => mixed,
|
|
37
|
+
readOnly?: boolean,
|
|
38
|
+
hideLabel?: boolean,
|
|
39
|
+
iconName?: string,
|
|
40
|
+
classNames?: ClassNames,
|
|
41
|
+
chipSemantic?: ChipSemanticType,
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export const Rating: React$AbstractComponent<RatingProps, HTMLDivElement> =
|
|
45
|
+
React.forwardRef<RatingProps, HTMLDivElement>(
|
|
46
|
+
(
|
|
47
|
+
{
|
|
48
|
+
size = 'medium',
|
|
49
|
+
start = 0,
|
|
50
|
+
end = 5,
|
|
51
|
+
quiet,
|
|
52
|
+
rating = 0,
|
|
53
|
+
labels = RATINGS,
|
|
54
|
+
onChange,
|
|
55
|
+
readOnly,
|
|
56
|
+
hideLabel,
|
|
57
|
+
iconName = 'star',
|
|
58
|
+
iconColor = TEXT_COLORS.favorite,
|
|
59
|
+
chipSemantic = CHIP_SEMANTIC.warning,
|
|
60
|
+
classNames,
|
|
61
|
+
}: RatingProps,
|
|
62
|
+
ref,
|
|
63
|
+
): React.Node => {
|
|
64
|
+
const [hoverIcon, setHoverIcon] = React.useState<number>(-1);
|
|
65
|
+
const chipText = getRatingLabel(rating - start, labels);
|
|
66
|
+
const totalIcons = end - start;
|
|
67
|
+
|
|
68
|
+
invariant(totalIcons, JSON.stringify(RATING_ERRORS.INVALID_RANGE));
|
|
69
|
+
invariant(
|
|
70
|
+
isUndefined(labels) || (isArray(labels) && labels.length),
|
|
71
|
+
JSON.stringify(RATING_ERRORS.INVALID_RATING_LABELS),
|
|
72
|
+
);
|
|
73
|
+
|
|
74
|
+
return (
|
|
75
|
+
<div
|
|
76
|
+
data-testid="Rating"
|
|
77
|
+
ref={ref}
|
|
78
|
+
className={classify(css.ratingContainer, classNames?.wrapper)}
|
|
79
|
+
>
|
|
80
|
+
<div className={css.stars}>
|
|
81
|
+
{Array.from({length: totalIcons}).map((_, index) => {
|
|
82
|
+
const isHovering = hoverIcon > -1;
|
|
83
|
+
const iconCount = index + 1;
|
|
84
|
+
const iconType = (
|
|
85
|
+
isHovering
|
|
86
|
+
? iconCount <= hoverIcon
|
|
87
|
+
: iconCount <= rating - start
|
|
88
|
+
)
|
|
89
|
+
? 'solid'
|
|
90
|
+
: 'regular';
|
|
91
|
+
|
|
92
|
+
return (
|
|
93
|
+
<ConditionalWrapper
|
|
94
|
+
key={iconCount}
|
|
95
|
+
condition={!readOnly}
|
|
96
|
+
wrapper={(children) => (
|
|
97
|
+
<UnstyledButton
|
|
98
|
+
onKeyDown={(e) => {
|
|
99
|
+
if (e.key === 'Enter') {
|
|
100
|
+
onChange?.(start + iconCount, e);
|
|
101
|
+
}
|
|
102
|
+
}}
|
|
103
|
+
onFocus={() => !quiet && setHoverIcon(iconCount)}
|
|
104
|
+
className={classify(css.button, classNames?.button)}
|
|
105
|
+
onClick={(e) => onChange?.(start + iconCount, e)}
|
|
106
|
+
onMouseEnter={() => !quiet && setHoverIcon(iconCount)}
|
|
107
|
+
onMouseLeave={() => !quiet && setHoverIcon(-1)}
|
|
108
|
+
>
|
|
109
|
+
{children}
|
|
110
|
+
</UnstyledButton>
|
|
111
|
+
)}
|
|
112
|
+
>
|
|
113
|
+
<Icon
|
|
114
|
+
size={size}
|
|
115
|
+
name={iconName}
|
|
116
|
+
type={iconType}
|
|
117
|
+
color={iconColor}
|
|
118
|
+
className={classify(css.icon, classNames?.icon)}
|
|
119
|
+
/>
|
|
120
|
+
</ConditionalWrapper>
|
|
121
|
+
);
|
|
122
|
+
})}
|
|
123
|
+
</div>
|
|
124
|
+
{!hideLabel && (
|
|
125
|
+
// $FlowFixMe[incompatible-type]
|
|
126
|
+
<Chip size={size} semantic={chipSemantic}>
|
|
127
|
+
{chipText}
|
|
128
|
+
</Chip>
|
|
129
|
+
)}
|
|
130
|
+
</div>
|
|
131
|
+
);
|
|
132
|
+
},
|
|
133
|
+
);
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
@value (
|
|
2
|
+
spaceNone,
|
|
3
|
+
spaceXSmall,
|
|
4
|
+
spaceXXSmall
|
|
5
|
+
) from '../../styles/variables/_space.css';
|
|
6
|
+
@value (
|
|
7
|
+
colorFocusPrimary
|
|
8
|
+
) from '../../styles/variables/_color.css';
|
|
9
|
+
@value (
|
|
10
|
+
borderRadiusXSmall,
|
|
11
|
+
borderWidthNone,
|
|
12
|
+
borderWidthTertiary
|
|
13
|
+
) from '../../styles/variables/_border.css';
|
|
14
|
+
|
|
15
|
+
.ratingContainer {
|
|
16
|
+
width: fit-content;
|
|
17
|
+
display: flex;
|
|
18
|
+
align-items: center;
|
|
19
|
+
gap: spaceXSmall;
|
|
20
|
+
margin: spaceXXSmall spaceXSmall;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
.stars {
|
|
24
|
+
display: flex;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
.icon {
|
|
28
|
+
composes: motionEaseInEaseOut from '../../styles/animation.module.css';
|
|
29
|
+
margin: spaceNone spaceXXSmall;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
.button {
|
|
33
|
+
border-radius: borderRadiusXSmall;
|
|
34
|
+
outline: none;
|
|
35
|
+
cursor: pointer;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
.button:focus {
|
|
39
|
+
box-shadow: borderWidthNone borderWidthNone borderWidthNone
|
|
40
|
+
borderWidthTertiary colorFocusPrimary;
|
|
41
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
var _Rating = require("./Rating");
|
|
7
|
+
Object.keys(_Rating).forEach(function (key) {
|
|
8
|
+
if (key === "default" || key === "__esModule") return;
|
|
9
|
+
if (key in exports && exports[key] === _Rating[key]) return;
|
|
10
|
+
Object.defineProperty(exports, key, {
|
|
11
|
+
enumerable: true,
|
|
12
|
+
get: function () {
|
|
13
|
+
return _Rating[key];
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
});
|
package/lib/utils/index.js
CHANGED
|
@@ -113,6 +113,17 @@ Object.keys(_mergeRefs).forEach(function (key) {
|
|
|
113
113
|
}
|
|
114
114
|
});
|
|
115
115
|
});
|
|
116
|
+
var _rating = require("./rating");
|
|
117
|
+
Object.keys(_rating).forEach(function (key) {
|
|
118
|
+
if (key === "default" || key === "__esModule") return;
|
|
119
|
+
if (key in exports && exports[key] === _rating[key]) return;
|
|
120
|
+
Object.defineProperty(exports, key, {
|
|
121
|
+
enumerable: true,
|
|
122
|
+
get: function () {
|
|
123
|
+
return _rating[key];
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
});
|
|
116
127
|
var _string = require("./string");
|
|
117
128
|
Object.keys(_string).forEach(function (key) {
|
|
118
129
|
if (key === "default" || key === "__esModule") return;
|
package/lib/utils/index.js.flow
CHANGED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
var _rating = require("./rating");
|
|
7
|
+
Object.keys(_rating).forEach(function (key) {
|
|
8
|
+
if (key === "default" || key === "__esModule") return;
|
|
9
|
+
if (key in exports && exports[key] === _rating[key]) return;
|
|
10
|
+
Object.defineProperty(exports, key, {
|
|
11
|
+
enumerable: true,
|
|
12
|
+
get: function () {
|
|
13
|
+
return _rating[key];
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
});
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.getRatingLabel = exports.RATING_ERRORS = exports.RATINGS = void 0;
|
|
7
|
+
|
|
8
|
+
const RATING_ERRORS = Object.freeze({
|
|
9
|
+
INVALID_RANGE: {
|
|
10
|
+
type: 'INVALID_RANGE',
|
|
11
|
+
description: 'Given rating\'s stop value cannot come before the start value.'
|
|
12
|
+
},
|
|
13
|
+
INVALID_RATING_LABELS: {
|
|
14
|
+
type: 'INVALID_RATING_LABELS',
|
|
15
|
+
description: 'Given rating labels are invalid.'
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
exports.RATING_ERRORS = RATING_ERRORS;
|
|
19
|
+
const RATINGS = ['Not-Qualified', 'Proficient', 'Silver', 'Gold', 'Platinum', 'Diamond'];
|
|
20
|
+
exports.RATINGS = RATINGS;
|
|
21
|
+
const getRatingLabel = (rating, labels) => {
|
|
22
|
+
if (rating <= 0) {
|
|
23
|
+
return labels[0];
|
|
24
|
+
} else if (rating >= labels.length) {
|
|
25
|
+
return labels.slice(-1)[0];
|
|
26
|
+
} else {
|
|
27
|
+
return labels[rating];
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
exports.getRatingLabel = getRatingLabel;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// @flow strict
|
|
2
|
+
|
|
3
|
+
export const RATING_ERRORS = Object.freeze({
|
|
4
|
+
INVALID_RANGE: {
|
|
5
|
+
type: 'INVALID_RANGE',
|
|
6
|
+
description:
|
|
7
|
+
'Given rating\'s stop value cannot come before the start value.',
|
|
8
|
+
},
|
|
9
|
+
INVALID_RATING_LABELS: {
|
|
10
|
+
type: 'INVALID_RATING_LABELS',
|
|
11
|
+
description: 'Given rating labels are invalid.',
|
|
12
|
+
},
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
export const RATINGS: Array<string> = [
|
|
16
|
+
'Not-Qualified',
|
|
17
|
+
'Proficient',
|
|
18
|
+
'Silver',
|
|
19
|
+
'Gold',
|
|
20
|
+
'Platinum',
|
|
21
|
+
'Diamond',
|
|
22
|
+
];
|
|
23
|
+
|
|
24
|
+
export const getRatingLabel = (
|
|
25
|
+
rating: number,
|
|
26
|
+
labels: Array<string>,
|
|
27
|
+
): string => {
|
|
28
|
+
if (rating <= 0) {
|
|
29
|
+
return labels[0];
|
|
30
|
+
} else if (rating >= labels.length) {
|
|
31
|
+
return labels.slice(-1)[0];
|
|
32
|
+
} else {
|
|
33
|
+
return labels[rating];
|
|
34
|
+
}
|
|
35
|
+
};
|