Skip to content

Commit 289ca0d

Browse files
committed
feat: add button direction support to Alert component and enhance App with new alert buttons
1 parent ab8ecff commit 289ca0d

File tree

5 files changed

+33
-6
lines changed

5 files changed

+33
-6
lines changed

example/src/App.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,22 @@ export default function App() {
3131
alert.show({
3232
title: 'Alert 2',
3333
message: 'I am an alert 2',
34+
buttons: [
35+
{
36+
text: 'OK',
37+
onPress: () => {
38+
alert.success({ message: 'You pressed OK button' });
39+
},
40+
},
41+
{
42+
text: 'Cancel',
43+
onPress: () => {
44+
alert.success({
45+
message: 'You pressed Cancel button',
46+
});
47+
},
48+
},
49+
],
3450
});
3551
}}
3652
text="Show 2 Alerts"

src/components/Alert/index.tsx

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export const Alert: FC<AlertViewProps> = (props) => {
3434
isHiding,
3535
testID,
3636
buttons,
37+
buttonsDirection = 'column',
3738
title,
3839
renderTitle,
3940
message,
@@ -75,10 +76,11 @@ export const Alert: FC<AlertViewProps> = (props) => {
7576

7677
const containerStyle = useMemo(
7778
() =>
78-
StyleSheet.compose(
79-
StyleSheet.compose(styles.container, containerDimensions),
80-
containerAnimation
81-
),
79+
StyleSheet.flatten([
80+
styles.container,
81+
containerDimensions,
82+
containerAnimation,
83+
]),
8284
[containerAnimation, containerDimensions]
8385
);
8486

@@ -149,10 +151,16 @@ export const Alert: FC<AlertViewProps> = (props) => {
149151
return dismissButton;
150152
}, [isDismissible, onDismissButtonPress, renderDismissButton]);
151153

154+
const buttonsContainerStyle = useMemo(() => {
155+
return StyleSheet.compose(styles.buttonsContainer, {
156+
flexDirection: buttonsDirection,
157+
});
158+
}, [buttonsDirection]);
159+
152160
const renderButtons = useCallback(() => {
153161
if (buttons?.length) {
154162
return (
155-
<View style={styles.buttonsContainer}>
163+
<View style={buttonsContainerStyle}>
156164
{buttons.map((button) => (
157165
<Button
158166
key={button.text}
@@ -165,7 +173,7 @@ export const Alert: FC<AlertViewProps> = (props) => {
165173
}
166174

167175
return null;
168-
}, [buttons, onButtonPress]);
176+
}, [buttons, onButtonPress, buttonsContainerStyle]);
169177

170178
return (
171179
<Animated.View style={containerStyle} testID={testID || 'Alert'}>

src/components/Alert/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export interface Props<R = unknown> {
4242
onPress: () => void;
4343
}) => ReactElement<any>;
4444
buttons?: AlertButton[];
45+
buttonsDirection?: 'row' | 'column';
4546
title?: string;
4647
}
4748

src/components/Button/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export const Button = ({
1818

1919
const styles = StyleSheet.create({
2020
button: {
21+
flexGrow: 1,
2122
backgroundColor: '#f0f0f0',
2223
borderRadius: 10,
2324
paddingVertical: 10,

src/containers/AlertContainer/controller.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ export const useController = ({ animationDuration }: Props) => {
174174
return show({
175175
...alert,
176176
buttons,
177+
buttonsDirection: 'row',
177178
title,
178179
});
179180
},

0 commit comments

Comments
 (0)