You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Label.tsx 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import React, { useCallback } from 'react';
  2. import { makeStyles } from 'tss-react/mui';
  3. import Icon from '../../../icons/components/Icon';
  4. import { withPixelLineHeight } from '../../../styles/functions.web';
  5. import { COLORS } from '../../constants';
  6. interface IProps {
  7. /**
  8. * Own CSS class name.
  9. */
  10. className?: string;
  11. /**
  12. * The color of the label.
  13. */
  14. color?: string;
  15. /**
  16. * An SVG icon to be rendered as the content of the label.
  17. */
  18. icon?: Function;
  19. /**
  20. * Color for the icon.
  21. */
  22. iconColor?: string;
  23. /**
  24. * HTML ID attribute to add to the root of {@code Label}.
  25. */
  26. id?: string;
  27. /**
  28. * Click handler if any.
  29. */
  30. onClick?: (e?: React.MouseEvent) => void;
  31. /**
  32. * String or component that will be rendered as the label itself.
  33. */
  34. text?: string;
  35. }
  36. const useStyles = makeStyles()(theme => {
  37. return {
  38. label: {
  39. ...withPixelLineHeight(theme.typography.labelRegular),
  40. alignItems: 'center',
  41. background: theme.palette.ui04,
  42. borderRadius: '4px',
  43. color: theme.palette.text01,
  44. display: 'flex',
  45. margin: '0 2px',
  46. padding: '6px',
  47. height: 28,
  48. boxSizing: 'border-box'
  49. },
  50. withIcon: {
  51. marginLeft: 8
  52. },
  53. clickable: {
  54. cursor: 'pointer'
  55. },
  56. [COLORS.white]: {
  57. background: theme.palette.ui09,
  58. color: theme.palette.text04,
  59. '& svg': {
  60. fill: theme.palette.icon04
  61. }
  62. },
  63. [COLORS.green]: {
  64. background: theme.palette.success02
  65. },
  66. [COLORS.red]: {
  67. background: theme.palette.actionDanger
  68. }
  69. };
  70. });
  71. const Label = ({
  72. className,
  73. color,
  74. icon,
  75. iconColor,
  76. id,
  77. onClick,
  78. text
  79. }: IProps) => {
  80. const { classes, cx } = useStyles();
  81. const onKeyPress = useCallback(event => {
  82. if (!onClick) {
  83. return;
  84. }
  85. if (event.key === 'Enter' || event.key === ' ') {
  86. event.preventDefault();
  87. onClick();
  88. }
  89. }, [ onClick ]);
  90. return (
  91. <div
  92. className = { cx(classes.label, onClick && classes.clickable,
  93. color && classes[color], className
  94. ) }
  95. id = { id }
  96. onClick = { onClick }
  97. onKeyPress = { onKeyPress }
  98. role = { onClick ? 'button' : undefined }
  99. tabIndex = { onClick ? 0 : undefined }>
  100. {icon && <Icon
  101. color = { iconColor }
  102. size = '16'
  103. src = { icon } />}
  104. {text && <span className = { icon && classes.withIcon }>{text}</span>}
  105. </div>
  106. );
  107. };
  108. export default Label;