form-group.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import React from 'react';
  2. export default class extends React.Component {
  3. isValidated() {
  4. return typeof this.props.validation !== "undefined";
  5. }
  6. getClassName() {
  7. let className = 'form-group';
  8. if (this.isValidated()) {
  9. className += ' has-feedback';
  10. if (this.props.validation === null) {
  11. className += ' has-success';
  12. } else {
  13. className += ' has-error';
  14. }
  15. }
  16. return className;
  17. }
  18. getFeedback() {
  19. if (this.props.validation) {
  20. /* jshint ignore:start */
  21. return <div className="help-block errors">
  22. {this.props.validation.map((error, i) => {
  23. return <p key={this.props.for + 'FeedbackItem' + i}>{error}</p>;
  24. })}
  25. </div>;
  26. /* jshint ignore:end */
  27. } else {
  28. return null;
  29. }
  30. }
  31. getFeedbackIcon() {
  32. if (this.isValidated()) {
  33. /* jshint ignore:start */
  34. return <span className="material-icon form-control-feedback"
  35. aria-hidden="true" key={this.props.for + 'FeedbackIcon'}>
  36. {this.props.validation ? 'clear' : 'check'}
  37. </span>;
  38. /* jshint ignore:end */
  39. } else {
  40. return null;
  41. }
  42. }
  43. getFeedbackDescription() {
  44. if (this.isValidated()) {
  45. /* jshint ignore:start */
  46. return <span id={this.props.for + '_status'} className="sr-only">
  47. {this.props.validation ? gettext('(error)') : gettext('(success)')}
  48. </span>;
  49. /* jshint ignore:end */
  50. } else {
  51. return null;
  52. }
  53. }
  54. getHelpText() {
  55. if (this.props.helpText) {
  56. /* jshint ignore:start */
  57. return <p className="help-block">{this.props.helpText}</p>;
  58. /* jshint ignore:end */
  59. } else {
  60. return null;
  61. }
  62. }
  63. render() {
  64. /* jshint ignore:start */
  65. return <div className={this.getClassName()}>
  66. <label className={'control-label ' + (this.props.labelClass || '')}
  67. htmlFor={this.props.for || ''}>
  68. {this.props.label + ':'}
  69. </label>
  70. <div className={this.props.controlClass || ''}>
  71. {this.props.children}
  72. {this.getFeedbackIcon()}
  73. {this.getFeedbackDescription()}
  74. {this.getFeedback()}
  75. {this.getHelpText()}
  76. {this.props.extra || null}
  77. </div>
  78. </div>
  79. /* jshint ignore:end */
  80. }
  81. }