Skip to content

Commit b5fd7bd

Browse files
authored
Refactor grid-lite samples to use Ignite UI React components and improve code consistency (#1049)
1 parent 49d3e48 commit b5fd7bd

12 files changed

Lines changed: 722 additions & 390 deletions

File tree

Lines changed: 54 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,78 @@
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";
44

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";
1111

1212
import "igniteui-webcomponents/themes/light/bootstrap.css";
1313
import "./index.css";
1414

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",
2218
});
2319

20+
const formatCurrency = (value: number) => formatter.format(value);
21+
2422
// 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+
);
3026

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+
);
3830

3931
export default function Sample() {
40-
const gridRef = React.useRef<any>(null);
32+
const [data, setData] = React.useState<ProductInfo[]>([]);
4133

4234
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);
4838
}, []);
4939

5040
return (
5141
<div className="container sample ig-typography">
5242
<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>
6071
</div>
6172
</div>
6273
);
6374
}
6475

6576
// 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

Comments
 (0)