|
| 1 | +import * as React from 'react'; |
| 2 | +import * as ReactDOM from 'react-dom'; |
| 3 | +import { Grid, GridColumn as Column } from '@progress/kendo-react-grid'; |
| 4 | +import products from './products.json'; |
| 5 | +import { IconWrap } from '@progress/kendo-react-common'; |
| 6 | +import { minusIcon, plusIcon } from '@progress/kendo-svg-icons'; |
| 7 | +const DetailComponent = (props) => { |
| 8 | + const dataItem = props.dataItem; |
| 9 | + return ( |
| 10 | + <p> |
| 11 | + <strong>In Stock:</strong> {dataItem.UnitsInStock} units |
| 12 | + </p> |
| 13 | + ); |
| 14 | +}; |
| 15 | + |
| 16 | +const expandCell = (props) => { |
| 17 | + console.log(props); |
| 18 | + return ( |
| 19 | + <td {...props.tdProps}> |
| 20 | + <a |
| 21 | + onClick={(e) => { |
| 22 | + e.preventDefault(); |
| 23 | + if (props.onChange) { |
| 24 | + props.onChange({ |
| 25 | + dataItem: props.dataItem, |
| 26 | + dataIndex: props.dataIndex, |
| 27 | + syntheticEvent: e, |
| 28 | + field: 'expanded_custom', |
| 29 | + value: !props.dataItem['expanded'], |
| 30 | + }); |
| 31 | + } |
| 32 | + }} |
| 33 | + href="#" |
| 34 | + tabIndex={-1} |
| 35 | + > |
| 36 | + <IconWrap |
| 37 | + name={props.dataItem['expanded'] ? 'minus' : 'plus'} |
| 38 | + icon={props.dataItem['expanded'] ? minusIcon : plusIcon} |
| 39 | + /> |
| 40 | + </a> |
| 41 | + </td> |
| 42 | + ); |
| 43 | +}; |
| 44 | +const App = () => { |
| 45 | + const [data, setData] = React.useState(products); |
| 46 | + const onItemChange = (event) => { |
| 47 | + //This is for the expand only |
| 48 | + if (event.field == 'expanded_custom') { |
| 49 | + let newData = data.map((item) => { |
| 50 | + if (item.ProductID === event.dataItem.ProductID) { |
| 51 | + item.expanded = !event.dataItem.expanded; |
| 52 | + } |
| 53 | + |
| 54 | + return item; |
| 55 | + }); |
| 56 | + setData(newData); |
| 57 | + } |
| 58 | + |
| 59 | + //Add standard onItemChange here |
| 60 | + }; |
| 61 | + return ( |
| 62 | + <Grid |
| 63 | + data={data} |
| 64 | + detail={DetailComponent} |
| 65 | + style={{ |
| 66 | + height: '400px', |
| 67 | + }} |
| 68 | + expandField="expanded" |
| 69 | + onItemChange={onItemChange} |
| 70 | + > |
| 71 | + <Column |
| 72 | + field="expanded" |
| 73 | + title=" " |
| 74 | + width="50px" |
| 75 | + cells={{ data: expandCell }} |
| 76 | + editable={false} |
| 77 | + /> |
| 78 | + <Column field="ProductName" title="Product" width="300px" /> |
| 79 | + <Column field="ProductID" title="ID" width="50px" /> |
| 80 | + <Column field="UnitPrice" title="Unit Price" width="100px" /> |
| 81 | + <Column field="QuantityPerUnit" title="Qty Per Unit" /> |
| 82 | + </Grid> |
| 83 | + ); |
| 84 | +}; |
| 85 | +ReactDOM.render(<App />, document.querySelector('my-app')); |
0 commit comments