Skip to content

Commit 5761db6

Browse files
authored
feat: InputGroup component (#2383)
1 parent dafbb4e commit 5761db6

File tree

5 files changed

+72
-12
lines changed

5 files changed

+72
-12
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import React from 'react'
2+
import { render } from '@testing-library/react'
3+
4+
import { InputGroup } from './InputGroup'
5+
6+
describe('InputGroup component', () => {
7+
it('renders without errors', () => {
8+
const { queryByTestId } = render(<InputGroup>My Input Group</InputGroup>)
9+
expect(queryByTestId('inputGroup')).toBeInTheDocument()
10+
expect(queryByTestId('inputGroup')).toHaveClass('usa-input-group')
11+
})
12+
13+
it('renders its children', () => {
14+
const { queryByText } = render(<InputGroup>My Input Group</InputGroup>)
15+
expect(queryByText('My Input Group')).toBeInTheDocument()
16+
})
17+
18+
it('adds the error class when error is true', () => {
19+
const { queryByTestId } = render(
20+
<InputGroup error>My Input Group</InputGroup>
21+
)
22+
expect(queryByTestId('inputGroup')).toHaveClass('usa-input-group--error')
23+
})
24+
25+
it('adds the provided className', () => {
26+
const { queryByTestId } = render(
27+
<InputGroup className="my-class">My Input Group</InputGroup>
28+
)
29+
expect(queryByTestId('inputGroup')).toHaveClass('my-class')
30+
})
31+
})
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import React from 'react'
2+
import classnames from 'classnames'
3+
4+
export interface InputGroupProps {
5+
children: React.ReactNode
6+
className?: string
7+
error?: boolean
8+
}
9+
10+
export const InputGroup = ({
11+
children,
12+
className,
13+
error,
14+
}: InputGroupProps): React.ReactElement => {
15+
const classes = classnames(
16+
'usa-input-group',
17+
{ 'usa-input-group--error': error },
18+
className
19+
)
20+
21+
return (
22+
<div data-testid="inputGroup" className={classes}>
23+
{children}
24+
</div>
25+
)
26+
}

src/components/forms/InputPrefix/InputPrefix.stories.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React from 'react'
22
import { InputPrefix } from './InputPrefix'
33
import { Icon } from '../../Icon/Icons'
44
import { TextInput } from '../TextInput/TextInput'
5+
import { InputGroup } from '../InputGroup/InputGroup'
56
import { FormGroup } from '../FormGroup/FormGroup'
67

78
export default {
@@ -22,29 +23,29 @@ Source: https://designsystem.digital.gov/components/input-prefix-suffix/
2223

2324
export const InputWithTextInputPrefix = (): React.ReactElement => (
2425
<FormGroup>
25-
<div className="usa-input-group">
26+
<InputGroup>
2627
<InputPrefix>cvc</InputPrefix>
2728
<TextInput id="cvc" name="cvc" type="text" />
28-
</div>
29+
</InputGroup>
2930
</FormGroup>
3031
)
3132

3233
export const InputWithTextInputPrefixError = (): React.ReactElement => (
3334
<FormGroup>
34-
<div className="usa-input-group usa-input-group--error">
35+
<InputGroup error>
3536
<InputPrefix>cvc</InputPrefix>
3637
<TextInput id="cvc" name="cvc" type="text" validationStatus="error" />
37-
</div>
38+
</InputGroup>
3839
</FormGroup>
3940
)
4041

4142
export const InputWithIconInputPrefix = (): React.ReactElement => (
4243
<FormGroup>
43-
<div className="usa-input-group">
44+
<InputGroup>
4445
<InputPrefix>
4546
<Icon.CreditCard />
4647
</InputPrefix>
4748
<TextInput id="card" name="card" type="text" />
48-
</div>
49+
</InputGroup>
4950
</FormGroup>
5051
)

src/components/forms/InputSuffix/InputSuffix.stories.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react'
22
import { InputSuffix } from './InputSuffix'
3+
import { InputGroup } from '../InputGroup/InputGroup'
34
import { FormGroup } from '../FormGroup/FormGroup'
45
import { TextInput } from '../TextInput/TextInput'
56
import { Icon } from '../../Icon/Icons'
@@ -22,18 +23,18 @@ Source: https://designsystem.digital.gov/components/input-prefix-suffix/
2223

2324
export const InputWithIconInputSuffix = (): React.ReactElement => (
2425
<FormGroup>
25-
<div className="usa-input-group">
26+
<InputGroup>
2627
<TextInput id="search" name="search" type="search" />
2728
<InputSuffix>
2829
<Icon.Search />
2930
</InputSuffix>
30-
</div>
31+
</InputGroup>
3132
</FormGroup>
3233
)
3334

3435
export const InputWithIconInputSuffixError = (): React.ReactElement => (
3536
<FormGroup>
36-
<div className="usa-input-group usa-input-group--error">
37+
<InputGroup error>
3738
<TextInput
3839
id="search"
3940
name="search"
@@ -43,15 +44,15 @@ export const InputWithIconInputSuffixError = (): React.ReactElement => (
4344
<InputSuffix>
4445
<Icon.Search />
4546
</InputSuffix>
46-
</div>
47+
</InputGroup>
4748
</FormGroup>
4849
)
4950

5051
export const InputWithTextInputSuffix = (): React.ReactElement => (
5152
<FormGroup>
52-
<div className="usa-input-group usa-input-group--sm">
53+
<InputGroup>
5354
<TextInput id="weight" name="weight" type="text" />
5455
<InputSuffix>lbs.</InputSuffix>
55-
</div>
56+
</InputGroup>
5657
</FormGroup>
5758
)

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export { FileInput } from './components/forms/FileInput/FileInput'
5959
export type { FileInputRef } from './components/forms/FileInput/FileInput'
6060
export { Form } from './components/forms/Form/Form'
6161
export { FormGroup } from './components/forms/FormGroup/FormGroup'
62+
export { InputGroup } from './components/forms/InputGroup/InputGroup'
6263
export { InputPrefix } from './components/forms/InputPrefix/InputPrefix'
6364
export { InputSuffix } from './components/forms/InputSuffix/InputSuffix'
6465
export { Label } from './components/forms/Label/Label'

0 commit comments

Comments
 (0)