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

UI: Enable search for stories and fix / event listener #14062

Merged
merged 4 commits into from
Feb 26, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
9 changes: 4 additions & 5 deletions lib/api/src/lib/shortcut.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,10 @@ export const shortcutMatchesShortcut = (
inputShortcut: KeyCollection,
shortcut: KeyCollection
): boolean => {
return (
inputShortcut &&
inputShortcut.length === shortcut.length &&
!inputShortcut.find((key, i) => key !== shortcut[i])
);
if (!inputShortcut || !shortcut) return false;
if (inputShortcut.join('') === 'shift/') inputShortcut.shift(); // shift is optional for `/`
if (inputShortcut.length !== shortcut.length) return false;
return !inputShortcut.find((key, i) => key !== shortcut[i]);
};

// Should this keyboard event trigger this keyboard shortcut?
Expand Down
3 changes: 2 additions & 1 deletion lib/api/src/modules/layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,14 @@ export const init: ModuleFn = ({ store, provider }) => {
);
},

focusOnUIElement(elementId?: string) {
focusOnUIElement(elementId?: string, select?: boolean) {
if (!elementId) {
return;
}
const element = document.getElementById(elementId);
if (element) {
element.focus();
if (select) element.select();
}
},

Expand Down
2 changes: 1 addition & 1 deletion lib/api/src/modules/shortcuts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export const init: ModuleFn = ({ store, fullAPI }) => {
}

setTimeout(() => {
fullAPI.focusOnUIElement(focusableUIElements.storySearchField);
fullAPI.focusOnUIElement(focusableUIElements.storySearchField, true);
}, 0);
break;
}
Expand Down
42 changes: 12 additions & 30 deletions lib/ui/src/components/sidebar/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Downshift, { DownshiftState, StateChangeOptions } from 'downshift';
import Fuse, { FuseOptions } from 'fuse.js';
import { document } from 'global';
import { transparentize } from 'polished';
import React, { useEffect, useMemo, useRef, useState, useCallback } from 'react';
import React, { useMemo, useRef, useState, useCallback } from 'react';

import { DEFAULT_REF_ID } from './data';
import {
Expand All @@ -21,7 +21,6 @@ import {
isCloseType,
} from './types';
import { searchItem } from './utils';
import { matchesKeyCode, matchesModifiers } from '../../keybinding';

const DEFAULT_MAX_SEARCH_RESULTS = 50;

Expand Down Expand Up @@ -173,26 +172,6 @@ export const Search = React.memo<{
[api, inputRef, showAllComponents, DEFAULT_REF_ID]
);

useEffect(() => {
const focusSearch = (event: KeyboardEvent) => {
if (!enableShortcuts || isLoading || event.repeat) return;
if (!inputRef.current || inputRef.current === document.activeElement) return;
if (
// Shift is required to type `/` on some keyboard layouts
matchesModifiers({ ctrl: false, alt: false, meta: false }, event) &&
matchesKeyCode('Slash', event)
) {
inputRef.current.focus();
inputRef.current.select();
event.preventDefault();
}
};

// Keyup prevents slashes from ending up in the input field when held down
document.addEventListener('keyup', focusSearch);
return () => document.removeEventListener('keyup', focusSearch);
}, [inputRef, isLoading, enableShortcuts]);

const list: SearchItem[] = useMemo(() => {
return dataset.entries.reduce((acc: SearchItem[], [refId, { stories }]) => {
if (stories) {
Expand All @@ -209,17 +188,20 @@ export const Search = React.memo<{
if (!input) return [];

let results: DownshiftItem[] = [];
const componentResults = (fuse.search(input) as SearchResult[]).filter(
({ item }) => item.isComponent
);
const resultIds: string[] = [];
const distinctResults = (fuse.search(input) as SearchResult[]).filter(({ item }) => {
if (!(item.isComponent || item.isLeaf) || resultIds.includes(item.parent)) return false;
resultIds.push(item.id);
return true;
});

if (componentResults.length) {
results = componentResults.slice(0, allComponents ? 1000 : DEFAULT_MAX_SEARCH_RESULTS);
if (componentResults.length > DEFAULT_MAX_SEARCH_RESULTS && !allComponents) {
if (distinctResults.length) {
results = distinctResults.slice(0, allComponents ? 1000 : DEFAULT_MAX_SEARCH_RESULTS);
if (distinctResults.length > DEFAULT_MAX_SEARCH_RESULTS && !allComponents) {
results.push({
showAll: () => showAllComponents(true),
totalCount: componentResults.length,
moreCount: componentResults.length - DEFAULT_MAX_SEARCH_RESULTS,
totalCount: distinctResults.length,
moreCount: distinctResults.length - DEFAULT_MAX_SEARCH_RESULTS,
});
}
}
Expand Down