@redocly/theme 0.44.9 → 0.45.0

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,3 +1,4 @@
1
+ import { ReasonsSettingsSchema } from '@redocly/config';
1
2
  export declare enum MOOD_STATES {
2
3
  SATISFIED = "satisfied",
3
4
  NEUTRAL = "neutral",
@@ -18,11 +19,10 @@ export type MoodProps = {
18
19
  neutralLabel?: string;
19
20
  dissatisfiedLabel?: string;
20
21
  };
21
- reasons?: {
22
- hide?: boolean;
23
- label?: string;
24
- component?: string;
25
- items: string[];
22
+ reasons?: ReasonsSettingsSchema & {
23
+ satisfied?: ReasonsSettingsSchema;
24
+ neutral?: ReasonsSettingsSchema;
25
+ dissatisfied?: ReasonsSettingsSchema;
26
26
  };
27
27
  };
28
28
  className?: string;
@@ -53,6 +53,16 @@ function Mood({ settings, onSubmit, className }) {
53
53
  const [reasons, setReasons] = React.useState([]);
54
54
  const { useTranslate } = (0, hooks_1.useThemeHooks)();
55
55
  const { translate } = useTranslate();
56
+ const checkIfShouldDisplayReasons = (score) => {
57
+ if (!score || !reasonsSettings) {
58
+ return false;
59
+ }
60
+ const scoreSpecificReasonsSettings = reasonsSettings[score];
61
+ if (typeof (scoreSpecificReasonsSettings === null || scoreSpecificReasonsSettings === void 0 ? void 0 : scoreSpecificReasonsSettings.hide) === 'undefined') {
62
+ return !reasonsSettings.hide;
63
+ }
64
+ return !(scoreSpecificReasonsSettings === null || scoreSpecificReasonsSettings === void 0 ? void 0 : scoreSpecificReasonsSettings.hide);
65
+ };
56
66
  const renderCommentLabel = (score) => {
57
67
  if (!commentSettings || (commentSettings === null || commentSettings === void 0 ? void 0 : commentSettings.hide) === true)
58
68
  return '';
@@ -70,7 +80,27 @@ function Mood({ settings, onSubmit, className }) {
70
80
  return translate('feedback.settings.comment.satisfiedLabel', 'What can we improve?');
71
81
  }
72
82
  };
73
- const displayReasons = !!(score && reasonsSettings && !reasonsSettings.hide);
83
+ const renderReasonsLabel = (score) => {
84
+ var _a;
85
+ if (!reasonsSettings || (reasonsSettings === null || reasonsSettings === void 0 ? void 0 : reasonsSettings.hide) === true)
86
+ return '';
87
+ return (_a = reasonsSettings[score]) === null || _a === void 0 ? void 0 : _a.label;
88
+ };
89
+ const resolveReasonsItems = (score) => {
90
+ var _a, _b;
91
+ if (!reasonsSettings) {
92
+ return [];
93
+ }
94
+ const defaultReasonsItems = (reasonsSettings === null || reasonsSettings === void 0 ? void 0 : reasonsSettings.items) || [];
95
+ return defaultReasonsItems.concat((_b = (_a = reasonsSettings[score]) === null || _a === void 0 ? void 0 : _a.items) !== null && _b !== void 0 ? _b : []);
96
+ };
97
+ const resolveReasonsComponent = (score) => {
98
+ var _a;
99
+ if (!reasonsSettings || (reasonsSettings === null || reasonsSettings === void 0 ? void 0 : reasonsSettings.hide) === true)
100
+ return 'checkbox';
101
+ return (_a = reasonsSettings[score]) === null || _a === void 0 ? void 0 : _a.component;
102
+ };
103
+ const displayReasons = checkIfShouldDisplayReasons(score);
74
104
  const displayComment = !!(score && !(commentSettings === null || commentSettings === void 0 ? void 0 : commentSettings.hide));
75
105
  const displaySubmitBnt = !!(score && (displayReasons || displayComment));
76
106
  const onSubmitMoodForm = () => {
@@ -113,9 +143,9 @@ function Mood({ settings, onSubmit, className }) {
113
143
  }, icon: React.createElement(FaceSatisfiedIcon_1.FaceSatisfiedIcon, null) }))),
114
144
  (displayReasons || displayComment) && (React.createElement(StyledFormOptionalFields, null,
115
145
  displayReasons && (React.createElement(Reasons_1.Reasons, { settings: {
116
- label: reasonsSettings === null || reasonsSettings === void 0 ? void 0 : reasonsSettings.label,
117
- items: (reasonsSettings === null || reasonsSettings === void 0 ? void 0 : reasonsSettings.items) || [],
118
- component: reasonsSettings === null || reasonsSettings === void 0 ? void 0 : reasonsSettings.component,
146
+ label: renderReasonsLabel(score),
147
+ items: resolveReasonsItems(score),
148
+ component: resolveReasonsComponent(score),
119
149
  }, onChange: setReasons })),
120
150
  displayComment && (React.createElement(Comment_1.Comment, { standAlone: false, onSubmit: ({ comment }) => setComment(comment), settings: { label: renderCommentLabel(score) } })))),
121
151
  displaySubmitBnt && (React.createElement(ButtonsContainer, null,
@@ -1,3 +1,4 @@
1
+ import { ReasonsSettingsSchema } from '@redocly/config';
1
2
  export type SentimentProps = {
2
3
  onSubmit: (value: {
3
4
  score: number;
@@ -12,11 +13,9 @@ export type SentimentProps = {
12
13
  likeLabel?: string;
13
14
  dislikeLabel?: string;
14
15
  };
15
- reasons?: {
16
- hide?: boolean;
17
- label?: string;
18
- component?: string;
19
- items: string[];
16
+ reasons?: ReasonsSettingsSchema & {
17
+ like?: ReasonsSettingsSchema;
18
+ dislike?: ReasonsSettingsSchema;
20
19
  };
21
20
  };
22
21
  className?: string;
@@ -45,7 +45,27 @@ function Sentiment({ settings, onSubmit, className }) {
45
45
  const [reasons, setReasons] = React.useState([]);
46
46
  const { useTranslate } = (0, hooks_1.useThemeHooks)();
47
47
  const { translate } = useTranslate();
48
- const displayReasons = !!(score && reasonsSettings && !reasonsSettings.hide);
48
+ const getScoreSpecificReasonSettings = (score) => {
49
+ switch (score) {
50
+ case 1:
51
+ return reasonsSettings === null || reasonsSettings === void 0 ? void 0 : reasonsSettings.like;
52
+ case -1:
53
+ return reasonsSettings === null || reasonsSettings === void 0 ? void 0 : reasonsSettings.dislike;
54
+ default:
55
+ return reasonsSettings;
56
+ }
57
+ };
58
+ const checkIfShouldDisplayReasons = (score) => {
59
+ if (!score || !reasonsSettings) {
60
+ return false;
61
+ }
62
+ const scoreSpecificReasonsSettings = getScoreSpecificReasonSettings(score);
63
+ if (typeof (scoreSpecificReasonsSettings === null || scoreSpecificReasonsSettings === void 0 ? void 0 : scoreSpecificReasonsSettings.hide) === 'undefined') {
64
+ return !reasonsSettings.hide;
65
+ }
66
+ return !(scoreSpecificReasonsSettings === null || scoreSpecificReasonsSettings === void 0 ? void 0 : scoreSpecificReasonsSettings.hide);
67
+ };
68
+ const displayReasons = checkIfShouldDisplayReasons(score);
49
69
  const displayComment = !!(score && !(commentSettings === null || commentSettings === void 0 ? void 0 : commentSettings.hide));
50
70
  const displaySubmitBnt = !!(score && (displayReasons || displayComment));
51
71
  const commentLabel = score === 1
@@ -53,6 +73,20 @@ function Sentiment({ settings, onSubmit, className }) {
53
73
  translate('feedback.settings.comment.likeLabel', 'What was most helpful?')
54
74
  : (commentSettings && commentSettings.dislikeLabel) ||
55
75
  translate('feedback.settings.comment.dislikeLabel', 'What can we improve?');
76
+ const renderReasonsLabel = (score) => {
77
+ const scoreSpecificReasonSettings = getScoreSpecificReasonSettings(score);
78
+ return scoreSpecificReasonSettings === null || scoreSpecificReasonSettings === void 0 ? void 0 : scoreSpecificReasonSettings.label;
79
+ };
80
+ const resolveReasonsItems = (score) => {
81
+ var _a;
82
+ const defaultReasonsItems = (reasonsSettings === null || reasonsSettings === void 0 ? void 0 : reasonsSettings.items) || [];
83
+ const scoreSpecificReasonSettings = getScoreSpecificReasonSettings(score);
84
+ return defaultReasonsItems.concat((_a = scoreSpecificReasonSettings === null || scoreSpecificReasonSettings === void 0 ? void 0 : scoreSpecificReasonSettings.items) !== null && _a !== void 0 ? _a : []);
85
+ };
86
+ const resolveReasonsComponent = (score) => {
87
+ const scoreSpecificReasonSettings = getScoreSpecificReasonSettings(score);
88
+ return scoreSpecificReasonSettings === null || scoreSpecificReasonSettings === void 0 ? void 0 : scoreSpecificReasonSettings.component;
89
+ };
56
90
  const onSubmitSentimentForm = () => {
57
91
  onSubmit({
58
92
  score,
@@ -90,9 +124,9 @@ function Sentiment({ settings, onSubmit, className }) {
90
124
  }, "aria-label": "Dislike", icon: React.createElement(ThumbDownIcon_1.ThumbDownIcon, null) }))),
91
125
  (displayReasons || displayComment) && (React.createElement(StyledFormOptionalFields, null,
92
126
  displayReasons && (React.createElement(Reasons_1.Reasons, { settings: {
93
- label: reasonsSettings === null || reasonsSettings === void 0 ? void 0 : reasonsSettings.label,
94
- items: (reasonsSettings === null || reasonsSettings === void 0 ? void 0 : reasonsSettings.items) || [],
95
- component: reasonsSettings === null || reasonsSettings === void 0 ? void 0 : reasonsSettings.component,
127
+ label: renderReasonsLabel(score),
128
+ items: resolveReasonsItems(score),
129
+ component: resolveReasonsComponent(score),
96
130
  }, onChange: setReasons })),
97
131
  displayComment && (React.createElement(Comment_1.Comment, { standAlone: false, onSubmit: ({ comment }) => setComment(comment), settings: { label: commentLabel } })))),
98
132
  displaySubmitBnt && (React.createElement(ButtonsContainer, null,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@redocly/theme",
3
- "version": "0.44.9",
3
+ "version": "0.45.0",
4
4
  "description": "Shared UI components lib",
5
5
  "keywords": [
6
6
  "theme",
@@ -81,7 +81,7 @@
81
81
  "timeago.js": "4.0.2",
82
82
  "i18next": "22.4.15",
83
83
  "nprogress": "0.2.0",
84
- "@redocly/config": "0.15.0"
84
+ "@redocly/config": "0.16.0"
85
85
  },
86
86
  "scripts": {
87
87
  "watch": "tsc -p tsconfig.build.json && (concurrently \"tsc -w -p tsconfig.build.json\" \"tsc-alias -w -p tsconfig.build.json\")",
@@ -1,6 +1,7 @@
1
1
  import * as React from 'react';
2
2
  import { useEffect } from 'react';
3
3
  import styled from 'styled-components';
4
+ import { ReasonsSettingsSchema } from '@redocly/config';
4
5
 
5
6
  import type { ReasonsProps } from '@redocly/theme/components/Feedback/Reasons';
6
7
 
@@ -30,11 +31,10 @@ export type MoodProps = {
30
31
  neutralLabel?: string;
31
32
  dissatisfiedLabel?: string;
32
33
  };
33
- reasons?: {
34
- hide?: boolean;
35
- label?: string;
36
- component?: string;
37
- items: string[];
34
+ reasons?: ReasonsSettingsSchema & {
35
+ satisfied?: ReasonsSettingsSchema;
36
+ neutral?: ReasonsSettingsSchema;
37
+ dissatisfied?: ReasonsSettingsSchema;
38
38
  };
39
39
  };
40
40
  className?: string;
@@ -49,6 +49,20 @@ export function Mood({ settings, onSubmit, className }: MoodProps): JSX.Element
49
49
  const { useTranslate } = useThemeHooks();
50
50
  const { translate } = useTranslate();
51
51
 
52
+ const checkIfShouldDisplayReasons = (score: string) => {
53
+ if (!score || !reasonsSettings) {
54
+ return false;
55
+ }
56
+
57
+ const scoreSpecificReasonsSettings = reasonsSettings[score as MOOD_STATES];
58
+
59
+ if (typeof scoreSpecificReasonsSettings?.hide === 'undefined') {
60
+ return !reasonsSettings.hide;
61
+ }
62
+
63
+ return !scoreSpecificReasonsSettings?.hide;
64
+ };
65
+
52
66
  const renderCommentLabel = (score: string) => {
53
67
  if (!commentSettings || commentSettings?.hide === true) return '';
54
68
 
@@ -73,7 +87,29 @@ export function Mood({ settings, onSubmit, className }: MoodProps): JSX.Element
73
87
  }
74
88
  };
75
89
 
76
- const displayReasons = !!(score && reasonsSettings && !reasonsSettings.hide);
90
+ const renderReasonsLabel = (score: string) => {
91
+ if (!reasonsSettings || reasonsSettings?.hide === true) return '';
92
+
93
+ return reasonsSettings[score as MOOD_STATES]?.label;
94
+ };
95
+
96
+ const resolveReasonsItems = (score: string) => {
97
+ if (!reasonsSettings) {
98
+ return [];
99
+ }
100
+
101
+ const defaultReasonsItems = reasonsSettings?.items || [];
102
+
103
+ return defaultReasonsItems.concat(reasonsSettings[score as MOOD_STATES]?.items ?? []);
104
+ };
105
+
106
+ const resolveReasonsComponent = (score: string) => {
107
+ if (!reasonsSettings || reasonsSettings?.hide === true) return 'checkbox';
108
+
109
+ return reasonsSettings[score as MOOD_STATES]?.component;
110
+ };
111
+
112
+ const displayReasons = checkIfShouldDisplayReasons(score);
77
113
  const displayComment = !!(score && !commentSettings?.hide);
78
114
  const displaySubmitBnt = !!(score && (displayReasons || displayComment));
79
115
 
@@ -160,9 +196,9 @@ export function Mood({ settings, onSubmit, className }: MoodProps): JSX.Element
160
196
  {displayReasons && (
161
197
  <Reasons
162
198
  settings={{
163
- label: reasonsSettings?.label,
164
- items: reasonsSettings?.items || [],
165
- component: reasonsSettings?.component,
199
+ label: renderReasonsLabel(score),
200
+ items: resolveReasonsItems(score),
201
+ component: resolveReasonsComponent(score),
166
202
  }}
167
203
  onChange={setReasons}
168
204
  />
@@ -1,6 +1,7 @@
1
1
  import * as React from 'react';
2
2
  import { useEffect } from 'react';
3
3
  import styled from 'styled-components';
4
+ import { ReasonsSettingsSchema } from '@redocly/config';
4
5
 
5
6
  import type { ReasonsProps } from '@redocly/theme/components/Feedback/Reasons';
6
7
 
@@ -22,11 +23,9 @@ export type SentimentProps = {
22
23
  likeLabel?: string;
23
24
  dislikeLabel?: string;
24
25
  };
25
- reasons?: {
26
- hide?: boolean;
27
- label?: string;
28
- component?: string;
29
- items: string[];
26
+ reasons?: ReasonsSettingsSchema & {
27
+ like?: ReasonsSettingsSchema;
28
+ dislike?: ReasonsSettingsSchema;
30
29
  };
31
30
  };
32
31
  className?: string;
@@ -41,7 +40,32 @@ export function Sentiment({ settings, onSubmit, className }: SentimentProps): JS
41
40
  const { useTranslate } = useThemeHooks();
42
41
  const { translate } = useTranslate();
43
42
 
44
- const displayReasons = !!(score && reasonsSettings && !reasonsSettings.hide);
43
+ const getScoreSpecificReasonSettings = (score: number) => {
44
+ switch (score) {
45
+ case 1:
46
+ return reasonsSettings?.like;
47
+ case -1:
48
+ return reasonsSettings?.dislike;
49
+ default:
50
+ return reasonsSettings;
51
+ }
52
+ };
53
+
54
+ const checkIfShouldDisplayReasons = (score: number) => {
55
+ if (!score || !reasonsSettings) {
56
+ return false;
57
+ }
58
+
59
+ const scoreSpecificReasonsSettings = getScoreSpecificReasonSettings(score);
60
+
61
+ if (typeof scoreSpecificReasonsSettings?.hide === 'undefined') {
62
+ return !reasonsSettings.hide;
63
+ }
64
+
65
+ return !scoreSpecificReasonsSettings?.hide;
66
+ };
67
+
68
+ const displayReasons = checkIfShouldDisplayReasons(score);
45
69
  const displayComment = !!(score && !commentSettings?.hide);
46
70
  const displaySubmitBnt = !!(score && (displayReasons || displayComment));
47
71
 
@@ -52,6 +76,25 @@ export function Sentiment({ settings, onSubmit, className }: SentimentProps): JS
52
76
  : (commentSettings && commentSettings.dislikeLabel) ||
53
77
  translate('feedback.settings.comment.dislikeLabel', 'What can we improve?');
54
78
 
79
+ const renderReasonsLabel = (score: number) => {
80
+ const scoreSpecificReasonSettings = getScoreSpecificReasonSettings(score);
81
+
82
+ return scoreSpecificReasonSettings?.label;
83
+ };
84
+
85
+ const resolveReasonsItems = (score: number) => {
86
+ const defaultReasonsItems = reasonsSettings?.items || [];
87
+ const scoreSpecificReasonSettings = getScoreSpecificReasonSettings(score);
88
+
89
+ return defaultReasonsItems.concat(scoreSpecificReasonSettings?.items ?? []);
90
+ };
91
+
92
+ const resolveReasonsComponent = (score: number) => {
93
+ const scoreSpecificReasonSettings = getScoreSpecificReasonSettings(score);
94
+
95
+ return scoreSpecificReasonSettings?.component;
96
+ };
97
+
55
98
  const onSubmitSentimentForm = () => {
56
99
  onSubmit({
57
100
  score,
@@ -126,9 +169,9 @@ export function Sentiment({ settings, onSubmit, className }: SentimentProps): JS
126
169
  {displayReasons && (
127
170
  <Reasons
128
171
  settings={{
129
- label: reasonsSettings?.label,
130
- items: reasonsSettings?.items || [],
131
- component: reasonsSettings?.component,
172
+ label: renderReasonsLabel(score),
173
+ items: resolveReasonsItems(score),
174
+ component: resolveReasonsComponent(score),
132
175
  }}
133
176
  onChange={setReasons}
134
177
  />