booleanField.tsx 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import InputField from 'sentry/components/deprecatedforms/inputField';
  2. import Tooltip from 'sentry/components/tooltip';
  3. import {IconQuestion} from 'sentry/icons';
  4. import {defined} from 'sentry/utils';
  5. type Props = InputField['props'];
  6. type State = InputField['state'] & {
  7. value: boolean;
  8. };
  9. export default class BooleanField extends InputField<Props, State> {
  10. coerceValue(initialValue: string | number) {
  11. const value = super.coerceValue(initialValue);
  12. return value ? true : false;
  13. }
  14. onChange = (e: React.ChangeEvent<HTMLInputElement>) => {
  15. const value = e.target.checked;
  16. this.setValue(value);
  17. };
  18. getField() {
  19. return (
  20. <input
  21. id={this.getId()}
  22. type={this.getType()}
  23. checked={this.state.value}
  24. onChange={this.onChange.bind(this)}
  25. disabled={this.props.disabled}
  26. />
  27. );
  28. }
  29. render() {
  30. const {error} = this.state;
  31. let className = this.getClassName();
  32. if (error) {
  33. className += ' has-error';
  34. }
  35. return (
  36. <div className={className}>
  37. <div className="controls">
  38. <label className="control-label">
  39. {this.getField()}
  40. {this.props.label}
  41. {this.props.disabled && this.props.disabledReason && (
  42. <Tooltip title={this.props.disabledReason}>
  43. <IconQuestion size="xs" />
  44. </Tooltip>
  45. )}
  46. </label>
  47. {defined(this.props.help) && <p className="help-block">{this.props.help}</p>}
  48. {error && <p className="error">{error}</p>}
  49. </div>
  50. </div>
  51. );
  52. }
  53. getClassName() {
  54. return 'control-group checkbox';
  55. }
  56. getType() {
  57. return 'checkbox';
  58. }
  59. }