Vulcan/packages/nova-forms/lib/component.js
Comus Leong 464e20a96c eslint & clean up code, also fixed some bugs (#1515)
* [eslint] update eslint rules & add .eslintignore to ignore non-ready nova packages

* [clean-up] nova-voting

* [clean-up] [bug] nova-users: missing user parameter

* [clean-up] nova-users

* [clean-up] nova-subscribe

* [clean-up] nova-settings

* [clean-up] nova-rss

* [clean-up] [bug] nova-posts: correct UsersRemoveDeletePosts

* [clean-up] nova-posts

* [clean-up] nova-notifications

* [clean-up] [bug] nova-newsletter: no error.message on throw error

* [clean-up] nova-newsletter

* [clean-up] nova-lib

* [clean-up] nova-kadira

* [clean-up] nova-inject-data

* [clean-up] nova-getting-started

* [clean-up] nova-forms

* [clean-up] nova-events

* [clean-up] [bug] nova-embedly: no FlowRouter

* [clean-up] nova-embedly

* [clean-up] nova-email-templates

* [clean-up] nova-email

* [clean-up] nova-debug

* [clean-up] nova-core

* [clean-up] [bug] nova-comments: correct UsersRemoveDeleteComments

* [clean-up] nova-comments

* [clean-up] [bug] nova-cloudinary: use Telescope.settings.collection instand

* [clean-up] nova-cloudinary

* [clean-up] nova-categories

* [clean-up] nova-base-components

* [clean-up] nova-api

* [eslint] extends react recommended

* [clean-up] for jsx files

* [eslint] extends meteor recommended

* i forgot this one little change
2016-11-25 13:46:55 -05:00

161 lines
4.7 KiB
JavaScript

/* eslint-disable react/display-name */
'use strict';
var React = require('react');
module.exports = {
propTypes: {
layout: React.PropTypes.string,
validatePristine: React.PropTypes.bool,
rowClassName: React.PropTypes.oneOfType([
React.PropTypes.string,
React.PropTypes.array,
React.PropTypes.object
]),
labelClassName: React.PropTypes.oneOfType([
React.PropTypes.string,
React.PropTypes.array,
React.PropTypes.object
]),
elementWrapperClassName: React.PropTypes.oneOfType([
React.PropTypes.string,
React.PropTypes.array,
React.PropTypes.object
])
},
contextTypes: {
layout: React.PropTypes.string,
validatePristine: React.PropTypes.bool,
rowClassName: React.PropTypes.oneOfType([
React.PropTypes.string,
React.PropTypes.array,
React.PropTypes.object
]),
labelClassName: React.PropTypes.oneOfType([
React.PropTypes.string,
React.PropTypes.array,
React.PropTypes.object
]),
elementWrapperClassName: React.PropTypes.oneOfType([
React.PropTypes.string,
React.PropTypes.array,
React.PropTypes.object
])
},
getDefaultProps: function() {
return {
disabled: false,
validatePristine: false,
onChange: function() {},
onFocus: function() {},
onBlur: function() {}
};
},
/**
* Accessors for "special" properties.
*
* The following methods are used to merge master default properties that
* are optionally set on the parent form. This to to allow customising these
* properties 'as a whole' for the form, while retaining the ability to
* override the properties on a component basis.
*
* Also see the parent-context mixin.
*/
getLayout: function() {
var defaultProperty = this.context.layout || 'horizontal';
return this.props.layout ? this.props.layout : defaultProperty;
},
getValidatePristine: function() {
var defaultProperty = this.context.validatePristine || false;
return this.props.validatePristine ? this.props.validatePristine : defaultProperty;
},
getRowClassName: function() {
return [this.context.rowClassName, this.props.rowClassName];
},
getLabelClassName: function() {
return [this.context.labelClassName, this.props.labelClassName];
},
getElementWrapperClassName: function() {
return [this.context.elementWrapperClassName, this.props.elementWrapperClassName];
},
getRowProperties: function() {
return {
label: this.props.label,
rowClassName: this.getRowClassName(),
labelClassName: this.getLabelClassName(),
elementWrapperClassName: this.getElementWrapperClassName(),
layout: this.getLayout(),
required: this.isRequired(),
hasErrors: this.showErrors()
};
},
hashString: function(string) {
var hash = 0;
for (var i = 0; i < string.length; i++) {
hash = (((hash << 5) - hash) + string.charCodeAt(i)) & 0xFFFFFFFF;
}
return hash;
},
/**
* getId
*
* The ID is used as an attribute on the form control, and is used to allow
* associating the label element with the form control.
*
* If we don't explicitly pass an `id` prop, we generate one based on the
* `name` and `label` properties.
*/
getId: function() {
if (this.props.id) {
return this.props.id;
}
var label = (typeof this.props.label === 'undefined' ? '' : this.props.label);
return [
'frc',
this.props.name.split('[').join('_').replace(']', ''),
this.hashString(JSON.stringify(label))
].join('-');
},
renderHelp: function() {
if (!this.props.help) {
return '';
}
return (
<span className="help-block">{this.props.help}</span>
);
},
renderErrorMessage: function() {
if (!this.showErrors()) {
return '';
}
var errorMessages = this.getErrorMessages() || [];
return errorMessages.map((message, key) => {
return (
<span key={key} className="help-block validation-message">{message}</span>
);
});
},
showErrors: function() {
if (this.isPristine() === true) {
if (this.getValidatePristine() === false) {
return false;
}
}
return (this.isValid() === false);
}
};