Skip to content

Commit

Permalink
Fixed passed onBlur & onFocus being ignored on Search component (#331)
Browse files Browse the repository at this point in the history
* Fixed passed onBlur & onFocus being ignored

* Added onBlur/onFocus to story

* Updated test with onBlur & onFocus
  • Loading branch information
Michael Marszalek authored May 28, 2020
1 parent f26a1d6 commit 80df497
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 3 deletions.
10 changes: 10 additions & 0 deletions apps/storybook-react/stories/Search.stories.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export default {
}

const handleOnChange = action('onChange')
const handleOnBlur = action('onBlur')
const handleOnFocus = action('onFocus')

export const Examples = () => (
<div>
Expand All @@ -40,6 +42,8 @@ export const Examples = () => (
id="search-normal"
placeholder="Search"
onChange={handleOnChange}
onFocus={handleOnFocus}
onBlur={handleOnBlur}
/>
</Wrapper>
<Wrapper>
Expand All @@ -49,6 +53,8 @@ export const Examples = () => (
id="search-predefined"
placeholder="Search"
onChange={handleOnChange}
onFocus={handleOnFocus}
onBlur={handleOnBlur}
value="Predefined value"
/>
</Wrapper>
Expand All @@ -61,6 +67,8 @@ export const Examples = () => (
id="search-contained"
placeholder="Search"
onChange={handleOnChange}
onFocus={handleOnFocus}
onBlur={handleOnBlur}
/>
</OuterContainer>
</Wrapper>
Expand All @@ -77,6 +85,8 @@ export const Examples = () => (
id="search-disabled"
placeholder="Search"
onChange={handleOnChange}
onFocus={handleOnFocus}
onBlur={handleOnBlur}
disabled
/>
</Wrapper>
Expand Down
22 changes: 19 additions & 3 deletions libraries/core-react/src/Search/Search.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ const InsideButton = styled.div`
`

export const Search = React.forwardRef(function EdsSearch(
{ onChange, value: initValue, className, disabled, ...rest },
{ onChange, value: initValue, className, disabled, onBlur, onFocus, ...rest },
ref,
) {
const inputRef = useCombinedRefs(useRef(null), ref)
Expand Down Expand Up @@ -204,8 +204,18 @@ export const Search = React.forwardRef(function EdsSearch(
type: 'search',
role: 'searchbox',
'aria-label': 'search input',
onBlur: handleBlur,
onFocus: handleFocus,
onBlur: (e) => {
handleBlur(e)
if (onBlur) {
onBlur(e)
}
},
onFocus: (e) => {
handleFocus(e)
if (onFocus) {
onFocus(e)
}
},
onChange: (e) => {
handleOnChange(e)
if (onChange) {
Expand Down Expand Up @@ -248,6 +258,10 @@ Search.propTypes = {
onChange: PropTypes.func,
/** Value for search field */
value: PropTypes.string,
/** onBlur handler */
onBlur: PropTypes.func,
/** onFocus handler */
onFocus: PropTypes.func,
}

Search.defaultProps = {
Expand All @@ -256,6 +270,8 @@ Search.defaultProps = {
disabled: false,
onChange: undefined,
value: '',
onBlur: undefined,
onFocus: undefined,
}

Search.displayName = 'eds-search'
31 changes: 31 additions & 0 deletions libraries/core-react/src/Search/Search.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,35 @@ describe('Search', () => {
expect(callbackValue).toEqual('')
expect(callbackId).toEqual(searchId)
})

it('Has called onFocus when search if focused', () => {
const searchId = 'search-id-when-testing'
let callbackId = ''
const handleOnFocus = jest.fn(({ target: { id } }) => {
callbackId = id
})

render(<Search id={searchId} onFocus={handleOnFocus} />)
const searchBox = screen.queryByRole('searchbox')

fireEvent.focus(searchBox)

expect(handleOnFocus).toHaveBeenCalled()
expect(callbackId).toEqual(searchId)
})
it('Has called onBlur when search if blured', () => {
const searchId = 'search-id-when-testing'
let callbackId = ''
const handleOnBlur = jest.fn(({ target: { id } }) => {
callbackId = id
})

render(<Search id={searchId} onBlur={handleOnBlur} />)
const searchBox = screen.queryByRole('searchbox')

fireEvent.blur(searchBox)

expect(handleOnBlur).toHaveBeenCalled()
expect(callbackId).toEqual(searchId)
})
})

0 comments on commit 80df497

Please sign in to comment.