-
-
Notifications
You must be signed in to change notification settings - Fork 11
feat: [DSRN] Added HeaderAlert #1064
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
Closed
+249
−0
Closed
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
03287fd
Added HeaderAlert to DSRN
brianacnguyen 295015c
Added additional tests to HeaderAlert
brianacnguyen 4e3db3a
Reverted solution
brianacnguyen 34e13fb
Merge branch 'main' into dsrn/headeralert
brianacnguyen 2dbdf2b
Merge branch 'main' into dsrn/headeralert
brianacnguyen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
49 changes: 49 additions & 0 deletions
49
packages/design-system-react-native/src/components/HeaderAlert/HeaderAlert.stories.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| import { IconAlertSeverity } from '@metamask/design-system-shared'; | ||
| import type { Meta, StoryObj } from '@storybook/react-native'; | ||
| import React from 'react'; | ||
|
|
||
| import { Box, BoxAlignItems } from '../Box'; | ||
|
|
||
| import { HeaderAlert } from './HeaderAlert'; | ||
| import type { HeaderAlertProps } from './HeaderAlert.types'; | ||
|
|
||
| const meta: Meta<HeaderAlertProps> = { | ||
| title: 'Components/HeaderAlert', | ||
| component: HeaderAlert, | ||
| argTypes: { | ||
| severity: { | ||
| control: 'select', | ||
| options: Object.values(IconAlertSeverity), | ||
| }, | ||
| twClassName: { control: 'text' }, | ||
| }, | ||
| }; | ||
|
|
||
| export default meta; | ||
|
|
||
| type Story = StoryObj<HeaderAlertProps>; | ||
|
|
||
| export const Default: Story = { | ||
| args: { | ||
| severity: IconAlertSeverity.Info, | ||
| onBack: () => null, | ||
| onClose: () => null, | ||
| }, | ||
| }; | ||
|
|
||
| export const Severity: Story = { | ||
| render: () => ( | ||
| <Box gap={4}> | ||
| {Object.values(IconAlertSeverity).map((severity) => ( | ||
| <Box key={severity} alignItems={BoxAlignItems.Center} gap={2}> | ||
| <HeaderAlert | ||
| severity={severity} | ||
| onBack={() => null} | ||
| onClose={() => null} | ||
| iconAlertProps={{ testID: `header-alert-icon-${severity}` }} | ||
| /> | ||
| </Box> | ||
| ))} | ||
| </Box> | ||
| ), | ||
| }; |
81 changes: 81 additions & 0 deletions
81
packages/design-system-react-native/src/components/HeaderAlert/HeaderAlert.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| import { IconAlertSeverity } from '@metamask/design-system-shared'; | ||
| import { useTailwind } from '@metamask/design-system-twrnc-preset'; | ||
| import { renderHook } from '@testing-library/react-hooks'; | ||
| import { render } from '@testing-library/react-native'; | ||
| import React from 'react'; | ||
|
|
||
| import { IconSize } from '../../types'; | ||
| import { TWCLASSMAP_ICON_SIZE_DIMENSION } from '../Icon/Icon.constants'; | ||
| import type { IconAlertProps } from '../IconAlert'; | ||
| import { ICON_ALERT_SEVERITY_MAP } from '../IconAlert/IconAlert.constants'; | ||
|
|
||
| import { HeaderAlert } from './HeaderAlert'; | ||
|
|
||
| type IconAlertSeverityUnion = | ||
| (typeof IconAlertSeverity)[keyof typeof IconAlertSeverity]; | ||
|
|
||
| describe('HeaderAlert', () => { | ||
| let tw: ReturnType<typeof useTailwind>; | ||
|
|
||
| beforeAll(() => { | ||
| tw = renderHook(() => useTailwind()).result.current; | ||
| }); | ||
|
|
||
| describe('when a severity is provided', () => { | ||
| it.each(Object.values(IconAlertSeverity) as IconAlertSeverityUnion[])( | ||
| 'renders IconAlert at Lg with mapped color for %s', | ||
| (severity) => { | ||
| const { getByTestId } = render( | ||
| <HeaderAlert | ||
| severity={severity} | ||
| iconAlertProps={{ testID: 'header-alert-icon' }} | ||
| />, | ||
| ); | ||
|
|
||
| const icon = getByTestId('header-alert-icon'); | ||
| const { color } = ICON_ALERT_SEVERITY_MAP[severity]; | ||
|
|
||
| expect(icon).toBeOnTheScreen(); | ||
| expect(icon).toHaveStyle( | ||
| tw.style(color, TWCLASSMAP_ICON_SIZE_DIMENSION[IconSize.Lg]), | ||
| ); | ||
| }, | ||
| ); | ||
| }); | ||
|
|
||
| describe('when iconAlertProps includes a severity at runtime', () => { | ||
| it('uses HeaderAlert severity for icon mapping', () => { | ||
| const { getByTestId } = render( | ||
| <HeaderAlert | ||
| severity={IconAlertSeverity.Error} | ||
| iconAlertProps={ | ||
| { | ||
| severity: IconAlertSeverity.Info, | ||
| testID: 'header-alert-icon', | ||
| } as IconAlertProps | ||
| } | ||
| />, | ||
| ); | ||
|
|
||
| const icon = getByTestId('header-alert-icon'); | ||
| const { color } = ICON_ALERT_SEVERITY_MAP[IconAlertSeverity.Error]; | ||
|
|
||
| expect(icon).toHaveStyle( | ||
| tw.style(color, TWCLASSMAP_ICON_SIZE_DIMENSION[IconSize.Lg]), | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| it('forwards HeaderBase props such as testID', () => { | ||
| const { getByTestId } = render( | ||
| <HeaderAlert | ||
| severity={IconAlertSeverity.Info} | ||
| testID="header-alert-root" | ||
| iconAlertProps={{ testID: 'header-alert-icon' }} | ||
| />, | ||
| ); | ||
|
|
||
| expect(getByTestId('header-alert-root')).toBeOnTheScreen(); | ||
| expect(getByTestId('header-alert-icon')).toBeOnTheScreen(); | ||
| }); | ||
| }); |
73 changes: 73 additions & 0 deletions
73
packages/design-system-react-native/src/components/HeaderAlert/HeaderAlert.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| // Third party dependencies. | ||
| import React, { useMemo } from 'react'; | ||
|
|
||
| // External dependencies. | ||
| import { IconSize } from '../../types'; | ||
| import type { ButtonIconProps } from '../ButtonIcon'; | ||
| import { HeaderBase } from '../HeaderBase'; | ||
| import { IconName } from '../Icon'; | ||
| import { IconAlert } from '../IconAlert'; | ||
|
|
||
| // Internal dependencies. | ||
| import type { HeaderAlertProps } from './HeaderAlert.types'; | ||
|
|
||
| export const HeaderAlert: React.FC<HeaderAlertProps> = ({ | ||
| severity, | ||
| iconAlertProps, | ||
| onBack, | ||
| backButtonProps, | ||
| onClose, | ||
| closeButtonProps, | ||
| endButtonIconProps, | ||
| startButtonIconProps, | ||
| twClassName = '', | ||
| testID, | ||
| ...headerBaseProps | ||
| }) => { | ||
| const resolvedStartButtonIconProps = useMemo(() => { | ||
| if (startButtonIconProps) { | ||
| return startButtonIconProps; | ||
| } | ||
| if (onBack || backButtonProps) { | ||
| const startProps: ButtonIconProps = { | ||
| iconName: IconName.ArrowLeft, | ||
| ...(backButtonProps ?? {}), | ||
| onPress: backButtonProps?.onPress ?? onBack, | ||
| }; | ||
| return startProps; | ||
| } | ||
| return undefined; | ||
| }, [onBack, backButtonProps, startButtonIconProps]); | ||
|
|
||
| const resolvedEndButtonIconProps = useMemo(() => { | ||
| const props: ButtonIconProps[] = []; | ||
|
|
||
| if (onClose || closeButtonProps) { | ||
| const closeProps: ButtonIconProps = { | ||
| iconName: IconName.Close, | ||
| ...(closeButtonProps || {}), | ||
| onPress: closeButtonProps?.onPress ?? onClose, | ||
| }; | ||
| props.push(closeProps); | ||
| } | ||
|
|
||
| if (endButtonIconProps) { | ||
| props.push(...endButtonIconProps); | ||
| } | ||
| return props.length > 0 ? props : undefined; | ||
| }, [endButtonIconProps, onClose, closeButtonProps]); | ||
|
|
||
| return ( | ||
| <HeaderBase | ||
| testID={testID} | ||
| startButtonIconProps={resolvedStartButtonIconProps} | ||
| endButtonIconProps={resolvedEndButtonIconProps} | ||
| {...headerBaseProps} | ||
| twClassName={`px-2 ${twClassName}`} | ||
| > | ||
| <IconAlert {...iconAlertProps} severity={severity} size={IconSize.Lg} /> | ||
| </HeaderBase> | ||
| ); | ||
| }; | ||
|
|
||
| HeaderAlert.displayName = 'HeaderAlert'; | ||
23 changes: 23 additions & 0 deletions
23
packages/design-system-react-native/src/components/HeaderAlert/HeaderAlert.types.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import type { HeaderAlertPropsShared } from '@metamask/design-system-shared'; | ||
|
|
||
| import type { HeaderBaseProps } from '../HeaderBase'; | ||
| import type { HeaderStandardProps } from '../HeaderStandard'; | ||
| import type { IconAlertProps } from '../IconAlert'; | ||
|
|
||
| type HeaderStandardNavigationShortcuts = Pick< | ||
| HeaderStandardProps, | ||
| 'onBack' | 'backButtonProps' | 'onClose' | 'closeButtonProps' | ||
| >; | ||
|
|
||
| /** | ||
| * HeaderAlert component props (React Native). | ||
| */ | ||
| export type HeaderAlertProps = Omit<HeaderBaseProps, 'children'> & | ||
| HeaderStandardNavigationShortcuts & | ||
| HeaderAlertPropsShared & { | ||
| /** | ||
| * Props for the inner IconAlert. `severity` and `size` are always set by | ||
| * HeaderAlert and are omitted from this type. | ||
| */ | ||
| iconAlertProps?: Omit<IconAlertProps, 'severity' | 'size'>; | ||
| }; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.