Skip to content

Commit

Permalink
feat(input): Add sublabel for smaller text next to input label (#421)
Browse files Browse the repository at this point in the history
* [DJR-471] [Ed Thome/Peter Marshall] Add sublabel for smaller text next to input label

* [DJR-471] [Ed Thome] Refactor tests to get Box instead of Text. Change docs to express intent of the sublabel property.
  • Loading branch information
ethome authored and ryanoglesby08 committed Oct 9, 2017
1 parent 1111067 commit 87d5cfd
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 8 deletions.
14 changes: 13 additions & 1 deletion src/components/Input/Input.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class Input extends React.Component {
}

render() {
const { type, label, feedback, error, helper, tooltip, ...rest } = this.props
const { type, label, sublabel, feedback, error, helper, tooltip, ...rest } = this.props

const inputId = generateId(rest.id, rest.name, label)
const helperId = helper && inputId.postfix('helper')
Expand All @@ -156,6 +156,14 @@ class Input extends React.Component {
<Text size="medium" bold>
{label}
</Text>

{sublabel &&
<Box inline spacing="margin" left={2} data-testid="sublabel">
<Text size="small">
{sublabel}
</Text>
</Box>
}
</label>
{tooltip && React.cloneElement(tooltip, { connectedFieldLabel: label })}
</Flexbox>
Expand Down Expand Up @@ -216,6 +224,10 @@ Input.propTypes = {
* The label.
*/
label: PropTypes.string.isRequired,
/**
* Clarify attributes of the expected input.
*/
sublabel: PropTypes.string,
/**
* The value.
*/
Expand Down
9 changes: 9 additions & 0 deletions src/components/Input/Input.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@ const validate = (event) => {

### Supplying extra information

Use `sublabel` to clarify attributes of the expected input.
We recommend using this over the html `placeholder` attribute because it's more usable and accessible.

```
<Input
label="Transit number" sublabel="5 digits" type="number"
/>
```

Use a `helper` to offer the user a detailed explanation of the input expected by a form field. Use the `Input.Helper`
component, which can contain any content.

Expand Down
37 changes: 30 additions & 7 deletions src/components/Input/__tests__/Input.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,37 @@ describe('Input', () => {
expect(findInputElement(input)).toHaveProp('type', 'number')
})

it('must have a label', () => {
const input = doShallow({ label: 'The label' })
describe('label', () => {
it('must have a label', () => {
const input = doShallow({ label: 'The label' })

expect(input.find('label')).toContainReact(
<Text size="medium" bold>
The label
</Text>
)
expect(input.find('label')).toContainReact(
<Text size="medium" bold>
The label
</Text>
)
})

it('should contain sublabel', () => {
const input = doShallow({ sublabel: 'The sublabel' })

const sublabel = input
.find('[data-testid="sublabel"]')

expect(sublabel).toContainReact(
<Text size="small">
The sublabel
</Text>
)
})

it('should not contain sublabel when not provided', () => {
const input = doShallow({ sublabel: undefined })

const sublabel = input.find('[data-testid="sublabel"]')

expect(sublabel).not.toBePresent()
})
})

// FIXME: Test for override of the global styles in forms.scss. This can be removed
Expand Down

0 comments on commit 87d5cfd

Please sign in to comment.