-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from ritz078/feat/fuzzy-search
Added fuzzy search
- Loading branch information
Showing
8 changed files
with
142 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import React from 'react'; | ||
import FuzzySearch from 'react-fuzzy'; | ||
|
||
import { features } from '../../../libs/key_events'; | ||
import { baseFonts } from './theme'; | ||
|
||
const searchBoxStyle = { | ||
position: 'absolute', | ||
backgroundColor: '#FFF', | ||
top: '100px', | ||
left: '50%', | ||
marginLeft: '-215px', | ||
...baseFonts | ||
}; | ||
|
||
const formatStories = function (stories) { | ||
const formattedStories = []; | ||
let i = 0; | ||
stories.forEach((val) => { | ||
formattedStories.push({ | ||
type: 'kind', | ||
value: val.kind, | ||
id: i++, | ||
}); | ||
|
||
val.stories.forEach((story) => { | ||
formattedStories.push({ | ||
type: 'story', | ||
value: story, | ||
id: i++, | ||
kind: val.kind, | ||
}); | ||
}); | ||
}); | ||
|
||
return formattedStories; | ||
}; | ||
|
||
const suggestionTemplate = function (props, state, styles) { | ||
return state.results.map((val, i) => { | ||
const style = state.selectedIndex === i ? styles.selectedResultStyle : styles.resultsStyle; | ||
return ( | ||
<div key={i} style={style}> | ||
{val.value} | ||
<span style={{ float: 'right', opacity: 0.5 }}> | ||
{val.type === 'story' ? `in ${val.kind}` : 'Kind'} | ||
</span> | ||
</div> | ||
); | ||
}); | ||
}; | ||
|
||
export default class SearchBox extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
|
||
this.onSelect = this.onSelect.bind(this); | ||
this.fireOnStory = this.fireOnStory.bind(this); | ||
this.fireOnKind = this.fireOnKind.bind(this); | ||
} | ||
|
||
onSelect(selected) { | ||
const { handleEvent } = this.props; | ||
if (selected.type === 'story') this.fireOnStory(selected.value, selected.kind); | ||
else this.fireOnKind(selected.value); | ||
handleEvent(features.SEARCH); | ||
} | ||
|
||
fireOnKind(kind) { | ||
const { onSelectStory } = this.props; | ||
if (onSelectStory) onSelectStory(kind, null); | ||
} | ||
|
||
fireOnStory(story, kind) { | ||
const { onSelectStory } = this.props; | ||
if (onSelectStory) onSelectStory(kind, story); | ||
} | ||
|
||
render() { | ||
return ( | ||
<div style={searchBoxStyle}> | ||
{this.props.showSearchBox && <FuzzySearch | ||
list={formatStories(this.props.stories)} | ||
onSelect={this.onSelect} | ||
keys={['value', 'type']} | ||
resultsTemplate={suggestionTemplate} | ||
autoFocus | ||
/>} | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
SearchBox.propTypes = { | ||
showSearchBox: React.PropTypes.bool.isRequired, | ||
stories: React.PropTypes.arrayOf(React.PropTypes.object).isRequired, | ||
onSelectStory: React.PropTypes.func.isRequired, | ||
handleEvent: React.PropTypes.func.isRequired, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import SearchBox from '../components/search_box'; | ||
import { useDeps, composeAll } from 'mantra-core'; | ||
import reduxComposer from '../libs/redux_composer'; | ||
|
||
export const composer = ({ api, shortcuts }, { actions }) => { | ||
const actionMap = actions(); | ||
return { | ||
showSearchBox: shortcuts.showSearchBox, | ||
stories: api.stories, | ||
onSelectStory: actionMap.api.selectStory, | ||
handleEvent: actionMap.shortcuts.handleEvent, | ||
}; | ||
}; | ||
|
||
export default composeAll( | ||
reduxComposer(composer), | ||
useDeps() | ||
)(SearchBox); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters