actionLink.tsx 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import * as React from 'react';
  2. import styled from '@emotion/styled';
  3. import classNames from 'classnames';
  4. import ActionButton from './button';
  5. import ConfirmableAction from './confirmableAction';
  6. const StyledAction = styled('a')<{disabled?: boolean}>`
  7. display: flex;
  8. align-items: center;
  9. ${p => p.disabled && 'cursor: not-allowed;'}
  10. `;
  11. const StyledActionButton = styled(ActionButton)`
  12. display: flex;
  13. align-items: center;
  14. ${p => p.disabled && 'cursor: not-allowed;'}
  15. `;
  16. type ConfirmableActionProps = React.ComponentProps<typeof ConfirmableAction>;
  17. type CommonProps = Omit<
  18. ConfirmableActionProps,
  19. 'onConfirm' | 'confirmText' | 'children' | 'stopPropagation' | 'priority'
  20. > & {
  21. title: string;
  22. onAction?: () => void;
  23. children?: React.ReactNode;
  24. disabled?: boolean;
  25. className?: string;
  26. shouldConfirm?: boolean;
  27. confirmPriority?: ConfirmableActionProps['priority'];
  28. confirmLabel?: string;
  29. };
  30. type Props = CommonProps &
  31. ({type?: 'button'} & Partial<
  32. Omit<React.ComponentProps<typeof StyledActionButton>, 'as'>
  33. >);
  34. export default function ActionLink({
  35. message,
  36. className,
  37. title,
  38. onAction,
  39. type,
  40. confirmLabel,
  41. disabled,
  42. children,
  43. shouldConfirm,
  44. confirmPriority,
  45. header,
  46. ...props
  47. }: Props) {
  48. const actionCommonProps = {
  49. ['aria-label']: title,
  50. className: classNames(className, {disabled}),
  51. onClick: disabled ? undefined : onAction,
  52. disabled,
  53. children,
  54. ...props,
  55. };
  56. const action =
  57. type === 'button' ? (
  58. <StyledActionButton {...actionCommonProps} />
  59. ) : (
  60. <StyledAction {...actionCommonProps} />
  61. );
  62. if (shouldConfirm && onAction) {
  63. return (
  64. <ConfirmableAction
  65. shouldConfirm={shouldConfirm}
  66. priority={confirmPriority}
  67. disabled={disabled}
  68. message={message}
  69. header={header}
  70. confirmText={confirmLabel}
  71. onConfirm={onAction}
  72. stopPropagation={disabled}
  73. >
  74. {action}
  75. </ConfirmableAction>
  76. );
  77. }
  78. return action;
  79. }