Skip to content

Added showAllCommandsWhenNoneMatches boolean option #1122

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ const commands = [{

* ```open``` a _boolean_, when set to true it forces the command palette to be displayed. Defaults to "false".

* ```showAllCommandsWhenNoneMatches``` a boolean, Set it to true if you'd like to render all suggestions when the input query matches none. Defaults to "true".

* ```alwaysRenderCommands``` a boolean, Set it to true if you'd like to render suggestions even when the input is not focused.

* ```display``` one of "modal" or "inline", when set to "modal" the command palette is rendered centered inside a modal. When set to "inline", it is render inline with other page content. Defaults to "modal".
Expand Down
15 changes: 9 additions & 6 deletions src/command-palette.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,8 @@ class CommandPalette extends React.Component {
}

if (!equal(prevProps.commands, commands)) {
// eslint-disable-next-line react/no-did-update-set-state
this.setState({
suggestions: this.fetchData(),
});
this.fetchData(); // set this.allCommands
this.onSuggestionsFetchRequested({ value: this.state.value }); // updates matching suggestions
}
}

Expand Down Expand Up @@ -160,9 +158,9 @@ class CommandPalette extends React.Component {
// Autosuggest will call this function every time you need to update suggestions.
// You already implemented this logic above, so just use it.
onSuggestionsFetchRequested({ value }) {
const { options, filterSearchQuery } = this.props;
const { options, filterSearchQuery, showAllCommandsWhenNoneMatches } = this.props;
this.setState({
suggestions: getSuggestions(value, this.allCommands, options, filterSearchQuery),
suggestions: getSuggestions(value, this.allCommands, options, filterSearchQuery, showAllCommandsWhenNoneMatches),
});
}

Expand Down Expand Up @@ -367,6 +365,7 @@ class CommandPalette extends React.Component {
}

CommandPalette.defaultProps = {
showAllCommandsWhenNoneMatches: true,
alwaysRenderCommands: true,
placeholder: "Type a command",
hotKeys: "command+shift+p",
Expand All @@ -393,6 +392,10 @@ CommandPalette.defaultProps = {
};

CommandPalette.propTypes = {
/** showAllCommandsWhenNoneMatches a boolean, Set it to true if you'd like to render all suggestions
* the input query matches none. Defaults to "true". */
showAllCommandsWhenNoneMatches: PropTypes.bool,

/** alwaysRenderCommands a boolean, Set it to true if you'd like to render suggestions
* even when the input is not focused. */
alwaysRenderCommands: PropTypes.bool,
Expand Down
12 changes: 8 additions & 4 deletions src/suggestions.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function filterFuzzySortSearch(search, filterSearchQuery) {
}

// Teach Autosuggest how to calculate suggestions for any given input value.
const getSuggestions = function (unfilteredSearch, allCommands, options, filterSearchQuery) {
const getSuggestions = function (unfilteredSearch, allCommands, options, filterSearchQuery, showAllCommandsWhenNoneMatches) {

const search = filterFuzzySortSearch(unfilteredSearch, filterSearchQuery);

Expand All @@ -45,13 +45,17 @@ const getSuggestions = function (unfilteredSearch, allCommands, options, filterS
// allCommands.forEach(s => (s.namePrepared = fuzzysort.prepare(s.name)));
// });

// if the user didnt suggest a specific term or there's a search term
// but no matches were found return all the commands
if (!search) {
return allCommands;
}

// If the user specified an autosuggest term
// search for close matches
const suggestionResults = fuzzysort.go(search, allCommands, options);

// if the user didnt suggest a specific term or there's a search term
// but no matches were found return all the commands
if (!search || !suggestionResults.length) {
if (showAllCommandsWhenNoneMatches && !suggestionResults.length) {
return allCommands;
}

Expand Down