jsonForm.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import {Component, Fragment} from 'react';
  2. // eslint-disable-next-line no-restricted-imports
  3. import {withRouter, WithRouterProps} from 'react-router';
  4. import * as Sentry from '@sentry/react';
  5. import scrollToElement from 'scroll-to-element';
  6. import {defined} from 'sentry/utils';
  7. import {sanitizeQuerySelector} from 'sentry/utils/sanitizeQuerySelector';
  8. import FormPanel from './formPanel';
  9. import {Field, FieldObject, JsonFormObject} from './types';
  10. type Props = {
  11. additionalFieldProps?: {[key: string]: any};
  12. /**
  13. * If `forms` is not defined, `title` + `fields` must be required.
  14. * Allows more fine grain control of title/fields
  15. */
  16. fields?: FieldObject[];
  17. /**
  18. * Fields that are grouped by "section"
  19. */
  20. forms?: JsonFormObject[];
  21. } & WithRouterProps &
  22. Omit<
  23. React.ComponentProps<typeof FormPanel>,
  24. 'highlighted' | 'fields' | 'additionalFieldProps'
  25. >;
  26. type State = {
  27. // Field name that should be highlighted
  28. highlighted?: string;
  29. };
  30. class JsonForm extends Component<Props, State> {
  31. state: State = {
  32. // location.hash is optional because of tests.
  33. highlighted: this.props.location?.hash,
  34. };
  35. componentDidMount() {
  36. this.scrollToHash();
  37. }
  38. componentDidUpdate(prevProps: Props) {
  39. if (this.props.location && this.props.location.hash !== prevProps.location.hash) {
  40. const hash = this.props.location.hash;
  41. this.scrollToHash(hash);
  42. this.setState({highlighted: hash});
  43. }
  44. }
  45. scrollToHash(toHash?: string): void {
  46. // location.hash is optional because of tests.
  47. const hash = toHash || this.props.location?.hash;
  48. if (!hash) {
  49. return;
  50. }
  51. // Push onto callback queue so it runs after the DOM is updated,
  52. // this is required when navigating from a different page so that
  53. // the element is rendered on the page before trying to getElementById.
  54. try {
  55. scrollToElement(sanitizeQuerySelector(decodeURIComponent(hash)), {
  56. align: 'middle',
  57. offset: -100,
  58. });
  59. } catch (err) {
  60. Sentry.captureException(err);
  61. }
  62. }
  63. shouldDisplayForm(fields: FieldObject[]) {
  64. const fieldsWithVisibleProp = fields.filter(
  65. field => typeof field !== 'function' && defined(field?.visible)
  66. ) as Array<Omit<Field, 'visible'> & Required<Pick<Field, 'visible'>>>;
  67. if (fields.length === fieldsWithVisibleProp.length) {
  68. const {additionalFieldProps, ...props} = this.props;
  69. const areAllFieldsHidden = fieldsWithVisibleProp.every(field => {
  70. if (typeof field.visible === 'function') {
  71. return !field.visible({...props, ...additionalFieldProps});
  72. }
  73. return !field.visible;
  74. });
  75. return !areAllFieldsHidden;
  76. }
  77. return true;
  78. }
  79. renderForm({
  80. fields,
  81. formPanelProps,
  82. title,
  83. }: {
  84. fields: FieldObject[];
  85. formPanelProps: Pick<
  86. Props,
  87. | 'access'
  88. | 'disabled'
  89. | 'features'
  90. | 'additionalFieldProps'
  91. | 'renderFooter'
  92. | 'renderHeader'
  93. > &
  94. Pick<State, 'highlighted'>;
  95. title?: React.ReactNode;
  96. }) {
  97. const shouldDisplayForm = this.shouldDisplayForm(fields);
  98. if (
  99. !shouldDisplayForm &&
  100. !formPanelProps?.renderFooter &&
  101. !formPanelProps?.renderHeader
  102. ) {
  103. return null;
  104. }
  105. return <FormPanel title={title} fields={fields} {...formPanelProps} />;
  106. }
  107. render() {
  108. const {
  109. access,
  110. collapsible,
  111. fields,
  112. title,
  113. forms,
  114. disabled,
  115. features,
  116. additionalFieldProps,
  117. renderFooter,
  118. renderHeader,
  119. location: _location,
  120. ...otherProps
  121. } = this.props;
  122. const formPanelProps = {
  123. access,
  124. disabled,
  125. features,
  126. additionalFieldProps,
  127. renderFooter,
  128. renderHeader,
  129. highlighted: this.state.highlighted,
  130. collapsible,
  131. };
  132. return (
  133. <div {...otherProps}>
  134. {typeof forms !== 'undefined' &&
  135. forms.map((formGroup, i) => (
  136. <Fragment key={i}>{this.renderForm({formPanelProps, ...formGroup})}</Fragment>
  137. ))}
  138. {typeof forms === 'undefined' &&
  139. typeof fields !== 'undefined' &&
  140. this.renderForm({fields, formPanelProps, title})}
  141. </div>
  142. );
  143. }
  144. }
  145. export default withRouter(JsonForm);