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

TypeScriptified basic_table. #2428

Merged
merged 82 commits into from
Dec 9, 2019
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
82 commits
Select commit Hold shift + click to select a range
ee2b251
TypeScriptify custom_item_action.
sainthkh Oct 11, 2019
250bd2b
TypeScriptify default_item_action.
sainthkh Oct 11, 2019
ea8d657
Created action_types.ts.
sainthkh Oct 11, 2019
df8a094
Added missing props in DefaultItemAction.
sainthkh Oct 11, 2019
766404b
TypeScriptify expanded_item_actions.
sainthkh Oct 11, 2019
a73bce9
TypeScriptify CollapsedItemActions.
sainthkh Oct 13, 2019
b1cbb97
TypeScriptify LoadingTableBody.
sainthkh Oct 13, 2019
6e62a2c
TypeScriptify PaginationBar.
sainthkh Oct 13, 2019
c5b0e76
TypeScriptify BasicTable.
sainthkh Oct 14, 2019
781ed0e
Removed anys.
sainthkh Oct 14, 2019
dcf83da
Removed more anys.
sainthkh Oct 14, 2019
95450c2
Removed any in item and itemId.
sainthkh Oct 14, 2019
f5565ed
TypeScriptify EuiInMemoryTable.
sainthkh Oct 15, 2019
ec27dd7
Fixed bug in PropType generation.
sainthkh Oct 15, 2019
306c70e
Clean up.
sainthkh Oct 15, 2019
06afcbf
Used Omit.
sainthkh Oct 15, 2019
9ca68f6
Thread generics throughout EuiBasicTable
pugnascotia Oct 16, 2019
d999c21
Tweaks
pugnascotia Oct 16, 2019
30fb9d2
More tweaks
pugnascotia Oct 16, 2019
049c959
Even more tweaks
pugnascotia Oct 16, 2019
3fb8714
Merge pull request #1 from pugnascotia/ts-basic-table
sainthkh Oct 16, 2019
dd1d652
Added responsive prop to EuiInMemoryTable.
sainthkh Oct 16, 2019
e7f8b89
any -> Event.
sainthkh Oct 16, 2019
d767d71
Fixed selectable test failure.
sainthkh Oct 16, 2019
e209494
Renamed prop interfaces.
sainthkh Oct 16, 2019
467bd7f
Merge upstream.
sainthkh Oct 22, 2019
cf5c203
Some typescript updates
chandlerprall Oct 23, 2019
8c29e77
ts->proptypes test for direct-usage literal values
chandlerprall Oct 24, 2019
2022116
Update: State -> CollapsedItemActionsState
sainthkh Oct 24, 2019
8533bba
Update: State -> CollapsedItemActionsState
sainthkh Oct 24, 2019
7ca9ef3
Update State -> CustomItemActionState
sainthkh Oct 24, 2019
9e6a007
Update State -> CustomItemActionState
sainthkh Oct 24, 2019
db56653
Merge remote-tracking branch 'upstream/master' into ts-basic-table
sainthkh Oct 24, 2019
fce5c12
clean up types in in_memory_table
chandlerprall Oct 28, 2019
17e967d
Merge pull request #2 from chandlerprall/ts-basic-table
sainthkh Oct 28, 2019
bc413a4
Fixed ExpandedItemActions snapshot. Because it has some props that do…
sainthkh Oct 29, 2019
45f27a1
Remove more anys.
sainthkh Oct 29, 2019
07730c4
Updated i18ntokens.json.
sainthkh Oct 29, 2019
a7dbdb6
Merge remote-tracking branch 'upstream/master' into ts-basic-table
sainthkh Nov 12, 2019
96f0e3b
TypeScriptify custom_item_action.
sainthkh Oct 11, 2019
d870291
TypeScriptify default_item_action.
sainthkh Oct 11, 2019
c0edfe3
Created action_types.ts.
sainthkh Oct 11, 2019
890a01e
Added missing props in DefaultItemAction.
sainthkh Oct 11, 2019
7b94c7c
TypeScriptify expanded_item_actions.
sainthkh Oct 11, 2019
11cac1f
TypeScriptify CollapsedItemActions.
sainthkh Oct 13, 2019
41a23b6
TypeScriptify LoadingTableBody.
sainthkh Oct 13, 2019
ae3c8a1
TypeScriptify PaginationBar.
sainthkh Oct 13, 2019
3259a1c
TypeScriptify BasicTable.
sainthkh Oct 14, 2019
1b785c7
Removed anys.
sainthkh Oct 14, 2019
ab91fa7
Removed more anys.
sainthkh Oct 14, 2019
262f1f3
Removed any in item and itemId.
sainthkh Oct 14, 2019
5bfc737
TypeScriptify EuiInMemoryTable.
sainthkh Oct 15, 2019
b6eb031
Fixed bug in PropType generation.
sainthkh Oct 15, 2019
18dc72d
Clean up.
sainthkh Oct 15, 2019
9aa27ec
Used Omit.
sainthkh Oct 15, 2019
e944eee
Thread generics throughout EuiBasicTable
pugnascotia Oct 16, 2019
7ec868f
Tweaks
pugnascotia Oct 16, 2019
b221494
More tweaks
pugnascotia Oct 16, 2019
bac79f8
Even more tweaks
pugnascotia Oct 16, 2019
927e9c0
Added responsive prop to EuiInMemoryTable.
sainthkh Oct 16, 2019
48c8a92
any -> Event.
sainthkh Oct 16, 2019
528838b
Fixed selectable test failure.
sainthkh Oct 16, 2019
8ff3883
Renamed prop interfaces.
sainthkh Oct 16, 2019
cbd291e
Some typescript updates
chandlerprall Oct 23, 2019
548a89f
ts->proptypes test for direct-usage literal values
chandlerprall Oct 24, 2019
93ec991
Update: State -> CollapsedItemActionsState
sainthkh Oct 24, 2019
572716e
Update: State -> CollapsedItemActionsState
sainthkh Oct 24, 2019
b3820d6
Update State -> CustomItemActionState
sainthkh Oct 24, 2019
65dc1b1
Update State -> CustomItemActionState
sainthkh Oct 24, 2019
82e5e54
clean up types in in_memory_table
chandlerprall Oct 28, 2019
2de08e0
Fixed ExpandedItemActions snapshot. Because it has some props that do…
sainthkh Oct 29, 2019
32c1e16
Remove more anys.
sainthkh Oct 29, 2019
75d9e32
more types
chandlerprall Nov 1, 2019
18a39b4
guarantee the page argument in table callbacks if pagination is provided
chandlerprall Nov 1, 2019
10de16e
rebased against master
chandlerprall Nov 13, 2019
2b07266
export EuiBasicTableColumn type
chandlerprall Nov 18, 2019
dda88bd
Added TD attributes to field and computed column types
chandlerprall Nov 18, 2019
21d3836
Accept string | number for EuiTableSortMobile's key
chandlerprall Nov 18, 2019
d25f49c
Merge chandlerprall/ts-basic-table into ts-basic-table.
sainthkh Nov 23, 2019
d11b1c1
Merge remote-tracking branch 'upstream/master' into ts-basic-table
sainthkh Nov 23, 2019
e3620a8
Merge branch 'master' into saintkh-ts-basic-table
chandlerprall Dec 9, 2019
8e513a3
changelog
chandlerprall Dec 9, 2019
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
25 changes: 24 additions & 1 deletion scripts/babel/proptypes-from-ts-props/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,25 @@ function convertLiteralToOneOf(types, literalNode) {
);
}

/**
* This function resolves discriminated union types (aka tagged union, algebraic data type).
*/
function resolveDiscriminatedUnionType(node, optional, state) {
const types = state.get('types');

if (node.type === 'TSLiteralType') {
return types.callExpression(
types.memberExpression(
types.identifier('PropTypes'),
types.identifier('oneOf')
),
[types.arrayExpression([types.stringLiteral(node.literal.value)])]
);
} else {
return getPropTypesForNode(node, optional, state);
}
}

/**
* Heavy lifter to generate the proptype AST for a node. Initially called by `processComponentDeclaration`,
* its return value is set as the component's `propTypes` value. This function calls itself recursively to translate
Expand All @@ -353,7 +372,11 @@ function getPropTypesForNode(node, optional, state) {
// Array<Foo>
// ^^^ Foo
case 'TSTypeAnnotation':
propType = getPropTypesForNode(node.typeAnnotation, true, state);
propType = resolveDiscriminatedUnionType(
node.typeAnnotation,
true,
state
);
break;

// translates intersections (Foo & Bar & Baz) to a shape with the types' members (Foo, Bar, Baz) merged together
Expand Down
36 changes: 35 additions & 1 deletion scripts/babel/proptypes-from-ts-props/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1095,7 +1095,7 @@ const FooComponent = () => {
};

FooComponent.propTypes = {
type: PropTypes.oneOfType([PropTypes.oneOf(["foo"]), PropTypes.oneOf(["bar"])]),
type: PropTypes.oneOfType([PropTypes.oneOf(["foo"]).isRequired, PropTypes.oneOf(["bar"]).isRequired]).isRequired,
value: PropTypes.oneOfType([PropTypes.string.isRequired, PropTypes.number.isRequired]).isRequired
};`);
});
Expand Down Expand Up @@ -1457,6 +1457,40 @@ FooComponent.propTypes = {
};`);
});

it('understands discriminated unions', () => {
const result = transform(
`
import React from 'react'
interface OptA { type: 'a'; value: string; }
interface OptB { type: 'b'; valid: boolean; }
type Option = OptA | OptB;
interface Props { option: Option }
const Foo: React.SFC<Props> = ({ option }: Props) => {
return (<div>{option.type == 'a' ? option.value : option.valid}</div>);
}`,
babelOptions
);

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

const Foo = ({
option
}) => {
return <div>{option.type == 'a' ? option.value : option.valid}</div>;
};

Foo.propTypes = {
option: PropTypes.oneOfType([PropTypes.shape({
type: PropTypes.oneOf(["a"]).isRequired,
value: PropTypes.string.isRequired
}).isRequired, PropTypes.shape({
type: PropTypes.oneOf(["b"]).isRequired,
valid: PropTypes.bool.isRequired
}).isRequired]).isRequired
};`);
});

it('understands an optional Array of strings and numbers', () => {
const result = transform(
`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ exports[`EuiBasicTable empty renders a node as a custom message 1`] = `
<p>
no items, click
<a
href={true}
href=""
>
here
</a>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

exports[`CustomItemAction render 1`] = `
<div
style={null}
className="test"
>
<Component
onBlur={[Function]}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`DefaultItemAction render - default button 1`] = `
<EuiToolTip
content="action 1"
delay="long"
position="top"
>
<EuiButtonEmpty
color="primary"
flush="right"
isDisabled={false}
onClick={[Function]}
size="s"
>
action1
</EuiButtonEmpty>
</EuiToolTip>
`;

exports[`DefaultItemAction render - button 1`] = `
<EuiToolTip
content="action 1"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`ExpandedItemActions render 1`] = `
Array [
<Fragment>
<DefaultItemAction
action={
Object {
Expand All @@ -20,7 +20,7 @@ Array [
}
itemId="xyz"
key="item_action_xyz_0"
/>,
/>
<CustomItemAction
action={
Object {
Expand All @@ -39,6 +39,6 @@ Array [
}
itemId="xyz"
key="item_action_xyz_1"
/>,
]
/>
</Fragment>
`;
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ exports[`EuiInMemoryTable behavior pagination 1`] = `
]
}
data-test-subj="test subject string"
executeQueryOptions={Object {}}
items={
Array [
Object {
Expand Down Expand Up @@ -44,8 +43,6 @@ exports[`EuiInMemoryTable behavior pagination 1`] = `
],
}
}
responsive={true}
sorting={false}
>
<EuiBasicTable
aria-label="aria-label"
Expand Down Expand Up @@ -1007,7 +1004,7 @@ exports[`EuiInMemoryTable with pagination and selection 1`] = `
responsive={true}
selection={
Object {
"onSelectionChanged": [Function],
"onSelectionChange": [Function],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why has this been renamed? I think it should be done as a separate PR, assuming it's actually necessary.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

onSelectionChanged was never correct, see #2432 / #2433

}
}
/>
Expand Down Expand Up @@ -1154,7 +1151,7 @@ exports[`EuiInMemoryTable with pagination, selection and sorting 1`] = `
responsive={true}
selection={
Object {
"onSelectionChanged": [Function],
"onSelectionChange": [Function],
}
}
sorting={
Expand Down Expand Up @@ -1191,7 +1188,6 @@ exports[`EuiInMemoryTable with pagination, selection, sorting and simple search
"description": "edit",
"name": "Edit",
"onClick": [Function],
"type": "button",
},
],
"name": "Actions",
Expand Down Expand Up @@ -1234,7 +1230,7 @@ exports[`EuiInMemoryTable with pagination, selection, sorting and simple search
responsive={true}
selection={
Object {
"onSelectionChanged": [Function],
"onSelectionChange": [Function],
}
}
sorting={
Expand Down Expand Up @@ -1265,7 +1261,6 @@ exports[`EuiInMemoryTable with pagination, selection, sorting and a single recor
"description": "edit",
"name": "Edit",
"onClick": [Function],
"type": "button",
},
],
"name": "Actions",
Expand Down Expand Up @@ -1308,7 +1303,7 @@ exports[`EuiInMemoryTable with pagination, selection, sorting and a single recor
responsive={true}
selection={
Object {
"onSelectionChanged": [Function],
"onSelectionChange": [Function],
}
}
sorting={
Expand Down Expand Up @@ -1367,7 +1362,7 @@ exports[`EuiInMemoryTable with pagination, selection, sorting and column rendere
responsive={true}
selection={
Object {
"onSelectionChanged": [Function],
"onSelectionChange": [Function],
}
}
sorting={
Expand All @@ -1384,9 +1379,6 @@ exports[`EuiInMemoryTable with pagination, selection, sorting and configured sea
<EuiSearchBar
box={
Object {
"aria-label": "aria-label",
"className": "testClass1 testClass2",
"data-test-subj": "test subject string",
"incremental": true,
}
}
Expand Down Expand Up @@ -1424,7 +1416,6 @@ exports[`EuiInMemoryTable with pagination, selection, sorting and configured sea
"description": "edit",
"name": "Edit",
"onClick": [Function],
"type": "button",
},
],
"name": "Actions",
Expand Down Expand Up @@ -1459,7 +1450,7 @@ exports[`EuiInMemoryTable with pagination, selection, sorting and configured sea
responsive={true}
selection={
Object {
"onSelectionChanged": [Function],
"onSelectionChange": [Function],
}
}
sorting={
Expand Down
43 changes: 43 additions & 0 deletions src/components/basic_table/action_types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { EuiIconType } from '../icon/icon';
import { ButtonIconColor } from '../button/button_icon/button_icon';
import { EuiButtonEmptyColor } from '../button/button_empty/button_empty';

type IconFunction = (item: any) => EuiIconType;
type ButtonColor = ButtonIconColor | EuiButtonEmptyColor;
type ButtonIconColorFunction = (item: any) => ButtonColor;

interface DefaultItemActionBase {
name: string;
description: string;
onClick?: (item: any) => void;
href?: string;
target?: string;
available?: (item: any) => boolean;
enabled?: (item: any) => boolean;
isPrimary?: boolean;
'data-test-subj'?: string;
}

export interface DefaultItemEmptyButtonAction extends DefaultItemActionBase {
type?: 'button';
color?: EuiButtonEmptyColor | ButtonIconColorFunction;
}

export interface DefaultItemIconButtonAction extends DefaultItemActionBase {
type: 'icon';
icon: EuiIconType | IconFunction;
color?: ButtonIconColor | ButtonIconColorFunction;
}

export type DefaultItemAction =
| DefaultItemEmptyButtonAction
| DefaultItemIconButtonAction;

export interface CustomItemAction {
render: (item: any, enabled?: boolean) => any;
available?: (item: any) => boolean;
enabled?: (item: any) => boolean;
isPrimary?: boolean;
}

export type Action = DefaultItemAction | CustomItemAction;
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import React from 'react';
import { mount } from 'enzyme';
import { mount, ReactWrapper } from 'enzyme';
import { findTestSubject } from '../../test';

import { EuiBasicTable } from './basic_table';
import { EuiBasicTable, Props } from './basic_table';

describe('EuiBasicTable', () => {
describe('behavior', () => {
describe('selected items', () => {
let props;
let component;
let props: Props;
let component: ReactWrapper;

beforeEach(() => {
props = {
Expand All @@ -22,7 +22,7 @@ describe('EuiBasicTable', () => {
},
],
selection: {
onSelectionChanged: () => {},
onSelectionChange: () => {},
},
onChange: () => {},
};
Expand Down
Loading