-
Notifications
You must be signed in to change notification settings - Fork 351
refactor(notification): migrate Notification from Flow to TypeScript #4777
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
Open
bonchevskyi
wants to merge
1
commit into
box:master
Choose a base branch
from
bonchevskyi:refactor/flow-to-ts-notification
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
File renamed without changes.
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,152 @@ | ||
| import * as React from 'react'; | ||
| import { defineMessages, injectIntl } from 'react-intl'; | ||
| import type { WrappedComponentProps } from 'react-intl'; | ||
| import classNames from 'classnames'; | ||
|
|
||
| import { | ||
| AlertCircle, | ||
| InformationCircle, | ||
| CheckmarkCircle, | ||
| AlertTriangle, | ||
| XMark, | ||
| } from '@box/blueprint-web-assets/icons/Medium'; | ||
|
|
||
| import InfoBadge16 from '../../icon/line/InfoBadge16'; | ||
| import CircleCheck16 from '../../icon/line/CircleCheck16'; | ||
| import TriangleAlert16 from '../../icon/line/TriangleAlert16'; | ||
|
|
||
| import XBadge16 from '../../icon/line/XBadge16'; | ||
| import X16 from '../../icon/fill/X16'; | ||
|
|
||
| import type { NotificationType } from '../../common/types/core'; | ||
|
|
||
| import './Notification.scss'; | ||
|
|
||
| // @NOTE: We can't import these constants from ./constant.js because `react-docgen` | ||
| // can't handle imported variables appear in propTypes | ||
| // see https://github.com/reactjs/react-docgen/issues/33 | ||
| const DURATION_SHORT = 'short'; | ||
| const DURATION_LONG = 'long'; | ||
| const OVERFLOW_WRAP = 'wrap'; | ||
| const TYPE_DEFAULT = 'default'; | ||
| const TYPE_INFO = 'info'; | ||
| const TYPE_WARN = 'warn'; | ||
| const TYPE_ERROR = 'error'; | ||
|
|
||
| const DURATION_TIMES = { | ||
| [DURATION_SHORT]: 5000, | ||
| [DURATION_LONG]: 10000, | ||
| }; | ||
|
|
||
| const ICON_RENDERER: Record<NotificationType, (useV2Icons?: boolean) => React.ReactElement> = { | ||
| [TYPE_DEFAULT]: useV2Icons => (useV2Icons ? <InformationCircle /> : <InfoBadge16 />), | ||
| [TYPE_ERROR]: useV2Icons => (useV2Icons ? <AlertCircle /> : <XBadge16 />), | ||
| [TYPE_INFO]: useV2Icons => (useV2Icons ? <CheckmarkCircle /> : <CircleCheck16 />), | ||
| [TYPE_WARN]: useV2Icons => (useV2Icons ? <AlertTriangle /> : <TriangleAlert16 />), | ||
| }; | ||
|
|
||
| const messages = defineMessages({ | ||
| clearNotificationButtonText: { | ||
| defaultMessage: 'Clear Notification', | ||
| description: 'Button to clear notification', | ||
| id: 'boxui.notification.clearNotification', | ||
| }, | ||
| }); | ||
|
|
||
| export interface NotificationProps { | ||
| /** | ||
| * The contents of the `Notification`. | ||
| * - Notification text must be wrapped in a `<span />` tag. | ||
| * - Notification buttons must be the `<Button />` component. | ||
| */ | ||
| children: React.ReactNode; | ||
| /** Additional CSS class for the notification */ | ||
| className?: string; | ||
| /** | ||
| * When set, dictates how long the notification will exist before calling `onClose`. | ||
| * If unset, the notification will not automatically call `onClose`. | ||
| * - `short`: 5s | ||
| * - `long`: 10s | ||
| */ | ||
| duration?: 'short' | 'long'; | ||
| /** Function that gets executed when close button is clicked or when duration expires. */ | ||
| onClose?: (event?: React.SyntheticEvent) => void; | ||
| /** | ||
| * Determines notification colors | ||
| * - `default`: black | ||
| * - `info`: green | ||
| * - `warn`: yellow | ||
| * - `error`: red | ||
| */ | ||
| type?: NotificationType; | ||
| /** How notification text overflow is handled */ | ||
| overflow?: 'wrap' | 'ellipsis'; | ||
| /** When true, render Blueprint v2 icons instead of the local icon set */ | ||
| useV2Icons?: boolean; | ||
| } | ||
|
|
||
| class Notification extends React.Component<NotificationProps & WrappedComponentProps> { | ||
| static defaultProps: Pick<NotificationProps, 'overflow' | 'type'> = { | ||
| overflow: OVERFLOW_WRAP, | ||
| type: TYPE_DEFAULT, | ||
| }; | ||
|
|
||
| componentDidMount() { | ||
| const { duration, onClose } = this.props; | ||
| this.timeout = duration && onClose ? setTimeout(onClose, DURATION_TIMES[duration]) : null; | ||
| } | ||
|
|
||
| componentWillUnmount() { | ||
| if (this.timeout) { | ||
| clearTimeout(this.timeout); | ||
| } | ||
| } | ||
|
|
||
| onClose = (event?: React.SyntheticEvent) => { | ||
| const { onClose } = this.props; | ||
| if (this.timeout) { | ||
| clearTimeout(this.timeout); | ||
| } | ||
|
|
||
| if (onClose) { | ||
| onClose(event); | ||
| } | ||
| }; | ||
|
|
||
| getChildren() { | ||
| const { children } = this.props; | ||
| return typeof children === 'string' ? <span>{children}</span> : children; | ||
| } | ||
|
|
||
| timeout: ReturnType<typeof setTimeout> | null; | ||
|
|
||
| render() { | ||
| const contents = this.getChildren(); | ||
| const { intl, type = TYPE_DEFAULT, overflow, className, useV2Icons } = this.props; | ||
| const { formatMessage } = intl; | ||
| const classes = classNames('notification', type, overflow, className); | ||
| const iconRenderer = ICON_RENDERER[type](useV2Icons); | ||
| const iconColor = useV2Icons ? '#222' : '#fff'; | ||
|
|
||
| return ( | ||
| <div className={classes}> | ||
| {React.cloneElement(iconRenderer, { | ||
| color: iconColor, | ||
| height: 20, | ||
| width: 20, | ||
| })} | ||
| {contents} | ||
| <button | ||
| aria-label={formatMessage(messages.clearNotificationButtonText)} | ||
| className="close-btn" | ||
| onClick={this.onClose} | ||
| type="button" | ||
| > | ||
| {useV2Icons ? <XMark height={32} width={32} /> : <X16 />} | ||
| </button> | ||
| </div> | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| export default injectIntl(Notification); | ||
File renamed without changes.
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,18 @@ | ||
| import * as React from 'react'; | ||
|
|
||
| import FocusTrap from '../focus-trap'; | ||
| import Portal from '../portal'; | ||
|
|
||
| export interface NotificationsWrapperProps { | ||
| /** Notification elements to render inside the live region */ | ||
| children?: React.ReactNode; | ||
| } | ||
|
|
||
| const NotificationsWrapper = ({ children }: NotificationsWrapperProps) => ( | ||
| // @ts-ignore Portal forwards children and extra HTML attributes at runtime | ||
| <Portal className="notifications-wrapper" aria-live="polite"> | ||
| {children ? <FocusTrap className="notification-container">{children}</FocusTrap> : null} | ||
| </Portal> | ||
| ); | ||
|
|
||
| export default NotificationsWrapper; |
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
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
File renamed without changes.
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,13 @@ | ||
| // Duration constants | ||
| export const DURATION_SHORT = 'short'; | ||
| export const DURATION_LONG = 'long'; | ||
|
|
||
| // Type constants | ||
| export const TYPE_DEFAULT = 'default'; | ||
| export const TYPE_INFO = 'info'; | ||
| export const TYPE_WARN = 'warn'; | ||
| export const TYPE_ERROR = 'error'; | ||
|
|
||
| // Overflow constants | ||
| export const OVERFLOW_WRAP = 'wrap'; | ||
| export const OVERFLOW_ELLIPSIS = 'ellipsis'; |
File renamed without changes.
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,7 @@ | ||
| import * as NotificationConstants from './constants'; | ||
| import Notification from './Notification'; | ||
| import NotificationsWrapper from './NotificationsWrapper'; | ||
|
|
||
| export { Notification, NotificationConstants, NotificationsWrapper }; | ||
| export type { NotificationProps } from './Notification'; | ||
| export type { NotificationsWrapperProps } from './NotificationsWrapper'; |
1 change: 0 additions & 1 deletion
1
...ification/stories/Notification.stories.js → ...fication/stories/Notification.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 |
|---|---|---|
| @@ -1,4 +1,3 @@ | ||
| // @flow | ||
| import * as React from 'react'; | ||
| import { IntlProvider } from 'react-intl'; | ||
|
|
||
|
|
||
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
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.