Skip to content

Commit

Permalink
Merge pull request #56 from ritz078/patrickW/feature--add-default-val…
Browse files Browse the repository at this point in the history
…ue-prop

[FEATURE] Add `isDropdown` and `inputProps` options
  • Loading branch information
ritz078 authored Oct 19, 2021
2 parents 83561e0 + 6511dd0 commit 689f4d8
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,10 @@ attribute|default|description
---------|-------|-----------
caseSensitive|false|Indicates whether comparisons should be case sensitive.
className|null|give a custom class name to the root element
inputProps|{}|Props passed directly to the input element. i.e. `defaultValue`, `onChange`, etc.
inputStyle|{}|Styles passed directly to the `input` element.
inputWrapperStyle|{}|Styles passed directly to the `input` wrapper `div`.
isDropdown|false|Hide the result list on blur.
listItemStyle|{}|Styles passed to each item in the dropdown list.
listWrapperStyle|{}|Styles passed directly to the dropdown wrapper.
selectedListItemStyle|{}|Styles passed directly to current 'active' item.
Expand Down
25 changes: 22 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ export default class FuzzySearch extends Component {
distance: PropTypes.number,
id: PropTypes.string,
include: PropTypes.array,
inputProps: PropTypes.object,
isDropdown: PropTypes.bool,
maxPatternLength: PropTypes.number,
onSelect: PropTypes.func.isRequired,
width: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
Expand Down Expand Up @@ -99,6 +101,8 @@ export default class FuzzySearch extends Component {
caseSensitive: false,
distance: 100,
include: [],
inputProps: {},
isDropdown: false,
keyForDisplayName: 'title',
location: 0,
width: 430,
Expand Down Expand Up @@ -127,7 +131,7 @@ export default class FuzzySearch extends Component {
isOpen: !this.props.shouldShowDropdownAtStart,
results: [],
selectedIndex: 0,
value: '',
value: props.inputProps.defaultValue || '',
};
this.handleChange = this.handleChange.bind(this);
this.handleKeyDown = this.handleKeyDown.bind(this);
Expand Down Expand Up @@ -173,9 +177,16 @@ export default class FuzzySearch extends Component {
}

handleChange(e) {
e.persist();

if (this.props.inputProps.onChange) {
this.props.inputProps.onChange(e);
}

const shouldDisplayAllListItems = this.props.shouldShowDropdownAtStart && !e.target.value;

this.setState({
isOpen: true,
results: shouldDisplayAllListItems
? this.props.list
: this.fuse.search(e.target.value).slice(0, this.props.maxResults - 1),
Expand Down Expand Up @@ -231,6 +242,8 @@ export default class FuzzySearch extends Component {
const {
autoFocus,
className,
inputProps,
isDropdown,
list,
placeholder,
resultsTemplate,
Expand All @@ -253,7 +266,8 @@ export default class FuzzySearch extends Component {
onBlur={(e) => {
if (this.dropdownRef.contains(e.relatedTarget)) return;

if (shouldShowDropdownAtStart) {
// Check shouldShowDropdownAtStart for backwards-compatibility.
if (isDropdown || shouldShowDropdownAtStart) {
this.setState({
isOpen: false,
});
Expand All @@ -263,19 +277,24 @@ export default class FuzzySearch extends Component {
>
<div style={{...styles.searchBoxWrapper, ...this.props.inputWrapperStyle}}>
<input
{...inputProps}
autoFocus={autoFocus}
onChange={this.handleChange}
placeholder={placeholder}
style={{...styles.searchBoxStyle, ...this.props.inputStyle}}
type="text"
value={this.state.value}
onFocus={() => {
onFocus={(e) => {
if (shouldShowDropdownAtStart) {
this.setState({
isOpen: true,
results: this.state.value ? this.state.results : list,
});
}

if(inputProps.onFocus) {
inputProps.onFocus(e)
}
}}
/>
</div>
Expand Down
9 changes: 9 additions & 0 deletions src/stories/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ storiesOf('SearchBox', module)
.add('Basic', () => (
<FuzzySearch list={list} keys={['author', 'title']} width={430} onSelect={action('selected')} />
))
.add('Dropdown behavior', () => (
<FuzzySearch
isDropdown
list={list}
keys={['author', 'title']}
width={430}
onSelect={action('selected')}
/>
))
.add('Custom Styles', () => (
<FuzzySearch
list={list}
Expand Down
29 changes: 29 additions & 0 deletions src/tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,33 @@ describe('<FuzzySearch />', () => {

expect(wrapper.state('results').length).to.not.equal(0);
});

it('with inputProps provided', () => {
const onChange = sinon.spy();
const wrapper = mount(
<FuzzySearch
list={list}
inputProps={{
defaultValue: 'Hello there!',
onChange,
}}
onSelect={onChange}
keys={['author', 'title']}
options={{ includeMatches: true }}
defaultValue="Hello there!"
/>,
);

const input = wrapper.find('input');

expect(input.get(0).value).to.equal('Hello there!');

input.simulate('change', {
target: {
value: 't',
},
});

expect(onChange.calledOnce).to.equal(true);
});
});

0 comments on commit 689f4d8

Please sign in to comment.