Skip to content

Commit acb7ba3

Browse files
committed
feat: add dismissible alert functionality and improve Alert component with title rendering and styles
1 parent 403599e commit acb7ba3

File tree

5 files changed

+56
-8
lines changed

5 files changed

+56
-8
lines changed

example/src/App.tsx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,31 @@ export default function App() {
3535
}}
3636
text="Show 2 Alerts"
3737
/>
38+
<Button
39+
onPress={async () => {
40+
await alert.show({
41+
title: 'Lorem ipsum dolor sit amet',
42+
message: 'I am a dismissible alert',
43+
isDismissible: true,
44+
onDismiss: () => {
45+
alert.success({
46+
message: 'You dismissed the alert',
47+
});
48+
},
49+
buttons: [
50+
{
51+
text: 'OK',
52+
onPress: () => {
53+
alert.success({
54+
message: 'You pressed OK button',
55+
});
56+
},
57+
},
58+
],
59+
});
60+
}}
61+
text="Show Dismissible Alert"
62+
/>
3863
<Button
3964
onPress={() => {
4065
alert.success({

src/components/Alert/controller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export const useController = <R = unknown>({
2121
if (button.onAwaitablePress) {
2222
button.onAwaitablePress(resolveWrapper);
2323
} else {
24-
button.onClick?.();
24+
button.onPress?.();
2525

2626
resolveWrapper(undefined as R);
2727
}

src/components/Alert/index.tsx

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import {
66
StyleSheet,
77
Text,
88
View,
9+
type StyleProp,
10+
type ViewStyle,
911
} from 'react-native';
1012

1113
import type { AlertViewProps } from './types';
@@ -32,7 +34,9 @@ export const Alert: FC<AlertViewProps> = (props) => {
3234
isHiding,
3335
testID,
3436
buttons,
37+
title,
3538
titleAlign = 'center',
39+
renderTitle,
3640
} = props;
3741

3842
const { animation } = useAnimation({
@@ -77,22 +81,38 @@ export const Alert: FC<AlertViewProps> = (props) => {
7781
[containerAnimation, containerDimensions]
7882
);
7983

84+
const beforeTitleSlotElement = useMemo(() => {
85+
return beforeTitleSlot?.() || null;
86+
}, [beforeTitleSlot]);
87+
8088
const titleStyle = useMemo(
8189
() => StyleSheet.compose(styles.title, { textAlign: titleAlign }),
8290
[titleAlign]
8391
);
8492

93+
const titleContainerStyle = useMemo(() => {
94+
const style: StyleProp<ViewStyle>[] = [styles.titleContainer];
95+
96+
if (isDismissible && !(icon || beforeTitleSlotElement)) {
97+
style.push(styles.dismissibleTitleContainer);
98+
}
99+
100+
return StyleSheet.flatten(style);
101+
}, [isDismissible, icon, beforeTitleSlotElement]);
102+
85103
const renderTitleCb = useCallback(() => {
86-
const title = props.title ? (
104+
const titleElement = title ? (
87105
<Text numberOfLines={4} style={titleStyle}>
88-
{props.title}
106+
{title}
89107
</Text>
90108
) : (
91-
props.renderTitle?.() || null
109+
renderTitle?.() || null
92110
);
93111

94-
return title ? <View style={styles.titleContainer}>{title}</View> : null;
95-
}, [props, titleStyle]);
112+
return titleElement ? (
113+
<View style={titleContainerStyle}>{titleElement}</View>
114+
) : null;
115+
}, [title, titleStyle, renderTitle, titleContainerStyle]);
96116

97117
const renderMessageCb = useCallback(() => {
98118
const message = props.message ? (
@@ -154,7 +174,7 @@ export const Alert: FC<AlertViewProps> = (props) => {
154174
return (
155175
<Animated.View style={containerStyle} testID={testID || 'Alert'}>
156176
{renderIconCb()}
157-
{beforeTitleSlot?.() || null}
177+
{beforeTitleSlotElement}
158178
{renderTitleCb()}
159179
{beforeMessageSlot?.() || null}
160180
{renderMessageCb()}

src/components/Alert/styles.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ export const styles = StyleSheet.create({
3939
alignItems: 'center',
4040
justifyContent: 'center',
4141
},
42+
dismissibleTitleContainer: {
43+
paddingHorizontal: 60,
44+
},
4245
title: {
4346
fontSize: 20,
4447
lineHeight: 28,

src/components/Alert/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export interface Props<R = unknown> {
3636

3737
export type AlertButton<R = unknown> = {
3838
text: string;
39-
onClick?: (() => Promise<R>) | (() => R);
39+
onPress?: (() => Promise<R>) | (() => R);
4040
testID?: string;
4141
hideAlertOnPress?: boolean;
4242
onAwaitablePress?: (resolve: (value: R) => void) => void;

0 commit comments

Comments
 (0)