import React, { useState, useLayoutEffect, Component } from 'react';
import { storiesOf } from '@storybook/react';
import { Button, Modal, TreeSelect, Row, Col, Avatar, Toast, Select as BasicSelect,
Form,
useFormState,
useFormApi,
useFieldApi,
useFieldState,
withFormState,
withFormApi,
withField,
ArrayField,
AutoComplete,
Collapse,
Icon } from '../../../index';
import { ComponentUsingFormState } from '../Hook/hookDemo';
import Textarea from '../../../input/textarea';
const { Input, Select, DatePicker, Switch, Slider, CheckboxGroup, Checkbox, RadioGroup, Radio, TimePicker, InputNumber, InputGroup } = Form;
// Form level validate
class ValidateFieldsDemo extends Component {
constructor() {
super();
this.syncValidate = this.syncValidate.bind(this);
this.asyncValidate = this.asyncValidate.bind(this);
}
syncValidate(values) {
const errors = {};
if (values.name !== 'mike') {
errors.name = 'you must name mike';
}
if (values.sex !== 'female') {
errors.sex = 'must be woman';
// errors.group = {};
// errors.group.sort = '';
}
errors.familyName = [
{ before: 'before errror inject success ', after: 'after error inject success' },
'familyName[1] error inject success'
];
// // return '';
return errors;
}
asyncValidate(values) {
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
return sleep(2000).then(() => {
let errors = {};
if (values.name !== 'mike') {
errors.name = 'you must name mike';
}
if (values.sex !== 'female') {
errors.sex = 'sex not valid';
}
console.log(errors);
return errors;
});
}
render() {
return (
<>
同步校验
异步校验
>
);
}
}
// Field level
class CustomValidateDemo extends Component {
constructor() {
super();
this.validateName = this.validateName.bind(this);
this.asyncValidate = this.asyncValidate.bind(this);
}
validateName(val) {
if (!val) {
return '【sync】can\'t be empty';
} else if (val.length <= 5) {
return 我是传入的reactNode;
// // return '【sync】must more than 5';
}
return;
}
asyncValidate(val, values) {
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
return sleep(2000).then(() => {
if (!val) {
return '【async】can\'t be empty';
// throw '【async】can\'t be empty';
} else if (val.length <= 5) {
return '【async】must more than 5';
// throw '【async】must more than 5';
} else {
return;
}
});
}
render() {
return (
);
}
}
// validate partial fields
// reset partial fields
class PartValidAndResetDemo extends Component {
constructor() {
super();
this.validate = this.validate.bind(this);
this.getFormApi = this.getFormApi.bind(this);
this.validatePartial = this.validatePartial.bind(this);
this.resetPartial = this.resetPartial.bind(this);
}
getFormApi(formApi) {
this.formApi = formApi;
}
validate(val) {
if (!val) {
return '【sync】can\'t be empty';
} else if (val.length <= 5) {
return 我是传入的reactNode;
}
return;
}
validatePartial(type) {
let scope = this.formApi.getValue('validateScope');
!scope ? scope = [] : null;
this.formApi.validate(scope)
.then(values => {
console.log(values);
Toast.success('pass');
}).catch(error => {
Toast.error('error');
console.log(error);
});
}
resetPartial() {
let scope = this.formApi.getValue('resetScope');
this.formApi.reset(scope);
}
render() {
let options = ['a', 'b', 'c', 'd', 'b.name'].map(item => ({ label: item, value: item }));
return (
);
}
}
class RulesValidateDemo extends Component {
constructor() {
super();
this.validate = this.validate.bind(this);
this.getFormApi = this.getFormApi.bind(this);
this.set = this.set.bind(this);
}
validate(values) {
this.formApi.validate().then(value => {
Toast.info('reslove');
}).catch(error => {
Toast.error('error');
});
}
getFormApi(formApi) {
this.formApi = formApi;
}
set(val) {
this.formApi.setError('panel[1].a', val);
}
render() {
return (
<>
>
);
}
}
class SetBugDemo extends Component {
constructor() {
super();
this.state = {
key: 1
};
}
validate = values => {
this.formApi.validate().then(value => {
Toast.info('reslove');
}).catch(error => {
Toast.error('error');
});
}
getFormApi = formApi => {
this.formApi = formApi;
}
set = val => {
this.formApi.setError('panel[1].a', val);
}
refresh = val => {
this.setState({ key: new Date().valueOf() });
}
render() {
const commonRules = [{
required: true, message: '不能为空'
}];
const { key } = this.state;
return (
<>
{/* */}
>
)}
>
);
}
}
class UnmountedLeafDemo extends React.Component {
constructor() {
super();
this.state = {
show: true,
};
this.change = this.change.bind(this);
}
change() {
let { show } = this;
this.setState({ show: !show });
}
render() {
let { show } = this.state;
return (
: null}
>
)}
);
}
}
class RulesExample extends React.Component {
constructor() {
super();
this.state = {
initValues: {
name: 'semi',
role: 'rd'
}
};
this.getFormApi = this.getFormApi.bind(this);
}
getFormApi(formApi) {
this.formApi = formApi;
}
render() {
const { Select, Input } = Form;
const style = { width: '100%' };
return (
);
}
}
class RaceAsyncDemo extends React.Component {
constructor() {
super();
this.asyncValidate = this.asyncValidate.bind(this);
}
asyncValidate(val, values) {
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
let time = 1000;
if (val === 'semi') {
time = 4000;
}
return sleep(time).then(() => {
if (!val) {
return 'can\'t be empty';
} else if (val === 'semi') {
return 'sleep 4000';
} else if (val === 'sem') {
return 'sleep 1000';
} else {
return '';
}
});
}
render() {
return (
{
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
let time = 1000;
if (value === 'semi') {
time = 4000;
}
return sleep(time).then(() => {
if (value === 'semi') {
throw Error('sleep 4000');
} else if (value === 'sem') {
throw Error('sleep 1000');
} else {
return;
}
});
}
}
]}
/>
);
}
}
export { ValidateFieldsDemo, CustomValidateDemo, PartValidAndResetDemo, RulesValidateDemo, SetBugDemo, UnmountedLeafDemo, RulesExample, RaceAsyncDemo };