groupCheckBox.tsx 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import {Component} from 'react';
  2. import Checkbox from 'sentry/components/checkbox';
  3. import {t} from 'sentry/locale';
  4. import SelectedGroupStore from 'sentry/stores/selectedGroupStore';
  5. type Props = {
  6. disabled: boolean;
  7. id: string;
  8. };
  9. type State = {
  10. isSelected: boolean;
  11. };
  12. class GroupCheckBox extends Component<Props, State> {
  13. state: State = {
  14. isSelected: SelectedGroupStore.isSelected(this.props.id),
  15. };
  16. componentWillReceiveProps(nextProps: Props) {
  17. if (nextProps.id !== this.props.id) {
  18. this.setState({
  19. isSelected: SelectedGroupStore.isSelected(nextProps.id),
  20. });
  21. }
  22. }
  23. shouldComponentUpdate(_nextProps, nextState) {
  24. return nextState.isSelected !== this.state.isSelected;
  25. }
  26. componentWillUnmount() {
  27. this.unsubscribe();
  28. }
  29. unsubscribe = SelectedGroupStore.listen(() => {
  30. this.onSelectedGroupChange();
  31. }, undefined);
  32. onSelectedGroupChange() {
  33. const isSelected = SelectedGroupStore.isSelected(this.props.id);
  34. if (isSelected !== this.state.isSelected) {
  35. this.setState({
  36. isSelected,
  37. });
  38. }
  39. }
  40. handleSelect = () => {
  41. const id = this.props.id;
  42. SelectedGroupStore.toggleSelect(id);
  43. };
  44. render() {
  45. const {disabled, id} = this.props;
  46. const {isSelected} = this.state;
  47. return (
  48. <Checkbox
  49. aria-label={t('Select Issue')}
  50. value={id}
  51. checked={isSelected}
  52. onChange={this.handleSelect}
  53. disabled={disabled}
  54. />
  55. );
  56. }
  57. }
  58. export default GroupCheckBox;