@@ -16,82 +16,50 @@ import "./index.css";
1616IgcGridLite . register ( ) ;
1717defineComponents ( IgcRatingComponent ) ;
1818
19- export default class Sample extends React . Component < any , any > {
20- private dataService : GridLiteDataService ;
21- private formatter : Intl . NumberFormat ;
22- private gridRef : React . RefObject < any > ;
19+ const formatter = new Intl . NumberFormat ( 'en-EN' , {
20+ style : 'currency' ,
21+ currency : 'EUR'
22+ } ) ;
2323
24- constructor ( props : any ) {
25- super ( props ) ;
26- this . dataService = new GridLiteDataService ( ) ;
27- this . formatter = new Intl . NumberFormat ( 'en-EN' , {
28- style : 'currency' ,
29- currency : 'EUR'
30- } ) ;
31- this . gridRef = React . createRef ( ) ;
32- }
24+ // 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+ } ;
3330
34- componentDidMount ( ) {
35- if ( this . gridRef . current ) {
36- const data : ProductInfo [ ] = this . dataService . generateProducts ( 50 ) ;
37-
38- const columns = [
39- {
40- key : 'name' ,
41- headerText : 'Product Name'
42- } ,
43- {
44- key : 'price' ,
45- headerText : 'Price' ,
46- type : 'number' ,
47- cellTemplate : ( params : any ) => {
48- const span = document . createElement ( 'span' ) ;
49- span . textContent = this . formatter . format ( params . value ) ;
50- return span ;
51- }
52- } ,
53- {
54- key : 'sold' ,
55- type : 'number' ,
56- headerText : 'Units sold'
57- } ,
58- {
59- key : 'total' ,
60- headerText : 'Total sold' ,
61- cellTemplate : ( params : any ) => {
62- const span = document . createElement ( 'span' ) ;
63- span . textContent = this . formatter . format ( params . value ) ;
64- return span ;
65- }
66- } ,
67- {
68- key : 'rating' ,
69- type : 'number' ,
70- headerText : 'Customer rating' ,
71- cellTemplate : ( params : any ) => {
72- const rating = document . createElement ( 'igc-rating' ) ;
73- rating . setAttribute ( 'readonly' , '' ) ;
74- rating . setAttribute ( 'step' , '0.01' ) ;
75- rating . setAttribute ( 'value' , params . value . toString ( ) ) ;
76- return rating ;
77- }
78- }
79- ] ;
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+ } ;
8038
81- this . gridRef . current . columns = columns ;
82- this . gridRef . current . data = data ;
39+ export default function Sample ( ) {
40+ const gridRef = React . useRef < any > ( null ) ;
41+
42+ React . useEffect ( ( ) => {
43+ if ( gridRef . current ) {
44+ const dataService = new GridLiteDataService ( ) ;
45+ const data : ProductInfo [ ] = dataService . generateProducts ( 50 ) ;
46+ gridRef . current . data = data ;
8347 }
84- }
48+ } , [ ] ) ;
8549
86- public render ( ) : JSX . Element {
87- return (
88- < div className = "container sample ig-typography" >
89- < div className = "grid-lite-wrapper" >
90- < igc-grid-lite ref = { this . gridRef } id = "grid-lite" > </ igc-grid-lite >
91- </ div >
50+ return (
51+ < div className = "container sample ig-typography" >
52+ < 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 >
9260 </ div >
93- ) ;
94- }
61+ </ div >
62+ ) ;
9563}
9664
9765// rendering above component in the React DOM
0 commit comments