diff --git a/docs/src/pages/[platform]/connected-components/account-settings/change-password/props.ts b/docs/src/pages/[platform]/connected-components/account-settings/change-password/props.ts new file mode 100644 index 00000000000..dae00de363d --- /dev/null +++ b/docs/src/pages/[platform]/connected-components/account-settings/change-password/props.ts @@ -0,0 +1,86 @@ +export const CHANGE_PASSWORD = [ + { + name: `onSuccess?`, + description: + 'Callback function triggered when password is successfully updated', + type: `() => void`, + }, + { + name: `onError?`, + description: 'Callback function triggered when change password fails', + type: `(error: Error) => void`, + }, + { + name: `validators?`, + description: 'Custom password validations', + type: `ValidatorOptions[]`, + }, + { + name: `components?`, + description: 'Submit button', + type: `ChangePasswordComponents`, + }, +]; + +export const OVERRIDES = [ + { + name: `CurrentPasswordField?`, + description: 'Password field for current password', + type: `PasswordFieldProps`, + }, + { + name: `NewPasswordField?`, + description: 'Password field for new password', + type: `PasswordFieldProps`, + }, + { + name: `ConfirmPasswordField?`, + description: 'Password field for confirm password', + type: `PasswordFieldProps`, + }, + { + name: `ErrorMessage?`, + description: 'Error alert that displays on delete user errors', + type: `ErrorMessageComponentProps`, + }, + { + name: `SubmitButton?`, + description: 'Submit button', + type: `SubmitButtonProps`, + }, +]; + +export const PASSWORD_FIELDS = [ + { + name: `onBlur`, + description: + 'Blur handler for the input. This must be passed to your input element.', + type: `React.FocusEventHandler`, + }, + { + name: `onChange`, + description: + 'Change handler for the input. This must be passed to your input element.', + type: `React.ChangeEventHandler`, + }, + { + name: `name`, + description: + 'HTML name for the input. This must be passed to your input element.', + type: `React.ChangeEventHandler`, + }, + { + name: `fieldValidationErrors?`, + description: 'List of validation errors for the password field.', + type: `string[]`, + }, +]; + +export const SUBMIT_BUTTON = [ + { + name: `isDisabled`, + description: + 'Boolean representing whether account deletion is in progress. Your delete button should be disabled if this is set to true.', + type: `boolean`, + }, +]; diff --git a/docs/src/pages/[platform]/connected-components/account-settings/change-password/react.mdx b/docs/src/pages/[platform]/connected-components/account-settings/change-password/react.mdx index 89b8551b62b..716c213d08a 100644 --- a/docs/src/pages/[platform]/connected-components/account-settings/change-password/react.mdx +++ b/docs/src/pages/[platform]/connected-components/account-settings/change-password/react.mdx @@ -1,3 +1,368 @@ -## Change Password +import { Alert, Expander, ExpanderItem } from '@aws-amplify/ui-react'; -TODO +import ReactPropsTable from '@/components/propsTable/ReactPropsTable'; +import { Example, ExampleCode } from '@/components/Example'; +import { ERROR_MESSAGE } from '../props'; +import { CHANGE_PASSWORD, OVERRIDES, PASSWORD_FIELDS, SUBMIT_BUTTON } from './props'; + + +### Props + + + +### Basic Usage + +`ChangePassword` has `onSuccess` handler that will be called after successful password change. You may use this callback to run any custom handling, such as navigating route or rendering confirmation messages. + + + + +```jsx +import React from 'react'; +import { AccountSettings } from '@aws-amplify/ui-react'; + +function App() { + const handleSuccess = () => { + alert('password is successfully changed!') + } + + return ( + + ); +} +``` + + + + +### Custom Validation + +You can override the default password validator with your own custom validator. To do so, you can use the `validators` prop, which takes an array of `ValidatorOptions`: + + + + +```ts +interface ValidatorOptions { + validationMode: 'onBlur' | 'onChange' | 'onTouched'; + validator: (value: string) => boolean; + message: string; +} +``` + + + + +- `onBlur` validates password on every blur event +- `onChange` validates password on every change event +- `onTouched` validates password on first blur, and on every change event after the first blur. + +For example, following is a minLength validator that validates on every change: + + + + +```js +const minLength = { + validationMode: 'onChange', + validator: (password) => password.length >= 4, + message: 'Password must have length 4 or greater', +}; +``` + + + + +You can pass multiple validators to `ChangePassword` to override the default validator: + + + + +```jsx +import React from 'react'; +import { AccountSettings } from '@aws-amplify/ui-react'; + +function App() { + const minLength = { + validationMode: 'onChange', + validator: (password) => password.length >= 4, + message: 'Password must have length 4 or greater', + }; + + const maxLength = { + validationMode: 'onChange', + validator: (password) => password.length <= 12, + message: 'Password must have length 12 or less', + }; + + const handleSuccess = () => { + alert('password is successfully changed!') + } + + return ( + + ); +} +``` + + + + +### Component Overrides + +You can provide your own UI components to override parts of `ChangePassword`. It supports the following slots: + + + +#### Reusing default components + +Default components are accessible through `AccountSettings.ChangePassword.COMPONENT_NAME` (e.g. `AccountSettings.ChangePassword.NewPassword`) for your convenience. This is useful if you're interested in modifying just a small part of UI instead of overriding the whole component. + + + + +```jsx +function App() { + const components = { + NewPassword: (props) => ( + + ), + } + + return ( + + ); +} +``` + + + + + +Note that spreading props is necessary so that event listeners like `onChange` or html attributes like `name` are passed correctly. If you're providing your own custom components, make sure required props are passed onto your elements. + + +#### Props and Examples + +Here are the required props and examples for overriding each slot. + + + + +`ChangePassword` has three password fields that you can override: `CurrentPassword`, `NewPassword`, and `ConfirmPassword`. They all take the same props: + + + + + +You can reuse the `AccountSettings.ChangePassword.`: + + + + +```jsx +function App() { + const components = { + NewPassword: (props) => ( + + ), + ConfirmPasswordField: (props) => ( + + ), + } + + return ( + + ); +} +``` + + + + +The following example overrides the `CurrentPasswordField` with native html: + + + + + +```jsx +function App() { + const components = { + CurrentPasswordField: ({ + fieldValidationErrors, + name, + onBlur, + onChange + }) => ( + <> + + {fieldValidationErrors?.map((error) => ( +

{error}

+ ))} + + ) + }; +} +``` + +
+
+ +
+ + + +Props: + + + +The following example overrides the error message component with [`Heading`](/components/heading) and [`Text`](/components/text) primitives. + + + + +```jsx +import { Heading, Text } from '@aws-amplify/ui-react'; + +function App() { + const components = { + ErrorMessage: ({ children }) => ( + <> + Custom Error Message + {children} + + ) + }; + + return ( + + ); +} +``` + + + + + + +Props: + + + +You can reuse the `AccountSettings.ChangePassword.SubmitButton`: + + + + +```jsx +import { AccountSettings } from '@aws-amplify/ui-react'; + +const components = { + SubmitButton: (props) => ( + + My Custom Button + + ) +} +``` + + + + +or use [button](/components/button) primitive directly: + + + + +```jsx +import { AccountSettings, Button } from '@aws-amplify/ui-react'; + +const components = { + SubmitButton: (props) => +} +``` + + + + +The following example replaces `SubmitButton` with native html button: + + + + +```jsx +function App() { + const components = { + SubmitButton: ({ onClick, isDisabled }) => ( + + ) + }; + + return ( + + ); +} +``` + + + + +
+ +### Theming + +`ChangePassword` component is built upon our robust [Amplify UI theming system](../../theming). To get started, add a theme object and set the appropriate [design tokens](../../theming#design-tokens). You can then pass that `theme` to the `ThemeProvider` so the changes can take affect. + + + + +```jsx +import { AccountSettings, Card, ThemeProvider } from '@aws-amplify/ui-react'; + +const theme = { + name: 'custom-theme', + tokens: { + colors: { + border: { + primary: { value: '{colors.neutral.40}' }, + secondary: { value: '{colors.neutral.20}' }, + tertiary: { value: '{colors.neutral.10}' }, + }, + }, + radii: { + small: { value: '2px' }, + medium: { value: '3px' }, + large: { value: '4px' }, + xl: { value: '6px' }, + }, + }, +}; + +function App() { + return ( + + + + + + ); +} +``` + + +