Skip to content

Commit

Permalink
feat: enhance AutocompleteInput with autoFocus
Browse files Browse the repository at this point in the history
  • Loading branch information
BenElferink committed Dec 20, 2024
1 parent 6f71ec4 commit 81a8131
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export const ActionModal: React.FC<Props> = ({ isOpen, onClose }) => {
>
<ModalBody>
<SectionTitle title='Select Action' description='Select an action to modify telemetry data before it`s sent to destinations. Choose an action type and configure its details.' />
<AutocompleteInput options={ACTION_OPTIONS} selectedOption={selectedItem} onOptionSelect={handleSelect} style={{ marginTop: '24px' }} />
<AutocompleteInput options={ACTION_OPTIONS} selectedOption={selectedItem} onOptionSelect={handleSelect} style={{ marginTop: '24px' }} autoFocus />

{!!selectedItem?.type ? (
<div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState, type ChangeEvent, type KeyboardEvent, type FC } from 'react';
import React, { useState, type ChangeEvent, type KeyboardEvent, type FC, type InputHTMLAttributes } from 'react';
import { SVG } from '@/assets';
import { Text } from '../text';
import styled from 'styled-components';
Expand All @@ -11,7 +11,7 @@ export interface Option {
items?: Option[]; // For handling a list of items
}

interface Props {
interface Props extends InputHTMLAttributes<HTMLInputElement> {
options: Option[];
placeholder?: string;
selectedOption?: Option;
Expand All @@ -34,7 +34,7 @@ const filterOptions = (optionsList: Option[], input: string): Option[] => {
}, []);
};

export const AutocompleteInput: FC<Props> = ({ placeholder = 'Type to search...', options, selectedOption, onOptionSelect, style, disabled }) => {
export const AutocompleteInput: FC<Props> = ({ placeholder = 'Type to search...', options, selectedOption, onOptionSelect, style, disabled, ...props }) => {
const [query, setQuery] = useState(selectedOption?.label || '');
const [filteredOptions, setFilteredOptions] = useState<Option[]>(filterOptions(options, ''));
const [showOptions, setShowOptions] = useState(false);
Expand Down Expand Up @@ -99,6 +99,7 @@ export const AutocompleteInput: FC<Props> = ({ placeholder = 'Type to search...'
disabled={disabled}
onBlur={() => !disabled && setShowOptions(false)}
onFocus={() => !disabled && setShowOptions(true)}
{...props}
/>
</InputWrapper>

Expand Down Expand Up @@ -199,10 +200,10 @@ const StyledInput = styled.input`

const OptionsList = styled.ul`
position: absolute;
max-height: 348px;
max-height: 500px;
top: 32px;
border-radius: 24px;
width: calc(100% - 24px);
width: calc(100% - 32px);
overflow-y: auto;
background-color: ${({ theme }) => theme.colors.dropdown_bg};
border: 1px solid ${({ theme }) => theme.colors.border};
Expand Down

0 comments on commit 81a8131

Please sign in to comment.