import React, { useCallback } from 'react';
import type { ValidationRule } from 'devextreme-react/common';
import {
Form,
Item, GroupItem, Label,
type FormTypes,
} from 'devextreme-react/form';
import type { ITextBoxOptions } from 'devextreme-react/text-box';
import type { ISelectBoxOptions } from 'devextreme-react/select-box';
import type { IDateBoxOptions } from 'devextreme-react/date-box';
import type { ITextAreaOptions } from 'devextreme-react/text-area';
import 'devextreme/ui/select_box';
import 'devextreme/ui/text_area';
import { employee, positions } from './data.ts';
import LabelTemplate from './LabelTemplate.tsx';
import LabelNotesTemplate from './LabelNotesTemplate.tsx';
const validationRules: {
position: ValidationRule[],
hireDate: ValidationRule[]
} = {
position: [
{ type: 'required', message: 'Position is required.' },
],
hireDate: [
{ type: 'required', message: 'Hire Date is required.' },
],
};
const nameEditorOptions: ITextBoxOptions = { disabled: true };
const positionEditorOptions: ISelectBoxOptions = { items: positions, searchEnabled: true, value: '' };
const hireDateEditorOptions: IDateBoxOptions = { width: '100%', value: null };
const birthDateEditorOptions: IDateBoxOptions = { width: '100%', disabled: true };
const notesEditorOptions: ITextAreaOptions = { height: 90, maxLength: 200 };
const phoneEditorOptions: ITextBoxOptions = { mask: '(X00) 000-0000', maskRules: { X: /[02-9]/ } };
const App = () => {
const validateForm = useCallback((e: FormTypes.ContentReadyEvent): void => {
e.component.validate();
}, []);
return (
<Form
onContentReady={validateForm}
formData={employee}
>
<GroupItem colCount={2} caption="Employee Details">
<Item dataField="FirstName" editorOptions={nameEditorOptions}>
<Label render={LabelTemplate('user')} />
</Item>
<Item dataField="Position" editorType="dxSelectBox" editorOptions={positionEditorOptions} validationRules={validationRules.position}>
<Label render={LabelTemplate('info')} />
</Item>
<Item dataField="LastName" editorOptions={nameEditorOptions}>
<Label render={LabelTemplate('user')} />
</Item>
<Item dataField="HireDate" editorType="dxDateBox" editorOptions={hireDateEditorOptions} validationRules={validationRules.hireDate}>
<Label render={LabelTemplate('event')} />
</Item>
<Item dataField="BirthDate" editorType="dxDateBox" editorOptions={birthDateEditorOptions}>
<Label render={LabelTemplate('event')} />
</Item>
<Item dataField="Address">
<Label render={LabelTemplate('home')} />
</Item>
<Item dataField="Notes" colSpan={2} editorType="dxTextArea" editorOptions={notesEditorOptions}>
<Label render={LabelNotesTemplate} />
</Item>
<Item dataField="Phone" editorOptions={phoneEditorOptions}>
<Label render={LabelTemplate('tel')} />
</Item>
<Item dataField="Email">
<Label render={LabelTemplate('email')} />
</Item>
</GroupItem>
</Form>
);
};
export default App;
import React, { useCallback } from 'react';
import {
Form, Item, GroupItem, Label,
} from 'devextreme-react/form';
import 'devextreme/ui/select_box';
import 'devextreme/ui/text_area';
import { employee, positions } from './data.js';
import LabelTemplate from './LabelTemplate.js';
import LabelNotesTemplate from './LabelNotesTemplate.js';
const validationRules = {
position: [{ type: 'required', message: 'Position is required.' }],
hireDate: [{ type: 'required', message: 'Hire Date is required.' }],
};
const nameEditorOptions = { disabled: true };
const positionEditorOptions = { items: positions, searchEnabled: true, value: '' };
const hireDateEditorOptions = { width: '100%', value: null };
const birthDateEditorOptions = { width: '100%', disabled: true };
const notesEditorOptions = { height: 90, maxLength: 200 };
const phoneEditorOptions = { mask: '(X00) 000-0000', maskRules: { X: /[02-9]/ } };
const App = () => {
const validateForm = useCallback((e) => {
e.component.validate();
}, []);
return (
<Form
onContentReady={validateForm}
formData={employee}
>
<GroupItem
colCount={2}
caption="Employee Details"
>
<Item
dataField="FirstName"
editorOptions={nameEditorOptions}
>
<Label render={LabelTemplate('user')} />
</Item>
<Item
dataField="Position"
editorType="dxSelectBox"
editorOptions={positionEditorOptions}
validationRules={validationRules.position}
>
<Label render={LabelTemplate('info')} />
</Item>
<Item
dataField="LastName"
editorOptions={nameEditorOptions}
>
<Label render={LabelTemplate('user')} />
</Item>
<Item
dataField="HireDate"
editorType="dxDateBox"
editorOptions={hireDateEditorOptions}
validationRules={validationRules.hireDate}
>
<Label render={LabelTemplate('event')} />
</Item>
<Item
dataField="BirthDate"
editorType="dxDateBox"
editorOptions={birthDateEditorOptions}
>
<Label render={LabelTemplate('event')} />
</Item>
<Item dataField="Address">
<Label render={LabelTemplate('home')} />
</Item>
<Item
dataField="Notes"
colSpan={2}
editorType="dxTextArea"
editorOptions={notesEditorOptions}
>
<Label render={LabelNotesTemplate} />
</Item>
<Item
dataField="Phone"
editorOptions={phoneEditorOptions}
>
<Label render={LabelTemplate('tel')} />
</Item>
<Item dataField="Email">
<Label render={LabelTemplate('email')} />
</Item>
</GroupItem>
</Form>
);
};
export default App;
import React from 'react';
import { Tooltip } from 'devextreme-react/tooltip';
interface LabelNotesTemplateProps {
text: string;
}
function LabelNotesTemplate({ text }: LabelNotesTemplateProps) {
return (
<>
<div id="template-content">
<i id="helpedInfo" className="dx-icon dx-icon-info"></i>
Additional
<br />
{ text }
</div>
<Tooltip
target="#helpedInfo"
showEvent="mouseenter"
hideEvent="mouseleave"
>
<div id="tooltip-content">This field must not exceed 200 characters</div>
</Tooltip>
</>
);
}
export default LabelNotesTemplate;
import React from 'react';
interface LabelTemplateProps {
text: string;
}
export default function LabelTemplate(iconName: string) {
return function template({ text }: LabelTemplateProps) {
return (<div><i className={`dx-icon dx-icon-${iconName}`}></i>{ text }</div>);
};
}
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App.tsx';
ReactDOM.render(
<App />,
document.getElementById('app'),
);
import type { Employee } from './types.ts';
export const employee: Employee = {
ID: 1,
FirstName: 'John',
LastName: 'Heart',
Position: 'CEO',
BirthDate: '1964/03/16',
HireDate: '1995/01/15',
Notes: 'John has been in the Audio/Video industry since 1990. He has led DevAv as its CEO since 2003.\r\n\r\nWhen not working hard as the CEO, John loves to golf and bowl. He once bowled a perfect game of 300.',
Address: '351 S Hill St., Los Angeles, CA',
Phone: '360-684-1334',
Email: 'jheart@dx-email.com',
};
export const positions: string[] = [
'HR Manager',
'IT Manager',
'CEO',
'Controller',
'Sales Manager',
'Support Manager',
'Shipping Manager',
];
export type Employee = {
ID: number,
FirstName: string,
LastName: string,
Position: string,
BirthDate: string,
HireDate: string,
Notes: string,
Address: string,
Phone: string,
Email: string,
};
import React from 'react';
import { Tooltip } from 'devextreme-react/tooltip';
function LabelNotesTemplate({ text }) {
return (
<>
<div id="template-content">
<i
id="helpedInfo"
className="dx-icon dx-icon-info"
></i>
Additional
<br />
{text}
</div>
<Tooltip
target="#helpedInfo"
showEvent="mouseenter"
hideEvent="mouseleave"
>
<div id="tooltip-content">This field must not exceed 200 characters</div>
</Tooltip>
</>
);
}
export default LabelNotesTemplate;
import React from 'react';
export default function LabelTemplate(iconName) {
return function template({ text }) {
return (
<div>
<i className={`dx-icon dx-icon-${iconName}`}></i>
{text}
</div>
);
};
}
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App.js';
ReactDOM.render(<App />, document.getElementById('app'));
export const employee = {
ID: 1,
FirstName: 'John',
LastName: 'Heart',
Position: 'CEO',
BirthDate: '1964/03/16',
HireDate: '1995/01/15',
Notes:
'John has been in the Audio/Video industry since 1990. He has led DevAv as its CEO since 2003.\r\n\r\nWhen not working hard as the CEO, John loves to golf and bowl. He once bowled a perfect game of 300.',
Address: '351 S Hill St., Los Angeles, CA',
Phone: '360-684-1334',
Email: 'jheart@dx-email.com',
};
export const positions = [
'HR Manager',
'IT Manager',
'CEO',
'Controller',
'Sales Manager',
'Support Manager',
'Shipping Manager',
];
<!DOCTYPE html>
<html lang="en">
<head>
<title>DevExtreme Demo</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0" />
<link rel="stylesheet" type="text/css" href="https://cdn3.devexpress.com/jslib/26.1.5/css/dx.light.css" />
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" />
<link rel="stylesheet" type="text/css" href="./bundle.css" />
</head>
<body class="dx-viewport">
<div class="demo-container">
<div id="app"></div>
</div>
<script src="bundles/vendor/react.vendor.js"></script>
<script src="./bundle.js"></script>
</body>
</html>
#helpedInfo {
color: #42a5f5;
}
#tooltip-content {
font-size: 14px;
font-weight: bold;
}
#template-content {
display: inline-flex;
}