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.

161 changes: 161 additions & 0 deletions src/components/ConfirmContent.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
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 '@styles/useThemeStyles';
import variables from '@styles/variables';
import Button from './Button';
import Header from './Header';
import Icon 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: (...args: unknown[]) => unknown;

/** A callback to call when the form has been closed */
onCancel: (...args: unknown[]) => unknown;
fvlvte marked this conversation as resolved.
Show resolved Hide resolved

/** Confirm button text */
confirmText: string;
fvlvte marked this conversation as resolved.
Show resolved Hide resolved

/** Cancel button text */
cancelText: string;
fvlvte marked this conversation as resolved.
Show resolved Hide resolved

/** Modal content text/element */
prompt: string | ReactNode;
fvlvte marked this conversation as resolved.
Show resolved Hide resolved

/** Whether we should use the success button color */
success: boolean;
fvlvte marked this conversation as resolved.
Show resolved Hide resolved

/** Whether we should use the danger button color. Use if the action is destructive */
danger: boolean;
fvlvte marked this conversation as resolved.
Show resolved Hide resolved

/** Whether we should disable the confirm button when offline */
shouldDisableConfirmButtonWhenOffline: boolean;
fvlvte marked this conversation as resolved.
Show resolved Hide resolved

/** Whether we should show the cancel button */
shouldShowCancelButton: boolean;
fvlvte marked this conversation as resolved.
Show resolved Hide resolved

/** Icon to display above the title */
iconSource: null | string | ((props: unknown) => ReactNode);
fvlvte marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't found specific case where iconSource being passed to this component, could you confirm if there is a case where iconSource is used

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not used anywhere but I think TS migration is not a place for feature removing - what do You think @fabioh8010?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if we should remove to be honest.. it was added 4 months ago, looks still "fresh". @grgia I saw that your commit added this support for icon along with other thing, but this prop is not being used anymore. Do you know if we can remove the prop alongside with the logic to show the icon? Thanks!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we got no response, @fvlvte let's keep it and proceed with this PR. Please let me know when the PR is ready to be re-reviewed again!


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

/** Whether to stack the buttons */
shouldStackButtons: boolean;
fvlvte marked this conversation as resolved.
Show resolved Hide resolved

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

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

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

/** Styles for icon */
iconAdditionalStyles: Array<StyleProp<ViewStyle>>;
fvlvte marked this conversation as resolved.
Show resolved Hide resolved
fvlvte marked this conversation as resolved.
Show resolved Hide resolved
};

function ConfirmContent({
title,
onConfirm,
onCancel = () => {},
confirmText = '',
cancelText = '',
prompt = '',
success = true,
danger = false,
shouldDisableConfirmButtonWhenOffline = false,
shouldShowCancelButton = false,
iconSource = null,
fvlvte marked this conversation as resolved.
Show resolved Hide resolved
shouldCenterContent = false,
shouldStackButtons = true,
titleStyles = [],
promptStyles = [],
contentStyles = [],
iconAdditionalStyles = [],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need in default values for styles

Suggested change
titleStyles = [],
promptStyles = [],
contentStyles = [],
iconAdditionalStyles = [],
titleStyles,
promptStyles,
contentStyles,
iconAdditionalStyles,

fvlvte marked this conversation as resolved.
Show resolved Hide resolved
}: 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
fvlvte marked this conversation as resolved.
Show resolved Hide resolved
<View style={isCentered ? [styles.alignItemsCenter, styles.mb6] : []}>
{(iconSource !== null && Object.keys(iconSource).length !== 0) ||
(typeof iconSource === 'function' && (
<View style={[styles.flexRow, styles.mb3]}>
<Icon
src={iconSource}
width={variables.appModalAppIconSize}
height={variables.appModalAppIconSize}
additionalStyles={[...iconAdditionalStyles]}
fvlvte marked this conversation as resolved.
Show resolved Hide resolved
/>
</View>
))}

<View style={[styles.flexRow, isCentered ? {} : styles.mb4]}>
<Header
title={title}
textStyles={[...titleStyles]}
fvlvte marked this conversation as resolved.
Show resolved Hide resolved
/>
</View>
{typeof prompt === 'string' ? <Text style={[...promptStyles, isCentered ? styles.textAlignCenter : {}]}>{prompt}</Text> : prompt}
fvlvte marked this conversation as resolved.
Show resolved Hide resolved
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;
Loading