Skip to content
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';
Comment thread
josephlouise74 marked this conversation as resolved.
Outdated

//--------------------------------------------------------------------//
// 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)}`);
Comment thread
josephlouise74 marked this conversation as resolved.
Outdated
const data = await response.json();
if (Array.isArray(data)) {
setRemoteOptions(data);
}
} catch (error) {
console.error('Failed to fetch remote suggestions:', error);
Comment thread
josephlouise74 marked this conversation as resolved.
Outdated
}
}
if (typeof onQuery === 'function') {
onQuery(query);
}
Comment thread
josephlouise74 marked this conversation as resolved.
Outdated
};
//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