booleanField.tsx 1.7 KB

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