AG-2653 Put React Hook example together, update docs
This commit is contained in:
@@ -5,12 +5,14 @@ import NavItem from "./NavItem";
|
|||||||
|
|
||||||
import RichGridDeclarativeExample from "./richGridDeclarativeExample/RichGridDeclarativeExample";
|
import RichGridDeclarativeExample from "./richGridDeclarativeExample/RichGridDeclarativeExample";
|
||||||
import SimpleReduxDynamicExample from "./simpleReduxDynamicComponentExample/SimpleReduxExample";
|
import SimpleReduxDynamicExample from "./simpleReduxDynamicComponentExample/SimpleReduxExample";
|
||||||
|
import SimpleReduxHookExample from "./simpleReduxHooksExample/SimpleReduxHookExample";
|
||||||
|
|
||||||
const SideBar = () => (
|
const SideBar = () => (
|
||||||
<div style={{float: "left", width: 335, marginRight: 25}}>
|
<div style={{float: "left", width: 335, marginRight: 25}}>
|
||||||
<ul className="nav nav-pills">
|
<ul className="nav nav-pills">
|
||||||
<NavItem to='/rich-grid-declarative'>Rich Grid with Declarative Markup</NavItem>
|
<NavItem to='/rich-grid-declarative'>Rich Grid with Declarative Markup</NavItem>
|
||||||
<NavItem to='/simple-redux-dynamic'>Simple Redux Dynamic Component Example</NavItem>
|
<NavItem to='/simple-redux-dynamic'>Simple Redux Dynamic Component Example</NavItem>
|
||||||
|
<NavItem to='/simple-redux-hook'>Simple Redux Dynamic Component Example</NavItem>
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
@@ -25,6 +27,7 @@ class App extends Component {
|
|||||||
<Redirect from="/" exact to="/rich-grid-declarative"/>
|
<Redirect from="/" exact to="/rich-grid-declarative"/>
|
||||||
<Route exact path='/rich-grid-declarative' component={RichGridDeclarativeExample}/>
|
<Route exact path='/rich-grid-declarative' component={RichGridDeclarativeExample}/>
|
||||||
<Route exact path='/simple-redux-dynamic' component={SimpleReduxDynamicExample}/>
|
<Route exact path='/simple-redux-dynamic' component={SimpleReduxDynamicExample}/>
|
||||||
|
<Route exact path='/simple-redux-hook' component={SimpleReduxHookExample}/>
|
||||||
</Switch>
|
</Switch>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
32
src-examples/simpleReduxHooksExample/GridComponent.jsx
Normal file
32
src-examples/simpleReduxHooksExample/GridComponent.jsx
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
import React, { useContext } from "react";
|
||||||
|
import {Context} from "./store";
|
||||||
|
import {AgGridReact} from "ag-grid-react";
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This component serves to display the row data (provided by redux)
|
||||||
|
*/
|
||||||
|
export default function GridComponent() {
|
||||||
|
const {store, dispatch} = useContext(Context);
|
||||||
|
const {columnDefs, rowData} = store;
|
||||||
|
|
||||||
|
const onGridReady = (params) => {
|
||||||
|
params.api.sizeColumnsToFit();
|
||||||
|
};
|
||||||
|
|
||||||
|
// row data will be provided via redux on this.props.rowData
|
||||||
|
return (
|
||||||
|
<div style={{height: 400, width: 900, marginTop: 15}}
|
||||||
|
className="ag-theme-balham">
|
||||||
|
<AgGridReact
|
||||||
|
// properties
|
||||||
|
columnDefs={columnDefs}
|
||||||
|
rowData={rowData}
|
||||||
|
|
||||||
|
reactNext={true}
|
||||||
|
|
||||||
|
// events
|
||||||
|
onGridReady={onGridReady}>
|
||||||
|
</AgGridReact>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
13
src-examples/simpleReduxHooksExample/PriceRenderer.jsx
Normal file
13
src-examples/simpleReduxHooksExample/PriceRenderer.jsx
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
import React, {Component} from "react";
|
||||||
|
|
||||||
|
export default class PriceRenderer extends Component {
|
||||||
|
constructor(props) {
|
||||||
|
super(props);
|
||||||
|
}
|
||||||
|
|
||||||
|
render() {
|
||||||
|
return (
|
||||||
|
<span>{this.props.value}</span>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
import React, {useReducer} from "react";
|
||||||
|
import GridComponent from "./GridComponent";
|
||||||
|
|
||||||
|
import {Context, initialState, reducer} from "./store";
|
||||||
|
|
||||||
|
export default function SimpleReduxHookExample() {
|
||||||
|
const [store, dispatch] = useReducer(reducer, initialState);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Context.Provider value={{store, dispatch}}>
|
||||||
|
<div>
|
||||||
|
<h1>Simple Example using Hooks (with useContext and useReducer)</h1>
|
||||||
|
<button onClick={() => dispatch({type: "SET_ROW_DATA"})}>Populate Row Data</button>
|
||||||
|
<GridComponent/>
|
||||||
|
</div>
|
||||||
|
</Context.Provider>
|
||||||
|
)
|
||||||
|
}
|
||||||
14
src-examples/simpleReduxHooksExample/gridDataActions.jsx
Normal file
14
src-examples/simpleReduxHooksExample/gridDataActions.jsx
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
export function updateRowData(rowData) {
|
||||||
|
return {
|
||||||
|
type: 'ROW_DATA_CHANGED',
|
||||||
|
rowData
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function setCurrency(currencySymbol, exchangeRate) {
|
||||||
|
return {
|
||||||
|
type: 'CURRENCY_CHANGED',
|
||||||
|
currencySymbol,
|
||||||
|
exchangeRate
|
||||||
|
}
|
||||||
|
}
|
||||||
75
src-examples/simpleReduxHooksExample/store.js
Normal file
75
src-examples/simpleReduxHooksExample/store.js
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
import React from "react";
|
||||||
|
import PriceRenderer from "./PriceRenderer";
|
||||||
|
|
||||||
|
export const initialState = {
|
||||||
|
rowData: [],
|
||||||
|
columnDefs: [
|
||||||
|
{
|
||||||
|
field: 'symbol'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
field: 'price',
|
||||||
|
cellClass: 'align-right',
|
||||||
|
cellRendererFramework: PriceRenderer
|
||||||
|
}
|
||||||
|
]
|
||||||
|
};
|
||||||
|
|
||||||
|
export const reducer = (state = {rowData: []}, action) => {
|
||||||
|
switch (action.type) {
|
||||||
|
case 'SET_ROW_DATA':
|
||||||
|
return {
|
||||||
|
...state,
|
||||||
|
rowData: createRowData()
|
||||||
|
};
|
||||||
|
default:
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const Context = React.createContext();
|
||||||
|
|
||||||
|
|
||||||
|
// for test data
|
||||||
|
// the following methods are for creating dummy row data
|
||||||
|
const createRowData = () => {
|
||||||
|
let rowData = [];
|
||||||
|
|
||||||
|
for (let i = 0; i < 14; i++) {
|
||||||
|
let newItem = createItem(rowData);
|
||||||
|
rowData.push(newItem);
|
||||||
|
}
|
||||||
|
|
||||||
|
return rowData;
|
||||||
|
};
|
||||||
|
|
||||||
|
const createItem = (rowData) => {
|
||||||
|
return {
|
||||||
|
symbol: createUniqueRandomSymbol(rowData),
|
||||||
|
price: Math.floor(Math.random() * 100)
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
// creates a unique symbol, eg 'ADG' or 'ZJD'
|
||||||
|
const createUniqueRandomSymbol = (rowData) => {
|
||||||
|
let symbol;
|
||||||
|
let possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||||
|
|
||||||
|
let isUnique = false;
|
||||||
|
while (!isUnique) {
|
||||||
|
symbol = '';
|
||||||
|
// create symbol
|
||||||
|
for (let i = 0; i < 3; i++) {
|
||||||
|
symbol += possible.charAt(Math.floor(Math.random() * possible.length));
|
||||||
|
}
|
||||||
|
// check uniqueness
|
||||||
|
isUnique = true;
|
||||||
|
rowData.forEach(function (oldItem) {
|
||||||
|
if (oldItem.symbol === symbol) {
|
||||||
|
isUnique = false;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return symbol;
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user