Skip to content
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
5 changes: 4 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions semcore/dropdown/src/AbstractDropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -312,10 +312,10 @@ export abstract class AbstractDropdown extends Component<AbstractDDProps, typeof
}

componentDidUpdate(prevProps: AbstractDDProps) {
const { visible } = this.asProps;
const { visible, interaction } = this.asProps;
const visibilityChanged = visible !== prevProps.visible;

if (visibilityChanged && !visible) {
if (visibilityChanged && !visible && interaction !== 'none') {
this.handlers.highlightedIndex(this.props.defaultHighlightedIndex);
this.prevHighlightedIndex = null;
this.highlightedItem = null;
Expand Down
2 changes: 1 addition & 1 deletion semcore/icon/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4414,4 +4414,4 @@
"require": "./lib/ZoomPlus/m/index.js"
}
}
}
}
3 changes: 2 additions & 1 deletion semcore/select/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"author": "UI-kit team <ui-kit-team@semrush.com>",
"license": "MIT",
"scripts": {
"build": "pnpm semcore-builder --source=js && pnpm vite build"
"build": "pnpm semcore-builder --source=js,ts && pnpm vite build"
},
"exports": {
"types": "./lib/types/index.d.ts",
Expand All @@ -23,6 +23,7 @@
"@semcore/dropdown": "^17.2.0",
"@semcore/dropdown-menu": "^17.2.0",
"@semcore/input": "^17.2.0",
"@semcore/spin": "^17.2.0",
"@semcore/typography": "^17.2.0",
"classnames": "2.2.6"
},
Expand Down
163 changes: 163 additions & 0 deletions semcore/select/src/components/AutoSuggest/AutoSuggest.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
import type { Intergalactic } from '@semcore/core';
import { Component, createComponent, Root } from '@semcore/core';
import i18nEnhance from '@semcore/core/lib/utils/enhances/i18nEnhance';
import uniqueIDEnhancement from '@semcore/core/lib/utils/uniqueID';
import Input from '@semcore/input';
import Spin from '@semcore/spin';
import React from 'react';

import type { NSAutoSuggest } from './AutoSuggest.type';
import { Highlight } from './Highlight';
import Select from '../../index';
import { localizedMessages } from '../../translations/__intergalactic-dynamic-locales';

class AutoSuggestRoot extends Component<
Intergalactic.InternalTypings.InferComponentProps<NSAutoSuggest.Component>,
typeof AutoSuggestRoot.enhance,
{ value: (value: string) => string },
{},
NSAutoSuggest.State,
NSAutoSuggest.DefaultProps
> {
static defaultProps: NSAutoSuggest.DefaultProps = {
defaultValue: '',
};

static enhance = [uniqueIDEnhancement(), i18nEnhance(localizedMessages)] as const;

private abortController: AbortController | undefined;
private changeDebounce = 0;

state: NSAutoSuggest.State = {
isVisible: false,
highlightedIndex: -1,
suggestions: [],
openOnChanges: true,
isLoading: false,
};

protected uncontrolledProps() {
return {
value: (value: string) => {
return value;
},
};
}

handleChange = (value: string) => {
if (this.changeDebounce) {
clearTimeout(this.changeDebounce);
}
if (this.abortController) {
this.abortController.abort();
}

if (value !== this.asProps.value && this.state.openOnChanges) {
const { suggestions } = this.asProps;

if (!Array.isArray(suggestions)) {
this.setState({ isLoading: true });
}

this.changeDebounce = window.setTimeout(async () => {
this.handleChangeVisible(true);

if (Array.isArray(suggestions)) {
const filteredSuggestions = value === '' ? [] : suggestions.filter((breed) => breed.toLowerCase().includes(value.toLowerCase()));

this.setState({ suggestions: filteredSuggestions });
} else {
this.abortController = new AbortController();
const abortSignal = this.abortController.signal;

const filteredSuggestions = await suggestions(value, abortSignal);
this.setState({ suggestions: filteredSuggestions, isLoading: false });
}
}, 300);
}
};

handleChangeVisible = (isVisible: boolean) => {
this.setState({ isVisible });
};

handleChangeHighlightedIndex = (index: number | null) => {
this.setState({ highlightedIndex: index ?? -1 });
};

handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
if (!e.key.startsWith('Array')) {
this.setState({ highlightedIndex: -1 });
}
if (e.key === 'Escape' && this.state.isVisible) {
this.setState({ openOnChanges: false });
}
};

handleChangeSelect = (value: string) => {
this.handlers.value(value);
};

handleFocus = () => {
const { value } = this.asProps;
this.setState({ openOnChanges: true, isVisible: value === '' });
};

handleBlur = () => {
this.handleChangeVisible(false);
};

render() {
const { value, uid, getI18nText } = this.asProps;
const { isVisible, highlightedIndex, suggestions, isLoading } = this.state;
const id = `${uid}_autosuggest-trigger`;

const isVisiblePopper = isVisible && (value === '' || suggestions.length > 0 || isLoading);

return (
<Select
interaction='none'
visible={isVisiblePopper}
onVisibleChange={this.handleChangeVisible}
highlightedIndex={highlightedIndex}
onHighlightedIndexChange={this.handleChangeHighlightedIndex}
defaultHighlightedIndex={null}
>
<Select.Trigger id={id} tag={Input} onFocus={this.handleFocus} onBlur={this.handleBlur}>
<Root
render={Input.Value}
value={value}
role='combobox'
onChange={this.handleChange}
onKeyDown={this.handleKeyDown}
autoComplete='off'
/>
{isLoading && (
<Input.Addon tag={Spin} size='l' />
)}
</Select.Trigger>
<Select.Popper aria-labelledby={id}>
{isLoading
? (<Select.StatusItem state='loading' />)
: (
<>
{suggestions.length === 0
? (value.length === 0 && <Select.StatusItem itemsCount={0}>{getI18nText('AutoSuggest.Popper.placeholderText')}</Select.StatusItem>)
: (
<Select.List>
{suggestions.map((option) => (
<Select.Option value={option} key={option} selected={false} onClick={() => this.handleChangeSelect(option)}>
<Highlight highlight={value}>{option}</Highlight>
</Select.Option>
))}
</Select.List>
)}
</>
)}
</Select.Popper>
</Select>
);
}
}

export const AutoSuggest = createComponent<NSAutoSuggest.Component, typeof AutoSuggestRoot>(AutoSuggestRoot);
29 changes: 29 additions & 0 deletions semcore/select/src/components/AutoSuggest/AutoSuggest.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import type { Intergalactic } from '@semcore/core';

declare namespace NSAutoSuggest {
type Suggestion = string;

type Props = {
value?: string;
onChange?: (value: string) => void;
suggestions: Suggestion[] | ((value: string, signal: AbortSignal) => Promise<Suggestion[]>);
};

type State = {
isVisible: boolean;
highlightedIndex: number;
suggestions: Suggestion[];
openOnChanges: boolean;
isLoading: boolean;
};

type DefaultProps = {
defaultValue: string;
};

type Component = Intergalactic.Component<'input', Props>;
}

export {
NSAutoSuggest,
};
20 changes: 20 additions & 0 deletions semcore/select/src/components/AutoSuggest/Highlight.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import React from 'react';

type HighlightProps = {
highlight: string;
children: string;
};

export function Highlight({ highlight, children }: HighlightProps) {
let html = children;
if (highlight) {
try {
const re = new RegExp(highlight.toLowerCase(), 'g');
html = html.replace(
re,
`<span style="font-weight: bold; padding: var(--intergalactic-spacing-05x, 2px) 0">${highlight}</span>`,
);
} catch (e) {}
}
return <span dangerouslySetInnerHTML={{ __html: html }} />;
}
6 changes: 5 additions & 1 deletion semcore/select/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import type Input from '@semcore/input';
import type { Text } from '@semcore/typography';
import type React from 'react';

import { NSAutoSuggest } from './components/AutoSuggest/AutoSuggest.type.ts';

export type SelectInputSearch = InputValueProps & {};

export type OptionValue = string | number;
Expand Down Expand Up @@ -172,5 +174,7 @@ declare const wrapSelect: <PropsExtending extends {}>(
) => React.ReactNode,
) => IntergalacticSelectComponent<PropsExtending>;

export { InputSearch, wrapSelect };
declare const AutoSuggest = NSAutoSuggest.Component;

export { InputSearch, wrapSelect, AutoSuggest };
export default Select;
1 change: 1 addition & 0 deletions semcore/select/src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { default as InputSearch } from './InputSearch';
export { default } from './Select';
export * from './Select';
export { AutoSuggest } from './components/AutoSuggest/AutoSuggest';
3 changes: 2 additions & 1 deletion semcore/select/src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"clearSearch": "Clear search field",
"selectPlaceholder": "Select option",
"Select.InputSearch.Value:placeholder": "Search",
"Select.InputSearch.Value:aria-label": "Search"
"Select.InputSearch.Value:aria-label": "Search",
"AutoSuggest.Popper.placeholderText": "Start typing to see options"
}
Loading
Loading