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 EuiSwitch to TS #2243

Merged
merged 6 commits into from
Aug 21, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 0 additions & 1 deletion src/components/form/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ 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,6 +5,7 @@ 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 @@ -40,7 +41,9 @@ exports[`EuiSwitch assigns automatically generated ID to label 1`] = `
<label
class="euiSwitch__label"
for="generated-id"
/>
>
Label
</label>
</div>
`;

Expand All @@ -49,6 +52,7 @@ 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 @@ -86,6 +90,8 @@ exports[`EuiSwitch is rendered 1`] = `
<label
class="euiSwitch__label"
for="test"
/>
>
Label
</label>
</div>
`;
21 changes: 0 additions & 21 deletions src/components/form/switch/index.d.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/components/form/switch/index.js

This file was deleted.

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

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,25 @@ 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" {...requiredProps} />);
const component = render(
<EuiSwitch id="test" {...props} {...requiredProps} />
);

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

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

expect(component).toMatchSnapshot();
});
Expand Down
77 changes: 77 additions & 0 deletions src/components/form/switch/switch.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import React, {
ButtonHTMLAttributes,
FunctionComponent,
ReactNode,
} from 'react';
import classNames from 'classnames';

import { CommonProps, Omit } from '../../common';
import makeId from '../../form/form_row/make_id';
import { EuiIcon } from '../../icon';

export type EuiSwitchProps = CommonProps &
Omit<ButtonHTMLAttributes<HTMLButtonElement>, 'onChange'> & {
label: ReactNode;
checked: boolean;
onChange: (event: React.ChangeEvent<HTMLInputElement>) => void;
disabled?: boolean;
compressed?: boolean;
};

export const EuiSwitch: FunctionComponent<EuiSwitchProps> = ({
label,
id,
name,
checked,
disabled,
compressed,
onChange,
className,
...rest
}) => {
const switchId = id || makeId();
thompsongl marked this conversation as resolved.
Show resolved Hide resolved

const onClick = (e: React.MouseEvent<HTMLButtonElement>) => {
const event = (e as unknown) as React.ChangeEvent<HTMLInputElement>;
thompsongl marked this conversation as resolved.
Show resolved Hide resolved
event.target.checked = !checked;
onChange(event);
};

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={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>
);
};
11 changes: 11 additions & 0 deletions src/components/form/switch/test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import React from 'react';
thompsongl marked this conversation as resolved.
Show resolved Hide resolved

import { EuiSwitch } from './switch';

export const Test = ({ checked }: { checked: boolean }) => (
<EuiSwitch
checked={checked}
label="Test"
onChange={e => console.log(e.target.checked)}
/>
);