form-group.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. return (
  21. <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. )
  27. } else {
  28. return null
  29. }
  30. }
  31. getFeedbackDescription() {
  32. if (this.isValidated()) {
  33. return (
  34. <span id={this.props.for + "_status"} className="sr-only">
  35. {this.props.validation ? gettext("(error)") : gettext("(success)")}
  36. </span>
  37. )
  38. } else {
  39. return null
  40. }
  41. }
  42. getHelpText() {
  43. if (this.props.helpText) {
  44. return <p className="help-block">{this.props.helpText}</p>
  45. } else {
  46. return null
  47. }
  48. }
  49. render() {
  50. return (
  51. <div className={this.getClassName()}>
  52. <label
  53. className={"control-label " + (this.props.labelClass || "")}
  54. htmlFor={this.props.for || ""}
  55. >
  56. {this.props.label + ":"}
  57. </label>
  58. <div className={this.props.controlClass || ""}>
  59. {this.props.children}
  60. {this.getFeedbackDescription()}
  61. {this.getFeedback()}
  62. {this.getHelpText()}
  63. {this.props.extra || null}
  64. </div>
  65. </div>
  66. )
  67. }
  68. }