Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search box: make found options selectable with click #1697

Merged
merged 10 commits into from
Aug 25, 2017
2 changes: 1 addition & 1 deletion lib/ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
"dependencies": {
"@hypnosphi/fuse.js": "^3.0.9",
"@storybook/components": "^3.2.7",
"@storybook/react-fuzzy": "^0.4.0",
"babel-runtime": "^6.23.0",
"deep-equal": "^1.0.1",
"events": "^1.1.1",
Expand All @@ -30,6 +29,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
27 changes: 26 additions & 1 deletion lib/ui/src/modules/ui/components/search_box.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { shallow } from 'enzyme';
import React from 'react';
import ReactModal from 'react-modal';
import FuzzySearch from '@storybook/react-fuzzy';
import FuzzySearch from 'react-fuzzy';

import SearchBox from './search_box';

Expand Down Expand Up @@ -95,5 +95,30 @@ describe('manager.ui.components.search_box', () => {
expect(onSelectStory).toHaveBeenCalledWith('b', 'a');
expect(onClose).toHaveBeenCalled();
});

test('should handle selecting a story with click', () => {
const stories = [
{
kind: 'a',
stories: ['b', 'c'],
},
];
const onSelectStory = jest.fn();
const onClose = jest.fn();
const wrap = shallow(
<SearchBox onSelectStory={onSelectStory} onClose={onClose} stories={stories} />
);

const modal = wrap.find(FuzzySearch).dive();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, haven't seen this before, TIL

modal.find('input').simulate('change', {
target: { value: 'b' },
});

const option = modal.findWhere(el => el.key() === 'b');
option.simulate('click');

expect(onSelectStory).toHaveBeenCalledWith('a', 'b');
expect(onClose).toHaveBeenCalled();
});
});
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"husky": "^0.14.3",
"jest": "^20.0.4",
"jest-enzyme": "^3.6.1",
"lerna": "2.0.0",
"lerna": "2.1.0",
"lint-staged": "^4.0.2",
"lodash": "^4.17.4",
"nodemon": "^1.11.0",
Expand Down