Skip to content
This repository was archived by the owner on Jun 9, 2024. It is now read-only.

Commit bbe2666

Browse files
committed
feat(Label): add Label component
1 parent b9e64f6 commit bbe2666

File tree

6 files changed

+192
-0
lines changed

6 files changed

+192
-0
lines changed

__tests__/Label.test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import React from 'react';
2+
import { renderToStaticMarkup } from 'react-dom/server';
3+
import { Label } from '../src/index';
4+
5+
describe('<Label />', () => {
6+
it('renders basic <Label />', () => {
7+
expect(renderToStaticMarkup(<Label />)).toEqual(
8+
'<label class="pt-label"></label>'
9+
);
10+
});
11+
12+
it('renders basic <Label /> with props', () => {
13+
expect(renderToStaticMarkup(<Label text="Label A" inline />)).toEqual(
14+
'<label class="pt-label pt-inline">Label A</label>'
15+
);
16+
});
17+
18+
it('renders disabled <Label /> with muted', () => {
19+
expect(
20+
renderToStaticMarkup(<Label text="Label A" disabled muted="(required)" />)
21+
).toEqual(
22+
'<label class="pt-label pt-disabled">Label A<span class="pt-text-muted">(required)</span></label>'
23+
);
24+
});
25+
26+
it('renders <Label /> with basic input', () => {
27+
expect(
28+
renderToStaticMarkup(
29+
<Label text="Label A">
30+
<input disabled className="pt-input" />
31+
</Label>
32+
)
33+
).toEqual(
34+
'<label class="pt-label">Label A<input disabled="" class="pt-input"/></label>'
35+
);
36+
});
37+
});

src/components/Label/Label.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import React from 'react';
2+
import PropTypes from 'prop-types';
3+
import classnames from 'classnames';
4+
5+
const Label = ({
6+
children,
7+
className,
8+
disabled,
9+
inline,
10+
text,
11+
muted,
12+
...rest
13+
}) => {
14+
return (
15+
<label
16+
className={classnames(
17+
'pt-label',
18+
{ 'pt-disabled': disabled },
19+
{ 'pt-inline': inline },
20+
className
21+
)}
22+
{...rest}
23+
>
24+
{text}
25+
{muted && <span className="pt-text-muted">{muted}</span>}
26+
{children}
27+
</label>
28+
);
29+
};
30+
31+
/**
32+
* Label property types.
33+
*/
34+
Label.propTypes = {
35+
/**
36+
* Primary content.
37+
*/
38+
children: PropTypes.node,
39+
40+
/**
41+
* Additional CSS classes.
42+
*/
43+
className: PropTypes.string,
44+
45+
/**
46+
* Adds appereance of `disabled` style.
47+
*/
48+
disabled: PropTypes.bool,
49+
50+
/**
51+
* Align label and children elements on same line.
52+
*/
53+
inline: PropTypes.bool,
54+
55+
/**
56+
* Text to be displayed in label.
57+
*/
58+
text: PropTypes.string,
59+
60+
/**
61+
* Adds extra information to label.
62+
*/
63+
muted: PropTypes.string,
64+
};
65+
66+
/**
67+
* Label default properties.
68+
*/
69+
Label.defaultProps = {
70+
children: null,
71+
className: '',
72+
disabled: false,
73+
inline: false,
74+
text: '',
75+
muted: '',
76+
};
77+
78+
export default Label;

src/components/Label/Label.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
Labels enhance the usability of your forms.
2+
3+
```js static
4+
import { Label } from 'blueprint-components';
5+
```
6+
7+
Additionally:
8+
9+
```js static
10+
import { InputGroup, Button, Classes } from '@blueprintjs/core';
11+
```
12+
13+
Simple labels are useful for basic forms for a single `<input />`.
14+
15+
```jsx
16+
<div className="">
17+
<Label text="Label A">
18+
<input className="pt-input" placeholder="Text input" />
19+
</Label>
20+
<Label text="Label A" muted="(optional)">
21+
<input className="pt-input" placeholder="Text input" />
22+
</Label>
23+
</div>
24+
```
25+
26+
### Inline Label
27+
To place `label` text and children elements on same line, add optioanl `inline` prop:
28+
29+
```html static
30+
<Label inline>
31+
...
32+
</Label>
33+
```
34+
35+
```jsx
36+
<Label text="Date of Birth" inline>
37+
<InputGroup
38+
leftIconName="calendar"
39+
placeholder="Select date..."
40+
/>
41+
</Label>
42+
```
43+
44+
### Disabled Label
45+
46+
To make `label` appear as disabled, add optional `disabled` prop:
47+
48+
> Note: This prop will not add `disabled` as attribute to it's children elements. You have to add it manualy on form elments (`input`, `select`) and `.pt-disabled` CSS class to input groups.
49+
50+
```html static
51+
<Label disabled>
52+
...
53+
</Label>
54+
```
55+
56+
```jsx
57+
<Label text="Date of Birth" disabled muted=" (disabled only label)">
58+
<InputGroup
59+
leftIconName="calendar"
60+
placeholder="Select date..."
61+
/>
62+
</Label>
63+
```
64+
65+
```jsx
66+
<Label text="Date of Birth" disabled muted=" (with disabled input)">
67+
<InputGroup
68+
disabled
69+
leftIconName="calendar"
70+
placeholder="Select date..."
71+
/>
72+
</Label>
73+
```

src/components/Label/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default as Label } from './Label';

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export { ButtonGroup } from './components/ButtonGroup';
22
export { Callout } from './components/Callout';
33
export { Card } from './components/Card';
44
export { ControlGroup } from './components/ControlGroup';
5+
export { Label } from './components/Label';
56
export { Navbar } from './components/Navbar';
67
export { NavbarDivider } from './components/NavbarDivider';
78
export { NavbarGroup } from './components/NavbarGroup';

styleguide/styleguide.setup.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
Callout,
1111
Card,
1212
ControlGroup,
13+
Label,
1314
Navbar,
1415
NavbarGroup,
1516
NavbarHeading,
@@ -19,6 +20,7 @@ global.ButtonGroup = ButtonGroup;
1920
global.Callout = Callout;
2021
global.Card = Card;
2122
global.ControlGroup = ControlGroup;
23+
global.Label = Label;
2224
global.Navbar = Navbar;
2325
global.NavbarGroup = NavbarGroup;
2426
global.NavbarHeading = NavbarHeading;

0 commit comments

Comments
 (0)