@plone/volto 19.0.0-alpha.11 → 19.0.0-alpha.12

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 (58) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/locales/ca/LC_MESSAGES/volto.po +10 -0
  3. package/locales/de/LC_MESSAGES/volto.po +10 -0
  4. package/locales/en/LC_MESSAGES/volto.po +10 -0
  5. package/locales/es/LC_MESSAGES/volto.po +10 -0
  6. package/locales/eu/LC_MESSAGES/volto.po +10 -0
  7. package/locales/fi/LC_MESSAGES/volto.po +10 -0
  8. package/locales/fr/LC_MESSAGES/volto.po +10 -0
  9. package/locales/hi/LC_MESSAGES/volto.po +10 -0
  10. package/locales/it/LC_MESSAGES/volto.po +10 -0
  11. package/locales/ja/LC_MESSAGES/volto.po +10 -0
  12. package/locales/nl/LC_MESSAGES/volto.po +10 -0
  13. package/locales/pt/LC_MESSAGES/volto.po +10 -0
  14. package/locales/pt_BR/LC_MESSAGES/volto.po +10 -0
  15. package/locales/ro/LC_MESSAGES/volto.po +10 -0
  16. package/locales/ru/LC_MESSAGES/volto.po +10 -0
  17. package/locales/volto.pot +11 -1
  18. package/locales/zh_CN/LC_MESSAGES/volto.po +10 -0
  19. package/package.json +5 -4
  20. package/src/components/manage/Controlpanels/Relations/Relations.jsx +1 -1
  21. package/src/components/manage/Widgets/AlignWidget.stories.jsx +9 -0
  22. package/src/components/manage/Widgets/AlignWidget.test.tsx +95 -0
  23. package/src/components/manage/Widgets/{AlignWidget.jsx → AlignWidget.tsx} +23 -7
  24. package/src/components/manage/Widgets/BlockAlignment.stories.tsx +104 -0
  25. package/src/components/manage/Widgets/BlockAlignment.test.tsx +104 -0
  26. package/src/components/manage/Widgets/BlockAlignment.tsx +88 -0
  27. package/src/components/manage/Widgets/BlockWidth.stories.tsx +69 -0
  28. package/src/components/manage/Widgets/BlockWidth.test.tsx +62 -0
  29. package/src/components/manage/Widgets/BlockWidth.tsx +101 -0
  30. package/src/components/manage/Widgets/ButtonsWidget.stories.jsx +61 -0
  31. package/src/components/manage/Widgets/ButtonsWidget.test.tsx +138 -0
  32. package/src/components/manage/Widgets/ButtonsWidget.tsx +176 -0
  33. package/src/components/manage/Widgets/Size.stories.tsx +69 -0
  34. package/src/components/manage/Widgets/Size.test.tsx +59 -0
  35. package/src/components/manage/Widgets/Size.tsx +78 -0
  36. package/src/components/manage/Widgets/index.tsx +21 -0
  37. package/src/components/theme/App/App.jsx +2 -0
  38. package/src/components/theme/InjectPloneComponentsCSS/InjectPloneComponentsCSS.tsx +7 -0
  39. package/src/config/Widgets.jsx +7 -0
  40. package/src/config/slots.js +19 -0
  41. package/theme/themes/pastanaga/extras/widgets.less +45 -0
  42. package/types/components/manage/Widgets/AlignWidget.d.ts +8 -10
  43. package/types/components/manage/Widgets/AlignWidget.stories.d.ts +1 -0
  44. package/types/components/manage/Widgets/BlockAlignment.d.ts +7 -0
  45. package/types/components/manage/Widgets/BlockAlignment.stories.d.ts +8 -0
  46. package/types/components/manage/Widgets/BlockWidth.d.ts +7 -0
  47. package/types/components/manage/Widgets/BlockWidth.stories.d.ts +6 -0
  48. package/types/components/manage/Widgets/ButtonsWidget.d.ts +48 -1
  49. package/types/components/manage/Widgets/ButtonsWidget.stories.d.ts +3 -0
  50. package/types/components/manage/Widgets/Size.d.ts +7 -0
  51. package/types/components/manage/Widgets/Size.stories.d.ts +6 -0
  52. package/types/components/manage/Widgets/index.d.ts +7 -2
  53. package/types/components/theme/InjectPloneComponentsCSS/InjectPloneComponentsCSS.d.ts +3 -0
  54. package/types/config/Widgets.d.ts +6 -0
  55. package/types/config/slots.d.ts +7 -0
  56. package/src/components/manage/Widgets/AlignWidget.test.jsx +0 -59
  57. package/src/components/manage/Widgets/ButtonsWidget.jsx +0 -41
  58. package/src/components/manage/Widgets/ButtonsWidget.test.jsx +0 -70
@@ -0,0 +1,62 @@
1
+ import React from 'react';
2
+ import { describe, it, expect, vi } from 'vitest';
3
+ import { render, screen, fireEvent } from '@testing-library/react';
4
+ import configureStore from 'redux-mock-store';
5
+ import { Provider } from 'react-intl-redux';
6
+
7
+ import BlockWidth from './BlockWidth';
8
+ import type { ButtonsWidgetProps } from './ButtonsWidget';
9
+
10
+ const mockStore = configureStore();
11
+
12
+ const renderWidget = (props: Partial<ButtonsWidgetProps> = {}) => {
13
+ const onChange = props.onChange ?? vi.fn();
14
+ const widgetProps: ButtonsWidgetProps = {
15
+ id: 'blockWidth',
16
+ title: 'Block width',
17
+ fieldSet: 'default',
18
+ onChange,
19
+ value: undefined,
20
+ default: undefined,
21
+ disabled: false,
22
+ isDisabled: false,
23
+ ...props,
24
+ };
25
+
26
+ return {
27
+ onChange,
28
+ ...render(
29
+ <Provider
30
+ store={mockStore({
31
+ intl: {
32
+ locale: 'en',
33
+ messages: {},
34
+ },
35
+ })}
36
+ >
37
+ <BlockWidth {...widgetProps} />
38
+ </Provider>,
39
+ ),
40
+ };
41
+ };
42
+
43
+ describe('BlockWidth', () => {
44
+ it('renders the default action buttons provided by the widget', () => {
45
+ renderWidget();
46
+
47
+ expect(screen.getByRole('radio', { name: 'Narrow' })).toBeInTheDocument();
48
+ expect(screen.getByRole('radio', { name: 'Default' })).toBeInTheDocument();
49
+ expect(screen.getByRole('radio', { name: 'Layout' })).toBeInTheDocument();
50
+ expect(screen.getByRole('radio', { name: 'Full' })).toBeInTheDocument();
51
+ });
52
+
53
+ it('emits the correct value for the default actions', () => {
54
+ const { onChange } = renderWidget();
55
+
56
+ fireEvent.click(screen.getByRole('radio', { name: 'Layout' }));
57
+
58
+ expect(onChange).toHaveBeenCalledWith('blockWidth', {
59
+ '--block-width': 'var(--layout-container-width)',
60
+ });
61
+ });
62
+ });
@@ -0,0 +1,101 @@
1
+ import React from 'react';
2
+ import { defineMessages, useIntl } from 'react-intl';
3
+ import type { IntlShape } from 'react-intl';
4
+ import ButtonsWidget, {
5
+ type ActionInfo,
6
+ type ButtonsWidgetProps,
7
+ } from './ButtonsWidget';
8
+ import imageFitSVG from '@plone/volto/icons/image-fit.svg';
9
+ import imageNarrowSVG from '@plone/volto/icons/image-narrow.svg';
10
+ import imageWideSVG from '@plone/volto/icons/image-wide.svg';
11
+ import imageFullSVG from '@plone/volto/icons/image-full.svg';
12
+ import type { StyleDefinition } from '@plone/types';
13
+
14
+ const messages = defineMessages({
15
+ narrow: {
16
+ id: 'Narrow',
17
+ defaultMessage: 'Narrow',
18
+ },
19
+ default: {
20
+ id: 'Default',
21
+ defaultMessage: 'Default',
22
+ },
23
+ layout: {
24
+ id: 'Layout',
25
+ defaultMessage: 'Layout',
26
+ },
27
+ full: {
28
+ id: 'Full',
29
+ defaultMessage: 'Full',
30
+ },
31
+ });
32
+
33
+ export const defaultActionsInfo = ({
34
+ intl,
35
+ }: {
36
+ intl: IntlShape;
37
+ }): Record<string, ActionInfo> => ({
38
+ narrow: [imageNarrowSVG, intl.formatMessage(messages.narrow)],
39
+ default: [imageFitSVG, intl.formatMessage(messages.default)],
40
+ layout: [imageWideSVG, intl.formatMessage(messages.layout)],
41
+ full: [imageFullSVG, intl.formatMessage(messages.full)],
42
+ });
43
+
44
+ const DEFAULT_ACTIONS: StyleDefinition[] = [
45
+ {
46
+ style: {
47
+ '--block-width': 'var(--narrow-container-width)',
48
+ },
49
+ name: 'narrow',
50
+ label: 'Narrow',
51
+ },
52
+ {
53
+ style: {
54
+ '--block-width': 'var(--default-container-width)',
55
+ },
56
+ name: 'default',
57
+ label: 'Default',
58
+ },
59
+ {
60
+ style: {
61
+ '--block-width': 'var(--layout-container-width)',
62
+ },
63
+ name: 'layout',
64
+ label: 'Layout',
65
+ },
66
+ {
67
+ style: {
68
+ '--block-width': 'unset',
69
+ },
70
+ name: 'full',
71
+ label: 'Full',
72
+ },
73
+ ];
74
+
75
+ const BlockWidthWidget = (props: ButtonsWidgetProps) => {
76
+ const intl = useIntl();
77
+
78
+ const { actions = DEFAULT_ACTIONS, actionsInfoMap, filterActions } = props;
79
+ const filteredActions =
80
+ filterActions && filterActions.length > 0
81
+ ? actions.filter((action) => {
82
+ const actionName = typeof action === 'string' ? action : action.name;
83
+ return filterActions.includes(actionName);
84
+ })
85
+ : actions;
86
+
87
+ const actionsInfo = {
88
+ ...defaultActionsInfo({ intl }),
89
+ ...actionsInfoMap,
90
+ };
91
+
92
+ return (
93
+ <ButtonsWidget
94
+ {...props}
95
+ actions={filteredActions}
96
+ actionsInfoMap={actionsInfo}
97
+ />
98
+ );
99
+ };
100
+
101
+ export default BlockWidthWidget;
@@ -21,6 +21,67 @@ export const Buttons = WidgetStory.bind({
21
21
  widget: ButtonsWidget,
22
22
  });
23
23
 
24
+ export const ButtonsWithDefaultValue = WidgetStory.bind({
25
+ props: {
26
+ id: 'buttons-default',
27
+ title: 'Buttons with Default Value',
28
+ default: 'centered',
29
+ actions: ['left', 'right', 'centered'],
30
+ actionsInfoMap: {
31
+ left: [textLeftSVG, 'Text Left'],
32
+ right: [textRightSVG, 'Text Right'],
33
+ centered: [textCenteredSVG, 'Text Centered'],
34
+ },
35
+ },
36
+ widget: ButtonsWidget,
37
+ });
38
+
39
+ export const ButtonsWithStyleDefinitions = WidgetStory.bind({
40
+ props: {
41
+ id: 'buttons-style-definitions',
42
+ title: 'Buttons with Style Definitions',
43
+ actions: [
44
+ {
45
+ name: 'small',
46
+ label: 'Small',
47
+ style: { '--button-size': 'small' },
48
+ },
49
+ {
50
+ name: 'medium',
51
+ label: 'Medium',
52
+ style: { '--button-size': 'medium' },
53
+ },
54
+ {
55
+ name: 'large',
56
+ label: 'Large',
57
+ style: { '--button-size': 'large' },
58
+ },
59
+ ],
60
+ actionsInfoMap: {
61
+ small: ['S', 'Small Buttons'],
62
+ medium: ['M', 'Medium Buttons'],
63
+ large: ['L', 'Large Buttons'],
64
+ },
65
+ },
66
+ widget: ButtonsWidget,
67
+ });
68
+
69
+ export const ButtonsFiltered = WidgetStory.bind({
70
+ props: {
71
+ id: 'buttons-filtered',
72
+ title: 'Filtered Buttons',
73
+ actions: ['left', 'right', 'centered', 'justified'],
74
+ filterActions: ['centered', 'justified'],
75
+ actionsInfoMap: {
76
+ left: [textLeftSVG, 'Text Left'],
77
+ right: [textRightSVG, 'Text Right'],
78
+ justified: [textJustifiedSVG, 'Text Justified'],
79
+ centered: [textCenteredSVG, 'Text Centered'],
80
+ },
81
+ },
82
+ widget: ButtonsWidget,
83
+ });
84
+
24
85
  export default {
25
86
  title: 'Edit Widgets/Buttons',
26
87
  component: ButtonsWidget,
@@ -0,0 +1,138 @@
1
+ import React from 'react';
2
+ import { describe, it, expect, vi } from 'vitest';
3
+ import { render, fireEvent, screen } from '@testing-library/react';
4
+ import configureStore from 'redux-mock-store';
5
+ import { Provider } from 'react-intl-redux';
6
+ import imageFullSVG from '@plone/volto/icons/image-full.svg';
7
+ import textJustifiedSVG from '@plone/volto/icons/align-justify.svg';
8
+ import textCenteredSVG from '@plone/volto/icons/align-center.svg';
9
+ import textLeftSVG from '@plone/volto/icons/align-left.svg';
10
+ import textRightSVG from '@plone/volto/icons/align-right.svg';
11
+
12
+ import ButtonsWidget from './ButtonsWidget';
13
+
14
+ const mockStore = configureStore();
15
+
16
+ const renderWidget = (ui: React.ReactElement) =>
17
+ render(
18
+ <Provider
19
+ store={mockStore({
20
+ intl: {
21
+ locale: 'en',
22
+ messages: {},
23
+ },
24
+ })}
25
+ >
26
+ {ui}
27
+ </Provider>,
28
+ );
29
+
30
+ describe('ButtonsWidget', () => {
31
+ it('renders string-based actions', () => {
32
+ const { asFragment } = renderWidget(
33
+ <ButtonsWidget
34
+ id="align"
35
+ title="Alignment"
36
+ fieldSet="default"
37
+ onChange={() => {}}
38
+ actions={['left', 'right', 'centered', 'justified']}
39
+ actionsInfoMap={{
40
+ left: [textLeftSVG, 'Text Left'],
41
+ right: [textRightSVG, 'Text Right'],
42
+ justified: [textJustifiedSVG, 'Text Justified'],
43
+ centered: [textCenteredSVG, 'Text Centered'],
44
+ }}
45
+ />,
46
+ );
47
+
48
+ expect(asFragment()).toMatchSnapshot();
49
+ });
50
+
51
+ it('renders actions info provided via props', () => {
52
+ const { asFragment } = renderWidget(
53
+ <ButtonsWidget
54
+ id="align"
55
+ title="Alignment"
56
+ fieldSet="default"
57
+ onChange={() => {}}
58
+ actions={['additional']}
59
+ actionsInfoMap={{
60
+ additional: [imageFullSVG, 'Additional action title'],
61
+ }}
62
+ />,
63
+ );
64
+
65
+ expect(asFragment()).toMatchSnapshot();
66
+ });
67
+
68
+ it('falls back to the action name when no info map entry is present', () => {
69
+ renderWidget(
70
+ <ButtonsWidget
71
+ id="align"
72
+ title="Alignment"
73
+ fieldSet="default"
74
+ onChange={() => {}}
75
+ actions={['missing']}
76
+ />,
77
+ );
78
+
79
+ expect(screen.getByRole('radio', { name: 'missing' })).toBeInTheDocument();
80
+ });
81
+
82
+ it('normalizes style definitions when an action is pressed', () => {
83
+ const handleChange = vi.fn();
84
+
85
+ renderWidget(
86
+ <ButtonsWidget
87
+ id="align"
88
+ title="Alignment"
89
+ fieldSet="default"
90
+ onChange={handleChange}
91
+ actions={[
92
+ {
93
+ name: 'wide',
94
+ label: 'Wide',
95
+ style: {
96
+ '--layout-width': 'wide',
97
+ },
98
+ },
99
+ ]}
100
+ actionsInfoMap={{
101
+ wide: ['Wide', 'Wide width'],
102
+ }}
103
+ />,
104
+ );
105
+
106
+ fireEvent.click(screen.getByRole('radio', { name: 'Wide width' }));
107
+
108
+ expect(handleChange).toHaveBeenCalledWith('align', {
109
+ '--layout-width': 'wide',
110
+ });
111
+ });
112
+
113
+ it('selects default value from string names', () => {
114
+ renderWidget(
115
+ <ButtonsWidget
116
+ id="align"
117
+ title="Alignment"
118
+ fieldSet="default"
119
+ onChange={() => {}}
120
+ actions={[
121
+ {
122
+ name: 'justified',
123
+ label: 'Justified',
124
+ style: {
125
+ '--layout-width': 'wide',
126
+ },
127
+ },
128
+ ]}
129
+ default="justified"
130
+ actionsInfoMap={{
131
+ justified: [textJustifiedSVG, 'Text Justified'],
132
+ }}
133
+ />,
134
+ );
135
+
136
+ expect(screen.getByRole('radio', { name: 'Text Justified' })).toBeChecked();
137
+ });
138
+ });
@@ -0,0 +1,176 @@
1
+ import React from 'react';
2
+ import FormFieldWrapper from '@plone/volto/components/manage/Widgets/FormFieldWrapper';
3
+ import Icon from '@plone/volto/components/theme/Icon/Icon';
4
+ import { Radio, RadioGroup } from '@plone/components';
5
+ import isEqual from 'lodash/isEqual';
6
+ import type { StyleDefinition } from '@plone/types';
7
+
8
+ /**
9
+ * A tuple that has an icon in the first element and a i18n string in the second.
10
+ */
11
+ export type ActionInfo = [React.ReactElement<any>, string] | [string, string];
12
+
13
+ type ActionValue = string | Record<`--${string}`, string>;
14
+
15
+ export type ButtonsWidgetProps = {
16
+ /**
17
+ * Unique identifier for the widget.
18
+ */
19
+ id: string;
20
+
21
+ /**
22
+ * Callback function to handle changes.
23
+ */
24
+ onChange: (id: string, value: ActionValue) => void;
25
+
26
+ /**
27
+ * List of actions available for the widget.
28
+ */
29
+ actions?: Array<StyleDefinition | string>;
30
+
31
+ /**
32
+ * Map containing additional the information (icon and i18n string) for each action.
33
+ */
34
+ actionsInfoMap?: Record<string, ActionInfo>;
35
+
36
+ /**
37
+ * List of actions to be filtered out. In case that we don't want the default ones
38
+ * we can filter them out.
39
+ */
40
+ filterActions?: string[];
41
+
42
+ /**
43
+ * Current value of the widget.
44
+ */
45
+ value?: ActionValue;
46
+
47
+ /**
48
+ * Default value of the widget.
49
+ */
50
+ default?: ActionValue;
51
+
52
+ /**
53
+ * Indicates if the widget is disabled.
54
+ */
55
+ disabled?: boolean;
56
+
57
+ /**
58
+ * Indicates if the widget is disabled (alternative flag for compatibility reasons).
59
+ */
60
+ isDisabled?: boolean;
61
+ [key: string]: any;
62
+ };
63
+
64
+ type NormalizedAction = {
65
+ name: string;
66
+ value: ActionValue;
67
+ };
68
+
69
+ const ButtonsWidget = (props: ButtonsWidgetProps) => {
70
+ const {
71
+ disabled,
72
+ id,
73
+ onChange,
74
+ actions = [],
75
+ actionsInfoMap,
76
+ value,
77
+ isDisabled,
78
+ default: defaultValue,
79
+ } = props;
80
+
81
+ const normalizedActions: NormalizedAction[] = actions.map((action) =>
82
+ typeof action === 'string'
83
+ ? { name: action, value: action }
84
+ : {
85
+ name: action.name,
86
+ value: action.style ?? action.name,
87
+ },
88
+ );
89
+
90
+ const selectedActionName = normalizedActions.find((action) =>
91
+ isEqual(value, action.value),
92
+ )?.name;
93
+
94
+ const defaultSelectedActionName = (() => {
95
+ if (!defaultValue) {
96
+ return undefined;
97
+ }
98
+
99
+ if (typeof defaultValue === 'string') {
100
+ const matchedByName = normalizedActions.find(
101
+ ({ name }) => name === defaultValue,
102
+ );
103
+
104
+ if (matchedByName) {
105
+ return matchedByName.name;
106
+ }
107
+ }
108
+
109
+ return normalizedActions.find(({ value: actionValue }) =>
110
+ isEqual(defaultValue, actionValue),
111
+ )?.name;
112
+ })();
113
+
114
+ const radioGroupValueProps: {
115
+ value?: string;
116
+ defaultValue?: string;
117
+ } = selectedActionName
118
+ ? { value: selectedActionName }
119
+ : defaultSelectedActionName
120
+ ? { defaultValue: defaultSelectedActionName }
121
+ : {};
122
+
123
+ const handleChange = (selectedName: string) => {
124
+ const selectedAction = normalizedActions.find(
125
+ ({ name }) => name === selectedName,
126
+ );
127
+
128
+ if (selectedAction) {
129
+ onChange(id, selectedAction.value);
130
+ }
131
+ };
132
+
133
+ return (
134
+ <FormFieldWrapper {...props} className="widget">
135
+ <RadioGroup
136
+ aria-label={props.title || props.label || id}
137
+ orientation="horizontal"
138
+ {...radioGroupValueProps}
139
+ onChange={handleChange}
140
+ isDisabled={disabled || isDisabled}
141
+ className="buttons buttons-widget"
142
+ >
143
+ {normalizedActions.map((action) => {
144
+ const actionInfo = actionsInfoMap?.[action.name];
145
+ const [iconOrText, ariaLabel] = actionInfo ?? [
146
+ action.name,
147
+ action.name,
148
+ ];
149
+
150
+ return (
151
+ <Radio
152
+ key={action.name}
153
+ aria-label={ariaLabel}
154
+ value={action.name}
155
+ className="buttons-widget-option"
156
+ >
157
+ {typeof iconOrText === 'string' ? (
158
+ <div className="image-sizes-text">{iconOrText}</div>
159
+ ) : (
160
+ <Icon
161
+ // TODO: Refactor Icon component and type it correctly
162
+ name={iconOrText as any}
163
+ title={ariaLabel || action.name}
164
+ size="24px"
165
+ ariaHidden={true}
166
+ />
167
+ )}
168
+ </Radio>
169
+ );
170
+ })}
171
+ </RadioGroup>
172
+ </FormFieldWrapper>
173
+ );
174
+ };
175
+
176
+ export default ButtonsWidget;
@@ -0,0 +1,69 @@
1
+ import React from 'react';
2
+ import type { Meta, StoryFn, StoryObj } from '@storybook/react';
3
+
4
+ import Size from './Size';
5
+ import type { ButtonsWidgetProps } from './ButtonsWidget';
6
+ import {
7
+ RealStoreWrapper as Wrapper,
8
+ FormUndoWrapper,
9
+ } from '@plone/volto/storybook';
10
+
11
+ const meta: Meta<typeof Size> = {
12
+ title: 'Edit Widgets/Size',
13
+ component: Size,
14
+ decorators: [
15
+ (Story) => (
16
+ <div className="ui segment form attached" style={{ width: '400px' }}>
17
+ <h4>Size widget</h4>
18
+ <Story />
19
+ </div>
20
+ ),
21
+ ],
22
+ };
23
+
24
+ export default meta;
25
+
26
+ type Story = StoryObj<typeof Size>;
27
+
28
+ type TemplateParameters = {
29
+ initialValue?: ButtonsWidgetProps['value'];
30
+ };
31
+
32
+ const Template: StoryFn<typeof Size> = (args, context) => {
33
+ const { initialValue } =
34
+ (context.parameters as TemplateParameters | undefined) ?? {};
35
+ const { value: _ignoredValue, onChange: argsOnChange, ...restArgs } = args;
36
+
37
+ return (
38
+ <Wrapper location={{ pathname: '/folder2/folder21/doc212' }}>
39
+ <FormUndoWrapper
40
+ initialState={{ value: initialValue ?? _ignoredValue }}
41
+ showControls
42
+ >
43
+ {({ state, onChange }) => (
44
+ <div className="ui segment form attached" style={{ width: '400px' }}>
45
+ <Size
46
+ {...(restArgs as ButtonsWidgetProps)}
47
+ value={state.value}
48
+ onChange={(id, nextValue) => {
49
+ argsOnChange?.(id, nextValue);
50
+ onChange({ value: nextValue });
51
+ }}
52
+ />
53
+ <pre>Value: {JSON.stringify(state.value, null, 2)}</pre>
54
+ </div>
55
+ )}
56
+ </FormUndoWrapper>
57
+ </Wrapper>
58
+ );
59
+ };
60
+
61
+ export const DefaultSize: Story = {
62
+ render: Template,
63
+ args: {
64
+ id: 'size',
65
+ title: 'Size',
66
+ block: 'block',
67
+ fieldSet: 'default',
68
+ },
69
+ };
@@ -0,0 +1,59 @@
1
+ import React from 'react';
2
+ import { describe, it, expect, vi } from 'vitest';
3
+ import { render, screen, fireEvent } from '@testing-library/react';
4
+ import configureStore from 'redux-mock-store';
5
+ import { Provider } from 'react-intl-redux';
6
+
7
+ import Size from './Size';
8
+ import type { ButtonsWidgetProps } from './ButtonsWidget';
9
+
10
+ const mockStore = configureStore();
11
+
12
+ const renderWidget = (props: Partial<ButtonsWidgetProps> = {}) => {
13
+ const onChange = props.onChange ?? vi.fn();
14
+ const widgetProps: ButtonsWidgetProps = {
15
+ id: 'size',
16
+ title: 'Size',
17
+ fieldSet: 'default',
18
+ onChange,
19
+ value: undefined,
20
+ default: undefined,
21
+ disabled: false,
22
+ isDisabled: false,
23
+ ...props,
24
+ };
25
+
26
+ return {
27
+ onChange,
28
+ ...render(
29
+ <Provider
30
+ store={mockStore({
31
+ intl: {
32
+ locale: 'en',
33
+ messages: {},
34
+ },
35
+ })}
36
+ >
37
+ <Size {...widgetProps} />
38
+ </Provider>,
39
+ ),
40
+ };
41
+ };
42
+
43
+ describe('Size widget', () => {
44
+ it('renders the default size options', () => {
45
+ renderWidget();
46
+
47
+ expect(screen.getByRole('radio', { name: 'Small' })).toBeInTheDocument();
48
+ expect(screen.getByRole('radio', { name: 'Medium' })).toBeInTheDocument();
49
+ expect(screen.getByRole('radio', { name: 'Large' })).toBeInTheDocument();
50
+ });
51
+
52
+ it('returns the selected size name', () => {
53
+ const { onChange } = renderWidget();
54
+
55
+ fireEvent.click(screen.getByRole('radio', { name: 'Medium' }));
56
+
57
+ expect(onChange).toHaveBeenCalledWith('size', 'm');
58
+ });
59
+ });