Skip to content

Commit

Permalink
chore(Select): convert Select and SelectField to TS
Browse files Browse the repository at this point in the history
  • Loading branch information
suevalov committed Mar 11, 2019
1 parent c5010ec commit 2ee2c85
Show file tree
Hide file tree
Showing 24 changed files with 93 additions and 209 deletions.
2 changes: 2 additions & 0 deletions packages/forma-36-react-components/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,7 @@ module.exports = {
],
'@typescript-eslint/no-for-in-array': 'error',
'@typescript-eslint/no-var-requires': 'off',
// enable again once all components are Typescript
'@typescript-eslint/no-explicit-any': 'off',
},
};
63 changes: 9 additions & 54 deletions packages/forma-36-react-components/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
/// <reference path="./dist/components/TextField/TextField.d.ts" />
/// <reference path="./dist/components/Illustration/Illustration.d.ts" />
/// <reference path="./dist/components/CopyButton/CopyButton.d.ts" />
/// <reference path="./dist/components/Select/Select/Select.d.ts" />
/// <reference path="./dist/components/Select/Option/Option.d.ts" />
/// <reference path="./dist/components/SelectField/SelectField.d.ts" />

import * as React from 'react';

Expand Down Expand Up @@ -111,6 +114,9 @@ import ParagraphComponent from './dist/components/Typography/Paragraph/Paragraph
import SectionHeadingComponent from './dist/components/Typography/SectionHeading/SectionHeading';
import SubheadingComponent from './dist/components/Typography/Subheading/Subheading';
import CopyButtonComponent from './dist/components/CopyButton/CopyButton';
import SelectComponent from './dist/components/Select/Select/Select';
import OptionComponent from './dist/components/Select/Option/Option';
import SelectFieldComponent from './dist/components/SelectField/SelectField';

export const Button: typeof ButtonComponent;
export const Spinner: typeof SpinnerComponent;
Expand Down Expand Up @@ -170,6 +176,9 @@ export const Paragraph: typeof ParagraphComponent;
export const SectionHeading: typeof SectionHeadingComponent;
export const Subheading: typeof SubheadingComponent;
export const CopyButton: typeof CopyButtonComponent;
export const Select: typeof SelectComponent;
export const Option: typeof OptionComponent;
export const SelectField: typeof SelectFieldComponent;

export interface EditorToolbarProps {
extraClassNames?: string;
Expand Down Expand Up @@ -252,60 +261,6 @@ export interface HeaderProps {

export const Header: React.SFC<HeaderProps>;

export type SelectWidth = 'auto' | 'small' | 'medium' | 'large' | 'full';

export interface SelectProps {
required?: boolean;
children: React.ReactNode;
name?: string;
id?: string;
hasError?: boolean;
value?: string;
isDisabled?: boolean;
onChange?: (...args: any[]) => any;
onBlur?: (...args: any[]) => any;
testId?: string;
onFocus?: (...args: any[]) => any;
extraClassNames?: string;
width?: SelectWidth;
}

export class Select extends React.Component<SelectProps, any> {
render(): JSX.Element;
}

export interface OptionProps {
value: string;
children: React.ReactNode;
}

export class Option extends React.Component<OptionProps, any> {
render(): JSX.Element;
}
export type SelectFieldValue = string | number;

export interface SelectFieldProps {
value?: SelectFieldValue;
validationMessage?: string;
testId?: string;
children: React.ReactNode;
extraClassNames?: string;
formLabelProps?: Object;
textLinkProps?: Object;
selectProps?: Object;
name: string;
id: string;
labelText?: string;
helpText?: string;
required?: boolean;
onChange?: (...args: any[]) => any;
onBlur?: (...args: any[]) => any;
}

export class SelectField extends React.Component<SelectFieldProps, any> {
render(): JSX.Element;
}

export type TooltipPlace = 'top' | 'bottom' | 'right' | 'left';

export type TooltipMaxWidth = number | string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ export class Button extends Component<ButtonProps> {
const iconColor =
buttonType === 'muted' || buttonType === 'naked' ? 'secondary' : 'white';

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const Element: any = href ? 'a' : 'button';

return (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/* eslint-disable @typescript-eslint/no-explicit-any */

import React, { Component } from 'react';
import CopyToClipboard from 'react-copy-to-clipboard';
import cn from 'classnames';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/* eslint-disable @typescript-eslint/no-explicit-any */

import React from 'react';
import { storiesOf } from '@storybook/react';
import { text, select } from '@storybook/addon-knobs';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/* eslint-disable @typescript-eslint/no-explicit-any */

import React from 'react';
import { shallow, mount } from 'enzyme';
import { axe } from 'jest-axe';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/* eslint-disable @typescript-eslint/no-explicit-any */

import React, { Component } from 'react';
import cn from 'classnames';

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import React, { Component } from 'react';

export interface OptionProps {
value: string;
children: React.ReactNode;
}

export class Option extends Component<OptionProps> {
render() {
const { value, children, ...otherProps } = this.props;

return (
<option value={value} {...otherProps}>
{children}
</option>
);
}
}

export default Option;
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import React from 'react';
import { storiesOf } from '@storybook/react';
import { action } from '@storybook/addon-actions';
import { text, boolean, select } from '@storybook/addon-knobs';
import { withInfo } from '@storybook/addon-info';

import Select from './Select';
import Option from '../Option';

storiesOf('Components|Select', module)
.addDecorator(withInfo)
.addParameters({
propTypes: [Select['__docgenInfo'], Option['__docgenInfo']],
})
.add('default', () => (
<Select
extraClassNames={text('Extra Class Names', '')}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,6 @@ it('renders the component with a value', () => {
expect(output).toMatchSnapshot();
});

it('renders the component with an onClick', () => {
const output = shallow(
<Select name="optionSelect" id="optionSelect" onClick={() => {}}>
<Option value="optionOne">Option One</Option>
</Select>,
);
expect(output).toMatchSnapshot();
});

it('renders the component as required', () => {
const output = shallow(
<Select name="optionSelect" id="optionSelect" required>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,35 @@
import React from 'react';
import PropTypes from 'prop-types';
import React, { Component } from 'react';
import cn from 'classnames';
import Icon from '../../Icon/Icon';
import styles from './Select.css';

export class Select extends React.Component {
static propTypes = {
required: PropTypes.bool,
children: PropTypes.node.isRequired,
name: PropTypes.string,
id: PropTypes.string,
hasError: PropTypes.bool,
value: PropTypes.string,
isDisabled: PropTypes.bool,
onChange: PropTypes.func,
onBlur: PropTypes.func,
testId: PropTypes.string,
onFocus: PropTypes.func,
extraClassNames: PropTypes.string,
width: PropTypes.oneOf(['auto', 'small', 'medium', 'large', 'full']),
};
export interface SelectProps {
required?: boolean;
children: React.ReactNode;
name?: string;
id?: string;
hasError?: boolean;
value?: string;
isDisabled?: boolean;
onChange?: (...args: any[]) => any;
onBlur?: (...args: any[]) => any;
testId?: string;
onFocus?: (...args: any[]) => any;
extraClassNames?: string;
width?: 'auto' | 'small' | 'medium' | 'large' | 'full';
}

export interface SelectState {
value?: string;
}

export class Select extends Component<SelectProps, SelectState> {
static defaultProps = {
name: undefined,
id: undefined,
extraClassNames: undefined,
testId: 'cf-ui-select',
required: false,
hasError: false,
isDisabled: false,
width: 'full',
value: undefined,
onBlur: () => {},
onFocus: () => {},
onChange: () => {},
Expand Down Expand Up @@ -66,13 +65,13 @@ export class Select extends React.Component {
} = this.props;

const widthClass = `Select--${width}`;
const classNames = cn(styles.Select, {
const classNames = cn(styles['Select'], {
[styles['Select--disabled']]: isDisabled,
[styles['Select--negative']]: hasError,
});

const wrapperClassNames = cn(
styles.Select__wrapper,
styles['Select__wrapper'],
styles[widthClass],
extraClassNames,
);
Expand All @@ -84,8 +83,6 @@ export class Select extends React.Component {
required={required}
name={name}
aria-label={name}
// but select doesn't support for attribute :(
htmlFor={name}
data-test-id={testId}
className={classNames}
value={this.state.value}
Expand All @@ -105,7 +102,7 @@ export class Select extends React.Component {
{children}
</select>
<Icon
extraClassNames={styles.Select__icon}
extraClassNames={styles['Select__icon']}
icon="ArrowDown"
color="muted"
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ exports[`renders the component 1`] = `
className="Select"
data-test-id="cf-ui-select"
disabled={false}
htmlFor="optionSelect"
id="optionSelect"
name="optionSelect"
onBlur={[Function]}
Expand Down Expand Up @@ -42,7 +41,6 @@ exports[`renders the component as required 1`] = `
className="Select"
data-test-id="cf-ui-select"
disabled={false}
htmlFor="optionSelect"
id="optionSelect"
name="optionSelect"
onBlur={[Function]}
Expand Down Expand Up @@ -75,7 +73,6 @@ exports[`renders the component in disabled state 1`] = `
className="Select Select--disabled"
data-test-id="cf-ui-select"
disabled={true}
htmlFor="optionSelect"
id="optionSelect"
name="optionSelect"
onBlur={[Function]}
Expand Down Expand Up @@ -108,7 +105,6 @@ exports[`renders the component with a value 1`] = `
className="Select"
data-test-id="cf-ui-select"
disabled={false}
htmlFor="optionSelect"
id="optionSelect"
name="optionSelect"
onBlur={[Function]}
Expand Down Expand Up @@ -142,7 +138,6 @@ exports[`renders the component with an extra class name 1`] = `
className="Select"
data-test-id="cf-ui-select"
disabled={false}
htmlFor="optionSelect"
id="optionSelect"
name="optionSelect"
onBlur={[Function]}
Expand All @@ -166,40 +161,6 @@ exports[`renders the component with an extra class name 1`] = `
</div>
`;

exports[`renders the component with an onClick 1`] = `
<div
className="Select__wrapper Select--full"
>
<select
aria-label="optionSelect"
className="Select"
data-test-id="cf-ui-select"
disabled={false}
htmlFor="optionSelect"
id="optionSelect"
name="optionSelect"
onBlur={[Function]}
onChange={[Function]}
onClick={[Function]}
onFocus={[Function]}
required={false}
>
<Option
value="optionOne"
>
Option One
</Option>
</select>
<Icon
color="muted"
extraClassNames="Select__icon"
icon="ArrowDown"
size="small"
testId="cf-ui-icon"
/>
</div>
`;

exports[`renders the component with error 1`] = `
<div
className="Select__wrapper Select--full"
Expand All @@ -209,7 +170,6 @@ exports[`renders the component with error 1`] = `
className="Select Select--negative"
data-test-id="cf-ui-select"
disabled={false}
htmlFor="optionSelect"
id="optionSelect"
name="optionSelect"
onBlur={[Function]}
Expand Down
Loading

0 comments on commit 2ee2c85

Please sign in to comment.