jsonForm.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. initiallyCollapsed,
  85. }: {
  86. fields: FieldObject[];
  87. formPanelProps: Pick<
  88. Props,
  89. | 'access'
  90. | 'disabled'
  91. | 'features'
  92. | 'additionalFieldProps'
  93. | 'renderFooter'
  94. | 'renderHeader'
  95. | 'initiallyCollapsed'
  96. > &
  97. Pick<State, 'highlighted'>;
  98. initiallyCollapsed?: boolean;
  99. title?: React.ReactNode;
  100. }) {
  101. const shouldDisplayForm = this.shouldDisplayForm(fields);
  102. if (
  103. !shouldDisplayForm &&
  104. !formPanelProps?.renderFooter &&
  105. !formPanelProps?.renderHeader
  106. ) {
  107. return null;
  108. }
  109. return (
  110. <FormPanel
  111. title={title}
  112. fields={fields}
  113. {...formPanelProps}
  114. initiallyCollapsed={initiallyCollapsed ?? formPanelProps.initiallyCollapsed}
  115. />
  116. );
  117. }
  118. render() {
  119. const {
  120. access,
  121. collapsible,
  122. initiallyCollapsed = false,
  123. fields,
  124. title,
  125. forms,
  126. disabled,
  127. features,
  128. additionalFieldProps,
  129. renderFooter,
  130. renderHeader,
  131. location: _location,
  132. params: _params,
  133. router: _router,
  134. routes: _routes,
  135. ...otherProps
  136. } = this.props;
  137. const formPanelProps = {
  138. access,
  139. disabled,
  140. features,
  141. additionalFieldProps,
  142. renderFooter,
  143. renderHeader,
  144. highlighted: this.state.highlighted,
  145. collapsible,
  146. initiallyCollapsed,
  147. };
  148. return (
  149. <div {...otherProps}>
  150. {typeof forms !== 'undefined' &&
  151. forms.map((formGroup, i) => (
  152. <Fragment key={i}>{this.renderForm({formPanelProps, ...formGroup})}</Fragment>
  153. ))}
  154. {typeof forms === 'undefined' &&
  155. typeof fields !== 'undefined' &&
  156. this.renderForm({fields, formPanelProps, title})}
  157. </div>
  158. );
  159. }
  160. }
  161. export default withSentryRouter(JsonForm);