Skip to content
This repository has been archived by the owner on Jan 9, 2023. It is now read-only.

Commit

Permalink
Merge pull request #290 from MatthewDorner/disabled-typeahead
Browse files Browse the repository at this point in the history
fix(typeahead): add disabled prop to Typeahead
  • Loading branch information
matteovivona authored Feb 21, 2020
2 parents b7987a0 + 081de30 commit 85a4f86
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/components/Typeahead/Typeahead.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ interface Props {
minLength?: number
placeholder?: string
value?: any
disabled?: boolean
}

const Typeahead = (props: Props) => {
Expand All @@ -25,6 +26,7 @@ const Typeahead = (props: Props) => {
renderMenuItemChildren,
minLength,
value,
disabled,
} = props

const search = async (query: string) => {
Expand All @@ -51,6 +53,7 @@ const Typeahead = (props: Props) => {
onChange={onChange}
renderMenuItemChildren={renderMenuItemChildren}
defaultSelected={selectedValues}
disabled={disabled}
/>
)
}
Expand Down
24 changes: 24 additions & 0 deletions stories/typeahead.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,27 @@ storiesOf('Typeahead', module)
/>
</div>
))
.add('Disabled Typeahead', () => (
<div>
<Typeahead
id="typeahead"
searchAccessor="fullName"
placeholder="placeholder"
onSearch={() => getOptions()}
onChange={(selected) => alert(JSON.stringify(selected))}
disabled
value={{
id: '3',
firstName: 'first3',
lastName: 'last3',
fullName: 'first3 last3',
}}
renderMenuItemChildren={(option) => (
// eslint-disable-next-line
<div>
{`${option.fullName}`}
</div>
)}
/>
</div>
))
53 changes: 52 additions & 1 deletion test/typeahead.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'
import { shallow } from 'enzyme'
import { shallow, mount } from 'enzyme'
import { AsyncTypeahead } from 'react-bootstrap-typeahead'
import { act } from 'react-dom/test-utils'
import { Typeahead } from '../src/components/Typeahead'
Expand Down Expand Up @@ -111,4 +111,55 @@ describe('Typeahead', () => {
expect(change).toHaveBeenCalledTimes(1)
expect(change).toHaveBeenLastCalledWith(expectedData)
})

it('renders a non-disabled Typeahead', () => {
const search = jest.fn()
const render = jest.fn()
const change = jest.fn()
const id = 'id'
const searchAccessor = 'search'
const placeholder = 'placeholder'
const minLength = 5

const wrapper = mount(
<Typeahead
placeholder={placeholder}
minLength={minLength}
onSearch={search}
onChange={change}
renderMenuItemChildren={render}
id={id}
searchAccessor={searchAccessor}
/>,
)

const input = wrapper.find(AsyncTypeahead)
expect(input.prop('disabled')).not.toBe(true)
})

it('renders a disabled Typeahead', () => {
const search = jest.fn()
const render = jest.fn()
const change = jest.fn()
const id = 'id'
const searchAccessor = 'search'
const placeholder = 'placeholder'
const minLength = 5

const wrapper = mount(
<Typeahead
placeholder={placeholder}
minLength={minLength}
onSearch={search}
onChange={change}
disabled
renderMenuItemChildren={render}
id={id}
searchAccessor={searchAccessor}
/>,
)

const input = wrapper.find(AsyncTypeahead)
expect(input.prop('disabled')).toBe(true)
})
})

0 comments on commit 85a4f86

Please sign in to comment.