Skip to content

Commit

Permalink
Merge pull request #31303 from kubabutkiewicz/ts-migration/FormAlertW…
Browse files Browse the repository at this point in the history
…ithSubmitButton/component

[TS migration] Migrate 'FormAlertWithSubmitButton.js' component to TypeScript
  • Loading branch information
rlinoz authored Dec 18, 2023
2 parents 37d34e8 + 7f17798 commit 11d7821
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 125 deletions.
125 changes: 0 additions & 125 deletions src/components/FormAlertWithSubmitButton.js

This file was deleted.

120 changes: 120 additions & 0 deletions src/components/FormAlertWithSubmitButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import React from 'react';
import {StyleProp, View, ViewStyle} from 'react-native';
import useThemeStyles from '@hooks/useThemeStyles';
import Button from './Button';
import FormAlertWrapper from './FormAlertWrapper';

type FormAlertWithSubmitButtonProps = {
/** Error message to display above button */
message?: string;

/** Whether the button is disabled */
isDisabled?: boolean;

/** Whether message is in html format */
isMessageHtml?: boolean;

/** Styles for container element */
containerStyles?: StyleProp<ViewStyle>;

/** Is the button in a loading state */
isLoading?: boolean;

/** Callback fired when the "fix the errors" link is pressed */
onFixTheErrorsLinkPressed?: () => void;

/** Submit function */
onSubmit: () => void;

/** Should the button be enabled when offline */
enabledWhenOffline?: boolean;

/** Disable press on enter for submit button */
disablePressOnEnter?: boolean;

/** Whether the form submit action is dangerous */
isSubmitActionDangerous?: boolean;

/** Custom content to display in the footer after submit button */
footerContent?: React.ReactNode;

/** Styles for the button */
buttonStyles?: StyleProp<ViewStyle>;

/** Whether to show the alert text */
isAlertVisible: boolean;

/** Text for the button */
buttonText: string;

/** Whether to use a smaller submit button size */
useSmallerSubmitButtonSize?: boolean;

/** Style for the error message for submit button */
errorMessageStyle?: StyleProp<ViewStyle>;
};

function FormAlertWithSubmitButton({
message = '',
isDisabled = false,
isMessageHtml = false,
containerStyles,
isLoading = false,
onFixTheErrorsLinkPressed = () => {},
enabledWhenOffline = false,
disablePressOnEnter = false,
isSubmitActionDangerous = false,
footerContent = null,
buttonStyles,
buttonText,
isAlertVisible,
onSubmit,
useSmallerSubmitButtonSize = false,
errorMessageStyle,
}: FormAlertWithSubmitButtonProps) {
const styles = useThemeStyles();
const style = [!footerContent ? {} : styles.mb3, buttonStyles];

return (
<FormAlertWrapper
containerStyles={[styles.mh5, styles.mb5, styles.justifyContentEnd, containerStyles]}
isAlertVisible={isAlertVisible}
isMessageHtml={isMessageHtml}
message={message}
onFixTheErrorsLinkPressed={onFixTheErrorsLinkPressed}
errorMessageStyle={errorMessageStyle}
>
{(isOffline: boolean | undefined) => (
<View>
{isOffline && !enabledWhenOffline ? (
<Button
success
isDisabled
text={buttonText}
style={style}
danger={isSubmitActionDangerous}
medium={useSmallerSubmitButtonSize}
/>
) : (
<Button
success
pressOnEnter={!disablePressOnEnter}
text={buttonText}
style={style}
onPress={onSubmit}
isDisabled={isDisabled}
isLoading={isLoading}
danger={isSubmitActionDangerous}
medium={useSmallerSubmitButtonSize}
/>
)}
{footerContent}
</View>
)}
</FormAlertWrapper>
);
}

FormAlertWithSubmitButton.displayName = 'FormAlertWithSubmitButton';

export default FormAlertWithSubmitButton;

0 comments on commit 11d7821

Please sign in to comment.