Vulcan/packages/vulcan-forms/lib/components/FormComponent.jsx

154 lines
4.7 KiB
React
Raw Normal View History

2017-05-19 14:42:43 -06:00
import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';
2017-06-01 11:42:30 +09:00
import { intlShape } from 'meteor/vulcan:i18n';
import classNames from 'classnames';
import { Components } from 'meteor/vulcan:core';
2017-05-19 14:42:43 -06:00
class FormComponent extends PureComponent {
constructor(props) {
super(props);
this.handleBlur = this.handleBlur.bind(this);
this.updateCharacterCount = this.updateCharacterCount.bind(this);
this.renderErrors = this.renderErrors.bind(this);
if (props.limit) {
this.state = {
limit: props.value ? props.limit - props.value.length : props.limit
}
}
}
componentWillReceiveProps(nextProps) {
this.updateCharacterCount(nextProps.name, nextProps.value)
}
handleBlur() {
// see https://facebook.github.io/react/docs/more-about-refs.html
if (this.formControl !== null) {
this.props.updateCurrentValues({[this.props.name]: this.formControl.getValue()});
}
}
updateCharacterCount(name, value) {
if (this.props.limit) {
2017-08-16 16:24:50 +09:00
const characterCount = value ? value.length : 0;
this.setState({
2017-08-16 16:24:50 +09:00
limit: this.props.limit - characterCount
});
}
}
renderComponent() {
// see https://facebook.github.io/react/warnings/unknown-prop.html
const { control, group, updateCurrentValues, document, beforeComponent, afterComponent, limit, errors, ...rest } = this.props; // eslint-disable-line
2017-08-19 16:17:52 +09:00
// const base = typeof this.props.control === 'function' ? this.props : rest;
const properties = {
2017-08-16 16:24:50 +09:00
value: '', // default value, will be overridden by `rest` if real value has been passed down through props
...rest,
onBlur: this.handleBlur,
ref: (ref) => this.formControl = ref,
};
2016-04-07 15:24:38 +09:00
// if control is a React component, use it
2017-08-19 16:17:52 +09:00
if (typeof this.props.control === 'function') {
2016-04-07 14:20:44 +09:00
2016-11-15 10:44:01 +01:00
return <this.props.control {...properties} document={document} />
2016-04-04 16:50:07 +09:00
2016-04-07 15:24:38 +09:00
} else { // else pick a predefined component
2016-04-04 16:50:07 +09:00
2017-08-19 16:17:52 +09:00
// for text fields, update character count on change
if (!this.props.control || ['number', 'url', 'email', 'textarea', 'text'].includes(this.props.control)) {
properties.onChange = this.updateCharacterCount;
}
2016-04-07 15:24:38 +09:00
switch (this.props.control) {
2017-08-19 16:17:52 +09:00
case 'number':
return <Components.FormComponentNumber {...properties}/>;
2017-08-19 16:17:52 +09:00
case 'url':
return <Components.FormComponentUrl {...properties}/>;
2017-08-19 16:17:52 +09:00
case 'email':
return <Components.FormComponentEmail {...properties}/>;
2017-08-19 16:17:52 +09:00
case 'textarea':
return <Components.FormComponentTextarea {...properties}/>;
2017-08-19 16:17:52 +09:00
case 'checkbox':
return <Components.FormComponentCheckbox {...properties} />;
2017-08-19 16:17:52 +09:00
case 'checkboxgroup':
return <Components.FormComponentCheckboxGroup {...properties} />;
2017-08-19 16:17:52 +09:00
case 'radiogroup':
// not sure why, but onChange needs to be specified here
properties.onChange = (name, value) => {this.props.updateCurrentValues({[name]: value})};
return <Components.FormComponentRadioGroup {...properties} />;
2017-08-19 16:17:52 +09:00
case 'select':
properties.options = [{label: this.context.intl.formatMessage({id: 'forms.select_option'}), disabled: true}, ...properties.options];
return <Components.FormComponentSelect {...properties} />;
2017-08-19 16:17:52 +09:00
case 'datetime':
return <Components.FormComponentDateTime {...properties} />;
2017-08-19 16:17:52 +09:00
case 'text':
default:
return <Components.FormComponentDefault {...properties}/>;
2017-08-16 16:24:50 +09:00
2016-04-04 16:50:07 +09:00
}
}
}
renderErrors() {
return (
2017-08-19 16:17:52 +09:00
<ul className='form-input-errors'>
{this.props.errors.map((error, index) => <li key={index}>{error.message}</li>)}
</ul>
)
}
render() {
const hasErrors = this.props.errors && this.props.errors.length;
const inputClass = classNames('form-input', `input-${this.props.name}`, {'input-error': hasErrors});
return (
<div className={inputClass}>
{this.props.beforeComponent ? this.props.beforeComponent : null}
{this.renderComponent()}
{hasErrors ? this.renderErrors() : null}
{this.props.limit ? <div className={classNames('form-control-limit', {danger: this.state.limit < 10})}>{this.state.limit}</div> : null}
{this.props.afterComponent ? this.props.afterComponent : null}
</div>
)
}
}
FormComponent.propTypes = {
2017-05-19 14:42:43 -06:00
document: PropTypes.object,
name: PropTypes.string,
label: PropTypes.string,
value: PropTypes.any,
placeholder: PropTypes.string,
prefilledValue: PropTypes.any,
options: PropTypes.any,
control: PropTypes.any,
datatype: PropTypes.any,
disabled: PropTypes.bool,
updateCurrentValues: PropTypes.func
}
FormComponent.contextTypes = {
intl: intlShape
};
export default FormComponent;