import React from 'react';
import SelectBox from 'devextreme-react/select-box';
import CheckBox from 'devextreme-react/check-box';
import { TextBox, Button as TextBoxButton } from 'devextreme-react/text-box';
import DateBox from 'devextreme-react/date-box';
import Button from 'devextreme-react/button';
import ValidationSummary from 'devextreme-react/validation-summary';
import {
Validator,
RequiredRule,
CompareRule,
EmailRule,
PatternRule,
StringLengthRule,
RangeRule,
AsyncRule,
} from 'devextreme-react/validator';
import notify from 'devextreme/ui/notify';
import {
countries, nameLabel, passwordLabel, emailLabel,
maskLabel, dateLabel, cityLabel, addressLabel, countryLabel,
} from './data.js';
class App extends React.Component {
constructor(props) {
super(props);
const currentDate = new Date();
this.maxDate = new Date(currentDate.setFullYear(currentDate.getFullYear() - 21));
this.validatorInstance = null;
this.cityPattern = '^[^0-9]+$';
this.namePattern = /^[^0-9]+$/;
this.phonePattern = /^[02-9]\d{9}$/;
this.phoneRules = {
X: /[02-9]/,
};
this.state = {
password: '',
confirmPassword: '',
passwordMode: 'password',
confirmPasswordMode: 'password',
};
this.passwordButton = {
icon: '../../../../images/icons/eye.png',
type: 'default',
onClick: () => {
this.setState({
passwordMode: (this.state.passwordMode === 'text' ? 'password' : 'text'),
});
},
};
this.confirmPasswordButton = {
icon: '../../../../images/icons/eye.png',
type: 'default',
onClick: () => {
this.setState({
confirmPasswordMode: (this.state.confirmPasswordMode === 'text' ? 'password' : 'text'),
});
},
};
this.passwordComparison = this.passwordComparison.bind(this);
this.onPasswordChanged = this.onPasswordChanged.bind(this);
this.onInit = this.onInit.bind(this);
this.onConfirmPasswordChanged = this.onConfirmPasswordChanged.bind(this);
}
render() {
return (
<form action="your-action" onSubmit={this.onFormSubmit}>
<div className="dx-fieldset">
<div className="dx-fieldset-header">Credentials</div>
<div className="dx-field">
<div className="dx-field-label">Email</div>
<div className="dx-field-value">
<TextBox inputAttr={emailLabel}>
<Validator>
<RequiredRule message="Email is required" />
<EmailRule message="Email is invalid" />
<AsyncRule
message="Email is already registered"
validationCallback={asyncValidation} />
</Validator>
</TextBox>
</div>
</div>
<div className="dx-field">
<div className="dx-field-label">Password</div>
<div className="dx-field-value">
<TextBox
mode={this.state.passwordMode}
value={this.state.password}
inputAttr={passwordLabel}
onValueChanged={this.onPasswordChanged}>
<TextBoxButton
name="password"
location="after"
options={this.passwordButton}
/>
<Validator>
<RequiredRule message="Password is required" />
</Validator>
</TextBox>
</div>
</div>
<div className="dx-field">
<div className="dx-field-label">Confirm Password</div>
<div className="dx-field-value">
<TextBox
value={this.state.confirmPassword}
inputAttr={passwordLabel}
onValueChanged={this.onConfirmPasswordChanged}
mode={this.state.confirmPasswordMode}>
<TextBoxButton
name="password"
location="after"
options={this.confirmPasswordButton}
/>
<Validator onInitialized={this.onInit}>
<RequiredRule message="Confirm Password is required" />
<CompareRule message="Password and Confirm Password do not match" comparisonTarget={this.passwordComparison} />
</Validator>
</TextBox>
</div>
</div>
</div>
<div className="dx-fieldset">
<div className="dx-fieldset-header">Personal Data</div>
<div className="dx-field">
<div className="dx-field-label">Name</div>
<div className="dx-field-value">
<TextBox value="Peter" inputAttr={nameLabel}>
<Validator>
<RequiredRule message="Name is required" />
<PatternRule message="Do not use digits in the Name" pattern={this.namePattern} />
<StringLengthRule message="Name must have at least 2 symbols" min={2} />
</Validator>
</TextBox>
</div>
</div>
<div className="dx-field">
<div className="dx-field-label">Date of birth</div>
<div className="dx-field-value">
<DateBox
invalidDateMessage="The date must have the following format: MM/dd/yyyy" inputAttr={dateLabel}>
<Validator>
<RequiredRule message="Date of birth is required" />
<RangeRule message="You must be at least 21 years old" max={this.maxDate} />
</Validator>
</DateBox>
</div>
</div>
</div>
<div className="dx-fieldset">
<div className="dx-fieldset-header">Billing address</div>
<div className="dx-field">
<div className="dx-field-label">Country</div>
<div className="dx-field-value">
<SelectBox dataSource={countries} validationMessagePosition="left" inputAttr={countryLabel}>
<Validator>
<RequiredRule message="Country is required" />
</Validator>
</SelectBox>
</div>
</div>
<div className="dx-field">
<div className="dx-field-label">City</div>
<div className="dx-field-value">
<TextBox validationMessagePosition="left" inputAttr={cityLabel}>
<Validator>
<RequiredRule message="City is required" />
<PatternRule message="Do not use digits in the City name" pattern={this.cityPattern} />
<StringLengthRule message="City must have at least 2 symbols" min={2} />
</Validator>
</TextBox>
</div>
</div>
<div className="dx-field">
<div className="dx-field-label">Address</div>
<div className="dx-field-value">
<TextBox validationMessagePosition="left" inputAttr={addressLabel}>
<Validator>
<RequiredRule message="Address is required" />
</Validator>
</TextBox>
</div>
</div>
<div className="dx-field">
<div className="dx-field-label">Phone <i>(optional)</i></div>
<div className="dx-field-value">
<TextBox
mask="+1 (X00) 000-0000"
inputAttr={maskLabel}
maskRules={this.phoneRules}
maskInvalidMessage="The phone must have a correct USA phone format"
validationMessagePosition="left">
<Validator>
<PatternRule message="The phone must have a correct USA phone format" pattern={this.phonePattern} />
</Validator>
</TextBox>
</div>
</div>
<div>
<CheckBox
id="check"
defaultValue={false}
text="I agree to the Terms and Conditions">
<Validator>
<CompareRule message="You must agree to the Terms and Conditions" comparisonTarget={this.checkComparison} />
</Validator>
</CheckBox>
</div>
</div>
<div className="dx-fieldset">
<ValidationSummary id="summary" />
<Button
width="100%"
id="button"
text="Register"
type="success"
useSubmitBehavior={true} />
</div>
</form>
);
}
onInit(e) {
this.validatorInstance = e.component;
}
passwordComparison() {
return this.state.password;
}
checkComparison() {
return true;
}
onPasswordChanged(e) {
this.setState({
password: e.value,
});
if (this.state.confirmPassword) {
this.validatorInstance.validate();
}
}
onConfirmPasswordChanged(e) {
this.setState({
confirmPassword: e.value,
});
}
onFormSubmit(e) {
notify({
message: 'You have submitted the form',
position: {
my: 'center top',
at: 'center top',
},
}, 'success', 3000);
e.preventDefault();
}
}
function sendRequest(value) {
const invalidEmail = 'test@dx-email.com';
return new Promise((resolve) => {
setTimeout(() => {
resolve(value !== invalidEmail);
}, 1000);
});
}
function asyncValidation(params) {
return sendRequest(params.value);
}
export default App;
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App.js';
ReactDOM.render(
<App />,
document.getElementById('app'),
);
<!DOCTYPE html>
<html>
<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=1.0" />
<link rel="stylesheet" type="text/css" href="https://cdn3.devexpress.com/jslib/23.1.5/css/dx.light.css" />
<link rel="stylesheet" type="text/css" href="styles.css" />
<script src="https://unpkg.com/core-js@2.6.12/client/shim.min.js"></script>
<script src="https://unpkg.com/systemjs@0.21.3/dist/system.js"></script>
<script type="text/javascript" src="config.js"></script>
<script type="text/javascript">
System.import("./index.js");
</script>
</head>
<body class="dx-viewport">
<div class="demo-container">
<div id="app"></div>
</div>
</body>
</html>
#summary {
padding-left: 10px;
margin-top: 20px;
margin-bottom: 10px;
}
export const countries = ['Afghanistan', 'Albania', 'Algeria', 'Andorra', 'Angola',
'Antigua and Barbuda', 'Argentina', 'Armenia', 'Australia', 'Austria', 'Azerbaijan',
'The Bahamas', 'Bahrain', 'Bangladesh', 'Barbados', 'Belarus', 'Belgium', 'Belize',
'Benin', 'Bhutan', 'Bolivia', 'Bosnia and Herzegovina', 'Botswana', 'Brazil', 'Brunei',
'Bulgaria', 'Burkina Faso', 'Burma', 'Burundi', 'Cambodia', 'Cameroon', 'Canada', 'Cape Verde',
'Central African Republic', 'Chad', 'Chile', 'China', 'Colombia', 'Comoros',
'Democratic Republic of the Congo', 'Republic of the Congo', 'Costa Rica', 'Ivory Coast',
'Croatia', 'Cuba', 'Cyprus', 'Czech Republic', 'Denmark', 'Djibouti', 'Dominica', 'Dominican Republic',
'East Timor', 'Ecuador', 'Egypt', 'El Salvador', 'Equatorial Guinea', 'Eritrea', 'Estonia', 'Ethiopia',
'Fiji', 'Finland', 'France', 'Gabon', 'The Gambia', 'Georgia', 'Germany', 'Ghana', 'Greece', 'Grenada',
'Guatemala', 'Guinea', 'Guinea-Bissau', 'Guyana', 'Haiti', 'Honduras', 'Hungary', 'Iceland', 'India',
'Indonesia', 'Iran', 'Iraq', 'Republic of Ireland', 'Israel', 'Italy', 'Jamaica', 'Japan', 'Jordan',
'Kazakhstan', 'Kenya', 'Kiribati', 'North Korea', 'South Korea', 'Kuwait', 'Kyrgyzstan', 'Laos', 'Latvia',
'Lebanon', 'Lesotho', 'Liberia', 'Libya', 'Liechtenstein', 'Lithuania', 'Luxembourg', 'Republic of Macedonia',
'Madagascar', 'Malawi', 'Malaysia', 'Maldives', 'Mali', 'Malta', 'Marshall Islands', 'Mauritania', 'Mauritius',
'Mexico', 'Federated States of Micronesia', 'Moldova', 'Monaco', 'Mongolia', 'Montenegro', 'Morocco', 'Mozambique',
'Namibia', 'Nauru', 'Nepal', 'Kingdom of the Netherlands', 'New Zealand', 'Nicaragua', 'Niger', 'Nigeria', 'Norway',
'Oman', 'Pakistan', 'Palau', 'State of Palestine', 'Panama', 'Papua New Guinea', 'Paraguay', 'Peru', 'Philippines',
'Poland', 'Portugal', 'Qatar', 'Romania', 'Russia', 'Rwanda', 'Saint Kitts and Nevis', 'Saint Lucia',
'Saint Vincent and the Grenadines', 'Samoa', 'San Marino', 'São Tomé and Príncipe', 'Saudi Arabia', 'Senegal',
'Serbia', 'Seychelles', 'Sierra Leone', 'Singapore', 'Slovakia', 'Slovenia', 'Solomon Islands', 'Somalia', 'South Africa',
'South Sudan', 'Spain', 'Sri Lanka', 'Sudan', 'Suriname', 'Swaziland', 'Sweden', 'Switzerland', 'Syria', 'Tajikistan',
'Tanzania', 'Thailand', 'Togo', 'Tonga', 'Trinidad and Tobago', 'Tunisia', 'Turkey', 'Turkmenistan', 'Tuvalu', 'Uganda',
'Ukraine', 'United Arab Emirates', 'United Kingdom', 'United States', 'Uruguay', 'Uzbekistan', 'Vanuatu', 'Vatican City',
'Venezuela', 'Vietnam', 'Yemen', 'Zambia', 'Zimbabwe'];
export const nameLabel = { 'aria-label': 'Name' };
export const emailLabel = { 'aria-label': 'Email' };
export const maskLabel = { 'aria-label': 'Mask' };
export const dateLabel = { 'aria-label': 'Date' };
export const cityLabel = { 'aria-label': 'City' };
export const addressLabel = { 'aria-label': 'Address' };
export const passwordLabel = { 'aria-label': 'Password' };
export const countryLabel = { 'aria-label': 'Country' };
window.exports = window.exports || {};
window.config = {
transpiler: 'plugin-babel',
meta: {
'devextreme/localization.js': {
'esModule': true,
},
},
paths: {
'npm:': 'https://unpkg.com/',
},
defaultExtension: 'js',
map: {
'react': 'npm:react@17.0.2/umd/react.development.js',
'react-dom': 'npm:react-dom@17.0.2/umd/react-dom.development.js',
'prop-types': 'npm:prop-types@15.8.1/prop-types.js',
'rrule': 'npm:rrule@2.6.4/dist/es5/rrule.js',
'luxon': 'npm:luxon@1.28.1/build/global/luxon.min.js',
'es6-object-assign': 'npm:es6-object-assign@1.1.0',
'devextreme': 'npm:devextreme@23.1.6/cjs',
'devextreme-react': 'npm:devextreme-react@23.1.6',
'jszip': 'npm:jszip@3.7.1/dist/jszip.min.js',
'devextreme-quill': 'npm:devextreme-quill@1.6.2/dist/dx-quill.min.js',
'devexpress-diagram': 'npm:devexpress-diagram@2.2.2/dist/dx-diagram.js',
'devexpress-gantt': 'npm:devexpress-gantt@4.1.49/dist/dx-gantt.js',
'@devextreme/runtime': 'npm:@devextreme/runtime@3.0.12',
'inferno': 'npm:inferno@7.4.11/dist/inferno.min.js',
'inferno-compat': 'npm:inferno-compat/dist/inferno-compat.min.js',
'inferno-create-element': 'npm:inferno-create-element@7.4.11/dist/inferno-create-element.min.js',
'inferno-dom': 'npm:inferno-dom/dist/inferno-dom.min.js',
'inferno-hydrate': 'npm:inferno-hydrate@7.4.11/dist/inferno-hydrate.min.js',
'inferno-clone-vnode': 'npm:inferno-clone-vnode/dist/inferno-clone-vnode.min.js',
'inferno-create-class': 'npm:inferno-create-class/dist/inferno-create-class.min.js',
'inferno-extras': 'npm:inferno-extras/dist/inferno-extras.min.js',
// SystemJS plugins
'plugin-babel': 'npm:systemjs-plugin-babel@0.0.25/plugin-babel.js',
'systemjs-babel-build': 'npm:systemjs-plugin-babel@0.0.25/systemjs-babel-browser.js',
// Prettier
'prettier/standalone': 'npm:prettier@2.8.4/standalone.js',
'prettier/parser-html': 'npm:prettier@2.8.4/parser-html.js',
},
packages: {
'devextreme': {
defaultExtension: 'js',
},
'devextreme-react': {
main: 'index.js',
},
'devextreme/events/utils': {
main: 'index',
},
'devextreme/events': {
main: 'index',
},
'es6-object-assign': {
main: './index.js',
defaultExtension: 'js',
},
},
packageConfigPaths: [
'npm:@devextreme/*/package.json',
'npm:@devextreme/runtime@3.0.12/inferno/package.json',
],
babelOptions: {
sourceMaps: false,
stage0: true,
react: true,
},
};
System.config(window.config);