fieldFromConfig.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import {FieldGroupProps} from 'sentry/components/forms/field/types';
  2. import SeparatorField from 'sentry/components/forms/fields/separatorField';
  3. import {Field} from 'sentry/components/forms/types';
  4. import {Scope} from 'sentry/types';
  5. import BlankField from './fields/blankField';
  6. import BooleanField, {BooleanFieldProps} from './fields/booleanField';
  7. import ChoiceMapperField, {ChoiceMapperFieldProps} from './fields/choiceMapperField';
  8. import DateTimeField, {DateTimeFieldProps} from './fields/dateTimeField';
  9. import EmailField, {EmailFieldProps} from './fields/emailField';
  10. import FileField, {FileFieldProps} from './fields/fileField';
  11. import HiddenField, {HiddenFieldProps} from './fields/hiddenField';
  12. import NumberField, {NumberFieldProps} from './fields/numberField';
  13. import ProjectMapperField, {ProjectMapperProps} from './fields/projectMapperField';
  14. import RadioField, {RadioFieldProps} from './fields/radioField';
  15. import RangeField, {RangeFieldProps} from './fields/rangeField';
  16. import SecretField, {SecretFieldProps} from './fields/secretField';
  17. import SelectAsyncField, {SelectAsyncFieldProps} from './fields/selectAsyncField';
  18. import SelectField, {SelectFieldProps} from './fields/selectField';
  19. import SentryProjectSelectorField, {
  20. RenderFieldProps,
  21. } from './fields/sentryProjectSelectorField';
  22. import TableField, {TableFieldProps} from './fields/tableField';
  23. import TextareaField, {TextareaFieldProps} from './fields/textareaField';
  24. import TextField, {TextFieldProps} from './fields/textField';
  25. interface FieldFromConfigProps {
  26. field: Field;
  27. access?: Set<Scope>;
  28. disabled?: boolean | ((props) => boolean);
  29. flexibleControlStateSize?: boolean;
  30. getData?: (data) => any;
  31. highlighted?: boolean;
  32. inline?: boolean;
  33. noOptionsMessage?: () => string;
  34. onBlur?: (value, event) => void;
  35. stacked?: boolean;
  36. }
  37. function FieldFromConfig(props: FieldFromConfigProps): React.ReactElement | null {
  38. const {field, ...otherProps} = props;
  39. const componentProps = {
  40. ...otherProps,
  41. ...field,
  42. };
  43. switch (field.type) {
  44. case 'separator':
  45. return <SeparatorField />;
  46. case 'secret':
  47. return <SecretField {...(componentProps as SecretFieldProps)} />;
  48. case 'range':
  49. return <RangeField {...(componentProps as RangeFieldProps)} />;
  50. case 'blank':
  51. return <BlankField {...(componentProps as FieldGroupProps)} />;
  52. case 'bool':
  53. case 'boolean':
  54. return <BooleanField {...(componentProps as BooleanFieldProps)} />;
  55. case 'email':
  56. return <EmailField {...(componentProps as EmailFieldProps)} />;
  57. case 'hidden':
  58. return <HiddenField {...(componentProps as HiddenFieldProps)} />;
  59. case 'string':
  60. case 'text':
  61. case 'url':
  62. if (componentProps.multiline) {
  63. return <TextareaField {...(componentProps as TextareaFieldProps)} />;
  64. }
  65. return <TextField {...(componentProps as TextFieldProps)} />;
  66. case 'number':
  67. return <NumberField {...(componentProps as NumberFieldProps)} />;
  68. case 'textarea':
  69. return <TextareaField {...(componentProps as TextareaFieldProps)} />;
  70. case 'choice':
  71. case 'select':
  72. case 'array':
  73. return <SelectField {...(componentProps as SelectFieldProps<any>)} />;
  74. case 'choice_mapper':
  75. return <ChoiceMapperField {...(componentProps as ChoiceMapperFieldProps)} />;
  76. case 'radio':
  77. if (Array.isArray(componentProps.choices)) {
  78. return <RadioField {...(componentProps as RadioFieldProps)} />;
  79. }
  80. throw new Error('Invalid `choices` type. Use an array of options');
  81. case 'table':
  82. return <TableField {...(componentProps as TableFieldProps)} />;
  83. case 'project_mapper':
  84. return <ProjectMapperField {...(componentProps as ProjectMapperProps)} />;
  85. case 'sentry_project_selector':
  86. return <SentryProjectSelectorField {...(componentProps as RenderFieldProps)} />;
  87. case 'select_async':
  88. return <SelectAsyncField {...(componentProps as SelectAsyncFieldProps)} />;
  89. case 'file':
  90. return <FileField {...(componentProps as FileFieldProps)} />;
  91. case 'datetime':
  92. return <DateTimeField {...(componentProps as DateTimeFieldProps)} />;
  93. case 'custom':
  94. return field.Component(field);
  95. default:
  96. return null;
  97. }
  98. }
  99. export default FieldFromConfig;