Skip to content

Commit 2a6229e

Browse files
committed
Update types
1 parent dbe4ba0 commit 2a6229e

File tree

4 files changed

+40
-33
lines changed

4 files changed

+40
-33
lines changed

Diff for: src/components/Typeahead/Typeahead.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export interface RenderMenuProps extends MenuProps {
3636
renderMenuItemChildren?: RenderMenuItemChildren;
3737
}
3838

39-
export interface TypeaheadComponentProps extends Partial<TypeaheadProps> {
39+
export interface TypeaheadComponentProps extends TypeaheadProps {
4040
align?: Align;
4141
className?: string;
4242
/**

Diff for: src/core/useTypeahead.ts

+4-8
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import useValidateProps from './useValidateProps';
2222

2323
import {
2424
FilterByCallback,
25+
InternalProps,
2526
Option,
2627
SelectEvent,
2728
TypeaheadProps,
@@ -62,6 +63,7 @@ const defaultProps = {
6263
filterBy: [],
6364
highlightOnlyResult: false,
6465
ignoreDiacritics: true,
66+
inputProps: {},
6567
labelKey: DEFAULT_LABELKEY,
6668
maxResults: 100,
6769
minLength: 0,
@@ -138,7 +140,7 @@ function useOnMenuToggle(
138140
isInitialRender.current = false;
139141
return;
140142
}
141-
onMenuToggle(isMenuShown);
143+
onMenuToggle?.(isMenuShown);
142144
}, [isMenuShown, onMenuToggle]);
143145
}
144146

@@ -151,14 +153,8 @@ export interface TypeaheadRef {
151153
toggleMenu: () => void;
152154
}
153155

154-
interface Props extends Partial<TypeaheadProps> {
155-
options: Option[];
156-
}
157-
158-
type InternalProps = Omit<TypeaheadProps, 'onChange'>;
159-
160156
function useTypeahead(
161-
{ onChange, ...partialProps }: Props,
157+
{ onChange, ...partialProps }: TypeaheadProps,
162158
ref?: Ref<TypeaheadRef>
163159
) {
164160
const props: InternalProps = {

Diff for: src/core/useValidateProps.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function useValidateProps({
2828
multiple,
2929
onChange,
3030
selected,
31-
}: Partial<TypeaheadProps>) {
31+
}: TypeaheadProps) {
3232
useEffect(() => {
3333
const name = defaultSelected.length ? 'defaultSelected' : 'selected';
3434

Diff for: src/types.ts

+34-23
Original file line numberDiff line numberDiff line change
@@ -74,39 +74,39 @@ export interface TypeaheadProps {
7474
* If a function is specified, it will be used to determine whether a custom
7575
* option should be included. The return value should be true or false.
7676
*/
77-
allowNew: AllowNew;
77+
allowNew?: AllowNew;
7878
/**
7979
* Autofocus the input when the component initially mounts.
8080
*/
81-
autoFocus: boolean;
81+
autoFocus?: boolean;
8282
/**
8383
* Whether or not filtering should be case-sensitive.
8484
*/
85-
caseSensitive: boolean;
85+
caseSensitive?: boolean;
8686
children?: TypeaheadChildren;
8787
/**
8888
* The initial value displayed in the text input.
8989
*/
90-
defaultInputValue: string;
90+
defaultInputValue?: string;
9191
/**
9292
* Whether or not the menu is displayed upon initial render.
9393
*/
94-
defaultOpen: boolean;
94+
defaultOpen?: boolean;
9595
/**
9696
* Specify any pre-selected options. Use only if you want the component to
9797
* be uncontrolled.
9898
*/
99-
defaultSelected: Option[];
99+
defaultSelected?: Option[];
100100
/**
101101
* Either an array of fields in `option` to search, or a custom filtering
102102
* callback.
103103
*/
104-
filterBy: string[] | FilterByCallback;
104+
filterBy?: string[] | FilterByCallback;
105105
/**
106106
* Highlights the menu item if there is only one result and allows selecting
107107
* that item by hitting enter. Does not work with `allowNew`.
108108
*/
109-
highlightOnlyResult: boolean;
109+
highlightOnlyResult?: boolean;
110110
/**
111111
* An html id attribute, required for assistive technologies such as screen
112112
* readers.
@@ -115,57 +115,57 @@ export interface TypeaheadProps {
115115
/**
116116
* Whether the filter should ignore accents and other diacritical marks.
117117
*/
118-
ignoreDiacritics: boolean;
118+
ignoreDiacritics?: boolean;
119119
inputProps?: InputProps;
120120
/**
121121
* Specify the option key to use for display or a function returning the
122122
* display string. By default, the selector will use the `label` key.
123123
*/
124-
labelKey: LabelKey;
124+
labelKey?: LabelKey;
125125
/**
126126
* Maximum number of results to display by default. Mostly done for
127127
* performance reasons so as not to render too many DOM nodes in the case of
128128
* large data sets.
129129
*/
130-
maxResults: number;
130+
maxResults?: number;
131131
/**
132132
* Number of input characters that must be entered before showing results.
133133
*/
134-
minLength: number;
134+
minLength?: number;
135135
/**
136136
* Whether or not multiple selections are allowed.
137137
*/
138-
multiple: boolean;
138+
multiple?: boolean;
139139
/**
140140
* Invoked when the input is blurred. Receives an event.
141141
*/
142-
onBlur: FocusEventHandler<HTMLInputElement>;
142+
onBlur?: FocusEventHandler<HTMLInputElement>;
143143
/**
144144
* Invoked whenever items are added or removed. Receives an array of the
145145
* selected options.
146146
*/
147-
onChange: (selected: Option[]) => void;
147+
onChange?: (selected: Option[]) => void;
148148
/**
149149
* Invoked when the input is focused. Receives an event.
150150
*/
151-
onFocus: FocusEventHandler<HTMLInputElement>;
151+
onFocus?: FocusEventHandler<HTMLInputElement>;
152152
/**
153153
* Invoked when the input value changes. Receives the string value of the
154154
* input.
155155
*/
156-
onInputChange: (text: string, event: ChangeEvent<HTMLInputElement>) => void;
156+
onInputChange?: (text: string, event: ChangeEvent<HTMLInputElement>) => void;
157157
/**
158158
* Invoked when a key is pressed. Receives an event.
159159
*/
160-
onKeyDown: KeyboardEventHandler<HTMLInputElement>;
160+
onKeyDown?: KeyboardEventHandler<HTMLInputElement>;
161161
/**
162162
* Invoked when menu visibility changes.
163163
*/
164-
onMenuToggle: (isOpen: boolean) => void;
164+
onMenuToggle?: (isOpen: boolean) => void;
165165
/**
166166
* Invoked when the pagination menu item is clicked. Receives an event.
167167
*/
168-
onPaginate: (event: SelectEvent<HTMLElement>, shownResults: number) => void;
168+
onPaginate?: (event: SelectEvent<HTMLElement>, shownResults: number) => void;
169169
/**
170170
* Whether or not the menu should be displayed. `undefined` allows the
171171
* component to control visibility, while `true` and `false` show and hide
@@ -181,7 +181,7 @@ export interface TypeaheadProps {
181181
* Give user the ability to display additional results if the number of
182182
* results exceeds `maxResults`.
183183
*/
184-
paginate: boolean;
184+
paginate?: boolean;
185185
/**
186186
* The selected option(s) displayed in the input. Use this prop if you want
187187
* to control the component via its parent.
@@ -190,6 +190,18 @@ export interface TypeaheadProps {
190190
selectHint?: SelectHint;
191191
}
192192

193+
type OptionalProps<T, K extends keyof T> = Partial<Pick<T, K>> &
194+
Required<Omit<T, K>>;
195+
196+
/**
197+
* Most props used internally become "required" since they're given default
198+
* values.
199+
*/
200+
export type InternalProps = OptionalProps<
201+
Required<Omit<TypeaheadProps, 'onChange'>>,
202+
'children' | 'id' | 'open' | 'selected' | 'selectHint'
203+
>;
204+
193205
export interface TypeaheadState {
194206
activeIndex: number;
195207
activeItem?: Option;
@@ -201,8 +213,7 @@ export interface TypeaheadState {
201213
text: string;
202214
}
203215

204-
export type TypeaheadPropsAndState = Omit<TypeaheadProps, 'onChange'> &
205-
TypeaheadState;
216+
export type TypeaheadPropsAndState = InternalProps & TypeaheadState;
206217

207218
export interface TypeaheadChildProps {
208219
getInputProps: (

0 commit comments

Comments
 (0)