Skip to content

Commit f20d861

Browse files
fix(MessageBox): allow custom action texts (#561)
Closes #551
1 parent d877eb7 commit f20d861

5 files changed

Lines changed: 44 additions & 43 deletions

File tree

packages/main/src/components/MessageBox/MessageBox.jss.ts

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@ const style = {
99
textAlign: 'center',
1010
width: 'auto',
1111
alignItems: 'center',
12-
'& $icon': {
13-
'& >*': {
14-
verticalAlign: 'middle'
15-
}
16-
},
1712
padding: '0.25rem 1rem',
1813
boxSizing: 'border-box',
1914
borderBottom: `1px solid var(--messageBoxBorderColor)`,
@@ -27,44 +22,32 @@ const style = {
2722
'&[data-type="Error"]': {
2823
'--sapPageFooter_BorderColor': ThemingParameters.sapErrorBorderColor,
2924
'--messageBoxBorderColor': ThemingParameters.sapErrorBorderColor,
30-
'& $icon': {
31-
'--sapContent_NonInteractiveIconColor': ThemingParameters.sapNegativeElementColor
32-
}
25+
'--sapContent_NonInteractiveIconColor': ThemingParameters.sapNegativeElementColor
3326
},
3427
'&[data-type="Warning"]': {
3528
'--sapPageFooter_BorderColor': ThemingParameters.sapWarningBorderColor,
3629
'--messageBoxBorderColor': ThemingParameters.sapWarningBorderColor,
37-
'& $icon': {
38-
'--sapContent_NonInteractiveIconColor': ThemingParameters.sapCriticalElementColor
39-
}
30+
'--sapContent_NonInteractiveIconColor': ThemingParameters.sapCriticalElementColor
4031
},
4132
'&[data-type="Success"]': {
4233
'--sapPageFooter_BorderColor': ThemingParameters.sapSuccessBorderColor,
4334
'--messageBoxBorderColor': ThemingParameters.sapSuccessBorderColor,
44-
'& $icon': {
45-
'--sapContent_NonInteractiveIconColor': ThemingParameters.sapPositiveElementColor
46-
}
35+
'--sapContent_NonInteractiveIconColor': ThemingParameters.sapPositiveElementColor
4736
},
4837
'&[data-type="Confirm"]': {
4938
'--sapPageFooter_BorderColor': ThemingParameters.sapNeutralBorderColor,
5039
'--messageBoxBorderColor': ThemingParameters.sapNeutralBorderColor,
51-
'& $icon': {
52-
'--sapContent_NonInteractiveIconColor': ThemingParameters.sapNeutralElementColor
53-
}
40+
'--sapContent_NonInteractiveIconColor': ThemingParameters.sapNeutralElementColor
5441
},
5542
'&[data-type="Information"]': {
5643
'--sapPageFooter_BorderColor': ThemingParameters.sapNeutralBorderColor,
5744
'--messageBoxBorderColor': ThemingParameters.sapNeutralBorderColor,
58-
'& $icon': {
59-
'--sapContent_NonInteractiveIconColor': ThemingParameters.sapNeutralElementColor
60-
}
45+
'--sapContent_NonInteractiveIconColor': ThemingParameters.sapNeutralElementColor
6146
},
6247
'&[data-type="Highlight"]': {
6348
'--sapPageFooter_BorderColor': ThemingParameters.sapInformationBorderColor,
6449
'--messageBoxBorderColor': ThemingParameters.sapInformationBorderColor,
65-
'& $icon': {
66-
'--sapContent_NonInteractiveIconColor': ThemingParameters.sapInformativeElementColor
67-
}
50+
'--sapContent_NonInteractiveIconColor': ThemingParameters.sapInformativeElementColor
6851
}
6952
},
7053
content: {
@@ -80,7 +63,8 @@ const style = {
8063
margin: '0 0.25rem'
8164
},
8265
'& > ui5-button': {
83-
display: 'flex'
66+
display: 'flex',
67+
minWidth: '4rem'
8468
}
8569
}
8670
};

packages/main/src/components/MessageBox/MessageBox.test.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { createPassThroughPropsTest, getEventFromCallback } from '@shared/tests/utils';
2+
import { render, screen, fireEvent } from '@tests/index';
23
import { mount } from 'enzyme';
34
import { MessageBox } from '@ui5/webcomponents-react/lib/MessageBox';
45
import { MessageBoxActions } from '@ui5/webcomponents-react/lib/MessageBoxActions';
@@ -144,5 +145,25 @@ describe('MessageBox', () => {
144145
expect(wrapper.render()).toMatchSnapshot();
145146
});
146147

148+
test('Custom Action Text', async () => {
149+
const onClose = jest.fn();
150+
render(
151+
<MessageBox type={MessageBoxTypes.CONFIRM} actions={[MessageBoxActions.OK, 'My Custom Action']} onClose={onClose}>
152+
My Message Box Content
153+
</MessageBox>
154+
);
155+
156+
const textOK = screen.getByText(MessageBoxActions.OK);
157+
expect(textOK).toBeInTheDocument();
158+
let customAction = screen.getByText('My Custom Action');
159+
expect(customAction).toBeInTheDocument();
160+
161+
fireEvent.click(textOK);
162+
fireEvent.click(customAction);
163+
164+
expect(onClose.mock.calls[0][0].detail.action).toEqual(MessageBoxActions.OK);
165+
expect(onClose.mock.calls[1][0].detail.action).toEqual('My Custom Action');
166+
});
167+
147168
createPassThroughPropsTest(MessageBox);
148169
});

packages/main/src/components/MessageBox/__snapshots__/MessageBox.test.tsx.snap

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,12 @@ exports[`MessageBox Confirm - Cancel 1`] = `
2525
<ui5-button
2626
data-action="OK"
2727
design="Emphasized"
28-
style="min-width: 4rem;"
2928
>
3029
OK
3130
</ui5-button>
3231
<ui5-button
3332
data-action="Cancel"
3433
design="Transparent"
35-
style="min-width: 4rem;"
3634
>
3735
Cancel
3836
</ui5-button>
@@ -70,14 +68,12 @@ exports[`MessageBox Confirm - OK 1`] = `
7068
<ui5-button
7169
data-action="OK"
7270
design="Emphasized"
73-
style="min-width: 4rem;"
7471
>
7572
OK
7673
</ui5-button>
7774
<ui5-button
7875
data-action="Cancel"
7976
design="Transparent"
80-
style="min-width: 4rem;"
8177
>
8278
Cancel
8379
</ui5-button>
@@ -115,7 +111,6 @@ exports[`MessageBox Error 1`] = `
115111
<ui5-button
116112
data-action="Close"
117113
design="Emphasized"
118-
style="min-width: 4rem;"
119114
>
120115
Close
121116
</ui5-button>
@@ -153,7 +148,6 @@ exports[`MessageBox Information 1`] = `
153148
<ui5-button
154149
data-action="OK"
155150
design="Emphasized"
156-
style="min-width: 4rem;"
157151
>
158152
OK
159153
</ui5-button>
@@ -191,14 +185,12 @@ exports[`MessageBox No Title 1`] = `
191185
<ui5-button
192186
data-action="OK"
193187
design="Emphasized"
194-
style="min-width: 4rem;"
195188
>
196189
OK
197190
</ui5-button>
198191
<ui5-button
199192
data-action="Cancel"
200193
design="Transparent"
201-
style="min-width: 4rem;"
202194
>
203195
Cancel
204196
</ui5-button>
@@ -234,7 +226,6 @@ exports[`MessageBox Not open 1`] = `
234226
<ui5-button
235227
data-action="OK"
236228
design="Emphasized"
237-
style="min-width: 4rem;"
238229
>
239230
OK
240231
</ui5-button>
@@ -272,14 +263,12 @@ exports[`MessageBox Show 1`] = `
272263
<ui5-button
273264
data-action="Yes"
274265
design="Emphasized"
275-
style="min-width: 4rem;"
276266
>
277267
Yes
278268
</ui5-button>
279269
<ui5-button
280270
data-action="No"
281271
design="Transparent"
282-
style="min-width: 4rem;"
283272
>
284273
No
285274
</ui5-button>
@@ -317,7 +306,6 @@ exports[`MessageBox Success 1`] = `
317306
<ui5-button
318307
data-action="OK"
319308
design="Emphasized"
320-
style="min-width: 4rem;"
321309
>
322310
OK
323311
</ui5-button>
@@ -355,7 +343,6 @@ exports[`MessageBox Success w/ custom title 1`] = `
355343
<ui5-button
356344
data-action="OK"
357345
design="Emphasized"
358-
style="min-width: 4rem;"
359346
>
360347
OK
361348
</ui5-button>
@@ -393,7 +380,6 @@ exports[`MessageBox Warning 1`] = `
393380
<ui5-button
394381
data-action="OK"
395382
design="Emphasized"
396-
style="min-width: 4rem;"
397383
>
398384
OK
399385
</ui5-button>

packages/main/src/components/MessageBox/demo.stories.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { action } from '@storybook/addon-actions';
22
import { boolean, select, text } from '@storybook/addon-knobs';
33
import { MessageBox } from '@ui5/webcomponents-react/lib/MessageBox';
44
import { MessageBoxTypes } from '@ui5/webcomponents-react/lib/MessageBoxTypes';
5+
import { MessageBoxActions } from '@ui5/webcomponents-react/lib/MessageBoxActions';
56
import React from 'react';
67

78
export const defaultStory = () => (
@@ -19,6 +20,18 @@ defaultStory.story = {
1920
name: 'Default'
2021
};
2122

23+
export const withCustomActions = () => (
24+
<MessageBox
25+
type={select('type', Object.values(MessageBoxTypes), MessageBoxTypes.CONFIRM)}
26+
open={boolean('open', true)}
27+
onClose={action('onClose')}
28+
title={text('title', 'A Custom Message Box Title')}
29+
actions={[MessageBoxActions.OK, 'A custom action', MessageBoxActions.CANCEL]}
30+
>
31+
Message Box Content
32+
</MessageBox>
33+
);
34+
2235
export default {
2336
title: 'Components / MessageBox',
2437
component: MessageBox

packages/main/src/components/MessageBox/index.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export interface MessageBoxPropTypes extends CommonProps {
5454
open?: boolean;
5555
title?: string;
5656
children: string;
57-
actions?: MessageBoxActions[];
57+
actions?: (MessageBoxActions | string)[];
5858
icon?: ReactNode;
5959
type?: MessageBoxTypes;
6060
onClose: (event: CustomEvent<{ action: MessageBoxActions }>) => void;
@@ -192,15 +192,12 @@ const MessageBox: FC<MessageBoxPropTypes> = forwardRef((props: MessageBoxPropTyp
192192
{actionsToRender.map((action, index) => {
193193
return (
194194
<Button
195-
style={{
196-
minWidth: '4rem'
197-
}}
198195
key={action}
199196
design={index === 0 ? ButtonDesign.Emphasized : ButtonDesign.Transparent}
200197
onClick={handleOnClose}
201198
data-action={action}
202199
>
203-
{actionTranslations[actionTextMap.get(action)]}
200+
{actionTextMap.has(action) ? actionTranslations[actionTextMap.get(action)] : action}
204201
</Button>
205202
);
206203
})}

0 commit comments

Comments
 (0)