FunctionParamEditor.tsx 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import { css } from '@emotion/css';
  2. import React from 'react';
  3. import { GrafanaTheme2, SelectableValue } from '@grafana/data';
  4. import { Segment, SegmentInput, useStyles2 } from '@grafana/ui';
  5. export type EditableParam = {
  6. name: string;
  7. value: string;
  8. optional: boolean;
  9. multiple: boolean;
  10. options: Array<SelectableValue<string>>;
  11. };
  12. type FieldEditorProps = {
  13. editableParam: EditableParam;
  14. onChange: (value: string) => void;
  15. onExpandedChange: (expanded: boolean) => void;
  16. autofocus: boolean;
  17. };
  18. /**
  19. * Render a function parameter with a segment dropdown for multiple options or simple input.
  20. */
  21. export function FunctionParamEditor({ editableParam, onChange, onExpandedChange, autofocus }: FieldEditorProps) {
  22. const styles = useStyles2(getStyles);
  23. if (editableParam.options?.length > 0) {
  24. return (
  25. <Segment
  26. autofocus={autofocus}
  27. value={editableParam.value}
  28. inputPlaceholder={editableParam.name}
  29. className={styles.segment}
  30. options={editableParam.options}
  31. placeholder={' +' + editableParam.name}
  32. onChange={(value) => {
  33. onChange(value.value || '');
  34. }}
  35. onExpandedChange={onExpandedChange}
  36. inputMinWidth={150}
  37. allowCustomValue={true}
  38. allowEmptyValue={true}
  39. ></Segment>
  40. );
  41. } else {
  42. return (
  43. <SegmentInput
  44. autofocus={autofocus}
  45. className={styles.input}
  46. value={editableParam.value || ''}
  47. placeholder={' +' + editableParam.name}
  48. inputPlaceholder={editableParam.name}
  49. onChange={(value) => {
  50. onChange(value.toString());
  51. }}
  52. onExpandedChange={onExpandedChange}
  53. // input style
  54. style={{ height: '25px', paddingTop: '2px', marginTop: '2px', paddingLeft: '4px', minWidth: '100px' }}
  55. ></SegmentInput>
  56. );
  57. }
  58. }
  59. const getStyles = (theme: GrafanaTheme2) => ({
  60. segment: css({
  61. margin: 0,
  62. padding: 0,
  63. }),
  64. input: css`
  65. margin: 0;
  66. padding: 0;
  67. input {
  68. height: 25px;
  69. },
  70. `,
  71. });