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.web.js 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // @flow
  2. import { withStyles } from '@material-ui/core/styles';
  3. import clsx from 'clsx';
  4. import React from 'react';
  5. import Icon from '../../icons/components/Icon';
  6. import { withPixelLineHeight } from '../../styles/functions.web';
  7. import { COLORS } from '../constants';
  8. import AbstractLabel, {
  9. type Props as AbstractProps
  10. } from './AbstractLabel';
  11. type Props = AbstractProps & {
  12. /**
  13. * An object containing the CSS classes.
  14. */
  15. classes: Object,
  16. /**
  17. * Own CSS class name.
  18. */
  19. className: string,
  20. /**
  21. * The color of the label.
  22. */
  23. color: string,
  24. /**
  25. * HTML ID attribute to add to the root of {@code Label}.
  26. */
  27. id: string,
  28. /**
  29. * Click handler if any.
  30. */
  31. onClick?: Function,
  32. };
  33. /**
  34. * Creates the styles for the component.
  35. *
  36. * @param {Object} theme - The current UI theme.
  37. *
  38. * @returns {Object}
  39. */
  40. const styles = theme => {
  41. return {
  42. label: {
  43. ...withPixelLineHeight(theme.typography.labelRegular),
  44. alignItems: 'center',
  45. background: theme.palette.ui04,
  46. borderRadius: theme.shape.borderRadius / 2,
  47. color: theme.palette.text01,
  48. display: 'flex',
  49. height: 28,
  50. margin: '0 0 4px 4px',
  51. padding: '0 8px'
  52. },
  53. withIcon: {
  54. marginLeft: 8
  55. },
  56. clickable: {
  57. cursor: 'pointer'
  58. },
  59. [COLORS.white]: {
  60. background: theme.palette.text01,
  61. color: theme.palette.ui04,
  62. '& svg': {
  63. fill: theme.palette.ui04
  64. }
  65. },
  66. [COLORS.green]: {
  67. background: theme.palette.success02
  68. },
  69. [COLORS.red]: {
  70. background: theme.palette.actionDanger
  71. }
  72. };
  73. };
  74. /**
  75. * React Component for showing short text in a circle.
  76. *
  77. * @extends Component
  78. */
  79. class Label extends AbstractLabel<Props, *> {
  80. /**
  81. * Implements React's {@link Component#render()}.
  82. *
  83. * @inheritdoc
  84. */
  85. render() {
  86. const {
  87. classes,
  88. className,
  89. color,
  90. icon,
  91. id,
  92. onClick,
  93. text
  94. } = this.props;
  95. const labelClassName = clsx(
  96. classes.label,
  97. onClick && classes.clickable,
  98. color && classes[color],
  99. className
  100. );
  101. return (
  102. <div
  103. className = { labelClassName }
  104. id = { id }
  105. onClick = { onClick }>
  106. { icon && <Icon
  107. size = '16'
  108. src = { icon } /> }
  109. { text && <span className = { icon && classes.withIcon }>{text}</span> }
  110. </div>
  111. );
  112. }
  113. }
  114. export default withStyles(styles)(Label);