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

chore(account-settings): overrides cleanup #3131

Merged
merged 12 commits into from
Dec 2, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function DeleteUser({
const { user, isLoading } = useAuth();

// subcomponents
const { Error, SubmitButton, Warning } = React.useMemo(
const { Error, DeleteButton, Warning } = React.useMemo(
() => ({ ...DEFAULTS, ...(components ?? {}) }),
[components]
);
Expand Down Expand Up @@ -88,12 +88,12 @@ function DeleteUser({

return (
<Flex direction="column">
<SubmitButton
<DeleteButton
isDisabled={state === 'CONFIRMATION'}
onClick={startConfirmation}
>
{deleteAccountText}
</SubmitButton>
</DeleteButton>
{state === 'CONFIRMATION' || state === 'DELETING' ? (
<Warning
onCancel={handleCancel}
Expand All @@ -107,7 +107,7 @@ function DeleteUser({
}

DeleteUser.Error = DEFAULTS.Error;
DeleteUser.SubmitButton = DEFAULTS.SubmitButton;
DeleteUser.DeleteButton = DEFAULTS.DeleteButton;
DeleteUser.Warning = DEFAULTS.Warning;

export default DeleteUser;
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,40 @@ jest.mock('../../../../internal', () => ({

const deleteUserSpy = jest.spyOn(UIModule, 'deleteUser');

function CustomWarning({ onCancel, onConfirm, isDisabled }) {
function CustomWarning({ onCancel, onConfirm }) {
return (
<Flex direction="column">
<Text variation="warning">Custom Warning Message</Text>
<Button onClick={onCancel}>Back</Button>
<Button variation="primary" onClick={onConfirm} isDisabled={isDisabled}>
<Button variation="primary" onClick={onConfirm}>
Custom Confirm Button
</Button>
</Flex>
);
}

const CustomDeleteButton = ({ onClick, isDisabled }) => {
return (
<Button isDisabled={isDisabled} onClick={onClick}>
Custom Delete Button
</Button>
);
};

const CustomError = ({ children }) => (
<>
<Heading>Custom Error Message</Heading>
<Text>{children}</Text>
</>
);

const components: DeleteUserComponents = {
SubmitButton: (props) => <Button {...props}>Custom Delete Button</Button>,
DeleteButton: CustomDeleteButton,
Warning: CustomWarning,
Error: ({ children }) => (
<>
<Heading>Custom Error Message</Heading>
<Text>{children}</Text>
</>
),
Error: CustomError,
};

describe('ChangePassword', () => {
describe('DeleteUser', () => {
beforeEach(() => {
jest.clearAllMocks();
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`ChangePassword renders as expected 1`] = `
exports[`DeleteUser renders as expected 1`] = `
<div>
<div
class="amplify-flex"
Expand All @@ -17,7 +17,7 @@ exports[`ChangePassword renders as expected 1`] = `
</div>
`;

exports[`ChangePassword renders as expected with components overrides 1`] = `
exports[`DeleteUser renders as expected with components overrides 1`] = `
<div>
<div
class="amplify-flex"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const DefaultWarning: WarningComponent = ({

const DEFAULTS: Required<DeleteUserComponents> = {
Error: DefaultError,
SubmitButton: Button,
DeleteButton: Button,
Warning: DefaultWarning,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';

import { AmplifyUser } from '@aws-amplify/ui';

import { ErrorComponent, SubmitButtonComponent } from '../types';
import { ButtonComponent, ErrorComponent } from '../types';

export interface WarningProps {
/** called when end user cancels account deletion */
Expand All @@ -26,7 +26,7 @@ export type DeleteUserState =

export interface DeleteUserComponents {
Error?: ErrorComponent;
SubmitButton?: SubmitButtonComponent;
DeleteButton?: ButtonComponent;
Warning?: WarningComponent;
}

Expand Down
34 changes: 25 additions & 9 deletions packages/react/src/components/AccountSettings/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,45 @@ import {
PrimitiveProps,
} from '../../primitives/types';

/**
/*
* These are primitive prop types that account settings component use.
*
* Note that `PrimitiveProps` is used to get native html types, like `onSubmit`.
*/
type CommonPasswordFieldProps = Partial<
PrimitiveProps<PasswordFieldProps, 'input'>
>;
type CommonButtonProps = Partial<PrimitiveProps<ButtonProps, 'button'>>;
type CommonAlertProps = Partial<PrimitiveProps<AlertProps, 'div'>>;
type PasswordFieldPrimitiveProps = PrimitiveProps<PasswordFieldProps, 'input'>;
type ButtonPrimtiveProps = PrimitiveProps<ButtonProps, 'button'>;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
type ButtonPrimtiveProps = PrimitiveProps<ButtonProps, 'button'>;
type ButtonPrimitiveProps = PrimitiveProps<ButtonProps, 'button'>;

Then fix below as well

Copy link
Contributor

Choose a reason for hiding this comment

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

This is an interesting pattern using the PrimitiveProps nice

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for catching it, 37ded7e

type AlertPrimitiveProps = PrimitiveProps<AlertProps, 'div'>;

/*
* These are common prop types for component overrides.
*
* Any essential props for overriding components are marked as required.
*/
type CommonPasswordFieldProps = Partial<PasswordFieldPrimitiveProps> &
Required<Pick<PasswordFieldPrimitiveProps, 'onBlur' | 'onChange' | 'name'>>;
type CommonButtonProps = Partial<ButtonPrimtiveProps> &
Required<Pick<ButtonPrimtiveProps, 'onClick' | 'isDisabled'>>;
type CommonSubmitButtonProps = Partial<PrimitiveProps<ButtonProps, 'button'>> &
Required<Pick<ButtonPrimtiveProps, 'isDisabled'>>;
type CommonAlertProps = Partial<PrimitiveProps<AlertProps, 'div'>> &
Required<Pick<AlertPrimitiveProps, 'children'>>;

/**
* These are overridable component types (e.g. submit button).
/*
* These are component override types.
*/
export type PasswordFieldComponent<Props = {}> = React.ComponentType<
// `Props` generic allows additional props passed on override components
Props & CommonPasswordFieldProps & { validationErrors?: string[] }
>;

export type SubmitButtonComponent<Props = {}> = React.ComponentType<
export type ButtonComponent<Props = {}> = React.ComponentType<
Props & CommonButtonProps
>;

export type SubmitButtonComponent<Props = {}> = React.ComponentType<
Props & CommonSubmitButtonProps
>;

export type ErrorComponent<Props = {}> = React.ComponentType<
Props & CommonAlertProps
>;
Expand Down