Skip to content

Commit

Permalink
Search box: make found options selectable with click
Browse files Browse the repository at this point in the history
  • Loading branch information
Hypnosphi authored and danielduan committed Aug 27, 2017
1 parent e877afc commit 1ac70f8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
1 change: 1 addition & 0 deletions lib/ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"podda": "^1.2.2",
"prop-types": "^15.5.10",
"qs": "^6.4.0",
"react-fuzzy": "^0.4.1",
"react-icons": "^2.2.5",
"react-inspector": "^2.1.1",
"react-komposer": "^2.0.0",
Expand Down
13 changes: 10 additions & 3 deletions lib/ui/src/modules/ui/components/search_box.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { document } from 'global';
import PropTypes from 'prop-types';
import React from 'react';
import ReactModal from 'react-modal';
import FuzzySearch from '@storybook/react-fuzzy';
import FuzzySearch from 'react-fuzzy';

import { baseFonts } from '@storybook/components';

Expand Down Expand Up @@ -48,11 +48,18 @@ const formatStories = stories => {
return formattedStories;
};

const suggestionTemplate = (props, state, styles) =>
const suggestionTemplate = (props, state, styles, clickHandler) =>
state.results.map((val, i) => {
const style = state.selectedIndex === i ? styles.selectedResultStyle : styles.resultsStyle;
return (
<div key={val.value} style={style}>
// eslint-disable-next-line jsx-a11y/interactive-supports-focus
<div
role="option"
aria-selected={state.selectedIndex === i}
key={val.value}
style={style}
onClick={() => clickHandler(i)}
>
{val.value}
<span style={{ float: 'right', opacity: 0.5 }}>
{val.type === 'story' ? `in ${val.kind}` : 'Kind'}
Expand Down
14 changes: 13 additions & 1 deletion lib/ui/src/modules/ui/components/search_box.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { shallow } from 'enzyme';
import { shallow, mount } from 'enzyme';
import React from 'react';
import ReactModal from 'react-modal';
import FuzzySearch from '@storybook/react-fuzzy';
Expand Down Expand Up @@ -104,5 +104,17 @@ describe('manager.ui.components.search_box', () => {
expect(onSelectStory).toHaveBeenCalledWith('b', 'a');
expect(onClose).toHaveBeenCalled();
});

test('should handle selecting a story with click', () => {
const onSelectStory = jest.fn();
const onClose = jest.fn();
const wrap = mount(<SearchBox onSelectStory={onSelectStory} onClose={onClose} />);

const option = wrap.findWhere(el => el.key() === 'a');
option.simulate('click');

expect(onSelectStory).toHaveBeenCalledWith('b', 'a');
expect(onClose).toHaveBeenCalled();
});
});
});

0 comments on commit 1ac70f8

Please sign in to comment.