Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: InputGroup component #2383

Merged
merged 9 commits into from
May 9, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/components/forms/InputGroup/InputGroup.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React from 'react'
import { render } from '@testing-library/react'

import { InputGroup } from './InputGroup'

describe('InputGroup component', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a test for checking that the right className is applied?

Copy link
Contributor

@brandonlenz brandonlenz May 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also one with the error state

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! I was just thinking the tests could probably be more thorough. Thanks!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jcbcapps sorry, I shoulda been more clear. Can you add a test that verifies that the component has the default class name applied? (usa-input-group)

It can go in the renders without errors test, imo, since it's jsut verifying that the component built with the right uswds class

it('renders without errors', () => {
const { queryByTestId } = render(<InputGroup>My Input Group</InputGroup>)
expect(queryByTestId('inputGroup')).toBeInTheDocument()
})

it('renders its children', () => {
const { queryByText } = render(<InputGroup>My Input Group</InputGroup>)
expect(queryByText('My Input Group')).toBeInTheDocument()
})

it('adds the error class when error is true', () => {
const { queryByTestId } = render(
<InputGroup error>My Input Group</InputGroup>
)
expect(queryByTestId('inputGroup')).toHaveClass('usa-input-group--error')
})

it('adds the provided className', () => {
const { queryByTestId } = render(
<InputGroup className="my-class">My Input Group</InputGroup>
)
expect(queryByTestId('inputGroup')).toHaveClass('my-class')
})
})
26 changes: 26 additions & 0 deletions src/components/forms/InputGroup/InputGroup.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import React from 'react'
import classnames from 'classnames'

export interface InputGroupProps {
children: React.ReactNode
className?: string
error?: boolean
}

export const InputGroup = ({
children,
className,
error,
}: InputGroupProps): React.ReactElement => {
const classes = classnames(
'usa-input-group',
{ 'usa-input-group--error': error },
className
)

return (
<div data-testid="inputGroup" className={classes}>
{children}
</div>
)
}
13 changes: 7 additions & 6 deletions src/components/forms/InputPrefix/InputPrefix.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react'
import { InputPrefix } from './InputPrefix'
import { Icon } from '../../Icon/Icons'
import { TextInput } from '../TextInput/TextInput'
import { InputGroup } from '../InputGroup/InputGroup'
import { FormGroup } from '../FormGroup/FormGroup'

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

export const InputWithTextInputPrefix = (): React.ReactElement => (
<FormGroup>
<div className="usa-input-group">
<InputGroup>
<InputPrefix>cvc</InputPrefix>
<TextInput id="cvc" name="cvc" type="text" />
</div>
</InputGroup>
</FormGroup>
)

export const InputWithTextInputPrefixError = (): React.ReactElement => (
<FormGroup>
<div className="usa-input-group usa-input-group--error">
<InputGroup error>
<InputPrefix>cvc</InputPrefix>
<TextInput id="cvc" name="cvc" type="text" validationStatus="error" />
</div>
</InputGroup>
</FormGroup>
)

export const InputWithIconInputPrefix = (): React.ReactElement => (
<FormGroup>
<div className="usa-input-group">
<InputGroup>
<InputPrefix>
<Icon.CreditCard />
</InputPrefix>
<TextInput id="card" name="card" type="text" />
</div>
</InputGroup>
</FormGroup>
)
13 changes: 7 additions & 6 deletions src/components/forms/InputSuffix/InputSuffix.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react'
import { InputSuffix } from './InputSuffix'
import { InputGroup } from '../InputGroup/InputGroup'
import { FormGroup } from '../FormGroup/FormGroup'
import { TextInput } from '../TextInput/TextInput'
import { Icon } from '../../Icon/Icons'
Expand All @@ -22,18 +23,18 @@ Source: https://designsystem.digital.gov/components/input-prefix-suffix/

export const InputWithIconInputSuffix = (): React.ReactElement => (
<FormGroup>
<div className="usa-input-group">
<InputGroup>
<TextInput id="search" name="search" type="search" />
<InputSuffix>
<Icon.Search />
</InputSuffix>
</div>
</InputGroup>
</FormGroup>
)

export const InputWithIconInputSuffixError = (): React.ReactElement => (
<FormGroup>
<div className="usa-input-group usa-input-group--error">
<InputGroup error>
<TextInput
id="search"
name="search"
Expand All @@ -43,15 +44,15 @@ export const InputWithIconInputSuffixError = (): React.ReactElement => (
<InputSuffix>
<Icon.Search />
</InputSuffix>
</div>
</InputGroup>
</FormGroup>
)

export const InputWithTextInputSuffix = (): React.ReactElement => (
<FormGroup>
<div className="usa-input-group usa-input-group--sm">
<InputGroup>
<TextInput id="weight" name="weight" type="text" />
<InputSuffix>lbs.</InputSuffix>
</div>
</InputGroup>
</FormGroup>
)
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export { FileInput } from './components/forms/FileInput/FileInput'
export type { FileInputRef } from './components/forms/FileInput/FileInput'
export { Form } from './components/forms/Form/Form'
export { FormGroup } from './components/forms/FormGroup/FormGroup'
export { InputGroup } from './components/forms/InputGroup/InputGroup'
export { InputPrefix } from './components/forms/InputPrefix/InputPrefix'
export { InputSuffix } from './components/forms/InputSuffix/InputSuffix'
export { Label } from './components/forms/Label/Label'
Expand Down