Skip to content

Commit c8c0d8b

Browse files
authored
fix: add support for styling dialog action buttons (#3760)
1 parent 7f8852b commit c8c0d8b

3 files changed

Lines changed: 122 additions & 3 deletions

File tree

src/components/Dialog/Dialog.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ export type Props = {
3939
* @optional
4040
*/
4141
theme?: ThemeProp;
42+
/**
43+
* testID to be used on tests.
44+
*/
45+
testID?: string;
4246
};
4347

4448
const DIALOG_ELEVATION: number = 24;
@@ -95,6 +99,7 @@ const Dialog = ({
9599
visible = false,
96100
style,
97101
theme: themeOverrides,
102+
testID,
98103
}: Props) => {
99104
const theme = useInternalTheme(themeOverrides);
100105
const { isV3, dark, mode, colors, roundness } = theme;
@@ -122,6 +127,7 @@ const Dialog = ({
122127
style,
123128
]}
124129
theme={theme}
130+
testID={testID}
125131
>
126132
{React.Children.toArray(children)
127133
.filter((child) => child != null && typeof child !== 'boolean')

src/components/Dialog/DialogActions.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,12 @@ const DialogActions = (props: Props) => {
6565
? React.cloneElement(child as React.ReactElement<any>, {
6666
compact: true,
6767
uppercase: !isV3,
68-
style: isV3 && {
69-
paddingRight: i + 1 === actionsLength ? 0 : 8,
70-
},
68+
style: [
69+
isV3 && {
70+
marginRight: i + 1 === actionsLength ? 0 : 8,
71+
},
72+
child.props.style,
73+
],
7174
})
7275
: child
7376
)}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import React from 'react';
2+
import { Text, StyleSheet } from 'react-native';
3+
4+
import { act, fireEvent, render } from '@testing-library/react-native';
5+
6+
import Dialog from '../../components/Dialog/Dialog';
7+
import Button from '../Button/Button';
8+
9+
jest.mock('react-native-safe-area-context', () => ({
10+
useSafeAreaInsets: () => ({ bottom: 44, left: 0, right: 0, top: 37 }),
11+
}));
12+
13+
describe('Dialog', () => {
14+
it('should render passed children', () => {
15+
const { getByTestId } = render(
16+
<Dialog visible testID="dialog">
17+
<Text>This is simple dialog</Text>
18+
</Dialog>
19+
);
20+
21+
expect(getByTestId('dialog')).toHaveTextContent('This is simple dialog');
22+
});
23+
24+
it('should call onDismiss when dismissable', () => {
25+
const onDismiss = jest.fn();
26+
const { getByTestId } = render(
27+
<Dialog visible onDismiss={onDismiss} dismissable testID="dialog">
28+
<Text>This is simple dialog</Text>
29+
</Dialog>
30+
);
31+
32+
fireEvent.press(getByTestId('dialog-backdrop'));
33+
34+
act(() => {
35+
jest.runAllTimers();
36+
});
37+
expect(onDismiss).toHaveBeenCalledTimes(1);
38+
});
39+
40+
it('should apply top margin to the first child if the dialog is V3', () => {
41+
const { getByTestId } = render(
42+
<Dialog visible={true}>
43+
<Dialog.Title testID="dialog-content">
44+
<Text>Test Dialog Content</Text>
45+
</Dialog.Title>
46+
</Dialog>
47+
);
48+
49+
expect(getByTestId('dialog-content')).toHaveStyle({
50+
marginTop: 24,
51+
});
52+
});
53+
});
54+
55+
describe('DialogActions', () => {
56+
it('should render passed children', () => {
57+
const { getByTestId } = render(
58+
<Dialog.Actions>
59+
<Button testID="button-cancel">Cancel</Button>
60+
<Button testID="button-ok">Ok</Button>
61+
</Dialog.Actions>
62+
);
63+
64+
expect(getByTestId('button-cancel')).toBeDefined();
65+
expect(getByTestId('button-ok')).toBeDefined();
66+
});
67+
68+
it('should apply default styles', () => {
69+
const { getByTestId } = render(
70+
<Dialog.Actions testID="dialog-actions">
71+
<Button>Cancel</Button>
72+
<Button>Ok</Button>
73+
</Dialog.Actions>
74+
);
75+
76+
const dialogActionsContainer = getByTestId('dialog-actions');
77+
const dialogActionButtons = dialogActionsContainer.children;
78+
79+
expect(dialogActionsContainer).toHaveStyle({
80+
paddingBottom: 24,
81+
paddingHorizontal: 24,
82+
});
83+
expect(dialogActionButtons[0]).toHaveStyle({ marginRight: 8 });
84+
expect(dialogActionButtons[1]).toHaveStyle({ marginRight: 0 });
85+
});
86+
87+
it('should apply custom styles', () => {
88+
const { getByTestId } = render(
89+
<Dialog.Actions testID="dialog-actions">
90+
<Button style={styles.spacing}>Cancel</Button>
91+
<Button style={styles.noSpacing}>Ok</Button>
92+
</Dialog.Actions>
93+
);
94+
95+
const dialogActionsContainer = getByTestId('dialog-actions');
96+
const dialogActionButtons = dialogActionsContainer.children;
97+
98+
expect(dialogActionButtons[0]).toHaveStyle({ margin: 10 });
99+
expect(dialogActionButtons[1]).toHaveStyle({ margin: 0 });
100+
});
101+
});
102+
103+
const styles = StyleSheet.create({
104+
spacing: {
105+
margin: 10,
106+
},
107+
noSpacing: {
108+
margin: 0,
109+
},
110+
});

0 commit comments

Comments
 (0)