Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added basic functions #3

Merged
merged 7 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<!-- default badges list -->
![](https://img.shields.io/endpoint?url=https://codecentral.devexpress.com/api/v1/VersionRange/723096689/23.1.3%2B)
[![](https://img.shields.io/badge/Open_in_DevExpress_Support_Center-FF7200?style=flat-square&logo=DevExpress&logoColor=white)](https://supportcenter.devexpress.com/ticket/details/T1202789)
[![](https://img.shields.io/badge/📖_How_to_use_DevExpress_Examples-e9f6fc?style=flat-square)](https://docs.devexpress.com/GeneralInformation/403183)
<!-- default badges end -->
Expand Down
95 changes: 87 additions & 8 deletions React/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,95 @@
import React, { useCallback, useState } from 'react';
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 Button from 'devextreme-react/button';
import type { DataChange } from 'devextreme/common/grids';
import dxDataGrid, { Row } from 'devextreme/ui/data_grid';
16adianay marked this conversation as resolved.
Show resolved Hide resolved
import DataGrid, {
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 {
var [count, setCount] = useState<number>(0);
const clickHandler = useCallback(() => {
setCount((prev) => prev + 1);
}, [setCount]);
let grid = React.useRef<DataGrid>(null);
const [clicked, setClicked] = useState<Boolean>(false);
const [changes, setChanges] = useState<DataChange[]>([]);

const validateVisibleRows = React.useCallback(() => {
let dataGrid: dxDataGrid | undefined = grid?.current?.instance;
const fakeChanges: DataChange[] = dataGrid
? dataGrid.getVisibleRows().map((row: Row) => ({ type: 'update', key: row.key, data: {} }))
16adianay marked this conversation as resolved.
Show resolved Hide resolved
: [];
// alternatively, you can use the DataGrid|option method to set a new changes array
let array: DataChange[] | [] | undefined = [...changes, ...fakeChanges];
setChanges(array);
setClicked(true);
}, [changes]);

useEffect(() => {
if (changes.length && clicked) {
let dataGrid: dxDataGrid | undefined = 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: DataChange[]): void => {
setChanges(changes);
}, []);

return (
<div className="main">
<Button text={`Click count: ${count}`} onClick={clickHandler} />
<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>
);
}
Expand Down
41 changes: 41 additions & 0 deletions React/src/data.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
export interface Customer {
ID: number;

CompanyName: string;

Address: string;

City: string;

State: string;

Zipcode: number;

Phone: string;

Fax: string;

Website: string;
}

export const customers: Customer[] = [{
ID: 1,
CompanyName: '',
Address: '702 SW 8th Street',
City: 'Bentonville',
State: 'Arkansas',
Zipcode: 72716,
Phone: '123456',
Fax: '(800) 555-2171',
Website: 'http://www.nowebsitesupermart.com',
}, {
ID: 2,
CompanyName: 'Electronics Depot',
Address: '2455 Paces Ferry Road NW',
City: 'NYC',
State: 'Georgia',
Zipcode: 30339,
Phone: '(800) 595-3232',
Fax: '(800) 595-3231',
Website: 'http://www.nowebsitedepot.com',
}];
5 changes: 5 additions & 0 deletions React/src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,8 @@ code {
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
monospace;
}

.demo-container {
margin: 50px;
width: 90vh;
}
Loading