ActionCanButton.tsx 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import React from 'react';
  2. import { Button, Props as ButtonProps } from 'components/common/Button/Button';
  3. import * as S from 'components/common/ActionComponent/ActionComponent.styled';
  4. import {
  5. ActionComponentProps,
  6. getDefaultActionMessage,
  7. } from 'components/common/ActionComponent/ActionComponent';
  8. import { useActionTooltip } from 'lib/hooks/useActionTooltip';
  9. interface Props extends Omit<ActionComponentProps, 'permission'>, ButtonProps {
  10. canDoAction: boolean;
  11. }
  12. const ActionButton: React.FC<Props> = ({
  13. placement = 'bottom-end',
  14. message = getDefaultActionMessage(),
  15. disabled,
  16. canDoAction,
  17. ...props
  18. }) => {
  19. const isDisabled = !canDoAction;
  20. const { x, y, reference, floating, strategy, open } = useActionTooltip(
  21. isDisabled,
  22. placement
  23. );
  24. return (
  25. <S.Wrapper ref={reference}>
  26. <Button {...props} disabled={disabled || isDisabled} />
  27. {open && (
  28. <S.MessageTooltipLimited
  29. ref={floating}
  30. style={{
  31. position: strategy,
  32. top: y ?? 0,
  33. left: x ?? 0,
  34. width: 'max-content',
  35. }}
  36. >
  37. {message}
  38. </S.MessageTooltipLimited>
  39. )}
  40. </S.Wrapper>
  41. );
  42. };
  43. export default ActionButton;