mirror of
https://github.com/strapi/strapi.git
synced 2025-08-02 13:58:18 +00:00
fix(files-required-validation): fix required prop save. add error display for component. fix validation file repeatedly
This commit is contained in:
parent
d4c2cb1009
commit
5a666d80df
@ -8,7 +8,8 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import PropTypes from 'prop-types';
|
import PropTypes from 'prop-types';
|
||||||
import { FormattedMessage } from 'react-intl';
|
import { FormattedMessage } from 'react-intl';
|
||||||
import { cloneDeep } from 'lodash';
|
import { cloneDeep, isArray, isObject } from 'lodash';
|
||||||
|
import cn from 'classnames';
|
||||||
|
|
||||||
import ImgPreview from 'components/ImgPreview';
|
import ImgPreview from 'components/ImgPreview';
|
||||||
import InputFileDetails from 'components/InputFileDetails';
|
import InputFileDetails from 'components/InputFileDetails';
|
||||||
@ -71,11 +72,12 @@ class InputFile extends React.Component {
|
|||||||
if (this.props.multiple) {
|
if (this.props.multiple) {
|
||||||
value.splice(this.state.position, 1);
|
value.splice(this.state.position, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update the parent's props
|
// Update the parent's props
|
||||||
const target = {
|
const target = {
|
||||||
name: this.props.name,
|
name: this.props.name,
|
||||||
type: 'file',
|
type: 'file',
|
||||||
value,
|
value: Object.keys(value).length === 0 ? '' : value,
|
||||||
};
|
};
|
||||||
|
|
||||||
this.props.onChange({ target });
|
this.props.onChange({ target });
|
||||||
@ -94,6 +96,19 @@ class InputFile extends React.Component {
|
|||||||
this.setState({ position: newPosition });
|
this.setState({ position: newPosition });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
isVisibleDetails = () => {
|
||||||
|
const {value} = this.props;
|
||||||
|
|
||||||
|
if (!value ||
|
||||||
|
(isArray(value) && value.length === 0) ||
|
||||||
|
(isObject(value) && Object.keys(value).length === 0)
|
||||||
|
) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const {
|
const {
|
||||||
multiple,
|
multiple,
|
||||||
@ -104,39 +119,43 @@ class InputFile extends React.Component {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<ImgPreview
|
<div className={cn("form-control", styles.inputFileControlForm, this.props.error && 'is-invalid')}>
|
||||||
didDeleteFile={this.state.didDeleteFile}
|
<ImgPreview
|
||||||
files={value}
|
didDeleteFile={this.state.didDeleteFile}
|
||||||
isUploading={this.state.isUploading}
|
files={value}
|
||||||
multiple={multiple}
|
isUploading={this.state.isUploading}
|
||||||
name={name}
|
|
||||||
onChange={onChange}
|
|
||||||
onBrowseClick={this.handleClick}
|
|
||||||
onDrop={this.onDrop}
|
|
||||||
position={this.state.position}
|
|
||||||
updateFilePosition={this.updateFilePosition}
|
|
||||||
/>
|
|
||||||
<label style={{ width: '100%'}}>
|
|
||||||
<input
|
|
||||||
className={styles.inputFile}
|
|
||||||
multiple={multiple}
|
multiple={multiple}
|
||||||
name={name}
|
name={name}
|
||||||
onChange={this.handleChange}
|
onChange={onChange}
|
||||||
type="file"
|
onBrowseClick={this.handleClick}
|
||||||
ref={(input) => this.inputFile = input}
|
onDrop={this.onDrop}
|
||||||
|
position={this.state.position}
|
||||||
|
updateFilePosition={this.updateFilePosition}
|
||||||
/>
|
/>
|
||||||
|
<label style={{"margin-bottom": 0, width: '100%'}}>
|
||||||
|
<input
|
||||||
|
className={styles.inputFile}
|
||||||
|
multiple={multiple}
|
||||||
|
name={name}
|
||||||
|
onChange={this.handleChange}
|
||||||
|
type="file"
|
||||||
|
ref={(input) => this.inputFile = input}
|
||||||
|
/>
|
||||||
|
|
||||||
<div className={styles.buttonContainer}>
|
<div className={styles.buttonContainer}>
|
||||||
<i className="fa fa-plus" />
|
<i className="fa fa-plus" />
|
||||||
<FormattedMessage id="app.components.InputFile.newFile" />
|
<FormattedMessage id="app.components.InputFile.newFile" />
|
||||||
</div>
|
</div>
|
||||||
</label>
|
</label>
|
||||||
<InputFileDetails
|
</div>
|
||||||
file={value[this.state.position] || value[0] || value}
|
{this.isVisibleDetails() && (
|
||||||
multiple={multiple}
|
<InputFileDetails
|
||||||
number={value.length}
|
file={value[this.state.position] || value[0] || value}
|
||||||
onFileDelete={this.handleFileDelete}
|
multiple={multiple}
|
||||||
/>
|
number={value.length}
|
||||||
|
onFileDelete={this.handleFileDelete}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -146,9 +165,12 @@ InputFile.defaultProps = {
|
|||||||
multiple: false,
|
multiple: false,
|
||||||
setLabel: () => {},
|
setLabel: () => {},
|
||||||
value: [],
|
value: [],
|
||||||
|
error: false,
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
InputFile.propTypes = {
|
InputFile.propTypes = {
|
||||||
|
error: PropTypes.bool,
|
||||||
multiple: PropTypes.bool,
|
multiple: PropTypes.bool,
|
||||||
name: PropTypes.string.isRequired,
|
name: PropTypes.string.isRequired,
|
||||||
onChange: PropTypes.func.isRequired,
|
onChange: PropTypes.func.isRequired,
|
||||||
|
@ -32,3 +32,7 @@
|
|||||||
.copy {
|
.copy {
|
||||||
cursor: copy !important;
|
cursor: copy !important;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.inputFileControlForm {
|
||||||
|
padding: 0;
|
||||||
|
}
|
@ -14,22 +14,39 @@ import Label from 'components/Label';
|
|||||||
import InputDescription from 'components/InputDescription';
|
import InputDescription from 'components/InputDescription';
|
||||||
import InputFile from 'components/InputFile';
|
import InputFile from 'components/InputFile';
|
||||||
import InputSpacer from 'components/InputSpacer';
|
import InputSpacer from 'components/InputSpacer';
|
||||||
|
import InputErrors from 'components/InputErrors';
|
||||||
|
|
||||||
|
// Styles
|
||||||
import styles from './styles.scss';
|
import styles from './styles.scss';
|
||||||
|
|
||||||
class InputFileWithErrors extends React.Component {
|
class InputFileWithErrors extends React.Component {
|
||||||
state = { label: false, hasValue: false };
|
state = { errors: [], label: false, hasValue: false };
|
||||||
|
|
||||||
componentDidMount() {
|
componentDidMount() {
|
||||||
|
const { errors } = this.props;
|
||||||
|
let newState = Object.assign({}, this.state);
|
||||||
|
|
||||||
if (this.props.multiple && !isEmpty(this.props.value)) {
|
if (this.props.multiple && !isEmpty(this.props.value)) {
|
||||||
this.setState({ label: 1, hasValue: true });
|
newState = Object.assign({}, newState, { label: 1, hasValue: true });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!isEmpty(errors)) {
|
||||||
|
newState = Object.assign({}, newState, { errors });
|
||||||
|
}
|
||||||
|
|
||||||
|
this.setState(newState);
|
||||||
}
|
}
|
||||||
|
|
||||||
componentWillReceiveProps(nextProps) {
|
componentWillReceiveProps(nextProps) {
|
||||||
if (!this.state.hasValue && !isEmpty(nextProps.value) && nextProps.multiple && differenceBy(nextProps.value, this.props.value, 'name').length > 0) {
|
if (!this.state.hasValue && !isEmpty(nextProps.value) && nextProps.multiple && differenceBy(nextProps.value, this.props.value, 'name').length > 0) {
|
||||||
this.setState({ label: 1, hasValue: true });
|
this.setState({ label: 1, hasValue: true });
|
||||||
}
|
}
|
||||||
|
// Check if errors have been updated during validations
|
||||||
|
if (nextProps.didCheckErrors !== this.props.didCheckErrors) {
|
||||||
|
// Remove from the state the errors that have already been set
|
||||||
|
const errors = isEmpty(nextProps.errors) ? [] : nextProps.errors;
|
||||||
|
this.setState({ errors });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
setLabel = (label) => {
|
setLabel = (label) => {
|
||||||
@ -40,6 +57,9 @@ class InputFileWithErrors extends React.Component {
|
|||||||
const {
|
const {
|
||||||
className,
|
className,
|
||||||
customBootstrapClass,
|
customBootstrapClass,
|
||||||
|
errorsClassName,
|
||||||
|
errorsStyle,
|
||||||
|
noErrorsDescription,
|
||||||
inputDescription,
|
inputDescription,
|
||||||
inputDescriptionClassName,
|
inputDescriptionClassName,
|
||||||
inputDescriptionStyle,
|
inputDescriptionStyle,
|
||||||
@ -76,6 +96,7 @@ class InputFileWithErrors extends React.Component {
|
|||||||
)}
|
)}
|
||||||
<InputFile
|
<InputFile
|
||||||
multiple={multiple}
|
multiple={multiple}
|
||||||
|
error={!isEmpty(this.state.errors)}
|
||||||
name={name}
|
name={name}
|
||||||
onChange={onChange}
|
onChange={onChange}
|
||||||
setLabel={this.setLabel}
|
setLabel={this.setLabel}
|
||||||
@ -86,6 +107,11 @@ class InputFileWithErrors extends React.Component {
|
|||||||
message={inputDescription}
|
message={inputDescription}
|
||||||
style={inputDescriptionStyle}
|
style={inputDescriptionStyle}
|
||||||
/>
|
/>
|
||||||
|
<InputErrors
|
||||||
|
className={errorsClassName}
|
||||||
|
errors={!noErrorsDescription && this.state.errors || []}
|
||||||
|
style={errorsStyle}
|
||||||
|
/>
|
||||||
{spacer}
|
{spacer}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
@ -93,8 +119,12 @@ class InputFileWithErrors extends React.Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
InputFileWithErrors.defaultProps = {
|
InputFileWithErrors.defaultProps = {
|
||||||
|
errors: [],
|
||||||
|
errorsClassName: '',
|
||||||
|
errorsStyle: {},
|
||||||
className: '',
|
className: '',
|
||||||
customBootstrapClass: 'col-md-6',
|
customBootstrapClass: 'col-md-6',
|
||||||
|
didCheckErrors: false,
|
||||||
inputDescription: '',
|
inputDescription: '',
|
||||||
inputDescriptionClassName: '',
|
inputDescriptionClassName: '',
|
||||||
inputDescriptionStyle: {},
|
inputDescriptionStyle: {},
|
||||||
@ -102,6 +132,7 @@ InputFileWithErrors.defaultProps = {
|
|||||||
labelClassName: '',
|
labelClassName: '',
|
||||||
labelStyle: {},
|
labelStyle: {},
|
||||||
multiple: false,
|
multiple: false,
|
||||||
|
noErrorsDescription: false,
|
||||||
style: {},
|
style: {},
|
||||||
value: [],
|
value: [],
|
||||||
};
|
};
|
||||||
@ -109,6 +140,10 @@ InputFileWithErrors.defaultProps = {
|
|||||||
InputFileWithErrors.propTypes = {
|
InputFileWithErrors.propTypes = {
|
||||||
className: PropTypes.string,
|
className: PropTypes.string,
|
||||||
customBootstrapClass: PropTypes.string,
|
customBootstrapClass: PropTypes.string,
|
||||||
|
didCheckErrors: PropTypes.bool,
|
||||||
|
errors: PropTypes.array,
|
||||||
|
errorsClassName: PropTypes.string,
|
||||||
|
errorsStyle: PropTypes.object,
|
||||||
inputDescription: PropTypes.oneOfType([
|
inputDescription: PropTypes.oneOfType([
|
||||||
PropTypes.string,
|
PropTypes.string,
|
||||||
PropTypes.func,
|
PropTypes.func,
|
||||||
@ -131,6 +166,7 @@ InputFileWithErrors.propTypes = {
|
|||||||
labelStyle: PropTypes.object,
|
labelStyle: PropTypes.object,
|
||||||
multiple: PropTypes.bool,
|
multiple: PropTypes.bool,
|
||||||
name: PropTypes.string.isRequired,
|
name: PropTypes.string.isRequired,
|
||||||
|
noErrorsDescription: PropTypes.bool,
|
||||||
onChange: PropTypes.func.isRequired,
|
onChange: PropTypes.func.isRequired,
|
||||||
style: PropTypes.object,
|
style: PropTypes.object,
|
||||||
value: PropTypes.oneOfType([
|
value: PropTypes.oneOfType([
|
||||||
|
@ -178,7 +178,8 @@ module.exports = {
|
|||||||
if (params.plugin === 'upload' && relation.model || relation.collection === 'file') {
|
if (params.plugin === 'upload' && relation.model || relation.collection === 'file') {
|
||||||
params = {
|
params = {
|
||||||
type: 'media',
|
type: 'media',
|
||||||
multiple: params.collection ? true : false
|
multiple: params.collection ? true : false,
|
||||||
|
required: params.required
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
params = _.omit(params, ['collection', 'model', 'via']);
|
params = _.omit(params, ['collection', 'model', 'via']);
|
||||||
@ -288,7 +289,8 @@ module.exports = {
|
|||||||
attrs[attribute.name] = {
|
attrs[attribute.name] = {
|
||||||
[attribute.params.multiple ? 'collection' : 'model']: 'file',
|
[attribute.params.multiple ? 'collection' : 'model']: 'file',
|
||||||
via,
|
via,
|
||||||
plugin: 'upload'
|
plugin: 'upload',
|
||||||
|
required: attribute.params.required === true ? true : false
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
} else if (_.has(attribute, 'params.target')) {
|
} else if (_.has(attribute, 'params.target')) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user