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

Convert EuiFlex* and EuiDescriptionList* to typescript #1365

Merged
merged 13 commits into from
Mar 4, 2019
Merged
Show file tree
Hide file tree
Changes from 6 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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## [`master`](https://github.com/elastic/eui/tree/master)

No public interface changes since `5.7.0`.
- Converted `EuiFlexGrid`, `EuiFlexGroup`, `EuiFlexItem`, `EuiDescriptionList`, `EuiDescriptionListTitle`, and `EuiDescriptionListDescription` to TypeScript ([#1365](https://github.com/elastic/eui/pull/1365))

## [`5.7.0`](https://github.com/elastic/eui/tree/v5.7.0)

Expand Down
18 changes: 14 additions & 4 deletions scripts/babel/proptypes-from-ts-props/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ function getPropTypesForNode(node, optional, state) {

const reducedUnionTypes = tsUnionTypes.reduce(
(foundTypes, tsUnionType) => {
if (types.isLiteral(tsUnionType)) {
if (types.isLiteral(tsUnionType) || (types.isIdentifier(tsUnionType) && tsUnionType.name === 'undefined')) {
if (foundTypes.oneOfPropType == null) {
foundTypes.oneOfPropType = types.arrayExpression([]);
foundTypes.unionTypes.push(
Expand Down Expand Up @@ -511,10 +511,20 @@ function getPropTypesForNode(node, optional, state) {
optional = true; // cannot call `.isRequired` on a boolean literal
break;

case 'TSNullKeyword':
propType = types.nullLiteral();
optional = true; // cannot call `.isRequired` on a boolean literal
chandlerprall marked this conversation as resolved.
Show resolved Hide resolved
break;

case 'TSUndefinedKeyword':
propType = types.identifier('undefined');
optional = true; // cannot call `.isRequired` on a boolean literal
chandlerprall marked this conversation as resolved.
Show resolved Hide resolved
break;

// very helpful debugging code
// default:
// debugger;
// throw new Error(`Could not generate prop types for node type ${node.type}`);
default:
debugger;
chandlerprall marked this conversation as resolved.
Show resolved Hide resolved
throw new Error(`Could not generate prop types for node type ${node.type}`);
}

// if the node was unable to be translated to a prop type, fallback to PropTypes.any
Expand Down
23 changes: 23 additions & 0 deletions scripts/babel/proptypes-from-ts-props/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,29 @@ FooComponent.propTypes = {
};`);
});

it('treats null and undefined as literals', () => {
const result = transform(
`
import React from 'react';
interface IFooProps {bar: 'five' | null | undefined}
const FooComponent: React.SFC<IFooProps> = () => {
return (<div>Hello World</div>);
}`,
babelOptions
);

expect(result.code).toBe(`import React from 'react';
import PropTypes from "prop-types";

const FooComponent = () => {
return <div>Hello World</div>;
};

FooComponent.propTypes = {
bar: PropTypes.oneOf(["five", null, undefined]).isRequired
};`);
});

});

describe('array / arrayOf propTypes', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import React from 'react';

import {
EuiFlexGroup,
EuiFlexItem,
} from '../../../../src/components';
import { EuiFlexGroup, EuiFlexItem } from '../../../../src/components/flex';

export default () => (
<button onClick={() => { window.alert('click'); }}>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import React from 'react';

import {
EuiFlexGroup,
EuiFlexItem,
} from '../../../../src/components';
import { EuiFlexGroup, EuiFlexItem } from '../../../../src/components/flex';

export default () => (
<EuiFlexGroup direction="column">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import React from 'react';

import {
EuiFlexGrid,
EuiFlexItem,
} from '../../../../src/components';
import { EuiFlexGrid, EuiFlexItem } from '../../../../src/components/flex';

const ITEM_STYLE = { width: '300px' };

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import React from 'react';

import {
EuiFlexGrid,
EuiFlexItem,
} from '../../../../src/components';
import { EuiFlexGrid, EuiFlexItem } from '../../../../src/components/flex';

export default () => (
<div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import React from 'react';

import {
EuiFlexGroup,
EuiFlexItem,
EuiSpacer,
} from '../../../../src/components';
import { EuiFlexGroup, EuiFlexItem } from '../../../../src/components/flex';
import { EuiSpacer } from '../../../../src/components/spacer';

export default () => (
<EuiFlexGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import React from 'react';

import {
EuiFlexGroup,
EuiFlexItem,
} from '../../../../src/components';
import { EuiFlexGroup, EuiFlexItem } from '../../../../src/components/flex';

export default () => (
<EuiFlexGroup wrap>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import React from 'react';

import {
EuiFlexGroup,
EuiFlexItem,
EuiSpacer,
} from '../../../../src/components';
import { EuiFlexGroup, EuiFlexItem } from '../../../../src/components/flex';
import { EuiSpacer } from '../../../../src/components/spacer';

export default () => (
<div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import React from 'react';

import {
EuiFlexGroup,
EuiFlexItem,
} from '../../../../src/components';
import { EuiFlexGroup, EuiFlexItem } from '../../../../src/components/flex';

export default () => (
<div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import React from 'react';

import {
EuiFlexGroup,
EuiFlexItem,
EuiSpacer,
} from '../../../../src/components';
import { EuiFlexGroup, EuiFlexItem } from '../../../../src/components/flex';
import { EuiSpacer } from '../../../../src/components/spacer';

export default () => (
<div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import React from 'react';

import {
EuiFlexGroup,
EuiFlexItem,
} from '../../../../src/components';
import { EuiFlexGroup, EuiFlexItem } from '../../../../src/components/flex';

export default () => (
<EuiFlexGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import React from 'react';

import {
EuiFlexGroup,
EuiFlexItem,
EuiFlexGrid,
EuiSpacer,
} from '../../../../src/components';
import { EuiFlexGrid, EuiFlexGroup, EuiFlexItem } from '../../../../src/components/flex';
import { EuiSpacer } from '../../../../src/components/spacer';

export default () => (
<div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import React from 'react';

import {
EuiFlexGroup,
EuiFlexItem,
EuiIcon,
EuiSpacer,
} from '../../../../src/components';
import { EuiFlexGroup, EuiFlexItem } from '../../../../src/components/flex';
import { EuiSpacer } from '../../../../src/components/spacer';
import { EuiIcon } from '../../../../src/components/icon';

export default () => (
<div>
Expand Down
3 changes: 3 additions & 0 deletions src/components/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ export type RefCallback<Element extends HTMLElement> = (

export type Omit<T, K> = Pick<T, Exclude<keyof T, K>>;

/**
* Wraps Object.keys with proper typescript definition of the resulting array
*/
export function keysOf<T, K extends keyof T>(obj: T): K[] {
return Object.keys(obj) as K[];
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React from 'react';
import PropTypes from 'prop-types';
import React, { HTMLAttributes, ReactNode, SFC } from 'react';
import classNames from 'classnames';

import {
Expand All @@ -9,30 +8,56 @@ import {
import {
EuiDescriptionListDescription,
} from './description_list_description';
import { CommonProps, keysOf } from '../common';

const typesToClassNameMap = {
row: 'euiDescriptionList--row',
column: 'euiDescriptionList--column',
inline: 'euiDescriptionList--inline',
};

export const TYPES = Object.keys(typesToClassNameMap);
export type EuiDescriptionListType = 'row' | 'column' | 'inline';
export type EuiDescriptionListAlignment = 'center' | 'left';
export type EuiDescriptionListTextStyle = 'normal' | 'reverse';
chandlerprall marked this conversation as resolved.
Show resolved Hide resolved

export interface EuiDescriptionListProps {
listItems?: Array<{ title: ReactNode, description: ReactNode }>;
/**
* Text alignment
*/
align?: EuiDescriptionListAlignment;
/**
* Smaller text and condensed spacing
*/
compressed?: boolean;
/**
* How should the content be styled, by default
* this will emphasize the title
*/
textStyle?: EuiDescriptionListTextStyle;
/**
* How each item should be layed out
*/
type?: EuiDescriptionListType;
}

export const TYPES = keysOf(typesToClassNameMap);

const alignmentsToClassNameMap = {
center: 'euiDescriptionList--center',
left: '',
};

export const ALIGNMENTS = Object.keys(alignmentsToClassNameMap);
export const ALIGNMENTS = keysOf(alignmentsToClassNameMap);

const textStylesToClassNameMap = {
normal: '',
reverse: 'euiDescriptionList--reverse',
};

export const TEXT_STYLES = Object.keys(textStylesToClassNameMap);
export const TEXT_STYLES = keysOf(textStylesToClassNameMap);

export const EuiDescriptionList = ({
export const EuiDescriptionList: SFC<CommonProps & HTMLAttributes<HTMLDListElement> & EuiDescriptionListProps> = ({
children,
className,
listItems,
Expand All @@ -44,9 +69,9 @@ export const EuiDescriptionList = ({
}) => {
const classes = classNames(
'euiDescriptionList',
typesToClassNameMap[type],
alignmentsToClassNameMap[align],
textStylesToClassNameMap[textStyle],
type ? typesToClassNameMap[type] : undefined,
align ? alignmentsToClassNameMap[align] : undefined,
textStyle ? textStylesToClassNameMap[textStyle] : undefined,
{
'euiDescriptionList--compressed': compressed,
},
Expand All @@ -64,7 +89,7 @@ export const EuiDescriptionList = ({

<EuiDescriptionListDescription key={`description-${index}`}>
{item.description}
</EuiDescriptionListDescription>
</EuiDescriptionListDescription>,
];
})
);
Expand All @@ -82,36 +107,6 @@ export const EuiDescriptionList = ({
);
};

EuiDescriptionList.propTypes = {
listItems: PropTypes.arrayOf(PropTypes.shape({
title: PropTypes.node,
description: PropTypes.node,
})),
children: PropTypes.node,
className: PropTypes.string,

/**
* Text alignment
*/
align: PropTypes.oneOf(ALIGNMENTS),

/**
* Smaller text and condensed spacing
*/
compressed: PropTypes.bool,

/**
* How should the content be styled, by default
* this will emphasize the title
*/
textStyle: PropTypes.oneOf(TEXT_STYLES),

/**
* How each item should be layed out
*/
type: PropTypes.oneOf(TYPES),
};

EuiDescriptionList.defaultProps = {
align: 'left',
compressed: false,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React from 'react';
import PropTypes from 'prop-types';
import React, { HTMLAttributes, SFC } from 'react';
import classNames from 'classnames';
import { CommonProps } from '../common';

export const EuiDescriptionListDescription = ({
export const EuiDescriptionListDescription: SFC<CommonProps & HTMLAttributes<HTMLElement>> = ({
children,
className,
...rest
Expand All @@ -18,8 +18,3 @@ export const EuiDescriptionListDescription = ({
</dd>
);
};

EuiDescriptionListDescription.propTypes = {
children: PropTypes.node,
className: PropTypes.string,
};
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React from 'react';
import PropTypes from 'prop-types';
import React, { HTMLAttributes, SFC } from 'react';
import classNames from 'classnames';
import { CommonProps } from '../common';

export const EuiDescriptionListTitle = ({
export const EuiDescriptionListTitle: SFC<CommonProps & HTMLAttributes<HTMLElement>> = ({
children,
className,
...rest
Expand All @@ -18,8 +18,3 @@ export const EuiDescriptionListTitle = ({
</dt>
);
};

EuiDescriptionListTitle.propTypes = {
children: PropTypes.node,
className: PropTypes.string,
};
Loading