Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EuiComboBox type fixes #2971

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
- Fixed `EuiTitle` not rendering child classes ([#2925](https://github.com/elastic/eui/pull/2925))
- Extended `div` element in `EuiFlyout` type ([#2914](https://github.com/elastic/eui/pull/2914))
- Fixed popover positioning service to be more lenient when positioning 0-width or 0-height content ([#2948](https://github.com/elastic/eui/pull/2948))
- Fixed type definitions for `EuiComboBox` ([#2971](https://github.com/elastic/eui/pull/2971))

**Theme: Amsterdam**

Expand Down
27 changes: 24 additions & 3 deletions src/components/combo_box/combo_box.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type DrillProps<T> = Pick<
'onCreateOption' | 'options' | 'renderOption' | 'selectedOptions'
>;

export interface EuiComboBoxProps<T>
interface _EuiComboBoxProps<T>
extends CommonProps,
Omit<HTMLAttributes<HTMLDivElement>, 'onChange'>,
DrillProps<T> {
Expand All @@ -75,6 +75,27 @@ export interface EuiComboBoxProps<T>
singleSelection: boolean | EuiComboBoxSingleSelectionShape;
}

/**
* Because of how TypeScript's LibraryManagedAttributes is designed to handle defaultProps (https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-0.html#support-for-defaultprops-in-jsx)
* we can't directly export the above Props definitions, as the defaulted values are not made optional
* as it isn't processed by LibraryManagedAttributes. To get around this, we:
* - remove the props which have default values applied
* - additionally re-define `options` and `selectedOptions` defaults, necessary as static members can't access generics and become never[]
* - export (Props - Defaults) & Partial<Defaults>
*/
type DefaultProps<T> = Omit<
typeof EuiComboBox['defaultProps'],
'options' | 'selectedOptions'
> & {
options: Array<EuiComboBoxOptionOption<T>>;
selectedOptions: Array<EuiComboBoxOptionOption<T>>;
};
export type EuiComboBoxProps<T> = Omit<
_EuiComboBoxProps<T>,
keyof DefaultProps<T>
> &
Partial<DefaultProps<T>>;

interface EuiComboBoxState<T> {
activeOptionIndex: number;
hasFocus: boolean;
Expand All @@ -89,7 +110,7 @@ interface EuiComboBoxState<T> {
const initialSearchValue = '';

export class EuiComboBox<T> extends Component<
EuiComboBoxProps<T>,
_EuiComboBoxProps<T>,
EuiComboBoxState<T>
> {
static defaultProps = {
Expand Down Expand Up @@ -654,7 +675,7 @@ export class EuiComboBox<T> extends Component<
}

static getDerivedStateFromProps<T>(
nextProps: EuiComboBoxProps<T>,
nextProps: _EuiComboBoxProps<T>,
prevState: EuiComboBoxState<T>
) {
const { options, selectedOptions, singleSelection } = nextProps;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export type EuiComboBoxOptionsListProps<T> = CommonProps &
onCreateOption?: (
searchValue: string,
options: Array<EuiComboBoxOptionOption<T>>
) => boolean;
) => boolean | void;
onOptionClick?: OptionHandler<T>;
onOptionEnterKey?: OptionHandler<T>;
onScroll?: ListProps['onScroll'];
Expand Down