Skip to content
This repository has been archived by the owner on Jun 11, 2021. It is now read-only.

Commit

Permalink
feat(onSubmit): introduce onSubmit option (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
francoischalifour authored Feb 11, 2020
1 parent 4d3f792 commit ca0891c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
1 change: 1 addition & 0 deletions packages/autocomplete-core/defaultProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export function getDefaultProps<TItem>(
environment,
shouldDropdownOpen: ({ state }) => getItemsCount(state) > 0,
onStateChange: noop,
onSubmit: noop,
...props,
// The following props need to be deeply defaulted.
initialState: {
Expand Down
11 changes: 10 additions & 1 deletion packages/autocomplete-core/propGetters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,16 @@ export function getPropGetters<TItem>({
onSubmit: event => {
event.preventDefault();

// @TODO: call the `onInputChange` or `onSubmit` user prop?
props.onSubmit({
state: store.getState(),
setHighlightedIndex,
setQuery,
setSuggestions,
setIsOpen,
setStatus,
setContext,
event,
});

store.setState(
stateReducer(store.getState(), { type: 'submit', value: null }, props)
Expand Down
14 changes: 10 additions & 4 deletions packages/autocomplete-core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,15 +167,16 @@ export interface AutocompleteSourceOptions<TItem> {
/**
* Called when an item is selected.
*/
onSelect?: (options: ItemEventHandlerOptions<TItem>) => void;
onSelect?: (options: ItemEventHandlerParams<TItem>) => void;
}

export interface EventHandlerOptions<TItem> extends AutocompleteSetters<TItem> {
interface EventHandlerParams<TItem> extends AutocompleteSetters<TItem> {
state: AutocompleteState<TItem>;
event: Event;
}

export interface ItemEventHandlerOptions<TItem>
extends EventHandlerOptions<TItem> {
export interface ItemEventHandlerParams<TItem>
extends EventHandlerParams<TItem> {
suggestion: Suggestion<TItem>;
suggestionValue: ReturnType<AutocompleteSource['getInputValue']>;
suggestionUrl: ReturnType<AutocompleteSource['getSuggestionUrl']>;
Expand Down Expand Up @@ -285,6 +286,10 @@ export interface AutocompleteOptions<TItem> {
* The function called to determine whether the dropdown should open.
*/
shouldDropdownOpen?(options: { state: AutocompleteState<TItem> }): boolean;
/**
* The function called when the autocomplete form is submitted.
*/
onSubmit?(params: EventHandlerParams<TItem>): void;
}

export type NormalizedAutocompleteSource = {
Expand All @@ -310,4 +315,5 @@ export interface RequiredAutocompleteOptions<TItem> {
environment: Environment;
navigator: Navigator;
shouldDropdownOpen(options: { state: AutocompleteState<TItem> }): boolean;
onSubmit(params: EventHandlerParams<TItem>): void;
}

0 comments on commit ca0891c

Please sign in to comment.