Skip to content

Commit

Permalink
Clean up regressions found in Kibana testing
Browse files Browse the repository at this point in the history
  • Loading branch information
chandlerprall committed Apr 13, 2020
1 parent b4e8a9b commit a74b42b
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 8 deletions.
20 changes: 20 additions & 0 deletions src/components/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,26 @@ export type PropsOf<C> = C extends SFC<infer SFCProps>
? ComponentProps
: never;

// Utility methods for ApplyClassComponentDefaults
type ExtractDefaultProps<T> = T extends { defaultProps: infer D } ? D : never;
type ExtractProps<
C extends new (...args: any) => any,
IT = InstanceType<C>
> = IT extends Component<infer P> ? P : never;

/**
* 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 props definition as the defaulted values are not made optional,
* because it isn't processed by LibraryManagedAttributes. To get around this, we:
* - remove the props which have default values applied
* - export (Props - Defaults) & Partial<Defaults>
*/
export type ApplyClassComponentDefaults<
C extends new (...args: any) => any,
D = ExtractDefaultProps<C>,
P = ExtractProps<C>
> = Omit<P, keyof D> & Partial<D>;

/*
https://github.com/Microsoft/TypeScript/issues/28339
Problem: Pick and Omit do not distribute over union types, which manifests when
Expand Down
10 changes: 7 additions & 3 deletions src/components/date_picker/date_picker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { EuiFormControlLayoutIconsProps } from '../form/form_control_layout/form
import { EuiErrorBoundary } from '../error_boundary';

import { EuiI18nConsumer } from '../context';
import { CommonProps } from '../common';
import { ApplyClassComponentDefaults, CommonProps } from '../common';

// @ts-ignore the type is provided by react-datepicker.d.ts
import { ReactDatePicker as _ReactDatePicker } from '../../../packages';
Expand Down Expand Up @@ -72,9 +72,13 @@ interface EuiExtendedDatePickerProps extends ReactDatePickerProps {
showIcon?: boolean;
}

export type EuiDatePickerProps = CommonProps & EuiExtendedDatePickerProps;
type _EuiDatePickerProps = CommonProps & EuiExtendedDatePickerProps;

export class EuiDatePicker extends Component<EuiDatePickerProps> {
export type EuiDatePickerProps = ApplyClassComponentDefaults<
typeof EuiDatePicker
>;

export class EuiDatePicker extends Component<_EuiDatePickerProps> {
static defaultProps = {
adjustDateOnChange: true,
dateFormat: euiDatePickerDefaultDateFormat,
Expand Down
1 change: 1 addition & 0 deletions src/components/date_picker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export {
RelativeDateMode,
NowDateMode,
DateMode,
OnRefreshChangeProps,
ShortDate,
RelativeParts,
RelativeOption,
Expand Down
2 changes: 2 additions & 0 deletions src/components/date_picker/super_date_picker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ export { AsyncInterval } from './async_interval';
export {
EuiSuperDatePicker,
EuiSuperDatePickerProps,
OnTimeChangeProps,
OnRefreshProps,
} from './super_date_picker';

export {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ import { LocaleSpecifier } from 'moment'; // eslint-disable-line import/named

export { prettyDuration, commonDurationRanges };

interface OnTimeChangeProps extends DurationRange {
export interface OnTimeChangeProps extends DurationRange {
isInvalid: boolean;
isQuickSelection: boolean;
}

interface OnRefreshProps extends DurationRange {
export interface OnRefreshProps extends DurationRange {
refreshInterval: number;
}

Expand Down
8 changes: 5 additions & 3 deletions src/components/date_picker/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,12 @@ export interface RelativeOption {
value: TimeUnitId | TimeUnitFromNowId;
}

export type ApplyRefreshInterval = (args: {
isPaused?: boolean;
export type OnRefreshChangeProps = {
isPaused: boolean;
refreshInterval: number;
}) => void;
};

export type ApplyRefreshInterval = (args: OnRefreshChangeProps) => void;

export interface QuickSelect {
timeTense: string;
Expand Down

0 comments on commit a74b42b

Please sign in to comment.