Button.tsx 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright 2017-2020 @polkadot/react-components authors & contributors
  2. // This software may be modified and distributed under the terms
  3. // of the Apache-2.0 license. See the LICENSE file for details.
  4. import { ButtonProps } from './types';
  5. import React, { useState } from 'react';
  6. import SUIButton from 'semantic-ui-react/dist/commonjs/elements/Button/Button';
  7. import styled from 'styled-components';
  8. import { isUndefined } from '@polkadot/util';
  9. import Icon from '../Icon';
  10. import Tooltip from '../Tooltip';
  11. let idCounter = 0;
  12. function Button ({ children, className, floated, icon, isBasic = false, isCircular = false, isDisabled = false, isIcon, isFluid = false, isLoading = false, isNegative = false, isPositive = false, isPrimary = false, label, labelPosition, onClick, size, style, tabIndex, tooltip }: ButtonProps): React.ReactElement<ButtonProps> {
  13. const [triggerId] = useState(`button-${++idCounter}`);
  14. const props = {
  15. basic: isBasic,
  16. circular: isCircular,
  17. className: `${className} ${isIcon && 'isIcon'}`,
  18. 'data-tip': !!tooltip,
  19. 'data-for': triggerId,
  20. disabled: isDisabled,
  21. floated,
  22. fluid: isFluid,
  23. labelPosition,
  24. loading: isLoading,
  25. negative: isNegative,
  26. onClick,
  27. positive: isPositive,
  28. primary: isPrimary,
  29. size: size || (isIcon ? 'tiny' : undefined),
  30. secondary: !isBasic && !(isPositive || isPrimary || isNegative),
  31. style,
  32. tabIndex
  33. };
  34. return (
  35. <>
  36. {isUndefined(label) && isUndefined(children)
  37. ? <SUIButton {...props} icon={icon} />
  38. : (
  39. <SUIButton {...props}>
  40. {icon && (
  41. <><Icon className={icon} />{isIcon ? '' : ' '}</>
  42. )}
  43. {label}
  44. {children}
  45. </SUIButton>
  46. )
  47. }
  48. {tooltip && (
  49. <Tooltip
  50. place='top'
  51. text={tooltip}
  52. trigger={triggerId}
  53. />
  54. )}
  55. </>
  56. );
  57. }
  58. export default React.memo(styled(Button)`
  59. &:not(.isIcon) > i.icon {
  60. margin-left: 0.25rem;
  61. }
  62. &.isIcon {
  63. background: white !important;
  64. margin: 0 !important;
  65. padding: 0 !important;
  66. i.icon {
  67. margin: 0 0 0 0.25rem !important;
  68. }
  69. }
  70. `);