import React from 'react';
import TreeView from 'devextreme-react/tree-view';
import List from 'devextreme-react/list';
import { products } from './data.js';
class App extends React.Component {
constructor() {
super();
this.state = {
checkedItems: []
};
this.selectionChanged = this.selectionChanged.bind(this);
}
render() {
return (
<div className="form">
<h4>Store: Super Mart of the West</h4>
<TreeView
id="selection-treeview"
items={products}
width={320}
showCheckBoxesMode="normal"
onItemSelectionChanged={this.selectionChanged}
itemRender={renderTreeViewItem}
/>
{' '}
<div className="selected-data">
Selected Products
<List
id="checked-items"
width={400}
items={this.state.checkedItems}
itemRender={renderListItem}
/>
</div>
</div>
);
}
selectionChanged(e) {
const value = e.node;
if (this.isProduct(value)) {
this.processProduct({
id: value.key,
text: value.text,
itemData: value.itemData,
selected: value.selected,
category: value.parent.text
});
} else {
value.items.forEach(product => {
this.processProduct({
id: product.key,
text: product.text,
itemData: product.itemData,
selected: product.selected,
category: value.text
});
});
}
}
isProduct(data) {
return !data.items.length;
}
processProduct(product) {
this.setState(prevState => {
let itemIndex = -1;
const checkedItems = prevState.checkedItems;
checkedItems.forEach((item, index) => {
if (item.id === product.id) {
itemIndex = index;
return false;
}
});
if (product.selected && itemIndex === -1) {
checkedItems.push(product);
} else if (!product.selected) {
checkedItems.splice(itemIndex, 1);
}
return { checkedItems: checkedItems.slice(0) };
});
}
}
function renderTreeViewItem(value) {
return (<div>{value.text + (value.price ? ` ($${value.price})` : '')}</div>);
}
function renderListItem(item) {
return `${item.text} (${item.category}) - $${item.itemData.price}`;
}
export default App;
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App.js';
ReactDOM.render(
<App />,
document.getElementById('app')
);
<!DOCTYPE html>
<html>
<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=1.0" />
<link rel="stylesheet" type="text/css" href="https://cdn3.devexpress.com/jslib/19.2.4/css/dx.common.css" />
<link rel="stylesheet" type="text/css" href="https://cdn3.devexpress.com/jslib/19.2.4/css/dx.light.css" />
<link rel="stylesheet" type="text/css" href="styles.css" />
<script src="https://unpkg.com/core-js@2.4.1/client/shim.min.js"></script>
<script src="https://unpkg.com/systemjs@0.21.3/dist/system.js"></script>
<script type="text/javascript" src="config.js"></script>
<script type="text/javascript">
System.import('./index.js');
</script>
</head>
<body class="dx-viewport">
<div class="demo-container">
<div id="app"></div>
</div>
</body>
</html>
.form>h4 {
margin-bottom: 20px;
}
.form>div,
#selection-treeview {
display: inline-block;
vertical-align: top;
}
#checked-items {
margin-top: 20px;
}
.selected-data {
padding: 20px;
background: #f5f5f5;
font-size: 115%;
font-weight: bold;
}
.dx-list-item-content {
padding-left: 0;
}
export const products = [{
id: '1_1_1',
text: 'Video Players',
items: [{
id: '1_1_1_1',
text: 'HD Video Player',
price: 220
}, {
id: '1_1_1_2',
text: 'SuperHD Video Player',
price: 270
}]
}, {
id: '1_1_2',
text: 'Televisions',
expanded: true,
items: [{
id: '1_1_2_1',
text: 'SuperLCD 42',
price: 1200
}, {
id: '1_1_2_2',
text: 'SuperLED 42',
price: 1450
}, {
id: '1_1_2_3',
text: 'SuperLED 50',
price: 1600
}, {
id: '1_1_2_4',
text: 'SuperLCD 55',
price: 1350
}, {
id: '1_1_2_5',
text: 'SuperLCD 70',
price: 4000
}]
}, {
id: '1_1_4',
text: 'Projectors',
items: [{
id: '1_1_4_1',
text: 'Projector Plus',
price: 550
}, {
id: '1_1_4_2',
text: 'Projector PlusHD',
price: 750
}]
}];
System.config({
transpiler: 'plugin-babel',
paths: {
'npm:': 'https://unpkg.com/'
},
defaultExtension: 'js',
map: {
'react': 'npm:react@16/umd/react.development.js',
'react-dom': 'npm:react-dom@16/umd/react-dom.development.js',
'prop-types': 'npm:prop-types/prop-types.js',
'devextreme': 'npm:devextreme@19.2',
'devextreme-react': 'npm:devextreme-react@19.2',
'jszip': 'npm:jszip@3.1.3/dist/jszip.min.js',
'quill': 'npm:quill@1.3.7/dist/quill.js',
'devexpress-diagram': 'npm:devexpress-diagram',
'devexpress-gantt': 'npm:devexpress-gantt',
// SystemJS plugins
'plugin-babel': 'npm:systemjs-plugin-babel@0/plugin-babel.js',
'systemjs-babel-build': 'npm:systemjs-plugin-babel@0/systemjs-babel-browser.js'
},
packages: {
'devextreme': {
defaultExtension: 'js'
},
'devextreme-react': {
main: 'index.js'
}
},
babelOptions: {
sourceMaps: false,
stage0: true,
react: true
}
});