import React, { useCallback, useState } from 'react';
import Button from 'devextreme-react/button';
import RadioGroup from 'devextreme-react/radio-group';
import SelectBox from 'devextreme-react/select-box';
import type { SelectBoxTypes } from 'devextreme-react/select-box';
import NumberBox from 'devextreme-react/number-box';
import Notify from 'devextreme/ui/notify';
import HideToasts from 'devextreme/ui/toast/hide_toasts';
import type { NotifyStack } from './types.ts';
import {
directions,
positions,
types,
radioGroupItems,
positionTopLabel,
directionLabel,
positionBottomLabel,
positionLeftLabel,
positionRightLabel,
positionLabel,
} from './data.ts';
function App() {
const [id, setId] = useState<number>(1);
const [isPredefined, setIsPredefined] = useState<boolean>(true);
const [predefinedPosition, setPredefinedPosition] = useState<NotifyStack['position']>('bottom center');
const [coordinatePosition, setCoordinatePosition] = useState<NotifyStack['position']>({
top: undefined,
left: undefined,
bottom: undefined,
right: undefined,
});
const [direction, setDirection] = useState<NotifyStack['direction']>('up-push');
const topNumberBoxValueChanged = useCallback(
(top: number | undefined): void => setCoordinatePosition({ ...coordinatePosition, top }),
[coordinatePosition],
);
const bottomNumberBoxValueChanged = useCallback(
(bottom: number | undefined): void => setCoordinatePosition({ ...coordinatePosition, bottom }),
[coordinatePosition],
);
const leftNumberBoxValueChanged = useCallback(
(left: number | undefined): void => setCoordinatePosition({ ...coordinatePosition, left }),
[coordinatePosition],
);
const rightNumberBoxValueChanged = useCallback(
(right: number | undefined): void => setCoordinatePosition({ ...coordinatePosition, right }),
[coordinatePosition],
);
const show = useCallback(() => {
const position: NotifyStack['position'] = isPredefined ? predefinedPosition : coordinatePosition;
Notify({
message: `Toast ${id}`,
height: 45,
width: 150,
minWidth: 150,
type: types[Math.floor(Math.random() * 4)],
displayTime: 3500,
animation: {
show: {
type: 'fade', duration: 400, from: 0, to: 1,
},
hide: { type: 'fade', duration: 40, to: 0 },
},
}, {
position,
direction,
});
setId(id + 1);
}, [id, isPredefined, predefinedPosition, coordinatePosition, direction]);
return (
<>
<div className='options'>
<div>Position</div>
<RadioGroup
layout='horizontal'
defaultValue='predefined'
items={radioGroupItems}
onValueChange={(value: string): void => setIsPredefined(value === 'predefined')} />
<SelectBox
items={positions}
value={predefinedPosition}
inputAttr={positionLabel}
onSelectionChanged={({ selectedItem }: SelectBoxTypes.SelectionChangedEvent): void => setPredefinedPosition(selectedItem)}
visible={isPredefined} />
<div className='section'>
<NumberBox
visible={!isPredefined}
placeholder='top'
defaultValue={undefined}
valueChangeEvent='keyup'
disabled={!!coordinatePosition.bottom}
inputAttr={positionTopLabel}
onValueChange={topNumberBoxValueChanged} />
<NumberBox
visible={!isPredefined}
placeholder='bottom'
defaultValue={undefined}
valueChangeEvent='keyup'
inputAttr={positionBottomLabel}
disabled={!!coordinatePosition.top}
onValueChange={bottomNumberBoxValueChanged} />
</div>
<div className='section'>
<NumberBox
visible={!isPredefined}
placeholder='left'
defaultValue={undefined}
valueChangeEvent='keyup'
inputAttr={positionLeftLabel}
disabled={!!coordinatePosition.right}
onValueChange={leftNumberBoxValueChanged} />
<NumberBox
visible={!isPredefined}
placeholder='right'
defaultValue={undefined}
valueChangeEvent='keyup'
inputAttr={positionRightLabel}
disabled={!!coordinatePosition.left}
onValueChange={rightNumberBoxValueChanged} />
</div>
<div>Direction</div>
<SelectBox
items={directions}
inputAttr={directionLabel}
value={direction}
onSelectionChanged={({ selectedItem }: SelectBoxTypes.SelectionChangedEvent): void => setDirection(selectedItem)} />
<div className='section'>
<Button text='Show' width='48%' onClick={show} />
<Button text='Hide all' width='48%' onClick={() => HideToasts()} />
</div>
</div>
</>
);
}
export default App;
import React, { useCallback, useState } from 'react';
import Button from 'devextreme-react/button';
import RadioGroup from 'devextreme-react/radio-group';
import SelectBox from 'devextreme-react/select-box';
import NumberBox from 'devextreme-react/number-box';
import Notify from 'devextreme/ui/notify';
import HideToasts from 'devextreme/ui/toast/hide_toasts';
import {
directions,
positions,
types,
radioGroupItems,
positionTopLabel,
directionLabel,
positionBottomLabel,
positionLeftLabel,
positionRightLabel,
positionLabel,
} from './data.js';
function App() {
const [id, setId] = useState(1);
const [isPredefined, setIsPredefined] = useState(true);
const [predefinedPosition, setPredefinedPosition] = useState('bottom center');
const [coordinatePosition, setCoordinatePosition] = useState({
top: undefined,
left: undefined,
bottom: undefined,
right: undefined,
});
const [direction, setDirection] = useState('up-push');
const topNumberBoxValueChanged = useCallback(
(top) => setCoordinatePosition({ ...coordinatePosition, top }),
[coordinatePosition],
);
const bottomNumberBoxValueChanged = useCallback(
(bottom) => setCoordinatePosition({ ...coordinatePosition, bottom }),
[coordinatePosition],
);
const leftNumberBoxValueChanged = useCallback(
(left) => setCoordinatePosition({ ...coordinatePosition, left }),
[coordinatePosition],
);
const rightNumberBoxValueChanged = useCallback(
(right) => setCoordinatePosition({ ...coordinatePosition, right }),
[coordinatePosition],
);
const show = useCallback(() => {
const position = isPredefined ? predefinedPosition : coordinatePosition;
Notify(
{
message: `Toast ${id}`,
height: 45,
width: 150,
minWidth: 150,
type: types[Math.floor(Math.random() * 4)],
displayTime: 3500,
animation: {
show: {
type: 'fade',
duration: 400,
from: 0,
to: 1,
},
hide: { type: 'fade', duration: 40, to: 0 },
},
},
{
position,
direction,
},
);
setId(id + 1);
}, [id, isPredefined, predefinedPosition, coordinatePosition, direction]);
return (
<>
<div className="options">
<div>Position</div>
<RadioGroup
layout="horizontal"
defaultValue="predefined"
items={radioGroupItems}
onValueChange={(value) => setIsPredefined(value === 'predefined')}
/>
<SelectBox
items={positions}
value={predefinedPosition}
inputAttr={positionLabel}
onSelectionChanged={({ selectedItem }) => setPredefinedPosition(selectedItem)}
visible={isPredefined}
/>
<div className="section">
<NumberBox
visible={!isPredefined}
placeholder="top"
defaultValue={undefined}
valueChangeEvent="keyup"
disabled={!!coordinatePosition.bottom}
inputAttr={positionTopLabel}
onValueChange={topNumberBoxValueChanged}
/>
<NumberBox
visible={!isPredefined}
placeholder="bottom"
defaultValue={undefined}
valueChangeEvent="keyup"
inputAttr={positionBottomLabel}
disabled={!!coordinatePosition.top}
onValueChange={bottomNumberBoxValueChanged}
/>
</div>
<div className="section">
<NumberBox
visible={!isPredefined}
placeholder="left"
defaultValue={undefined}
valueChangeEvent="keyup"
inputAttr={positionLeftLabel}
disabled={!!coordinatePosition.right}
onValueChange={leftNumberBoxValueChanged}
/>
<NumberBox
visible={!isPredefined}
placeholder="right"
defaultValue={undefined}
valueChangeEvent="keyup"
inputAttr={positionRightLabel}
disabled={!!coordinatePosition.left}
onValueChange={rightNumberBoxValueChanged}
/>
</div>
<div>Direction</div>
<SelectBox
items={directions}
inputAttr={directionLabel}
value={direction}
onSelectionChanged={({ selectedItem }) => setDirection(selectedItem)}
/>
<div className="section">
<Button
text="Show"
width="48%"
onClick={show}
/>
<Button
text="Hide all"
width="48%"
onClick={() => HideToasts()}
/>
</div>
</div>
</>
);
}
export default App;
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App.tsx';
ReactDOM.render(
<App />,
document.getElementById('app'),
);
import type { ToastType } from 'devextreme/ui/toast';
import type { NotifyStack } from './types.ts';
export const directions: NotifyStack['direction'][] = [
'down-push',
'down-stack',
'up-push',
'up-stack',
'left-push',
'left-stack',
'right-push',
'right-stack',
];
export const positions: NotifyStack['position'][] = [
'top left',
'top center',
'top right',
'bottom left',
'bottom center',
'bottom right',
'left center',
'center',
'right center',
];
export const types: ToastType[] = ['error', 'info', 'success', 'warning'];
export const radioGroupItems: string[] = ['predefined', 'coordinates'];
export const positionTopLabel = { 'aria-label': 'Position Top' };
export const positionBottomLabel = { 'aria-label': 'Position Bottom' };
export const positionLeftLabel = { 'aria-label': 'Position Left' };
export const positionRightLabel = { 'aria-label': 'Position Right' };
export const directionLabel = { 'aria-label': 'Direction' };
export const positionLabel = { 'aria-label': 'Position' };
import Notify from 'devextreme/ui/notify';
export type NotifyStack = (typeof Notify)['arguments'][1];
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App.js';
ReactDOM.render(<App />, document.getElementById('app'));
export const directions = [
'down-push',
'down-stack',
'up-push',
'up-stack',
'left-push',
'left-stack',
'right-push',
'right-stack',
];
export const positions = [
'top left',
'top center',
'top right',
'bottom left',
'bottom center',
'bottom right',
'left center',
'center',
'right center',
];
export const types = ['error', 'info', 'success', 'warning'];
export const radioGroupItems = ['predefined', 'coordinates'];
export const positionTopLabel = { 'aria-label': 'Position Top' };
export const positionBottomLabel = { 'aria-label': 'Position Bottom' };
export const positionLeftLabel = { 'aria-label': 'Position Left' };
export const positionRightLabel = { 'aria-label': 'Position Right' };
export const directionLabel = { 'aria-label': 'Direction' };
export const positionLabel = { 'aria-label': 'Position' };
<!DOCTYPE html>
<html lang="en">
<head>
<title>DevExtreme Demo</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0" />
<link rel="stylesheet" type="text/css" href="https://cdn3.devexpress.com/jslib/26.1.5/css/dx.light.css" />
<link rel="stylesheet" type="text/css" href="./bundle.css" />
</head>
<body class="dx-viewport">
<div class="demo-container">
<div id="app"></div>
</div>
<script src="bundles/vendor/react.vendor.js"></script>
<script src="./bundle.js"></script>
</body>
</html>
.options {
padding: 20px;
background-color: rgba(191, 191, 191, 0.15);
position: absolute;
right: 0;
top: 0;
width: 260px;
display: flex;
flex-direction: column;
gap: 5px;
}
.section {
width: 100%;
display: flex;
justify-content: space-between;
}