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: Add validationStatus prop to Dropdown #2365

Merged
merged 15 commits into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from 11 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
60 changes: 34 additions & 26 deletions src/components/forms/Dropdown/Dropdown.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react'

import { Dropdown } from './Dropdown'
import { Label } from '../Label/Label'
import { ComponentMeta, ComponentStory } from '@storybook/react'

export default {
title: 'Components/Dropdown',
Expand All @@ -17,43 +18,50 @@ Source: https://designsystem.digital.gov/components/select/
},
},
},
}
argTypes: {
validationStatus: {
options: ['error', 'success'],
control: 'radio',
},
disabled: { control: 'boolean' },
},
} as ComponentMeta<typeof Dropdown>

export const defaultDropdown = (): React.ReactElement => (
<Dropdown id="input-dropdown" name="input-dropdown">
const options = (
<>
<option>- Select - </option>
<option value="value1">Option A</option>
<option value="value2">Option B</option>
<option value="value3">Option C</option>
</Dropdown>
</>
)

export const withDefaultValue = (): React.ReactElement => (
<Dropdown id="input-dropdown" name="input-dropdown" defaultValue="value2">
<option>- Select - </option>
<option value="value1">Option A</option>
<option value="value2">Option B</option>
<option value="value3">Option C</option>
</Dropdown>
const Template: ComponentStory<typeof Dropdown> = (args) => (
<Dropdown {...args}>{options}</Dropdown>
)

export const withLabel = (): React.ReactElement => (
export const Default = Template.bind({})
Default.args = { id: 'input-dropdown', name: 'input-dropdown' }

export const WithDefaultValue = Template.bind({})
WithDefaultValue.args = {
id: 'input-dropdown',
name: 'input-dropdown',
defaultValue: 'value2',
}

export const Disabled = Template.bind({})
Disabled.args = {
id: 'input-dropdown',
name: 'input-dropdown',
disabled: true,
}

export const WithLabel = () => (
<>
<Label htmlFor="options">Dropdown label</Label>
<Label htmlFor="input-dropdown">Dropdown label</Label>
<Dropdown id="input-dropdown" name="input-dropdown">
<option>- Select - </option>
<option value="value1">Option A</option>
<option value="value2">Option B</option>
<option value="value3">Option C</option>
{options}
</Dropdown>
</>
)

export const disabled = (): React.ReactElement => (
<Dropdown id="input-dropdown" name="input-dropdown" disabled>
<option>- Select - </option>
<option value="value1">Option A</option>
<option value="value2">Option B</option>
<option value="value3">Option C</option>
</Dropdown>
)
52 changes: 46 additions & 6 deletions src/components/forms/Dropdown/Dropdown.test.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,58 @@
import React from 'react'
import { render } from '@testing-library/react'
import React, { ComponentProps } from 'react'
import { render, screen } from '@testing-library/react'

import { Dropdown } from './Dropdown'

describe('Dropdown component', () => {
it('renders without errors', () => {
const { queryByTestId } = render(
<Dropdown id="input-type-text" name="input-type-text">
const renderDropdown = (
props?: Omit<ComponentProps<typeof Dropdown>, 'id' | 'name' | 'children'>
) => {
render(
<Dropdown id="input-type-dropdown" name="input-type-dropdown" {...props}>
<option>- Select - </option>
<option value="value1">Option A</option>
<option value="value2">Option B</option>
<option value="value3">Option C</option>
</Dropdown>
)
expect(queryByTestId('dropdown')).toBeInTheDocument()

const queryForDropdown = () => screen.queryByRole('combobox')

return {
queryForDropdown,
}
}

it('renders without errors', () => {
const { queryForDropdown } = renderDropdown()

const dropdown = queryForDropdown()

expect(dropdown).toBeInTheDocument()
expect(dropdown).toHaveClass('usa-select')
})

describe('validationStatus', () => {
it('renders with error styling', () => {
const { queryForDropdown } = renderDropdown({ validationStatus: 'error' })

const dropdown = queryForDropdown()

expect(dropdown).toBeInTheDocument()
expect(dropdown).toHaveClass('usa-select')
expect(dropdown).toHaveClass('usa-input--error')
})

it('renders with success styling', () => {
const { queryForDropdown } = renderDropdown({
validationStatus: 'success',
})

const dropdown = queryForDropdown()

expect(dropdown).toBeInTheDocument()
expect(dropdown).toHaveClass('usa-select')
expect(dropdown).toHaveClass('usa-input--success')
})
})
})
14 changes: 13 additions & 1 deletion src/components/forms/Dropdown/Dropdown.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import React from 'react'
import classnames from 'classnames'
import { ValidationStatus } from '../../../types/validationStatus'

type DropdownProps = {
id: string
name: string
className?: string
children: React.ReactNode
validationStatus?: ValidationStatus
inputRef?:
| string
| ((instance: HTMLSelectElement | null) => void)
Expand All @@ -20,9 +22,19 @@ export const Dropdown = ({
className,
inputRef,
children,
validationStatus,
...inputProps
}: DropdownProps & JSX.IntrinsicElements['select']): React.ReactElement => {
const classes = classnames('usa-select', className)
const isError = validationStatus === 'error'
const isSuccess = validationStatus === 'success'
const classes = classnames(
'usa-select',
{
'usa-input--error': isError,
'usa-input--success': isSuccess,
},
className
)

return (
<select
Expand Down
3 changes: 2 additions & 1 deletion src/components/forms/TextInput/TextInput.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react'
import classnames from 'classnames'
import { ValidationStatus } from '../../../types/validationStatus'

type TextInputRef =
| string
Expand All @@ -16,7 +17,7 @@ type RequiredTextInputProps = {

type CustomTextInputProps = {
className?: string
validationStatus?: 'error' | 'success'
validationStatus?: ValidationStatus
inputSize?: 'small' | 'medium'
inputRef?: TextInputRef
inputProps?: JSX.IntrinsicElements['input']
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export { TextInput } from './components/forms/TextInput/TextInput'
export { TimePicker } from './components/forms/TimePicker/TimePicker'
export { ValidationChecklist } from './components/forms/Validation/ValidationChecklist'
export { ValidationItem } from './components/forms/Validation/ValidationItem'
export type { ValidationStatus } from './types/validationStatus'

/** Header Components */
export { ExtendedNav } from './components/header/ExtendedNav/ExtendedNav'
Expand Down
1 change: 1 addition & 0 deletions src/types/validationStatus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type ValidationStatus = 'error' | 'success'