arrayValue.tsx 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import {Component} from 'react';
  2. import styled from '@emotion/styled';
  3. import {t} from 'sentry/locale';
  4. import space from 'sentry/styles/space';
  5. import {nullableValue} from './fieldRenderers';
  6. type Props = {
  7. value: string[];
  8. };
  9. type State = {
  10. expanded: boolean;
  11. };
  12. class ArrayValue extends Component<Props, State> {
  13. state: State = {
  14. expanded: false,
  15. };
  16. handleToggle = () => {
  17. this.setState(prevState => ({
  18. expanded: !prevState.expanded,
  19. }));
  20. };
  21. render() {
  22. const {expanded} = this.state;
  23. const {value} = this.props;
  24. return (
  25. <ArrayContainer expanded={expanded}>
  26. {expanded &&
  27. value
  28. .slice(0, value.length - 1)
  29. .map((item, i) => (
  30. <ArrayItem key={`${i}:${item}`}>{nullableValue(item)}</ArrayItem>
  31. ))}
  32. <ArrayItem>{nullableValue(value.slice(-1)[0])}</ArrayItem>
  33. {value.length > 1 ? (
  34. <ButtonContainer>
  35. <button onClick={this.handleToggle}>
  36. {expanded ? t('[collapse]') : t('[+%s more]', value.length - 1)}
  37. </button>
  38. </ButtonContainer>
  39. ) : null}
  40. </ArrayContainer>
  41. );
  42. }
  43. }
  44. const ArrayContainer = styled('div')<{expanded: boolean}>`
  45. display: flex;
  46. flex-direction: ${p => (p.expanded ? 'column' : 'row')};
  47. & button {
  48. background: none;
  49. border: 0;
  50. outline: none;
  51. padding: 0;
  52. cursor: pointer;
  53. color: ${p => p.theme.blue300};
  54. margin-left: ${space(0.5)};
  55. }
  56. `;
  57. const ArrayItem = styled('span')`
  58. flex-shrink: 1;
  59. display: block;
  60. ${p => p.theme.overflowEllipsis};
  61. width: unset;
  62. `;
  63. const ButtonContainer = styled('div')`
  64. white-space: nowrap;
  65. `;
  66. export default ArrayValue;