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
2 changes: 1 addition & 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 Down
146 changes: 146 additions & 0 deletions semcore/select/src/components/AutoSuggest/AutoSuggest.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
import type { Intergalactic } from '@semcore/core';
import { Component, createComponent, Root } from '@semcore/core';
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';

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

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 = 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 } = this.asProps;
const { isVisible, highlightedIndex, suggestions, isLoading } = this.state;

return (
<Select
interaction='none'
visible={isVisible}
onVisibleChange={this.handleChangeVisible}
highlightedIndex={highlightedIndex}
onHighlightedIndexChange={this.handleChangeHighlightedIndex}
defaultHighlightedIndex={null}
>
<Select.Trigger tag={Input} onFocus={this.handleFocus} onBlur={this.handleBlur}>
<Root
render={Input.Value}
value={value}
role='combobox'
placeholder='Start typing for options'
onChange={this.handleChange}
onKeyDown={this.handleKeyDown}
autoComplete='off'
/>
{isLoading && (
<Input.Addon tag={Spin} size='l' />
)}
</Select.Trigger>
{suggestions.length > 0 && (
<Select.Menu>
{suggestions.map((option) => (
<Select.Option value={option} key={option} selected={false} onClick={() => this.handleChangeSelect(option)}>
<Highlight highlight={value}>{option}</Highlight>
</Select.Option>
))}
</Select.Menu>
)}
</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: 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 @@ -16,6 +16,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 @@ -170,5 +172,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';
Loading
Loading