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

Make switch use a random id if none is provided #779

Merged
merged 5 commits into from
Jul 19, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

No public interface changes since `3.1.0`.

**Bug Fixes**

- To make it more accessible, added a random id to `EuiSwitch`'s id prop if none is passed. ([#779](https://github.com/elastic/eui/pull/779))


## [`3.1.0`](https://github.com/elastic/eui/tree/v3.1.0)

- Added `EuiMutationObserver` to expose Mutation Observer API to React components ([#966](https://github.com/elastic/eui/pull/966))
Expand Down
4 changes: 0 additions & 4 deletions src-docs/src/views/form_controls/switch.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import {
EuiSpacer,
} from '../../../../src/components';

import makeId from '../../../../src/components/form/form_row/make_id';

export default class extends Component {
constructor(props) {
super(props);
Expand All @@ -29,7 +27,6 @@ export default class extends Component {
return (
<Fragment>
<EuiSwitch
id={makeId()}
label="I am a switch"
checked={this.state.checked}
onChange={this.onChange}
Expand All @@ -38,7 +35,6 @@ export default class extends Component {
<EuiSpacer size="m" />

<EuiSwitch
id={makeId()}
label="I am a disabled switch"
checked={this.state.checked}
onChange={this.onChange}
Expand Down
131 changes: 74 additions & 57 deletions src/components/form/switch/switch.js
Original file line number Diff line number Diff line change
@@ -1,70 +1,87 @@
import React from 'react';
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 const EuiSwitch = ({
label,
id,
name,
checked,
disabled,
compressed,
onChange,
className,
...rest
}) => {
const classes = classNames(
'euiSwitch',
{
'euiSwitch--compressed': compressed,
},
className
);
export class EuiSwitch extends Component {
constructor(props) {
super(props);

return (
<div className={classes}>
<input
className="euiSwitch__input"
name={name}
id={id}
type="checkbox"
checked={checked}
disabled={disabled}
onChange={onChange}
{...rest}
/>
this.state = {
id: props.id || makeId(),
};
}

<span className="euiSwitch__body">
<span className="euiSwitch__thumb" />
<span className="euiSwitch__track">
<EuiIcon
type="cross"
size="m"
className="euiSwitch__icon"
/>
render() {
const {
label,
id,
name,
checked,
disabled,
compressed,
onChange,
className,
...rest
} = this.props;

<EuiIcon
type="check"
size="m"
className="euiSwitch__icon euiSwitch__icon--checked"
/>
</span>
</span>
const { switchId } = this.state;

{ label &&
<label
className="euiSwitch__label"
htmlFor={id}
>
{label}
</label>
}
const classes = classNames(
'euiSwitch',
{
'euiSwitch--compressed': compressed,
},
className
);

</div>
);
};
return (
<div className={classes}>
<input
className="euiSwitch__input"
name={name}
id={switchId}
type="checkbox"
checked={checked}
disabled={disabled}
onChange={onChange}
{...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>

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

EuiSwitch.propTypes = {
name: PropTypes.string,
Expand Down
2 changes: 1 addition & 1 deletion src/components/form/switch/switch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { EuiSwitch } from './switch';
describe('EuiSwitch', () => {
test('is rendered', () => {
const component = render(
<EuiSwitch {...requiredProps} />
<EuiSwitch id="test" {...requiredProps} />
Copy link
Contributor

Choose a reason for hiding this comment

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

Alternatively, you can mock the makeId function; having two tests, one where you pass id and one where id is generated, would provide the best coverage of the component's id logic.

);

expect(component)
Expand Down