@instructure/ui-rating 11.7.2-snapshot-19 → 11.7.2-snapshot-21

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 (39) hide show
  1. package/CHANGELOG.md +12 -2
  2. package/es/Rating/v2/index.js +114 -0
  3. package/es/Rating/v2/props.js +26 -0
  4. package/es/Rating/v2/styles.js +43 -0
  5. package/es/RatingIcon/v2/index.js +124 -0
  6. package/es/RatingIcon/v2/props.js +26 -0
  7. package/es/RatingIcon/v2/styles.js +59 -0
  8. package/es/exports/b.js +25 -0
  9. package/lib/Rating/v2/index.js +119 -0
  10. package/lib/Rating/v2/props.js +31 -0
  11. package/lib/Rating/v2/styles.js +49 -0
  12. package/lib/RatingIcon/v2/index.js +129 -0
  13. package/lib/RatingIcon/v2/props.js +31 -0
  14. package/lib/RatingIcon/v2/styles.js +65 -0
  15. package/lib/exports/b.js +19 -0
  16. package/package.json +23 -23
  17. package/src/Rating/v2/README.md +104 -0
  18. package/src/Rating/v2/index.tsx +131 -0
  19. package/src/Rating/v2/props.ts +90 -0
  20. package/src/Rating/v2/styles.ts +46 -0
  21. package/src/RatingIcon/v2/index.tsx +140 -0
  22. package/src/RatingIcon/v2/props.ts +55 -0
  23. package/src/RatingIcon/v2/styles.ts +71 -0
  24. package/src/exports/b.ts +28 -0
  25. package/tsconfig.build.tsbuildinfo +1 -1
  26. package/types/Rating/v2/index.d.ts +39 -0
  27. package/types/Rating/v2/index.d.ts.map +1 -0
  28. package/types/Rating/v2/props.d.ts +46 -0
  29. package/types/Rating/v2/props.d.ts.map +1 -0
  30. package/types/Rating/v2/styles.d.ts +14 -0
  31. package/types/Rating/v2/styles.d.ts.map +1 -0
  32. package/types/RatingIcon/v2/index.d.ts +39 -0
  33. package/types/RatingIcon/v2/index.d.ts.map +1 -0
  34. package/types/RatingIcon/v2/props.d.ts +19 -0
  35. package/types/RatingIcon/v2/props.d.ts.map +1 -0
  36. package/types/RatingIcon/v2/styles.d.ts +15 -0
  37. package/types/RatingIcon/v2/styles.d.ts.map +1 -0
  38. package/types/exports/b.d.ts +5 -0
  39. package/types/exports/b.d.ts.map +1 -0
@@ -0,0 +1,129 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.default = exports.RatingIcon = void 0;
8
+ var _react = require("react");
9
+ var _uiIcons = require("@instructure/ui-icons");
10
+ var _requestAnimationFrame = require("@instructure/ui-dom-utils/lib/requestAnimationFrame.js");
11
+ var _Transition = require("@instructure/ui-motion/lib/Transition");
12
+ var _emotion = require("@instructure/emotion");
13
+ var _styles = _interopRequireDefault(require("./styles"));
14
+ var _props = require("./props");
15
+ var _jsxRuntime = require("@emotion/react/jsx-runtime");
16
+ var _dec, _class, _RatingIcon;
17
+ /*
18
+ * The MIT License (MIT)
19
+ *
20
+ * Copyright (c) 2015 - present Instructure, Inc.
21
+ *
22
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
23
+ * of this software and associated documentation files (the "Software"), to deal
24
+ * in the Software without restriction, including without limitation the rights
25
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
26
+ * copies of the Software, and to permit persons to whom the Software is
27
+ * furnished to do so, subject to the following conditions:
28
+ *
29
+ * The above copyright notice and this permission notice shall be included in all
30
+ * copies or substantial portions of the Software.
31
+ *
32
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38
+ * SOFTWARE.
39
+ */
40
+ const ratingIconSizeToIconSize = {
41
+ small: 'md',
42
+ medium: 'lg',
43
+ large: 'xl'
44
+ };
45
+
46
+ /**
47
+ ---
48
+ parent: Rating
49
+ id: Rating.Icon
50
+ ---
51
+ **/
52
+ let RatingIcon = exports.RatingIcon = (_dec = (0, _emotion.withStyle)(_styles.default), _dec(_class = (_RatingIcon = class RatingIcon extends _react.Component {
53
+ constructor(props) {
54
+ super(props);
55
+ this.ref = null;
56
+ this._timeouts = [];
57
+ this._animation = void 0;
58
+ this.makeStyleProps = () => {
59
+ return {
60
+ filled: this.state.filled
61
+ };
62
+ };
63
+ this.fill = () => {
64
+ this._animation = (0, _requestAnimationFrame.requestAnimationFrame)(() => {
65
+ this.setState({
66
+ filled: true
67
+ });
68
+ });
69
+ };
70
+ this.state = {
71
+ filled: props.filled && !props.animateFill
72
+ };
73
+ }
74
+ componentDidMount() {
75
+ var _this$props$makeStyle, _this$props;
76
+ (_this$props$makeStyle = (_this$props = this.props).makeStyles) === null || _this$props$makeStyle === void 0 ? void 0 : _this$props$makeStyle.call(_this$props, this.makeStyleProps());
77
+ if (this.props.animateFill) {
78
+ this._timeouts.push(setTimeout(this.fill, this.props.animationDelay));
79
+ }
80
+ }
81
+ componentDidUpdate(prevProps) {
82
+ var _this$props$makeStyle2, _this$props2;
83
+ if (this.props.animateFill && this.props.filled && this.props.filled !== prevProps.filled) {
84
+ this.fill();
85
+ }
86
+ (_this$props$makeStyle2 = (_this$props2 = this.props).makeStyles) === null || _this$props$makeStyle2 === void 0 ? void 0 : _this$props$makeStyle2.call(_this$props2, this.makeStyleProps());
87
+ }
88
+ componentWillUnmount() {
89
+ this._animation && this._animation.cancel();
90
+ this._timeouts.forEach(timeout => clearTimeout(timeout));
91
+ }
92
+ render() {
93
+ var _this$props$styles, _this$props$styles2, _this$props$styles3;
94
+ const _this$props3 = this.props,
95
+ animateFill = _this$props3.animateFill,
96
+ size = _this$props3.size;
97
+ const Icon = this.state.filled ? _uiIcons.StarSolidInstUIIcon : _uiIcons.StarInstUIIcon;
98
+ const iconSize = ratingIconSizeToIconSize[size];
99
+ return (0, _jsxRuntime.jsx)("span", {
100
+ "aria-hidden": "true",
101
+ css: (_this$props$styles = this.props.styles) === null || _this$props$styles === void 0 ? void 0 : _this$props$styles.ratingIcon,
102
+ ref: el => {
103
+ this.ref = el;
104
+ },
105
+ children: this.state.filled && animateFill ? (0, _jsxRuntime.jsx)(_Transition.Transition, {
106
+ in: true,
107
+ transitionOnMount: true,
108
+ type: "scale",
109
+ children: (0, _jsxRuntime.jsx)("span", {
110
+ css: (_this$props$styles2 = this.props.styles) === null || _this$props$styles2 === void 0 ? void 0 : _this$props$styles2.icon,
111
+ children: (0, _jsxRuntime.jsx)(Icon, {
112
+ size: iconSize
113
+ })
114
+ })
115
+ }) : (0, _jsxRuntime.jsx)("span", {
116
+ css: (_this$props$styles3 = this.props.styles) === null || _this$props$styles3 === void 0 ? void 0 : _this$props$styles3.icon,
117
+ children: (0, _jsxRuntime.jsx)(Icon, {
118
+ size: iconSize
119
+ })
120
+ })
121
+ });
122
+ }
123
+ }, _RatingIcon.displayName = "RatingIcon", _RatingIcon.componentId = 'Rating.Icon', _RatingIcon.allowedProps = _props.allowedProps, _RatingIcon.defaultProps = {
124
+ animationDelay: 200,
125
+ animateFill: false,
126
+ filled: false,
127
+ size: 'medium'
128
+ }, _RatingIcon)) || _class);
129
+ var _default = exports.default = RatingIcon;
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.allowedProps = void 0;
7
+ /*
8
+ * The MIT License (MIT)
9
+ *
10
+ * Copyright (c) 2015 - present Instructure, Inc.
11
+ *
12
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
13
+ * of this software and associated documentation files (the "Software"), to deal
14
+ * in the Software without restriction, including without limitation the rights
15
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
+ * copies of the Software, and to permit persons to whom the Software is
17
+ * furnished to do so, subject to the following conditions:
18
+ *
19
+ * The above copyright notice and this permission notice shall be included in all
20
+ * copies or substantial portions of the Software.
21
+ *
22
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
+ * SOFTWARE.
29
+ */
30
+
31
+ const allowedProps = exports.allowedProps = ['animationDelay', 'animateFill', 'filled', 'size'];
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ /*
8
+ * The MIT License (MIT)
9
+ *
10
+ * Copyright (c) 2015 - present Instructure, Inc.
11
+ *
12
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
13
+ * of this software and associated documentation files (the "Software"), to deal
14
+ * in the Software without restriction, including without limitation the rights
15
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
+ * copies of the Software, and to permit persons to whom the Software is
17
+ * furnished to do so, subject to the following conditions:
18
+ *
19
+ * The above copyright notice and this permission notice shall be included in all
20
+ * copies or substantial portions of the Software.
21
+ *
22
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
+ * SOFTWARE.
29
+ */
30
+
31
+ /**
32
+ * ---
33
+ * private: true
34
+ * ---
35
+ * Generates the style object from the theme and provided additional information
36
+ * @param {Object} componentTheme The theme variable object.
37
+ * @param {Object} props the props of the component, the style is applied to
38
+ * @param {Object} state the state of the component, the style is applied to
39
+ * @return {Object} The final style object, which will be used in the component
40
+ */
41
+ const generateStyle = (componentTheme, _props, state, _sharedTokens) => {
42
+ const filled = state.filled;
43
+ return {
44
+ ratingIcon: {
45
+ label: 'ratingIcon',
46
+ display: 'inline-block',
47
+ verticalAlign: 'bottom',
48
+ margin: `0 ${componentTheme.iconMargin}`,
49
+ lineHeight: 1,
50
+ '&:first-of-type': {
51
+ marginLeft: 0
52
+ },
53
+ '&:last-of-type': {
54
+ marginRight: 0
55
+ }
56
+ },
57
+ icon: {
58
+ label: 'ratingIcon__icon',
59
+ display: 'inline-block',
60
+ verticalAlign: 'bottom',
61
+ color: filled ? componentTheme.iconFilledColor : componentTheme.iconEmptyColor
62
+ }
63
+ };
64
+ };
65
+ var _default = exports.default = generateStyle;
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ Object.defineProperty(exports, "Rating", {
7
+ enumerable: true,
8
+ get: function () {
9
+ return _v.Rating;
10
+ }
11
+ });
12
+ Object.defineProperty(exports, "RatingIcon", {
13
+ enumerable: true,
14
+ get: function () {
15
+ return _v2.RatingIcon;
16
+ }
17
+ });
18
+ var _v = require("../Rating/v2");
19
+ var _v2 = require("../RatingIcon/v2");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@instructure/ui-rating",
3
- "version": "11.7.2-snapshot-19",
3
+ "version": "11.7.2-snapshot-21",
4
4
  "description": "A static rating component",
5
5
  "author": "Instructure, Inc. Engineering and Product Design",
6
6
  "module": "./es/index.js",
@@ -15,23 +15,23 @@
15
15
  "license": "MIT",
16
16
  "dependencies": {
17
17
  "@babel/runtime": "^7.27.6",
18
- "@instructure/console": "11.7.2-snapshot-19",
19
- "@instructure/emotion": "11.7.2-snapshot-19",
20
- "@instructure/shared-types": "11.7.2-snapshot-19",
21
- "@instructure/ui-a11y-content": "11.7.2-snapshot-19",
22
- "@instructure/ui-icons": "11.7.2-snapshot-19",
23
- "@instructure/ui-dom-utils": "11.7.2-snapshot-19",
24
- "@instructure/ui-motion": "11.7.2-snapshot-19",
25
- "@instructure/ui-react-utils": "11.7.2-snapshot-19",
26
- "@instructure/ui-view": "11.7.2-snapshot-19"
18
+ "@instructure/console": "11.7.2-snapshot-21",
19
+ "@instructure/emotion": "11.7.2-snapshot-21",
20
+ "@instructure/shared-types": "11.7.2-snapshot-21",
21
+ "@instructure/ui-a11y-content": "11.7.2-snapshot-21",
22
+ "@instructure/ui-dom-utils": "11.7.2-snapshot-21",
23
+ "@instructure/ui-icons": "11.7.2-snapshot-21",
24
+ "@instructure/ui-motion": "11.7.2-snapshot-21",
25
+ "@instructure/ui-react-utils": "11.7.2-snapshot-21",
26
+ "@instructure/ui-view": "11.7.2-snapshot-21"
27
27
  },
28
28
  "devDependencies": {
29
29
  "@testing-library/jest-dom": "^6.6.3",
30
30
  "@testing-library/react": "15.0.7",
31
31
  "vitest": "^3.2.2",
32
- "@instructure/ui-axe-check": "11.7.2-snapshot-19",
33
- "@instructure/ui-babel-preset": "11.7.2-snapshot-19",
34
- "@instructure/ui-themes": "11.7.2-snapshot-19"
32
+ "@instructure/ui-axe-check": "11.7.2-snapshot-21",
33
+ "@instructure/ui-themes": "11.7.2-snapshot-21",
34
+ "@instructure/ui-babel-preset": "11.7.2-snapshot-21"
35
35
  },
36
36
  "peerDependencies": {
37
37
  "react": ">=18 <=19"
@@ -61,18 +61,18 @@
61
61
  "default": "./es/exports/a.js"
62
62
  },
63
63
  "./v11_7": {
64
- "src": "./src/exports/a.ts",
65
- "types": "./types/exports/a.d.ts",
66
- "import": "./es/exports/a.js",
67
- "require": "./lib/exports/a.js",
68
- "default": "./es/exports/a.js"
64
+ "src": "./src/exports/b.ts",
65
+ "types": "./types/exports/b.d.ts",
66
+ "import": "./es/exports/b.js",
67
+ "require": "./lib/exports/b.js",
68
+ "default": "./es/exports/b.js"
69
69
  },
70
70
  "./latest": {
71
- "src": "./src/exports/a.ts",
72
- "types": "./types/exports/a.d.ts",
73
- "import": "./es/exports/a.js",
74
- "require": "./lib/exports/a.js",
75
- "default": "./es/exports/a.js"
71
+ "src": "./src/exports/b.ts",
72
+ "types": "./types/exports/b.d.ts",
73
+ "import": "./es/exports/b.js",
74
+ "require": "./lib/exports/b.js",
75
+ "default": "./es/exports/b.js"
76
76
  }
77
77
  },
78
78
  "scripts": {
@@ -0,0 +1,104 @@
1
+ ---
2
+ describes: Rating
3
+ ---
4
+
5
+ Rating takes the `valueNow` and `valueMax` props and
6
+ outputs a 3- or 5-star rating. Decimals are rounded to the nearest
7
+ whole number.
8
+
9
+ ### 3- or 5-star ratings
10
+
11
+ Rating defaults to a 3-star rating system. Use `iconCount` to switch
12
+ to a 5-star system. Note how you can use the `formatValueText` prop to
13
+ create readable text for screenreaders that will be outputted in the
14
+ `aria-valuetext` attribute.
15
+
16
+ Note how the second example below has filled stars that animate in. Toggle
17
+ this feature using the `animateFill` prop.
18
+
19
+ ```js
20
+ ---
21
+ type: example
22
+ ---
23
+ <Flex gap="large">
24
+ <Flex gap="x-small">
25
+ <Rating
26
+ formatValueText={function (currentRating, maxRating) {
27
+ return currentRating + ' out of ' + maxRating
28
+ }}
29
+ label="Overall rating of freshman year experience"
30
+ valueNow={68.45}
31
+ valueMax={100}
32
+ />
33
+ <Text>2/3</Text>
34
+ </Flex>
35
+ <Flex gap="x-small">
36
+ <Rating
37
+ animateFill
38
+ formatValueText={function (currentRating, maxRating) {
39
+ return currentRating + ' out of ' + maxRating
40
+ }}
41
+ label="Overall rating of freshman year experience"
42
+ iconCount={5}
43
+ valueNow={68.45}
44
+ valueMax={100}
45
+ />
46
+ <Text>3/5</Text>
47
+ </Flex>
48
+ </Flex>
49
+ ```
50
+
51
+ ### Sizes
52
+
53
+ Choose from `small`, `medium`, or `large`. The `margin` prop has been added to add
54
+ space around the actual rating.
55
+
56
+ ```js
57
+ ---
58
+ type: example
59
+ ---
60
+ <Flex gap="large">
61
+ <Flex>
62
+ <Rating
63
+ label="Product rating"
64
+ size="small"
65
+ iconCount={5}
66
+ valueNow={3.76}
67
+ valueMax={5}
68
+ margin="small"/>
69
+ <Text>4/5</Text>
70
+ </Flex>
71
+ <Flex>
72
+ <Rating
73
+ label="Overall rating of college experience"
74
+ iconCount={5}
75
+ valueNow={30}
76
+ valueMax={100}
77
+ margin="small"
78
+ />
79
+ <Text>2/5</Text>
80
+ </Flex>
81
+ <Flex>
82
+ <Rating
83
+ animateFill
84
+ label="Rating of professor"
85
+ size="large"
86
+ iconCount={5}
87
+ valueNow={8}
88
+ valueMax={8}
89
+ margin="small"/>
90
+ <Text>5/5</Text>
91
+ </Flex>
92
+ </Flex>
93
+ ```
94
+
95
+ ```js
96
+ ---
97
+ type: embed
98
+ ---
99
+ <Guidelines>
100
+ <Figure recommendation="a11y" title="Accessibility">
101
+ <Figure.Item>Display a textual value for the rating to meet WCAG 2.1 Use of Color standards. "Non-text information within controls that uses a change of hue alone to convey the value or state of an input, such as a 1-5 star indicator where state depends on whether it's filled or empty is likely to fail the Use of color criterion"</Figure.Item>
102
+ </Figure>
103
+ </Guidelines>
104
+ ```
@@ -0,0 +1,131 @@
1
+ /*
2
+ * The MIT License (MIT)
3
+ *
4
+ * Copyright (c) 2015 - present Instructure, Inc.
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+
25
+ import { Component } from 'react'
26
+
27
+ import { View } from '@instructure/ui-view/latest'
28
+ import { ScreenReaderContent } from '@instructure/ui-a11y-content'
29
+ import { omitProps } from '@instructure/ui-react-utils'
30
+ import { RatingIcon } from '../../RatingIcon/v2'
31
+ import { withStyle } from '@instructure/emotion'
32
+ import generateStyle from './styles'
33
+ import type { RatingProps } from './props'
34
+ import { allowedProps } from './props'
35
+
36
+ /**
37
+ ---
38
+ category: components
39
+ ---
40
+ **/
41
+ @withStyle(generateStyle)
42
+ class Rating extends Component<RatingProps> {
43
+ static readonly componentId = 'Rating'
44
+
45
+ static allowedProps = allowedProps
46
+
47
+ static defaultProps = {
48
+ animateFill: false,
49
+ formatValueText: (filled: number, iconCount: number) =>
50
+ `${filled} / ${iconCount}`,
51
+ iconCount: 3,
52
+ size: 'medium',
53
+ valueNow: 0
54
+ }
55
+
56
+ static Icon = RatingIcon
57
+
58
+ ref: Element | null = null
59
+
60
+ handleRef = (el: Element | null) => {
61
+ this.ref = el
62
+ }
63
+
64
+ componentDidMount() {
65
+ this.props.makeStyles?.()
66
+ }
67
+
68
+ componentDidUpdate() {
69
+ this.props.makeStyles?.()
70
+ }
71
+
72
+ get filled() {
73
+ const { valueNow, iconCount, valueMax } = this.props
74
+
75
+ // prevent divide by zero errors
76
+ const max = !!valueMax && valueMax > 0 ? valueMax : iconCount!
77
+
78
+ const filledIcons = Math.round((valueNow! * iconCount!) / max)
79
+
80
+ // Handle edge case where valueNow is greater than valueMax
81
+ if (filledIcons > iconCount!) {
82
+ return iconCount!
83
+ } else {
84
+ return filledIcons
85
+ }
86
+ }
87
+
88
+ get empty() {
89
+ return this.props.iconCount! - this.filled!
90
+ }
91
+
92
+ render() {
93
+ const { iconCount, animateFill, size, margin, label, formatValueText } =
94
+ this.props
95
+
96
+ const valueText = label + ' ' + formatValueText?.(this.filled, iconCount!)
97
+
98
+ const passthroughProps = View.omitViewProps(
99
+ omitProps(this.props, Rating.allowedProps),
100
+ Rating
101
+ )
102
+
103
+ return (
104
+ <View
105
+ {...passthroughProps}
106
+ css={this.props.styles?.rating}
107
+ margin={margin}
108
+ display="inline-block"
109
+ elementRef={this.handleRef}
110
+ data-cid="Rating"
111
+ >
112
+ <ScreenReaderContent>{valueText}</ScreenReaderContent>
113
+ {[...Array(this.filled)].map((_x, i) => (
114
+ <RatingIcon
115
+ key={i + 1}
116
+ filled
117
+ animateFill={animateFill}
118
+ animationDelay={animateFill ? (i + 1) * 200 : undefined}
119
+ size={size}
120
+ />
121
+ ))}
122
+ {[...Array(this.empty)].map((_x, i) => (
123
+ <RatingIcon key={i + 1} size={size} />
124
+ ))}
125
+ </View>
126
+ )
127
+ }
128
+ }
129
+
130
+ export default Rating
131
+ export { Rating }
@@ -0,0 +1,90 @@
1
+ /*
2
+ * The MIT License (MIT)
3
+ *
4
+ * Copyright (c) 2015 - present Instructure, Inc.
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+
25
+ import type {
26
+ Spacing,
27
+ WithStyleProps,
28
+ ComponentStyle
29
+ } from '@instructure/emotion'
30
+ import type { OtherHTMLAttributes } from '@instructure/shared-types'
31
+
32
+ type RatingOwnProps = {
33
+ /**
34
+ * A label is required for accessibility
35
+ */
36
+ label: string
37
+ /**
38
+ * A function that returns the current value formatted for screen readers
39
+ */
40
+ formatValueText?: (filled: number, iconCount: number) => string
41
+ /**
42
+ * Choose from a 0-3 or 0-5 rating system
43
+ */
44
+ iconCount?: 3 | 5
45
+ /**
46
+ * Choose from different rating icon sizes
47
+ */
48
+ size?: 'small' | 'medium' | 'large'
49
+ /**
50
+ * The maximum rating (defaults to iconCount)
51
+ */
52
+ valueMax?: number
53
+ /**
54
+ * The current rating
55
+ */
56
+ valueNow?: number
57
+ /**
58
+ * Set to make the icons animate when they become filled
59
+ */
60
+ animateFill?: boolean
61
+ /**
62
+ * Valid values are `0`, `none`, `auto`, `xxx-small`, `xx-small`, `x-small`,
63
+ * `small`, `medium`, `large`, `x-large`, `xx-large`. Apply these values via
64
+ * familiar CSS-like shorthand. For example: `margin="small auto large"`.
65
+ */
66
+ margin?: Spacing
67
+ }
68
+
69
+ type PropKeys = keyof RatingOwnProps
70
+
71
+ type AllowedPropKeys = Readonly<Array<PropKeys>>
72
+
73
+ type RatingProps = RatingOwnProps &
74
+ WithStyleProps<null, RatingStyle> &
75
+ OtherHTMLAttributes<RatingOwnProps>
76
+
77
+ type RatingStyle = ComponentStyle<'rating'>
78
+ const allowedProps: AllowedPropKeys = [
79
+ 'label',
80
+ 'formatValueText',
81
+ 'iconCount',
82
+ 'size',
83
+ 'valueMax',
84
+ 'valueNow',
85
+ 'animateFill',
86
+ 'margin'
87
+ ]
88
+
89
+ export type { RatingProps, RatingStyle }
90
+ export { allowedProps }