arrayValue.tsx 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import {Component} from 'react';
  2. import styled from '@emotion/styled';
  3. import {t} from 'app/locale';
  4. import overflowEllipsis from 'app/styles/overflowEllipsis';
  5. import space from 'app/styles/space';
  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) => <ArrayItem key={`${i}:${item}`}>{item}</ArrayItem>)}
  30. <ArrayItem>{value.slice(-1)[0]}</ArrayItem>
  31. {value.length > 1 ? (
  32. <ButtonContainer>
  33. <button onClick={this.handleToggle}>
  34. {expanded ? t('[collapse]') : t('[+%s more]', value.length - 1)}
  35. </button>
  36. </ButtonContainer>
  37. ) : null}
  38. </ArrayContainer>
  39. );
  40. }
  41. }
  42. const ArrayContainer = styled('div')<{expanded: boolean}>`
  43. display: flex;
  44. flex-direction: ${p => (p.expanded ? 'column' : 'row')};
  45. & button {
  46. background: none;
  47. border: 0;
  48. outline: none;
  49. padding: 0;
  50. cursor: pointer;
  51. color: ${p => p.theme.blue300};
  52. margin-left: ${space(0.5)};
  53. }
  54. `;
  55. const ArrayItem = styled('span')`
  56. flex-shrink: 1;
  57. display: block;
  58. ${overflowEllipsis};
  59. width: unset;
  60. `;
  61. const ButtonContainer = styled('div')`
  62. white-space: nowrap;
  63. `;
  64. export default ArrayValue;