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

feat: supported initial search in settings #112

Merged
merged 2 commits into from
Sep 27, 2023
Merged
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
14 changes: 11 additions & 3 deletions src/components/Settings/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
filterPlaceholder?: string;
emptyPlaceholder?: string;
initialPage?: string;
initialSearch?: string;
onPageChange?: (page: string | undefined) => void;
renderNotFound?: () => React.ReactNode;
renderLoading?: () => React.ReactNode;
Expand Down Expand Up @@ -56,6 +57,7 @@

export interface SettingsItemProps {
title: string;
highlightedTitle?: React.ReactNode | null;
renderTitleComponent?: (highlightedTitle: React.ReactNode | null) => React.ReactNode;
align?: 'top' | 'center';
children: React.ReactNode;
Expand Down Expand Up @@ -115,6 +117,7 @@
type SettingsContentProps = Omit<SettingsProps, 'loading' | 'renderLoading'>;
function SettingsContent({
initialPage,
initialSearch,
children,
renderNotFound,
title = i18n('label_title'),
Expand All @@ -124,7 +127,7 @@
onPageChange,
onClose,
}: SettingsContentProps) {
const [search, setSearch] = React.useState('');
const [search, setSearch] = React.useState(initialSearch ?? '');
const {menu, pages} = getSettingsFromChildren(children, search);
const pageKeys = Object.keys(pages);
const [selectedPage, setCurrentPage] = React.useState<string | undefined>(
Expand Down Expand Up @@ -201,12 +204,12 @@
section.header
))}

{section.items.map(({hidden, title, element}) =>

Check warning on line 207 in src/components/Settings/Settings.tsx

View workflow job for this annotation

GitHub Actions / Verify Files

'title' is already declared in the upper scope
hidden ? null : (
<div key={title} className={b('section-item')}>
{React.cloneElement(element, {
...element.props,
title:
highlightedTitle:
search && title
? prepareTitle(title, search)
: title,
Expand All @@ -228,6 +231,7 @@
<SettingsSearch
inputRef={searchInputRef}
className={b('search')}
initialValue={initialSearch}
onChange={setSearch}
autoFocus={false}
inputSize={'l'}
Expand Down Expand Up @@ -259,6 +263,7 @@
<SettingsSearch
inputRef={searchInputRef}
className={b('search')}
initialValue={initialSearch}
onChange={setSearch}
placeholder={filterPlaceholder}
autoFocus
Expand Down Expand Up @@ -290,6 +295,7 @@

Settings.Item = function SettingsItem({
title,
highlightedTitle,
children,
align = 'center',
withBadge,
Expand All @@ -299,7 +305,9 @@
}: SettingsItemProps) {
const {renderRightAdornment, showRightAdornmentOnHover} = useSettingsContext();
const titleNode = (
<span className={b('item-title', {badge: withBadge})}>{renderTitleComponent(title)}</span>
<span className={b('item-title', {badge: withBadge})}>
{renderTitleComponent(highlightedTitle)}
</span>
);
return (
<div className={b('item', {align, mode})}>
Expand Down
13 changes: 11 additions & 2 deletions src/components/Settings/SettingsSearch/SettingsSearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const b = block('settings-search');

interface SettingsSearchProps {
className?: string;
initialValue?: string;
onChange: (search: string) => void;
debounce?: number;
inputRef?: React.Ref<HTMLInputElement>;
Expand All @@ -20,18 +21,26 @@ interface SettingsSearchProps {

export function SettingsSearch({
className,
initialValue,
onChange,
debounce = 200,
inputRef,
inputSize,
placeholder,
autoFocus = true,
}: SettingsSearchProps) {
const onChangeStable = useStableCallback(onChange);
const handleUpdate = React.useCallback(debounceFn(onChangeStable, debounce), [debounce]);
const [value, setValue] = React.useState(initialValue ?? '');

const onChangeDebounced = useStableCallback(debounceFn(onChange, debounce));
const handleUpdate = useStableCallback((updated: string) => {
setValue(updated);
onChangeDebounced(updated);
});

return (
<div className={b(null, className)}>
<TextInput
value={value}
controlRef={inputRef}
hasClear
autoFocus={autoFocus}
Expand Down
2 changes: 1 addition & 1 deletion src/components/Settings/__stories__/SettingsDemo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export const SettingsComponent = React.memo(
</Settings.Item>
<Settings.Item title="Code theme">
<Select
value={settings.codeTheme ?? 'default'}
value={settings.codeTheme ?? ['default']}
options={[{value: 'default', content: 'Default'}]}
onUpdate={(val) => {
handleChange(
Expand Down
Loading