Skip to content

Commit

Permalink
Merge pull request #1653 from egauci/master
Browse files Browse the repository at this point in the history
[added] Allow overriding element in FormControls.Static
  • Loading branch information
jquense committed Feb 11, 2016
2 parents 820e2bb + 8312acf commit 1bd0be6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/FormControls/Static.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react';
import classNames from 'classnames';
import InputBase from '../InputBase';
import childrenValueValidation from '../utils/childrenValueInputValidation';
import elementType from 'react-prop-types/lib/elementType';

class Static extends InputBase {
getValue() {
Expand All @@ -10,17 +11,26 @@ class Static extends InputBase {
}

renderInput() {
const {componentClass: ComponentClass, ...props} = this.props;
return (
<p {...this.props} className={classNames(this.props.className, 'form-control-static')} ref="input" key="input">
<ComponentClass {...props} className={classNames(props.className, 'form-control-static')} ref="input" key="input">
{this.getValue()}
</p>
</ComponentClass>
);
}
}

Static.propTypes = {
value: childrenValueValidation,
/**
* You can override the default 'p' with a custom element
*/
componentClass: elementType,
children: childrenValueValidation
};

Static.defaultProps = {
componentClass: 'p'
};

export default Static;
7 changes: 7 additions & 0 deletions test/FormControlsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,12 @@ describe('Form Controls', () => {
<FormControls.Static><span>blah</span></FormControls.Static>
);
});
it('allows choosing non-default element tag', () => {
const instance = ReactTestUtils.renderIntoDocument(
<FormControls.Static componentClass="output" value="v" />
);
const result = ReactTestUtils.findRenderedDOMComponentWithTag(instance, 'output');
result.innerHTML.should.equal('v');
});
});
});

0 comments on commit 1bd0be6

Please sign in to comment.