AddRemove.tsx 821 B

1234567891011121314151617181920212223242526272829
  1. import { css } from '@emotion/css';
  2. import React from 'react';
  3. import { IconButton } from './IconButton';
  4. interface Props {
  5. index: number;
  6. elements: any[];
  7. onAdd: () => void;
  8. onRemove: () => void;
  9. }
  10. /**
  11. * A component used to show add & remove buttons for mutable lists of values. Wether to show or not the add or the remove buttons
  12. * depends on the `index` and `elements` props. This enforces a consistent experience whenever this pattern is used.
  13. */
  14. export const AddRemove = ({ index, onAdd, onRemove, elements }: Props) => {
  15. return (
  16. <div
  17. className={css`
  18. display: flex;
  19. `}
  20. >
  21. {index === 0 && <IconButton iconName="plus" onClick={onAdd} label="add" />}
  22. {elements.length >= 2 && <IconButton iconName="minus" onClick={onRemove} label="remove" />}
  23. </div>
  24. );
  25. };