-
Notifications
You must be signed in to change notification settings - Fork 2
/
App.tsx
94 lines (87 loc) · 2.91 KB
/
App.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import React, { useCallback, useEffect, useState } from 'react';
import Button from 'devextreme-react/button';
import './App.css';
import 'devextreme/dist/css/dx.material.blue.light.compact.css';
import DataGrid, {
DataGridTypes, Column, Editing, PatternRule, RequiredRule, StringLengthRule, Toolbar, Item,
} from 'devextreme-react/data-grid';
import notify from 'devextreme/ui/notify';
import { customers } from './data';
const pattern = /^\(\d{3}\) \d{3}-\d{4}$/i;
function App(): JSX.Element {
let grid = React.useRef<DataGrid>(null);
const [clicked, setClicked] = useState<Boolean>(false);
const [changes, setChanges] = useState<DataGridTypes.DataChange[]>([]);
const validateVisibleRows = React.useCallback(() => {
let dataGrid = grid?.current?.instance;
const fakeChanges = dataGrid
? dataGrid.getVisibleRows().map((row: DataGridTypes.Row): DataGridTypes.DataChange => ({ type: 'update', key: row.key, data: {} }))
: [];
// alternatively, you can use the DataGrid|option method to set a new changes array
setChanges([...changes, ...fakeChanges]);
setClicked(true);
}, [changes]);
useEffect(() => {
if (changes.length && clicked) {
let dataGrid = grid?.current?.instance;
dataGrid?.repaint();
// @ts-expect-error - getController is a private method
dataGrid?.getController('validating').validate(true).then((result: Boolean) => {
const message = result ? 'Validation is passed' : 'Validation is failed';
const type = result ? 'success' : 'error';
notify(message, type);
});
setClicked(false);
}
}, [validateVisibleRows]);
const onChangesChange = useCallback((changes: DataGridTypes.DataChange[]): void => {
setChanges(changes);
}, []);
return (
<div className="demo-container">
<DataGrid
ref={grid}
id="grid-container"
dataSource={customers}
keyExpr="ID"
showBorders={true}
>
<Editing
onChangesChange={onChangesChange}
changes={changes}
mode="batch"
allowUpdating={true}
/>
<Column
dataField="CompanyName"
>
<RequiredRule />
</Column>
<Column dataField="City">
<StringLengthRule min={4} />
</Column>
<Column dataField="Phone">
<RequiredRule />
<PatternRule
message="Your phone must have '(555) 555-5555 format!'"
pattern={pattern}
/>
</Column>
<Column dataField="Fax"></Column>
<Column dataField="State"></Column>
<Toolbar>
<Item>
<Button
text="Validate DataGrid"
stylingMode="outlined"
onClick={validateVisibleRows}
/>
</Item>
<Item name="saveButton" />
<Item name="revertButton" />
</Toolbar>
</DataGrid>
</div>
);
}
export default App;