Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TS migration] Migrate 'ConfirmContent.js' component to TypeScript #31699

Merged
merged 12 commits into from
Dec 27, 2023
169 changes: 0 additions & 169 deletions src/components/ConfirmContent.js

This file was deleted.

159 changes: 159 additions & 0 deletions src/components/ConfirmContent.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
import React, {ReactNode} from 'react';
import {StyleProp, TextStyle, View, ViewStyle} from 'react-native';
import useLocalize from '@hooks/useLocalize';
import useNetwork from '@hooks/useNetwork';
import useThemeStyles from '@hooks/useThemeStyles';
import variables from '@styles/variables';
import Button from './Button';
import Header from './Header';
import Icon, {type SrcProps} from './Icon';
import Text from './Text';

type ConfirmContentProps = {
/** Title of the modal */
title: string;

/** A callback to call when the form has been submitted */
onConfirm: () => void;

/** A callback to call when the form has been closed */
onCancel?: () => void;

/** Confirm button text */
confirmText?: string;

/** Cancel button text */
cancelText?: string;

/** Modal content text/element */
prompt?: string | ReactNode;

/** Whether we should use the success button color */
success?: boolean;

/** Whether we should use the danger button color. Use if the action is destructive */
danger?: boolean;

/** Whether we should disable the confirm button when offline */
shouldDisableConfirmButtonWhenOffline?: boolean;

/** Whether we should show the cancel button */
shouldShowCancelButton?: boolean;

/** Icon to display above the title */
iconSource?: (props: SrcProps) => React.ReactNode;

/** Whether to center the icon / text content */
shouldCenterContent?: boolean;

/** Whether to stack the buttons */
shouldStackButtons?: boolean;

/** Styles for title */
titleStyles?: StyleProp<TextStyle>;

/** Styles for prompt */
promptStyles?: StyleProp<TextStyle>;

/** Styles for view */
contentStyles?: StyleProp<ViewStyle>;

/** Styles for icon */
iconAdditionalStyles?: StyleProp<ViewStyle>;
};

function ConfirmContent({
title,
onConfirm,
onCancel = () => {},
confirmText = '',
cancelText = '',
prompt = '',
success = true,
danger = false,
shouldDisableConfirmButtonWhenOffline = false,
shouldShowCancelButton = false,
iconSource,
shouldCenterContent = false,
shouldStackButtons = true,
titleStyles,
promptStyles,
contentStyles,
iconAdditionalStyles,
}: ConfirmContentProps) {
const styles = useThemeStyles();
const {translate} = useLocalize();
const {isOffline} = useNetwork();

const isCentered = shouldCenterContent;

return (
<View style={[styles.m5, contentStyles]}>
fvlvte marked this conversation as resolved.
Show resolved Hide resolved
<View style={isCentered ? [styles.alignItemsCenter, styles.mb6] : []}>
{typeof iconSource === 'function' && (
<View style={[styles.flexRow, styles.mb3]}>
<Icon
src={iconSource}
width={variables.appModalAppIconSize}
height={variables.appModalAppIconSize}
additionalStyles={iconAdditionalStyles}
/>
</View>
)}
<View style={[styles.flexRow, isCentered ? {} : styles.mb4]}>
<Header
title={title}
textStyles={titleStyles}
/>
</View>
{typeof prompt === 'string' ? <Text style={[promptStyles, isCentered ? styles.textAlignCenter : {}]}>{prompt}</Text> : prompt}
fvlvte marked this conversation as resolved.
Show resolved Hide resolved
</View>

{shouldStackButtons ? (
<>
<Button
success={success}
danger={danger}
style={[styles.mt4]}
onPress={onConfirm}
pressOnEnter
text={confirmText || translate('common.yes')}
isDisabled={isOffline && shouldDisableConfirmButtonWhenOffline}
/>
{shouldShowCancelButton && (
<Button
style={[styles.mt3, styles.noSelect]}
onPress={onCancel}
text={cancelText || translate('common.no')}
/>
)}
</>
) : (
<View style={[styles.flexRow, styles.gap4]}>
{shouldShowCancelButton && (
<Button
style={[styles.noSelect, styles.flex1]}
onPress={onCancel}
text={cancelText || translate('common.no')}
medium
/>
)}
<Button
success={success}
danger={danger}
style={[styles.flex1]}
onPress={onConfirm}
pressOnEnter
text={confirmText || translate('common.yes')}
isDisabled={isOffline && shouldDisableConfirmButtonWhenOffline}
medium
/>
</View>
)}
</View>
);
}

ConfirmContent.displayName = 'ConfirmContent';

export default ConfirmContent;
1 change: 1 addition & 0 deletions src/components/Icon/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,4 @@ function Icon({
Icon.displayName = 'Icon';

export default Icon;
export {type SrcProps};
Loading