|
1 | | -import React from 'react'; |
2 | | -import ReactDOM from 'react-dom/client'; |
3 | | -import { GridLiteDataService, ProductInfo } from './GridLiteDataService'; |
| 1 | +import React from "react"; |
| 2 | +import ReactDOM from "react-dom/client"; |
| 3 | +import { GridLiteDataService, ProductInfo } from "./GridLiteDataService"; |
4 | 4 |
|
5 | | -// Import the web component |
6 | | -import { IgcGridLite } from 'igniteui-grid-lite'; |
7 | | -import { |
8 | | - defineComponents, |
9 | | - IgcRatingComponent |
10 | | -} from 'igniteui-webcomponents'; |
| 5 | +import { IgrRating } from "igniteui-react"; |
| 6 | +import { |
| 7 | + IgrGridLite, |
| 8 | + IgrGridLiteColumn, |
| 9 | + type IgrCellContext, |
| 10 | +} from "igniteui-react/grid-lite"; |
11 | 11 |
|
12 | 12 | import "igniteui-webcomponents/themes/light/bootstrap.css"; |
13 | 13 | import "./index.css"; |
14 | 14 |
|
15 | | -// Register components |
16 | | -IgcGridLite.register(); |
17 | | -defineComponents(IgcRatingComponent); |
18 | | - |
19 | | -const formatter = new Intl.NumberFormat('en-EN', { |
20 | | - style: 'currency', |
21 | | - currency: 'EUR' |
| 15 | +const formatter = new Intl.NumberFormat("en-150", { |
| 16 | + style: "currency", |
| 17 | + currency: "EUR", |
22 | 18 | }); |
23 | 19 |
|
| 20 | +const formatCurrency = (value: number) => formatter.format(value); |
| 21 | + |
24 | 22 | // Define cellTemplate functions outside component |
25 | | -const currencyCellTemplate = (params: any) => { |
26 | | - const span = document.createElement('span'); |
27 | | - span.textContent = formatter.format(params.value); |
28 | | - return span; |
29 | | -}; |
| 23 | +const currencyCellTemplate = (ctx: IgrCellContext) => ( |
| 24 | + <span>{formatCurrency(ctx.value)}</span> |
| 25 | +); |
30 | 26 |
|
31 | | -const ratingCellTemplate = (params: any) => { |
32 | | - const rating = document.createElement('igc-rating'); |
33 | | - rating.setAttribute('readonly', ''); |
34 | | - rating.setAttribute('step', '0.01'); |
35 | | - rating.setAttribute('value', params.value.toString()); |
36 | | - return rating; |
37 | | -}; |
| 27 | +const ratingCellTemplate = (ctx: IgrCellContext) => ( |
| 28 | + <IgrRating readOnly max={5} value={ctx.value}></IgrRating> |
| 29 | +); |
38 | 30 |
|
39 | 31 | export default function Sample() { |
40 | | - const gridRef = React.useRef<any>(null); |
| 32 | + const [data, setData] = React.useState<ProductInfo[]>([]); |
41 | 33 |
|
42 | 34 | React.useEffect(() => { |
43 | | - if (gridRef.current) { |
44 | | - const dataService = new GridLiteDataService(); |
45 | | - const data: ProductInfo[] = dataService.generateProducts(50); |
46 | | - gridRef.current.data = data; |
47 | | - } |
| 35 | + const dataService = new GridLiteDataService(); |
| 36 | + const items: ProductInfo[] = dataService.generateProducts(50); |
| 37 | + setData(items); |
48 | 38 | }, []); |
49 | 39 |
|
50 | 40 | return ( |
51 | 41 | <div className="container sample ig-typography"> |
52 | 42 | <div className="grid-lite-wrapper"> |
53 | | - <igc-grid-lite ref={gridRef} id="grid-lite"> |
54 | | - <igc-grid-lite-column field="name" header="Product Name"></igc-grid-lite-column> |
55 | | - <igc-grid-lite-column field="price" header="Price" data-type="number" cellTemplate={currencyCellTemplate}></igc-grid-lite-column> |
56 | | - <igc-grid-lite-column field="sold" data-type="number" header="Units sold"></igc-grid-lite-column> |
57 | | - <igc-grid-lite-column field="total" header="Total sold" cellTemplate={currencyCellTemplate}></igc-grid-lite-column> |
58 | | - <igc-grid-lite-column field="rating" data-type="number" header="Customer rating" cellTemplate={ratingCellTemplate}></igc-grid-lite-column> |
59 | | - </igc-grid-lite> |
| 43 | + <IgrGridLite data={data} id="grid-lite"> |
| 44 | + <IgrGridLiteColumn |
| 45 | + field="name" |
| 46 | + header="Product Name" |
| 47 | + ></IgrGridLiteColumn> |
| 48 | + <IgrGridLiteColumn |
| 49 | + field="price" |
| 50 | + header="Price" |
| 51 | + dataType="number" |
| 52 | + cellTemplate={currencyCellTemplate} |
| 53 | + ></IgrGridLiteColumn> |
| 54 | + <IgrGridLiteColumn |
| 55 | + field="sold" |
| 56 | + dataType="number" |
| 57 | + header="Units Sold" |
| 58 | + ></IgrGridLiteColumn> |
| 59 | + <IgrGridLiteColumn |
| 60 | + field="total" |
| 61 | + header="Total sold" |
| 62 | + cellTemplate={currencyCellTemplate} |
| 63 | + ></IgrGridLiteColumn> |
| 64 | + <IgrGridLiteColumn |
| 65 | + field="rating" |
| 66 | + dataType="number" |
| 67 | + header="Customer Rating" |
| 68 | + cellTemplate={ratingCellTemplate} |
| 69 | + ></IgrGridLiteColumn> |
| 70 | + </IgrGridLite> |
60 | 71 | </div> |
61 | 72 | </div> |
62 | 73 | ); |
63 | 74 | } |
64 | 75 |
|
65 | 76 | // rendering above component in the React DOM |
66 | | -const root = ReactDOM.createRoot(document.getElementById('root')); |
67 | | -root.render(<Sample/>); |
| 77 | +const root = ReactDOM.createRoot(document.getElementById("root")); |
| 78 | +root.render(<Sample />); |
0 commit comments