import React, { useCallback, useState } from 'react';
import Accordion, { type AccordionTypes } from 'devextreme-react/accordion';
import CheckBox, { type CheckBoxTypes } from 'devextreme-react/check-box';
import TagBox, { type TagBoxTypes } from 'devextreme-react/tag-box';
import Slider, { Tooltip, Label, type SliderTypes } from 'devextreme-react/slider';
import { companies } from './data.ts';
import type { Company } from './types.ts';
import CustomTitle from './CustomTitle.tsx';
import CustomItem from './CustomItem.tsx';
const companyLabel = { 'aria-label': 'Company' };
const App = () => {
const [selectedItems, setSelectedItems] = useState<Company[]>([companies[0]]);
const [multiple, setMultiple] = useState<boolean>(false);
const [collapsible, setCollapsible] = useState<boolean>(false);
const [animationDuration, setAnimationDuration] = useState<number>(300);
const selectionChanged = useCallback((e: AccordionTypes.SelectionChangedEvent): void => {
let newItems = [...selectedItems];
e.removedItems.forEach((item: Company): void => {
const index = newItems.indexOf(item);
if (index >= 0) {
newItems.splice(index, 1);
}
});
if (e.addedItems.length) {
newItems = [...newItems, ...e.addedItems];
}
setSelectedItems(newItems);
}, [selectedItems, setSelectedItems]);
const selectedItemsChanged = useCallback((e: TagBoxTypes.ValueChangedEvent): void => {
setSelectedItems(e.value);
}, []);
const multipleChanged = useCallback((e: CheckBoxTypes.ValueChangedEvent): void => {
setMultiple(e.value);
}, []);
const collapsibleChanged = useCallback((e: CheckBoxTypes.ValueChangedEvent): void => {
setCollapsible(e.value);
}, []);
const animationDurationChanged = useCallback((e: SliderTypes.ValueChangedEvent): void => {
setAnimationDuration(e.value);
}, []);
return (
<div id="accordion">
<Accordion
dataSource={companies}
collapsible={collapsible}
multiple={multiple}
animationDuration={animationDuration}
selectedItems={selectedItems}
onSelectionChanged={selectionChanged}
itemTitleRender={CustomTitle}
itemRender={CustomItem}
id="accordion-container"
/>
<div className="options">
<div className="caption">Options</div>
<div className="option">
<CheckBox
text="Multiple enabled"
value={multiple}
onValueChanged={multipleChanged}
/>
</div>
<div className="option">
<CheckBox
text="Collapsible enabled"
value={collapsible}
onValueChanged={collapsibleChanged}
/>
</div>
<div className="option">
<span>Animation duration</span>
<Slider
min={0}
max={1000}
value={animationDuration}
onValueChanged={animationDurationChanged}
>
<Tooltip enabled={true} position="bottom" />
<Label visible={true} />
</Slider>
</div>
<div className="option">
<span className="caption">Selected Items</span>
<TagBox
dataSource={companies}
displayExpr="CompanyName"
value={selectedItems}
inputAttr={companyLabel}
onValueChanged={selectedItemsChanged}
disabled={!multiple}
/>
</div>
</div>
</div>
);
};
export default App;
import React, { useCallback, useState } from 'react';
import Accordion from 'devextreme-react/accordion';
import CheckBox from 'devextreme-react/check-box';
import TagBox from 'devextreme-react/tag-box';
import Slider, { Tooltip, Label } from 'devextreme-react/slider';
import { companies } from './data.js';
import CustomTitle from './CustomTitle.js';
import CustomItem from './CustomItem.js';
const companyLabel = { 'aria-label': 'Company' };
const App = () => {
const [selectedItems, setSelectedItems] = useState([companies[0]]);
const [multiple, setMultiple] = useState(false);
const [collapsible, setCollapsible] = useState(false);
const [animationDuration, setAnimationDuration] = useState(300);
const selectionChanged = useCallback(
(e) => {
let newItems = [...selectedItems];
e.removedItems.forEach((item) => {
const index = newItems.indexOf(item);
if (index >= 0) {
newItems.splice(index, 1);
}
});
if (e.addedItems.length) {
newItems = [...newItems, ...e.addedItems];
}
setSelectedItems(newItems);
},
[selectedItems, setSelectedItems],
);
const selectedItemsChanged = useCallback((e) => {
setSelectedItems(e.value);
}, []);
const multipleChanged = useCallback((e) => {
setMultiple(e.value);
}, []);
const collapsibleChanged = useCallback((e) => {
setCollapsible(e.value);
}, []);
const animationDurationChanged = useCallback((e) => {
setAnimationDuration(e.value);
}, []);
return (
<div id="accordion">
<Accordion
dataSource={companies}
collapsible={collapsible}
multiple={multiple}
animationDuration={animationDuration}
selectedItems={selectedItems}
onSelectionChanged={selectionChanged}
itemTitleRender={CustomTitle}
itemRender={CustomItem}
id="accordion-container"
/>
<div className="options">
<div className="caption">Options</div>
<div className="option">
<CheckBox
text="Multiple enabled"
value={multiple}
onValueChanged={multipleChanged}
/>
</div>
<div className="option">
<CheckBox
text="Collapsible enabled"
value={collapsible}
onValueChanged={collapsibleChanged}
/>
</div>
<div className="option">
<span>Animation duration</span>
<Slider
min={0}
max={1000}
value={animationDuration}
onValueChanged={animationDurationChanged}
>
<Tooltip
enabled={true}
position="bottom"
/>
<Label visible={true} />
</Slider>
</div>
<div className="option">
<span className="caption">Selected Items</span>
<TagBox
dataSource={companies}
displayExpr="CompanyName"
value={selectedItems}
inputAttr={companyLabel}
onValueChanged={selectedItemsChanged}
disabled={!multiple}
/>
</div>
</div>
</div>
);
};
export default App;
import React from 'react';
import type { Company } from './types.ts';
export default function CustomItem(company: Company) {
const {
Address,
City,
State,
Zipcode,
Phone,
Fax,
Website,
} = company;
return (
<div>
<div>
<p>
<b>{City} </b>
(<span>{State}</span>)
</p>
<p>
<span>{Zipcode} </span>
<span>{Address}</span>
</p>
</div>
<div>
<p>
Phone: <b>{Phone}</b>
</p>
<p>
Fax: <b>{Fax}</b>
</p>
<p>
Website: <span className="accordion-item-link">
{Website}
</span>
</p>
</div>
</div>
);
}
import React from 'react';
import type { Company } from './types.ts';
export default function CustomTitle(company: Company) {
const { CompanyName } = company;
return (
<div className='header'>{CompanyName}</div>
);
}
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App.tsx';
ReactDOM.render(
<App />,
document.getElementById('app'),
);
import type { Company } from './types.ts';
export const companies: Company[] = [{
ID: 1,
CompanyName: 'Super Mart of the West',
Address: '702 SW 8th Street',
City: 'Bentonville',
State: 'Arkansas',
Zipcode: 72716,
Phone: '(800) 555-2797',
Fax: '(800) 555-2171',
Website: 'http://www.nowebsitesupermart.dx',
}, {
ID: 2,
CompanyName: 'Electronics Depot',
Address: '2455 Paces Ferry Road NW',
City: 'Atlanta',
State: 'Georgia',
Zipcode: 30339,
Phone: '(800) 595-3232',
Fax: '(800) 595-3231',
Website: 'http://www.nowebsitedepot.dx',
}, {
ID: 3,
CompanyName: 'K&S Music',
Address: '1000 Nicllet Mall',
City: 'Minneapolis',
State: 'Minnesota',
Zipcode: 55403,
Phone: '(612) 304-6073',
Fax: '(612) 304-6074',
Website: 'http://www.nowebsitemusic.dx',
}, {
ID: 4,
CompanyName: "Tom's Club",
Address: '999 Lake Drive',
City: 'Issaquah',
State: 'Washington',
Zipcode: 98027,
Phone: '(800) 955-2292',
Fax: '(800) 955-2293',
Website: 'http://www.nowebsitetomsclub.dx',
}];
export type Company = {
ID: number;
CompanyName: string;
Address: string;
City: string;
State: string;
Zipcode: number;
Phone: string;
Fax: string;
Website: string;
};
import React from 'react';
export default function CustomItem(company) {
const {
Address, City, State, Zipcode, Phone, Fax, Website,
} = company;
return (
<div>
<div>
<p>
<b>{City} </b>(<span>{State}</span>)
</p>
<p>
<span>{Zipcode} </span>
<span>{Address}</span>
</p>
</div>
<div>
<p>
Phone: <b>{Phone}</b>
</p>
<p>
Fax: <b>{Fax}</b>
</p>
<p>
Website: <span className="accordion-item-link">{Website}</span>
</p>
</div>
</div>
);
}
import React from 'react';
export default function CustomTitle(company) {
const { CompanyName } = company;
return <div className="header">{CompanyName}</div>;
}
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App.js';
ReactDOM.render(<App />, document.getElementById('app'));
export const companies = [
{
ID: 1,
CompanyName: 'Super Mart of the West',
Address: '702 SW 8th Street',
City: 'Bentonville',
State: 'Arkansas',
Zipcode: 72716,
Phone: '(800) 555-2797',
Fax: '(800) 555-2171',
Website: 'http://www.nowebsitesupermart.dx',
},
{
ID: 2,
CompanyName: 'Electronics Depot',
Address: '2455 Paces Ferry Road NW',
City: 'Atlanta',
State: 'Georgia',
Zipcode: 30339,
Phone: '(800) 595-3232',
Fax: '(800) 595-3231',
Website: 'http://www.nowebsitedepot.dx',
},
{
ID: 3,
CompanyName: 'K&S Music',
Address: '1000 Nicllet Mall',
City: 'Minneapolis',
State: 'Minnesota',
Zipcode: 55403,
Phone: '(612) 304-6073',
Fax: '(612) 304-6074',
Website: 'http://www.nowebsitemusic.dx',
},
{
ID: 4,
CompanyName: "Tom's Club",
Address: '999 Lake Drive',
City: 'Issaquah',
State: 'Washington',
Zipcode: 98027,
Phone: '(800) 955-2292',
Fax: '(800) 955-2293',
Website: 'http://www.nowebsitetomsclub.dx',
},
];
<!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>
#accordion {
height: 700px;
}
#accordion .header {
font-size: 20px;
}
#accordion .header,
#accordion p {
margin: 0;
}
#accordion-container {
margin-right: 400px;
}
.dx-theme-material #accordion .dx-accordion-item-title {
display: flex;
}
.accordion-item-link {
color: var(--dx-color-link);
cursor: pointer;
text-decoration: underline;
}
.dx-theme-material #accordion .header {
align-self: center;
}
.options {
padding: 20px;
position: absolute;
bottom: 0;
right: 0;
width: 340px;
top: 0;
background-color: rgba(191, 191, 191, 0.15);
}
.options > .caption {
font-weight: 500;
font-size: 18px;
}
.option {
margin-top: 10px;
}
.option > .caption {
margin-top: 10px;
display: inline-block;
}
.option > .dx-tagbox {
margin-top: 2px;
}