actionLink.tsx 2.2 KB

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