Skip to content

Commit

Permalink
feat: Update Search component to support i18n (#1192)
Browse files Browse the repository at this point in the history
* Update Search component to pass in label instead of hardcoded string as Button text, update with test checking for ability to pass label, update stories with Spanish examples to match USWDS

* Simplify localization interface, update test, update Storybook examples
  • Loading branch information
SirenaBorracha authored May 5, 2021
1 parent 51f840e commit 5241d15
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 7 deletions.
21 changes: 21 additions & 0 deletions src/components/Search/Search.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ const mockSubmit = (): void => {
/* mock submit fn */
}

const sampleLocalization = {
buttonText: 'Buscar',
}

export const defaultSearch = (): React.ReactElement => (
<Search onSubmit={mockSubmit} />
)
Expand All @@ -36,3 +40,20 @@ export const smallSearch = (): React.ReactElement => (
onSubmit={mockSubmit}
/>
)

export const defaultSpanishSearch = (): React.ReactElement => (
<Search onSubmit={mockSubmit} i18n={sampleLocalization} />
)

export const bigSpanishSearch = (): React.ReactElement => (
<Search size="big" onSubmit={mockSubmit} i18n={sampleLocalization} />
)

export const smallSpanishSearch = (): React.ReactElement => (
<Search
placeholder="(Optional) Spanish Placeholder Text"
size="small"
onSubmit={mockSubmit}
i18n={sampleLocalization}
/>
)
16 changes: 16 additions & 0 deletions src/components/Search/Search.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ jest.mock('../../deprecation')
import { deprecationWarning } from '../../deprecation'
import { Search } from './Search'

const sampleLocalization = {
buttonText: 'Buscar',
}

describe('Search component', () => {
it('renders without errors', () => {
const mockSubmit = jest.fn()
Expand Down Expand Up @@ -32,6 +36,18 @@ describe('Search component', () => {
)
})

it('renders a label', () => {
const mockSubmit = jest.fn()
const { queryByLabelText } = render(
<Search
onSubmit={mockSubmit}
label="Buscar"
i18n={sampleLocalization}></Search>
)

expect(queryByLabelText('Buscar')).toBeInTheDocument()
})

describe('renders size classes', () => {
beforeEach(() => {
jest.clearAllMocks()
Expand Down
22 changes: 15 additions & 7 deletions src/components/Search/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import { Form, OptionalFormProps } from '../forms/Form/Form'
import { Label } from '../forms/Label/Label'
import { TextInput } from '../forms/TextInput/TextInput'

interface SearchLocalization {
buttonText: string
}

interface SearchInputProps {
onSubmit: (event: React.FormEvent<HTMLFormElement>) => void
inputId?: string
inputName?: string
size?: 'big' | 'small'
/**
* @deprecated since 1.6.0, use size
Expand All @@ -20,21 +22,25 @@ interface SearchInputProps {
* @deprecated since 1.6.0, use size
*/
small?: boolean
label?: React.ReactNode
className?: string
inputName?: string
inputId?: string
placeholder?: string
label?: React.ReactNode
i18n?: SearchLocalization
}

export const Search = ({
onSubmit,
inputId = 'search-field',
inputName = 'search',
size,
big,
small,
label = 'Search',
className,
placeholder,
inputName = 'search',
label = 'Search',
inputId = 'search-field',
i18n,
...formProps
}: SearchInputProps & OptionalFormProps): React.ReactElement => {
if (big) {
Expand All @@ -44,6 +50,8 @@ export const Search = ({
deprecationWarning('Search property small is deprecated. Use size')
}

const buttonText = i18n?.buttonText || 'Search'

const isBig = size ? size === 'big' : big
const isSmall = size ? size === 'small' : small
const classes = classnames(
Expand Down Expand Up @@ -73,7 +81,7 @@ export const Search = ({
/>
<Button type="submit">
<span className={isSmall ? 'usa-sr-only' : 'usa-search__submit-text'}>
Search
{buttonText}
</span>
</Button>
</Form>
Expand Down

0 comments on commit 5241d15

Please sign in to comment.