-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(SingleToast): add new component (#3314)
- Loading branch information
Showing
8 changed files
with
148 additions
and
112 deletions.
There are no files selected for viewing
This file contains 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,35 @@ | ||
`<SingleToast>` можно добавить в единственном месте в проекте, а статические методы будут всегда использовать последний отрендеренный экземпляр `<Toast>`. | ||
|
||
_На внешний вид этого примера влияет следующий пример_ | ||
|
||
```jsx harmony | ||
import { Button, SingleToast } from '@skbkontur/react-ui'; | ||
|
||
<div> | ||
<SingleToast /> | ||
<Button onClick={() => SingleToast.push('Статический тост')}> | ||
Показать статический тост | ||
</Button> | ||
</div> | ||
``` | ||
|
||
`<SingleToast>` можно кастомизировать с помощью переменных темы для `toast`. | ||
```jsx harmony | ||
import { Button, SingleToast, ThemeContext, ThemeFactory, THEME_2022 } from '@skbkontur/react-ui'; | ||
|
||
const rand = () => "Пример жёлтого тоста № " + Math.round(Math.random() * 100).toString(); | ||
|
||
const pushToast = () => { | ||
SingleToast.push(rand(), { | ||
label: "Cancel", | ||
handler: () => SingleToast.push("Canceled") | ||
}); | ||
}; | ||
|
||
<div> | ||
<SingleToast theme={{ toastBg: '#f1c40f' }} /> | ||
<Button onClick={pushToast}> | ||
Показать тост с жёлтым фоном | ||
</Button> | ||
</div> | ||
``` |
This file contains 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,21 @@ | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
|
||
import { Toast, ToastProps } from '../Toast/Toast'; | ||
|
||
/** | ||
* Позволяет вызывать тосты с помощью статических методов. В отличие от статических методов из компонента `<Toast>` - их можно кастомизировать и они работают с `React@18`. | ||
*/ | ||
export class SingleToast extends React.Component<ToastProps> { | ||
public static ref = React.createRef<Toast>(); | ||
public static push: typeof Toast.push = (...args) => { | ||
SingleToast.close(); | ||
SingleToast.ref.current?.push(...args); | ||
}; | ||
public static close: typeof Toast.close = () => { | ||
ReactDOM.flushSync(() => SingleToast.ref.current?.close()); | ||
}; | ||
render = () => { | ||
return <Toast ref={SingleToast.ref} {...this.props} />; | ||
}; | ||
} |
65 changes: 65 additions & 0 deletions
65
packages/react-ui/components/SingleToast/__tests__/SingleToast.test.tsx
This file contains 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,65 @@ | ||
import React from 'react'; | ||
import { render, screen } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
|
||
import { LangCodes } from '../../../lib/locale'; | ||
import { Button } from '../../Button'; | ||
import { SingleToast } from '../SingleToast'; | ||
import { ToastDataTids } from '../../Toast'; | ||
import { ToastLocaleHelper } from '../../Toast/locale'; | ||
|
||
describe('ToastView', () => { | ||
describe('a11y', () => { | ||
it('has correct aria-label on close button', () => { | ||
function showComplexNotification() { | ||
SingleToast.push( | ||
'Successfully saved', | ||
{ | ||
label: 'Cancel', | ||
handler: () => SingleToast.push('Canceled'), | ||
}, | ||
15000, | ||
); | ||
} | ||
render( | ||
<> | ||
<SingleToast /> | ||
<Button onClick={showComplexNotification}>Show notification</Button> | ||
</>, | ||
); | ||
|
||
userEvent.click(screen.getByRole('button')); | ||
|
||
expect(screen.getByTestId(ToastDataTids.close)).toHaveAttribute( | ||
'aria-label', | ||
ToastLocaleHelper.get(LangCodes.ru_RU).closeButtonAriaLabel, | ||
); | ||
}); | ||
|
||
it('sets value for aria-label attribute (action button)', async () => { | ||
const ariaLabel = 'aria-label'; | ||
const buttonName = 'button'; | ||
function showComplexNotification() { | ||
SingleToast.push( | ||
'Successfully saved', | ||
{ | ||
label: 'Cancel', | ||
handler: () => SingleToast.push('Canceled'), | ||
'aria-label': ariaLabel, | ||
}, | ||
15000, | ||
); | ||
} | ||
render( | ||
<> | ||
<SingleToast /> | ||
<Button onClick={showComplexNotification}>{buttonName}</Button> | ||
</>, | ||
); | ||
|
||
userEvent.click(screen.getByRole('button', { name: buttonName })); | ||
|
||
expect(screen.getByTestId(ToastDataTids.action)).toHaveAttribute('aria-label', ariaLabel); | ||
}); | ||
}); | ||
}); |
This file contains 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 @@ | ||
export * from './SingleToast'; |
This file contains 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 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 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 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