aboutsummaryrefslogtreecommitdiffstats
path: root/packages/forms/src/label/index.tsx
blob: 590270a06c52bae83b367652a5fc3304ed8faaef (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import classnames from 'classnames';
import { Classes } from 'jss';
import React, { Component } from 'react';
import injectSheet from 'react-jss';

import { IFormField } from '../typings/generic';

import styles from './styles';

interface ILabel extends IFormField, React.LabelHTMLAttributes<HTMLLabelElement> {
  classes: Classes;
}

class LabelComponent extends Component<ILabel> {
  static defaultProps = {
    showLabel: true,
  };

  render() {
    const {
      title,
      showLabel,
      classes,
      className,
      children,
      htmlFor,
    } = this.props;

    if (!showLabel) return children;

    return (
      <label
        className={classnames({
          [`${className}`]: className,
        })}
        htmlFor={htmlFor}
      >
        {showLabel && (
          <span className={classes.label}>{title}</span>
        )}
        <div className={classes.content}>
          {children}
        </div>
      </label>
    );
  }
}

export const Label = injectSheet(styles)(LabelComponent);