jsonForm.tsx 4.3 KB

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