Skip to content
Open
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
34 changes: 33 additions & 1 deletion src/form/SuggestInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import type { InputProps } from './Input.js';
import getSlotStyles from '../helpers/getSlotStyles.js';
import Dropdown from '../base/Dropdown.js';
import Input from './Input.js';
//modules
import { useState } from 'react';

//--------------------------------------------------------------------//
// Types
Expand Down Expand Up @@ -43,6 +45,8 @@ export type SuggestInputProps = Omit<InputProps
option?: CallableSlotStyleProp<DropdownStates>,
//serialized list of options as array or object
options?: DropdownOptionProp
//remote url to fetch suggestions
remote?: string
},
'multiple'
>;
Expand Down Expand Up @@ -143,12 +147,16 @@ export function SuggestInput(props: SuggestInputProps) {
left, //?: boolean
//dropdown handler
onDropdown, //?: (show: boolean) => void
//called whenever user types
onQuery, //?: (query: string) => void
//update handler
onUpdate, //?: (value: string) => void
//slot: style to apply to the select control
option, //: CallableSlotStyleProp<SelectStates>
//serialized list of options as array or object
options, //: SelectOption[]|Record<string, string>
//remote url to fetch suggestions
remote, //?: string
//position of the dropdown
right, //?: boolean
//custom inline styles
Expand All @@ -159,6 +167,11 @@ export function SuggestInput(props: SuggestInputProps) {
value, //?: T
...inputProps
} = props;
//hooks
const [
remoteOptions,
setRemoteOptions
] = useState<DropdownOptionProp | undefined>(options);
//variables
// determine classes
const classes = [ 'frui-form-suggest-input' ];
Expand All @@ -169,6 +182,24 @@ export function SuggestInput(props: SuggestInputProps) {
// get slot styles
const controlStyles = control ? getSlotStyles(control, {}) : {};
const dropdownStyles = dropdown ? getSlotStyles(dropdown, {}) : {};
//handlers
const handleQuery = async (query: string) => {
if (typeof remote === 'string' && query) {
try {
const response =
await fetch(`${remote}?q=${encodeURIComponent(query)}`);
const data = await response.json();
if (Array.isArray(data)) {
setRemoteOptions(data);
}
} catch (error) {
console.error('Failed to fetch remote suggestions:', error);
}
}
if (typeof onQuery === 'function') {
onQuery(query);
}
};
//render
return (
<Dropdown
Expand All @@ -181,7 +212,7 @@ export function SuggestInput(props: SuggestInputProps) {
onDropdown={onDropdown}
onUpdate={onUpdate}
option={option}
options={options}
options={remoteOptions}
right={right}
top={top}
value={value}
Expand All @@ -191,6 +222,7 @@ export function SuggestInput(props: SuggestInputProps) {
{...inputProps}
className={controlStyles.className}
style={controlStyles.style}
onQuery={handleQuery}
/>
</Dropdown.Control>
{children}
Expand Down
Loading