value.match(regex) ? null : setting.regex.error
+ );
+ return _.compact(errors).length ? errors : null;
+ }
+ },
+ propTypes: {
+ value: React.PropTypes.arrayOf(React.PropTypes.node).isRequired,
+ type: React.PropTypes.oneOf(['text_list', 'textarea_list']).isRequired,
+ name: React.PropTypes.node,
+ label: React.PropTypes.node,
+ description: React.PropTypes.node,
+ error: React.PropTypes.arrayOf(React.PropTypes.node),
+ disabled: React.PropTypes.bool,
+ wrapperClassName: React.PropTypes.node,
+ onChange: React.PropTypes.func,
+ min: React.PropTypes.number,
+ max: React.PropTypes.number,
+ tooltipPlacement: React.PropTypes.oneOf(['left', 'right', 'top', 'bottom']),
+ tooltipIcon: React.PropTypes.node,
+ tooltipText: React.PropTypes.node
+ },
+ getInitialState() {
+ return {};
+ },
+ getDefaultProps() {
+ return {
+ min: 1,
+ max: null,
+ tooltipIcon: 'glyphicon-warning-sign',
+ tooltipPlacement: 'right'
+ };
+ },
+ changeField(index, method = 'change') {
+ var value = _.clone(this.props.value);
+ switch (method) {
+ case 'add':
+ value.splice(index + 1, 0, '');
+ this.setState({key: _.now()});
+ break;
+ case 'remove':
+ value.splice(index, 1);
+ this.setState({key: _.now()});
+ break;
+ case 'change':
+ var input = ReactDOM.findDOMNode(this.refs['input' + index]);
+ value[index] = input.value;
+ break;
+ }
+ if (this.props.onChange) return this.props.onChange(this.props.name, value);
+ },
+ debouncedFieldChange: _.debounce(function(index) {
+ return this.changeField(index);
+ }, 200, {leading: true}),
+ renderMultipleInputControls(index) {
+ return (
+
+ {(!this.props.max || this.props.value.length < this.props.max) &&
+
+ }
+ {this.props.value.length > this.props.min &&
+
+ }
+
+ );
+ },
+ renderInput(value, index) {
+ var error = (this.props.error || [])[index] || null;
+ var Tag = this.props.type === 'textarea_list' ? 'textarea' : 'input';
+ return (
+
+
this.debouncedFieldChange(index)}
+ defaultValue={value}
+ />
+ {this.renderMultipleInputControls(index)}
+ {error &&
+ {error}
+ }
+
+ );
+ },
+ renderLabel() {
+ if (!this.props.label) return null;
+ return (
+
+ );
+ },
+ renderDescription() {
+ if (this.props.error) return null;
+ return (
+
+ {this.props.description}
+
+ );
+ },
+ renderWrapper(children) {
+ return (
+
+ {children}
+
+ );
+ },
+ render() {
+ return this.renderWrapper([
+ this.renderLabel(),
+
+ {_.map(this.props.value, this.renderInput)}
+
,
+ this.renderDescription()
+ ]);
+ }
+});
+
export default customControls;