columnFields.tsx 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import styled from '@emotion/styled';
  2. import FieldGroup from 'sentry/components/forms/fieldGroup';
  3. import {space} from 'sentry/styles/space';
  4. import type {Organization} from 'sentry/types/organization';
  5. import type {QueryFieldValue} from 'sentry/utils/discover/fields';
  6. import type {WidgetType} from 'sentry/views/dashboards/types';
  7. import {DisplayType} from 'sentry/views/dashboards/types';
  8. import ColumnEditCollection from 'sentry/views/discover/table/columnEditCollection';
  9. import type {FieldValueOption} from 'sentry/views/discover/table/queryField';
  10. import type {generateFieldOptions} from 'sentry/views/discover/utils';
  11. interface Props {
  12. displayType: DisplayType;
  13. fieldOptions: ReturnType<typeof generateFieldOptions>;
  14. fields: QueryFieldValue[];
  15. isOnDemandWidget: boolean;
  16. onChange: (newColumns: QueryFieldValue[]) => void;
  17. organization: Organization;
  18. widgetType: WidgetType;
  19. errors?: Record<string, string>[];
  20. filterAggregateParameters?: (option: FieldValueOption) => boolean;
  21. filterPrimaryOptions?: (option: FieldValueOption) => boolean;
  22. noFieldsMessage?: string;
  23. }
  24. export function ColumnFields({
  25. displayType,
  26. fieldOptions,
  27. widgetType,
  28. fields,
  29. organization,
  30. errors,
  31. onChange,
  32. filterAggregateParameters,
  33. filterPrimaryOptions,
  34. noFieldsMessage,
  35. isOnDemandWidget,
  36. }: Props) {
  37. return (
  38. <FieldGroup
  39. inline={false}
  40. error={errors?.find(error => error?.fields)?.fields}
  41. flexibleControlStateSize
  42. stacked
  43. >
  44. {displayType === DisplayType.TABLE ? (
  45. <ColumnCollectionEdit
  46. columns={fields}
  47. onChange={onChange}
  48. fieldOptions={fieldOptions}
  49. organization={organization}
  50. source={widgetType}
  51. showAliasField
  52. filterAggregateParameters={filterAggregateParameters}
  53. filterPrimaryOptions={filterPrimaryOptions}
  54. noFieldsMessage={noFieldsMessage}
  55. isOnDemandWidget={isOnDemandWidget}
  56. />
  57. ) : (
  58. // The only other display type this component
  59. // renders for is TOP_N, where the n - 1 fields
  60. // are columns and the nth field is the y-axis
  61. <ColumnCollectionEdit
  62. columns={fields.slice(0, fields.length - 1)}
  63. onChange={newColumns => {
  64. onChange([...newColumns, fields[fields.length - 1]]);
  65. }}
  66. fieldOptions={fieldOptions}
  67. organization={organization}
  68. source={widgetType}
  69. filterPrimaryOptions={filterPrimaryOptions}
  70. noFieldsMessage={noFieldsMessage}
  71. isOnDemandWidget={isOnDemandWidget}
  72. />
  73. )}
  74. </FieldGroup>
  75. );
  76. }
  77. const ColumnCollectionEdit = styled(ColumnEditCollection)`
  78. margin-top: ${space(1)};
  79. `;