Skip to content

Commit b2a7c38

Browse files
committed
Use Select instead of TextField, fix some bugs
1 parent 0a34f45 commit b2a7c38

23 files changed

+419
-448
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
**/*.js.map
88
**/*.d.ts
99
!declarations/*.d.ts
10+
!docs/project/src/*.d.ts
1011
!docs/**/*.js
1112
!docs/**/*.js.map
1213

API.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ API
1313
* [Autocomplete](#autocomplete)
1414
* [Hidden](#hidden)
1515
* [Custom Message](#custom-message)
16+
* [FormValidation](#form-validation)
1617

1718
### Base (Common props)
1819

@@ -73,7 +74,7 @@ All [material-ui-pickers](https://github.com/dmtrKovalenko/material-ui-pickers)
7374
| maxDate | false | Date | |
7475
| disablePast | false | boolean | |
7576
| disableFuture | false | boolean | |
76-
| format | false | string | date fns string format |
77+
| format | false | string | date fns string format |
7778
| locale | false | string | use dateLocale as default |
7879
| onChange | true | Function(date) | |
7980

@@ -116,3 +117,15 @@ All [material-ui](https://material-ui.com/api/select/) props and:
116117
|----------|----------|--------|--------------------|
117118
| rules | true | string | separated by comma |
118119
| children | true | string | message |
120+
121+
122+
#### Form Validation
123+
124+
| Props | Required | Type | Description |
125+
|----------|----------|----------|---------------------------|
126+
| onSubmit | true | function | params = isValid: boolean |
127+
128+
| Methods by Ref | Params | Return | Description |
129+
|----------------|---------------------------------------|---------|------------------------------|
130+
| isValid | formSubmitted: boolean = default true | boolean | Check is all field are valid |
131+
| reset | | void | Reset the validation state |

README.md

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ yarn add @react-form-fields/material-ui
4545

4646
```jsx
4747
// import
48-
import ValidationContext from '@react-form-fields/material-ui/components/ValidationContext';
48+
import FormValidation from '@react-form-fields/material-ui/components/FormValidation';
4949
import FieldText from '@react-form-fields/material-ui/components/Text';
5050

5151
// render()
52-
<ValidationContext ref={ref=> this.validation = ref}>
52+
<FormValidation onSubmit={this.onSubmit}>
5353
<FieldText
5454
ref={ref => this.field = ref}
5555
label='Email'
@@ -66,12 +66,17 @@ yarn add @react-form-fields/material-ui
6666
validation='required'
6767
onChange={v => this.setState({ password: v }))}
6868
/>
69-
</ValidationContext>
69+
</FormValidation>
7070

7171
// onSubmit()
72-
if(this.validation.isValid()) {
73-
console.log('all fields are valid');
72+
onSubmit = (isValid: boolean) => {
73+
if(isValid) {
74+
console.log('all fields are valid');
75+
}
76+
// or by React.createRef
77+
const isValid = this.formValidation.current.isValid();
7478
}
79+
7580
```
7681

7782
### Config
@@ -80,8 +85,8 @@ Global Setup example:
8085

8186
```js
8287
import { setConfig } from '@react-form-fields/material-ui/config';
83-
import commonMasks from '@react-form-fields/material-ui/mask/common/pt-br';
84-
import validationMessage from '@react-form-fields/material-ui/validator/custom-languages/pt-br';
88+
import commonMasks from '@react-form-fields/core/mask/common/pt-br';
89+
import validationMessage from '@react-form-fields/core/validator/custom-languages/pt-br';
8590

8691
setConfig({
8792
masks: commonMasks,

components/Autocomplete/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import * as Autosuggest from 'react-autosuggest';
1717

1818
import { getConfig } from '../../config';
1919
import { WithStyles } from '../../decorators/withStyles';
20-
import { ITextFieldProps } from '../../interfaces/props';
20+
import { IBaseFieldProps, TextFieldPropsResolver } from '../../interfaces/props';
2121
import Input from './Input';
2222
import SuggestionsContainer from './SuggestionsContainer';
2323

@@ -27,7 +27,7 @@ interface IState extends IStateFieldBase {
2727
suggestions: IProps['options'][0][];
2828
}
2929

30-
interface IProps extends ITextFieldProps {
30+
interface IProps extends IBaseFieldProps, TextFieldPropsResolver {
3131
value: any;
3232
onChange: (value: any) => void;
3333
options: { value: any, label: string }[];

components/Color/index.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,14 @@ import FieldCoreBase, { IStateFieldBase } from '@react-form-fields/core/componen
33
import * as React from 'react';
44

55
import { getConfig } from '../../config';
6-
import { ITextFieldProps } from '../../interfaces/props';
6+
import { IBaseFieldProps, TextFieldPropsResolver } from '../../interfaces/props';
77
import PickerDialog from './PickerDialog';
88

99
interface IState extends IStateFieldBase {
1010
showPicker: boolean;
1111
}
1212

13-
interface IProps extends ITextFieldProps {
14-
value: string;
15-
onChange: (value: string) => void;
13+
interface IProps extends IBaseFieldProps, TextFieldPropsResolver {
1614
}
1715

1816
export default class FieldColor extends FieldCoreBase<IProps, IState> {
File renamed without changes.

components/Date.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export default class FieldDate extends FieldCoreBase<IProps> {
3535
}
3636

3737
render() {
38-
const { value, label, format, helperText, ...extraProps } = this.props;
38+
const { value, label, format, helperText, validation, validationContext, ...extraProps } = this.props;
3939

4040
return (
4141
<React.Fragment>

components/Select.tsx

Lines changed: 45 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
1-
import CircularProgress from '@material-ui/core/CircularProgress/CircularProgress';
2-
import InputAdornment from '@material-ui/core/InputAdornment/InputAdornment';
1+
import CircularProgress from '@material-ui/core/CircularProgress';
2+
import FormControl from '@material-ui/core/FormControl';
3+
import FormHelperText from '@material-ui/core/FormHelperText';
4+
import InputAdornment from '@material-ui/core/InputAdornment';
5+
import InputLabel from '@material-ui/core/InputLabel';
36
import MenuItem from '@material-ui/core/MenuItem/MenuItem';
4-
import TextField from '@material-ui/core/TextField/TextField';
7+
import Select from '@material-ui/core/Select';
58
import FieldCoreBase from '@react-form-fields/core/components/FieldCoreBase';
69
import * as React from 'react';
710

811
import { getConfig } from '../config';
9-
import { ITextFieldProps } from '../interfaces/props';
12+
import { IBaseFieldProps, SelectPropsResolver } from '../interfaces/props';
1013

11-
interface IProps extends ITextFieldProps {
14+
interface IProps extends IBaseFieldProps, SelectPropsResolver {
1215
options?: { value: string | number, label: string }[];
13-
emptyOption?: boolean;
1416
loading?: boolean;
17+
helperText?: string;
1518
onChange: (value: any) => void;
19+
emptyOption?: string;
1620
}
1721

1822
export default class FieldSelect extends FieldCoreBase<IProps> {
@@ -29,46 +33,46 @@ export default class FieldSelect extends FieldCoreBase<IProps> {
2933
}
3034

3135
render() {
32-
const { value, options, children, loading, emptyOption } = this.props;
36+
const { value, options, children, label, loading, helperText, emptyOption, ...extra } = this.props;
3337

3438
return (
3539
<React.Fragment>
3640
{super.render()}
3741

38-
<TextField
39-
{...{
40-
fullWidth: true,
41-
margin: 'normal',
42-
...this.props,
43-
value: !value ? '' : value,
44-
required: this.isRequired,
45-
select: true,
46-
error: !!this.errorMessage,
47-
helperText: this.errorMessage,
48-
onChange: this.onChange,
49-
onBlur: this.onBlur,
50-
submitted: null,
51-
touched: null,
52-
loading: null
53-
}}
54-
InputProps={{
55-
endAdornment: !loading ? null : (
56-
<InputAdornment position='end'>
57-
<CircularProgress size={20} />
58-
</InputAdornment>
59-
)
60-
}}
61-
>
62-
{emptyOption &&
63-
<MenuItem value={''}>Selecione...</MenuItem>
64-
}
65-
{(options || []).map(option => (
66-
<MenuItem key={option.value} value={option.value}>
67-
{option.label}
68-
</MenuItem>
69-
))}
70-
{children}
71-
</TextField>
42+
<FormControl margin='normal' fullWidth error={!!this.errorMessage}>
43+
<InputLabel shrink={!!emptyOption} error={!!this.errorMessage}>{label}</InputLabel>
44+
<Select
45+
{...{
46+
fullWidth: true,
47+
endAdornment: !loading ? null : (
48+
<InputAdornment position='end'>
49+
<CircularProgress size={20} />
50+
</InputAdornment>
51+
),
52+
...extra,
53+
displayEmpty: !!emptyOption,
54+
value: !value ? '' : value,
55+
required: this.isRequired,
56+
error: !!this.errorMessage,
57+
onChange: this.onChange,
58+
onBlur: this.onBlur,
59+
submitted: null,
60+
touched: null,
61+
loading: null
62+
}}
63+
>
64+
{emptyOption &&
65+
<MenuItem value={''}>{emptyOption}</MenuItem>
66+
}
67+
{(options || []).map(option => (
68+
<MenuItem key={option.value} value={option.value}>
69+
{option.label}
70+
</MenuItem>
71+
))}
72+
{children}
73+
</Select>
74+
<FormHelperText error={!!this.errorMessage}>{this.errorMessage || helperText}</FormHelperText>
75+
</FormControl>
7276
</React.Fragment>
7377
);
7478
}

components/Text.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import FieldCoreBase from '@react-form-fields/core/components/FieldCoreBase';
55
import * as React from 'react';
66

77
import { getConfig } from '../config';
8-
import { ITextFieldProps } from '../interfaces/props';
8+
import { IBaseFieldProps, TextFieldPropsResolver } from '../interfaces/props';
99

10-
interface IProps extends ITextFieldProps {
10+
interface IProps extends IBaseFieldProps, TextFieldPropsResolver {
1111
mask?: 'phone';
1212
loading?: boolean;
1313
value: any;

docs/asset-manifest.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"main.css": "static/css/main.7f08d526.css",
33
"main.css.map": "static/css/main.7f08d526.css.map",
4-
"main.js": "static/js/main.ef712415.js",
5-
"main.js.map": "static/js/main.ef712415.js.map"
4+
"main.js": "static/js/main.20586e5c.js",
5+
"main.js.map": "static/js/main.20586e5c.js.map"
66
}

0 commit comments

Comments
 (0)