nameAndDescFields.tsx 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import {Fragment, useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import {Button} from 'sentry/components/button';
  4. import TextArea from 'sentry/components/forms/controls/textarea';
  5. import TextField from 'sentry/components/forms/fields/textField';
  6. import {t} from 'sentry/locale';
  7. import {space} from 'sentry/styles/space';
  8. import {SectionHeader} from 'sentry/views/dashboards/widgetBuilder/components/common/sectionHeader';
  9. import {useWidgetBuilderContext} from 'sentry/views/dashboards/widgetBuilder/contexts/widgetBuilderContext';
  10. import {BuilderStateAction} from 'sentry/views/dashboards/widgetBuilder/hooks/useWidgetBuilderState';
  11. interface WidgetBuilderNameAndDescriptionProps {
  12. error: Record<string, any>;
  13. setError: (error: Record<string, any>) => void;
  14. }
  15. function WidgetBuilderNameAndDescription({
  16. error,
  17. setError,
  18. }: WidgetBuilderNameAndDescriptionProps) {
  19. const {state, dispatch} = useWidgetBuilderContext();
  20. const [isDescSelected, setIsDescSelected] = useState(state.description ? true : false);
  21. return (
  22. <Fragment>
  23. <SectionHeader title={t('Widget Name & Description')} />
  24. <StyledInput
  25. name={t('Widget Name')}
  26. size="md"
  27. placeholder={t('Name')}
  28. title={t('Widget Name')}
  29. aria-label={t('Widget Name')}
  30. value={state.title}
  31. onChange={newTitle => {
  32. // clear error once user starts typing
  33. setError({...error, title: undefined});
  34. dispatch({type: BuilderStateAction.SET_TITLE, payload: newTitle});
  35. }}
  36. required
  37. error={error.title}
  38. inline={false}
  39. />
  40. {!isDescSelected && (
  41. <AddDescriptionButton
  42. priority="link"
  43. aria-label={t('Add Widget Description')}
  44. onClick={() => {
  45. setIsDescSelected(true);
  46. }}
  47. data-test-id={'add-description'}
  48. >
  49. {t('+ Add Widget Description')}
  50. </AddDescriptionButton>
  51. )}
  52. {isDescSelected && (
  53. <DescriptionTextArea
  54. placeholder={t('Description')}
  55. aria-label={t('Widget Description')}
  56. autosize
  57. rows={4}
  58. value={state.description}
  59. onChange={e => {
  60. dispatch({type: BuilderStateAction.SET_DESCRIPTION, payload: e.target.value});
  61. }}
  62. />
  63. )}
  64. </Fragment>
  65. );
  66. }
  67. export default WidgetBuilderNameAndDescription;
  68. const StyledInput = styled(TextField)`
  69. margin-bottom: ${space(1)};
  70. padding: 0;
  71. border: none;
  72. `;
  73. const AddDescriptionButton = styled(Button)`
  74. margin-bottom: ${space(1)};
  75. `;
  76. const DescriptionTextArea = styled(TextArea)`
  77. margin: ${space(2)} 0;
  78. `;