Skip to content

Commit 88bb78f

Browse files
author
Domino987
committed
fix: Hook edit cell up to validate
1 parent e0fa5ee commit 88bb78f

File tree

4 files changed

+39
-59
lines changed

4 files changed

+39
-59
lines changed

src/components/MTableEditRow/index.js

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Typography from '@material-ui/core/Typography';
55
import PropTypes from 'prop-types';
66
import { byString, setByString } from '../../utils';
77
import * as CommonValues from '../../utils/common-values';
8+
import { validateInput } from '../../utils/validate';
89

910
function MTableEditRow(props) {
1011
const [state, setState] = useState(() => {
@@ -97,21 +98,7 @@ function MTableEditRow(props) {
9798
} else {
9899
const { editComponent, ...cellProps } = columnDef;
99100
const EditComponent = editComponent || props.components.EditField;
100-
let error = { isValid: true, helperText: '' };
101-
if (columnDef.validate) {
102-
const validateResponse = columnDef.validate(state.data);
103-
switch (typeof validateResponse) {
104-
case 'object':
105-
error = { ...validateResponse };
106-
break;
107-
case 'boolean':
108-
error = { isValid: validateResponse, helperText: '' };
109-
break;
110-
case 'string':
111-
error = { isValid: false, helperText: validateResponse };
112-
break;
113-
}
114-
}
101+
const error = validateInput(columnDef, state.data);
115102
return (
116103
<TableCell
117104
size={size}
@@ -172,18 +159,8 @@ function MTableEditRow(props) {
172159
...props.localization
173160
};
174161
const isValid = props.columns.every((column) => {
175-
if (column.validate) {
176-
const response = column.validate(state.data);
177-
switch (typeof response) {
178-
case 'object':
179-
return response.isValid;
180-
case 'string':
181-
return response.length === 0;
182-
case 'boolean':
183-
return response;
184-
}
185-
}
186-
return true;
162+
const error = validateInput(column, state.data);
163+
return error.isValid;
187164
});
188165
const actions = [
189166
{

src/components/MTableEditRow/m-table-edit-row.js

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import PropTypes from 'prop-types';
3232
import React from 'react';
3333
import { byString, setByString } from '../../utils';
3434
import * as CommonValues from '../../utils/common-values';
35+
import { validateInput } from '../../utils/validate';
3536
/* eslint-enable no-unused-vars */
3637

3738
export default class MTableEditRow extends React.Component {
@@ -128,21 +129,7 @@ export default class MTableEditRow extends React.Component {
128129
const { editComponent, ...cellProps } = columnDef;
129130
const EditComponent =
130131
editComponent || this.props.components.EditField;
131-
let error = { isValid: true, helperText: '' };
132-
if (columnDef.validate) {
133-
const validateResponse = columnDef.validate(this.state.data);
134-
switch (typeof validateResponse) {
135-
case 'object':
136-
error = { ...validateResponse };
137-
break;
138-
case 'boolean':
139-
error = { isValid: validateResponse, helperText: '' };
140-
break;
141-
case 'string':
142-
error = { isValid: false, helperText: validateResponse };
143-
break;
144-
}
145-
}
132+
const error = validateInput(columnDef, this.state.data);
146133
return (
147134
<TableCell
148135
size={size}
@@ -206,19 +193,8 @@ export default class MTableEditRow extends React.Component {
206193
...this.props.localization
207194
};
208195
const isValid = this.props.columns.every((column) => {
209-
if (column.validate) {
210-
const response = column.validate(this.state.data);
211-
switch (typeof response) {
212-
case 'object':
213-
return response.isValid;
214-
case 'string':
215-
return response.length === 0;
216-
case 'boolean':
217-
return response;
218-
}
219-
} else {
220-
return true;
221-
}
196+
const error = validateInput(column, this.state.data);
197+
return error.isValid;
222198
});
223199
const actions = [
224200
{

src/components/m-table-edit-cell.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import PropTypes from 'prop-types';
33
import TableCell from '@material-ui/core/TableCell';
44
import CircularProgress from '@material-ui/core/CircularProgress';
55
import { withTheme } from '@material-ui/core';
6-
6+
import { validateInput } from '../utils/validate';
77
class MTableEditCell extends React.Component {
88
constructor(props) {
99
super(props);
@@ -60,6 +60,11 @@ class MTableEditCell extends React.Component {
6060
};
6161

6262
onApprove = () => {
63+
const isValid = validateInput(this.props.columnDef, this.state.value)
64+
.isValid;
65+
if (!isValid) {
66+
return;
67+
}
6368
this.setState({ isLoading: true }, () => {
6469
this.props.cellEditable
6570
.onCellEditApproved(
@@ -85,7 +90,7 @@ class MTableEditCell extends React.Component {
8590
this.props.onCellEditFinished(this.props.rowData, this.props.columnDef);
8691
};
8792

88-
renderActions() {
93+
renderActions(isValid) {
8994
if (this.state.isLoading) {
9095
return (
9196
<div style={{ display: 'flex', justifyContent: 'center', width: 60 }}>
@@ -99,7 +104,7 @@ class MTableEditCell extends React.Component {
99104
icon: this.props.icons.Check,
100105
tooltip: this.props.localization.saveTooltip,
101106
onClick: this.onApprove,
102-
disabled: this.state.isLoading
107+
disabled: this.state.isLoading || !isValid
103108
},
104109
{
105110
icon: this.props.icons.Clear,
@@ -119,12 +124,16 @@ class MTableEditCell extends React.Component {
119124
}
120125

121126
render() {
127+
const error = validateInput(this.props.columnDef, this.state.value);
128+
console.log(error);
122129
return (
123130
<TableCell size={this.props.size} style={this.getStyle()} padding="none">
124131
<div style={{ display: 'flex', alignItems: 'center' }}>
125132
<div style={{ flex: 1, marginRight: 4 }}>
126133
<this.props.components.EditField
127134
columnDef={this.props.columnDef}
135+
error={!error.isValid}
136+
helperText={error.helperText}
128137
value={this.state.value}
129138
onChange={(value) => this.setState({ value })}
130139
onKeyDown={this.handleKeyDown}
@@ -133,7 +142,7 @@ class MTableEditCell extends React.Component {
133142
autoFocus
134143
/>
135144
</div>
136-
{this.renderActions()}
145+
{this.renderActions(error.isValid)}
137146
</div>
138147
</TableCell>
139148
);

src/utils/validate.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function validateInput(columnDef, data) {
2+
if (columnDef.validate) {
3+
const validateResponse = columnDef.validate(data);
4+
switch (typeof validateResponse) {
5+
case 'object':
6+
return { ...validateResponse };
7+
case 'boolean':
8+
return { isValid: validateResponse, helperText: '' };
9+
case 'string':
10+
return { isValid: false, helperText: validateResponse };
11+
default:
12+
return { isValid: true, helperText: '' };
13+
}
14+
}
15+
return { isValid: true, helperText: '' };
16+
}
17+
18+
export { validateInput };

0 commit comments

Comments
 (0)