radioBooleanField.tsx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import {Fragment} from 'react';
  2. import InputField from 'sentry/components/deprecatedforms/inputField';
  3. import {defined} from 'sentry/utils';
  4. type Props = {
  5. noLabel: string;
  6. yesLabel: string;
  7. yesFirst?: boolean;
  8. } & InputField['props'];
  9. export default class RadioBooleanField extends InputField<Props> {
  10. static defaultProps = {
  11. ...InputField.defaultProps,
  12. yesLabel: 'Yes',
  13. noLabel: 'No',
  14. yesFirst: true,
  15. };
  16. coerceValue(props) {
  17. const value = super.coerceValue(props);
  18. return value ? true : false;
  19. }
  20. onChange = e => {
  21. const value = e.target.value === 'true';
  22. this.setValue(value);
  23. };
  24. getType() {
  25. return 'radio';
  26. }
  27. getField() {
  28. const yesOption = (
  29. <div className="radio" key="yes">
  30. <label style={{fontWeight: 'normal'}}>
  31. <input
  32. type="radio"
  33. value="true"
  34. name={this.props.name}
  35. checked={this.state.value === true}
  36. onChange={this.onChange.bind(this)}
  37. disabled={this.props.disabled}
  38. />{' '}
  39. {this.props.yesLabel}
  40. </label>
  41. </div>
  42. );
  43. const noOption = (
  44. <div className="radio" key="no">
  45. <label style={{fontWeight: 'normal'}}>
  46. <input
  47. type="radio"
  48. name={this.props.name}
  49. value="false"
  50. checked={this.state.value === false}
  51. onChange={this.onChange.bind(this)}
  52. disabled={this.props.disabled}
  53. />{' '}
  54. {this.props.noLabel}
  55. </label>
  56. </div>
  57. );
  58. return (
  59. <div className="control-group radio-boolean">
  60. {this.props.yesFirst ? (
  61. <Fragment>
  62. {yesOption}
  63. {noOption}
  64. </Fragment>
  65. ) : (
  66. <Fragment>
  67. {noOption}
  68. {yesOption}
  69. </Fragment>
  70. )}
  71. </div>
  72. );
  73. }
  74. render() {
  75. const {label, hideErrorMessage, help, style} = this.props;
  76. const {error} = this.state;
  77. const cx = this.getFinalClassNames();
  78. const shouldShowErrorMessage = error && !hideErrorMessage;
  79. return (
  80. <div style={style} className={cx}>
  81. <div className="controls">
  82. {label && (
  83. <label htmlFor={this.getId()} className="control-label">
  84. {label}
  85. </label>
  86. )}
  87. {defined(help) && <p className="help-block">{help}</p>}
  88. {this.getField()}
  89. {this.renderDisabledReason()}
  90. {shouldShowErrorMessage && <p className="error">{error}</p>}
  91. </div>
  92. </div>
  93. );
  94. }
  95. }