actionLink.tsx 2.0 KB

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