Skip to content

Commit

Permalink
add invalid on option click behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
iacopolea committed Oct 18, 2024
1 parent 607fefa commit 2a78bb5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
7 changes: 6 additions & 1 deletion src/stories/dropdowns/autocomplete/index.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ const TemplateCreatable: Story<AutocompleteProps> = (args) => {
onCreateNewOption={async (inputValue) => {
// mock a promise to create a new item
return await new Promise((resolve) => setTimeout(() => {
resolve({ label: inputValue, value: inputValue, id: inputValue });
if (inputValue === "invalid") {
alert("Invalid value");
resolve(false);
} else {
resolve({ label: inputValue, value: inputValue, id: inputValue });
}
}, 1000));
}}
onOptionClick={({ selectionValue }) => {
Expand Down
5 changes: 2 additions & 3 deletions src/stories/dropdowns/autocomplete/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export interface AutocompleteProps extends IComboboxProps {
onOptionClick?: ({ inputValue, selectionValue }: OnOptionClickArgs) => void;
onInputChange?: (inputValue: string) => void;
isCreatable?: boolean;
onCreateNewOption?: (inputValue: string) => Promise<IOption>;
onCreateNewOption?: (inputValue: string) => Promise<IOption | false>;
options: Array<IOptGroup | IOption>;
}
// debounce a function call
Expand Down Expand Up @@ -96,7 +96,6 @@ const Autocomplete = ({ options, onOptionClick, onInputChange, onChange, isCreat
if (typeof onInputChange === 'function') onInputChange(sanitizedInputValue);
}
if (event.type === "option:click" && typeof onOptionClick === 'function') {
// setSelectionValue(event.selectionValue);
setInputValue(undefined);
onOptionClick({ inputValue: event.inputValue, selectionValue: event.selectionValue });
}
Expand Down Expand Up @@ -131,7 +130,7 @@ const Autocomplete = ({ options, onOptionClick, onInputChange, onChange, isCreat
onClickCapture={async (e) => {
if (typeof onCreateNewOption === 'function') {
const newOption = await onCreateNewOption(e.currentTarget.title);
setOption(pre => [...pre, newOption]);
if (newOption) setOption(pre => [...pre, newOption]);
}
}}
>
Expand Down

0 comments on commit 2a78bb5

Please sign in to comment.