Default commands option

This commit is contained in:
Cássio Zen 2016-07-08 08:58:08 -03:00
parent 6806154369
commit eee6340879
4 changed files with 38 additions and 13 deletions

View File

@ -6,7 +6,6 @@ export const LIST_POSTS = 'LIST_POSTS';
export const LIST_FAQ = 'LIST_FAQ'; export const LIST_FAQ = 'LIST_FAQ';
export const HELP = 'HELP'; export const HELP = 'HELP';
export function run(commandName, payload) { export function run(commandName, payload) {
return { type: RUN_COMMAND, command: commandName, payload }; return { type: RUN_COMMAND, command: commandName, payload };
} }

View File

@ -3,7 +3,7 @@ import { connect } from 'react-redux';
import { loadConfig } from '../actions/config'; import { loadConfig } from '../actions/config';
import { loginUser } from '../actions/auth'; import { loginUser } from '../actions/auth';
import { currentBackend } from '../backends/backend'; import { currentBackend } from '../backends/backend';
import { LIST_POSTS, LIST_FAQ, HELP } from '../actions/findbar'; import { LIST_POSTS, LIST_FAQ, HELP, MORE_COMMANDS } from '../actions/findbar';
import FindBar from './FindBar'; import FindBar from './FindBar';
class App extends React.Component { class App extends React.Component {

View File

@ -121,6 +121,8 @@ class FindBar extends Component {
value: '', value: '',
placeholder: PLACEHOLDER, placeholder: PLACEHOLDER,
activeScope: null activeScope: null
}, () => {
this._input.blur();
}); });
const payload = paramName ? { [paramName]: enteredParamValue } : null; const payload = paramName ? { [paramName]: enteredParamValue } : null;
this.props.dispatch(runCommand(command.id, payload)); this.props.dispatch(runCommand(command.id, payload));
@ -137,11 +139,18 @@ class FindBar extends Component {
} }
getSuggestions() { getSuggestions() {
return this._getSuggestions(this.state.value, this.state.activeScope, this._compiledCommands); return this._getSuggestions(this.state.value, this.state.activeScope, this._compiledCommands, this.props.defaultCommands);
} }
// Memoized version // Memoized version
_getSuggestions(value, scope, commands) { _getSuggestions(value, scope, commands, defaultCommands) {
if (scope) return []; // No autocomplete for scoped input if (scope) return []; // No autocomplete for scoped input
if (value.length === 0 && defaultCommands) {
return commands
.filter(command => defaultCommands.indexOf(command.id) !== -1)
.map(result => (
Object.assign({}, result, { string: result.token }
)));
}
const results = fuzzy.filter(value, commands, { const results = fuzzy.filter(value, commands, {
pre: '<strong>', pre: '<strong>',
@ -205,9 +214,12 @@ class FindBar extends Component {
break; break;
case 'Escape': case 'Escape':
this.setState({ this.setState({
value: '',
highlightedIndex: 0, highlightedIndex: 0,
isOpen: false isOpen: false,
}, this.maybeRemoveActiveScope); activeScope: null,
placeholder: PLACEHOLDER
});
break; break;
case 'Backspace': case 'Backspace':
this.setState({ this.setState({
@ -348,6 +360,7 @@ FindBar.propTypes = {
id: PropTypes.string.isRequired, id: PropTypes.string.isRequired,
pattern: PropTypes.string.isRequired pattern: PropTypes.string.isRequired
})).isRequired, })).isRequired,
defaultCommands: PropTypes.arrayOf(PropTypes.string),
dispatch: PropTypes.func.isRequired, dispatch: PropTypes.func.isRequired,
}; };

View File

@ -3,14 +3,26 @@ import { storiesOf, action } from '@kadira/storybook';
import { FindBar } from '../FindBar'; import { FindBar } from '../FindBar';
const CREATE_COLLECTION = 'CREATE_COLLECTION';
const CREATE_POST = 'CREATE_POST';
const CREATE_ARTICLE = 'CREATE_ARTICLE';
const CREATE_FAQ = 'CREATE_FAQ';
const ADD_NEWS = 'ADD_NEWS';
const ADD_USER = 'ADD_USER';
const OPEN_SETTINGS = 'OPEN_SETTINGS';
const HELP = 'HELP';
const MORE_COMMANDS = 'MORE_COMMANDS';
const commands = [ const commands = [
{ id:'CREATE_COLLECTION', pattern: 'Create new Collection(:collectionName)' }, { id: CREATE_COLLECTION, pattern: 'Create new Collection(:collectionName)' },
{ id:'CREATE_POST', pattern: 'Create new Post(:postName)' }, { id: CREATE_POST, pattern: 'Create new Post(:postName)' },
{ id:'CREATE_ARTICLE', pattern: 'Create new Article(:articleName)' }, { id: CREATE_ARTICLE, pattern: 'Create new Article(:articleName)' },
{ id:'CREATE_FAQ', pattern: 'Create new FAQ item(:faqName as FAQ item name)' }, { id: CREATE_FAQ, pattern: 'Create new FAQ item(:faqName as FAQ item name)' },
{ id:'ADD_NEWS', pattern: 'Add news item(:headline)' }, { id: ADD_NEWS, pattern: 'Add news item(:headline)' },
{ id:'ADD_USER', pattern: 'Add new User(:userName as User name)' }, { id: ADD_USER, pattern: 'Add new User(:userName as User name)' },
{ id:'OPEN_SETTINGS', pattern: 'Go to Settings' }, { id: OPEN_SETTINGS, pattern: 'Go to Settings' },
{ id: HELP, pattern: 'Help' },
{ id: MORE_COMMANDS, pattern: 'More Commands...' },
]; ];
const style = { const style = {
@ -25,6 +37,7 @@ storiesOf('FindBar', module)
<div style={style}> <div style={style}>
<FindBar <FindBar
commands={commands} commands={commands}
defaultCommands={[CREATE_POST, CREATE_COLLECTION, OPEN_SETTINGS, HELP, MORE_COMMANDS]}
dispatch={f => f(dispatch)} dispatch={f => f(dispatch)}
/> />
</div> </div>