Skip to content

Commit

Permalink
Revert "Convert EuiSwitch to TS (elastic#2243)"
Browse files Browse the repository at this point in the history
This reverts commit 39dbcf6.
  • Loading branch information
thompsongl committed Aug 23, 2019
1 parent b859ad0 commit 25c4635
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 105 deletions.
1 change: 1 addition & 0 deletions src/components/form/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { CommonProps } from '../common';
/// <reference path="./range/index.d.ts" />
/// <reference path="./select/index.d.ts" />
/// <reference path="./super_select/index.d.ts" />
/// <reference path="./switch/index.d.ts" />
/// <reference path="./text_area/index.d.ts" />

import { FunctionComponent, FormHTMLAttributes, ReactNode } from 'react';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ exports[`EuiSwitch assigns automatically generated ID to label 1`] = `
class="euiSwitch"
>
<button
aria-checked="false"
class="euiSwitch__button"
id="generated-id"
role="switch"
Expand Down Expand Up @@ -41,9 +40,7 @@ exports[`EuiSwitch assigns automatically generated ID to label 1`] = `
<label
class="euiSwitch__label"
for="generated-id"
>
Label
</label>
/>
</div>
`;

Expand All @@ -52,7 +49,6 @@ exports[`EuiSwitch is rendered 1`] = `
class="euiSwitch testClass1 testClass2"
>
<button
aria-checked="false"
aria-label="aria-label"
class="euiSwitch__button"
data-test-subj="test subject string"
Expand Down Expand Up @@ -90,8 +86,6 @@ exports[`EuiSwitch is rendered 1`] = `
<label
class="euiSwitch__label"
for="test"
>
Label
</label>
/>
</div>
`;
21 changes: 21 additions & 0 deletions src/components/form/switch/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { CommonProps } from '../../common';

import { FunctionComponent, ButtonHTMLAttributes, ReactNode } from 'react';

declare module '@elastic/eui' {
/**
* @see './switch.js'
*/
export type EuiSwitchProps = CommonProps &
ButtonHTMLAttributes<HTMLButtonElement> & {
label: ReactNode;
checked: boolean;
onChange: (
event: React.FormEvent<HTMLButtonElement & { checked: boolean }>
) => void;
disabled?: boolean;
compressed?: boolean;
};

export const EuiSwitch: FunctionComponent<EuiSwitchProps>;
}
1 change: 1 addition & 0 deletions src/components/form/switch/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { EuiSwitch } from './switch';
1 change: 0 additions & 1 deletion src/components/form/switch/index.ts

This file was deleted.

86 changes: 86 additions & 0 deletions src/components/form/switch/switch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import React, { Component } from 'react';

import PropTypes from 'prop-types';
import classNames from 'classnames';

import makeId from '../../form/form_row/make_id';
import { EuiIcon } from '../../icon';

export class EuiSwitch extends Component {
constructor(props) {
super(props);

this.state = {
switchId: props.id || makeId(),
};
}

onClick = e => {
e.target.checked = !this.props.checked;
this.props.onChange(e);
};

render() {
const {
label,
id,
name,
checked,
disabled,
compressed,
onChange,
className,
...rest
} = this.props;

const { switchId } = this.state;

const classes = classNames(
'euiSwitch',
{
'euiSwitch--compressed': compressed,
},
className
);

return (
<div className={classes}>
<button
id={switchId}
aria-checked={checked}
className="euiSwitch__button"
role="switch"
disabled={disabled}
onClick={this.onClick}
{...rest}>
<span className="euiSwitch__body">
<span className="euiSwitch__thumb" />
<span className="euiSwitch__track">
<EuiIcon type="cross" size="m" className="euiSwitch__icon" />

<EuiIcon
type="check"
size="m"
className="euiSwitch__icon euiSwitch__icon--checked"
/>
</span>
</span>
</button>

<label className="euiSwitch__label" htmlFor={switchId}>
{label}
</label>
</div>
);
}
}

EuiSwitch.propTypes = {
name: PropTypes.string,
id: PropTypes.string,
label: PropTypes.node,
checked: PropTypes.bool,
onChange: PropTypes.func,
disabled: PropTypes.bool,
compressed: PropTypes.bool,
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,17 @@ import { requiredProps } from '../../../test/required_props';

import { EuiSwitch } from './switch';

const props = {
checked: false,
label: 'Label',
onChange: () => {},
};

jest.mock('../form_row/make_id', () => () => 'generated-id');

describe('EuiSwitch', () => {
test('is rendered', () => {
const component = render(
<EuiSwitch id="test" {...props} {...requiredProps} />
);
const component = render(<EuiSwitch id="test" {...requiredProps} />);

expect(component).toMatchSnapshot();
});

test('assigns automatically generated ID to label', () => {
const component = render(<EuiSwitch {...props} />);
const component = render(<EuiSwitch />);

expect(component).toMatchSnapshot();
});
Expand Down
86 changes: 0 additions & 86 deletions src/components/form/switch/switch.tsx

This file was deleted.

0 comments on commit 25c4635

Please sign in to comment.