Skip to content

Commit 362a3bb

Browse files
committed
feat: enhance Alert component to utilize config for customizable slots and rendering functions
1 parent 6e21776 commit 362a3bb

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

src/components/Alert/index.tsx

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ export const Alert: FC<AlertViewProps> = (props) => {
8686
);
8787

8888
const beforeTitleSlotElement = useMemo(() => {
89-
return beforeTitleSlot?.() || null;
90-
}, [beforeTitleSlot]);
89+
return beforeTitleSlot?.() || config?.beforeTitleSlot?.() || null;
90+
}, [beforeTitleSlot, config]);
9191

9292
const titleContainerStyle = useMemo(() => {
9393
const style: StyleProp<ViewStyle>[] = [styles.titleContainer];
@@ -105,27 +105,35 @@ export const Alert: FC<AlertViewProps> = (props) => {
105105
{title}
106106
</Text>
107107
) : (
108-
renderTitle?.({ style: styles.title, text: title ?? '' }) || null
108+
renderTitle?.({ style: styles.title, text: title ?? '' }) ||
109+
config?.renderTitle?.({ style: styles.title, text: title ?? '' }) ||
110+
null
109111
);
110112

111113
return titleElement ? (
112114
<View style={titleContainerStyle}>{titleElement}</View>
113115
) : null;
114-
}, [title, renderTitle, titleContainerStyle]);
116+
}, [title, renderTitle, titleContainerStyle, config]);
117+
118+
const beforeMessageSlotElement = useMemo(() => {
119+
return beforeMessageSlot?.() || config?.beforeMessageSlot?.() || null;
120+
}, [beforeMessageSlot, config]);
115121

116122
const renderMessageCb = useCallback(() => {
117123
const messageElement = message ? (
118124
<Text style={styles.message}>{message}</Text>
119125
) : (
120-
renderMessage?.({ style: styles.message, text: message ?? '' }) || null
126+
renderMessage?.({ style: styles.message, text: message ?? '' }) ||
127+
config?.renderMessage?.({ style: styles.message, text: message ?? '' }) ||
128+
null
121129
);
122130

123131
return messageElement ? (
124132
<ScrollView bounces={false} style={styles.messageContainer}>
125133
{messageElement}
126134
</ScrollView>
127135
) : null;
128-
}, [message, renderMessage]);
136+
}, [message, renderMessage, config]);
129137

130138
const renderIconCb = useCallback(() => {
131139
const Svg = icon;
@@ -155,18 +163,27 @@ export const Alert: FC<AlertViewProps> = (props) => {
155163

156164
const dismissButton = isDismissible
157165
? renderDismissButton?.({ onPress: onDismissButtonPress }) ||
166+
config?.renderDismissButton?.({ onPress: onDismissButtonPress }) ||
158167
defaultDismissButton
159168
: null;
160169

161170
return dismissButton;
162-
}, [isDismissible, onDismissButtonPress, renderDismissButton]);
171+
}, [isDismissible, onDismissButtonPress, renderDismissButton, config]);
163172

164173
const buttonsContainerStyle = useMemo(() => {
165174
return StyleSheet.compose(styles.buttonsContainer, {
166175
flexDirection: buttonsDirection,
167176
});
168177
}, [buttonsDirection]);
169178

179+
const beforeButtonsSlotElement = useMemo(() => {
180+
return beforeButtonsSlot?.() || config?.beforeButtonsSlot?.() || null;
181+
}, [beforeButtonsSlot, config]);
182+
183+
const afterButtonsSlotElement = useMemo(() => {
184+
return afterButtonsSlot?.() || config?.afterButtonsSlot?.() || null;
185+
}, [afterButtonsSlot, config]);
186+
170187
const renderButtons = useCallback(() => {
171188
if (buttons?.length) {
172189
return (
@@ -190,12 +207,12 @@ export const Alert: FC<AlertViewProps> = (props) => {
190207
{renderIconCb()}
191208
{beforeTitleSlotElement}
192209
{renderTitleCb()}
193-
{beforeMessageSlot?.() || null}
210+
{beforeMessageSlotElement}
194211
{renderMessageCb()}
195-
{beforeButtonsSlot?.() || null}
212+
{beforeButtonsSlotElement}
196213
{renderDismissButtonCb()}
197214
{renderButtons()}
198-
{afterButtonsSlot?.() || null}
215+
{afterButtonsSlotElement}
199216
</Animated.View>
200217
);
201218
};

src/containers/AlertContainer/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,11 @@ export type AlertConfig = {
4848
size?: number;
4949
color?: ColorValue;
5050
};
51+
renderTitle?: AlertViewProps['renderTitle'];
52+
renderMessage?: AlertViewProps['renderMessage'];
53+
renderDismissButton?: AlertViewProps['renderDismissButton'];
54+
afterButtonsSlot?: AlertViewProps['afterButtonsSlot'];
55+
beforeButtonsSlot?: AlertViewProps['beforeButtonsSlot'];
56+
beforeMessageSlot?: AlertViewProps['beforeMessageSlot'];
57+
beforeTitleSlot?: AlertViewProps['beforeTitleSlot'];
5158
};

0 commit comments

Comments
 (0)