Skip to content

Commit

Permalink
make switch use a random id if none is provided
Browse files Browse the repository at this point in the history
  • Loading branch information
snide committed May 5, 2018
1 parent a7c341b commit 3eb1abd
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 51 deletions.
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
111 changes: 64 additions & 47 deletions src/components/form/switch/switch.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,77 @@
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,
onChange,
className,
...rest
}) => {
const classes = classNames('euiSwitch', className);
export class EuiSwitch extends Component {
constructor(props) {
super(props);

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

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

return (
<div className={classes}>
<input
className="euiSwitch__input"
name={name}
id={id}
type="checkbox"
checked={checked}
disabled={disabled}
onChange={onChange}
{...rest}
/>
const { id } = this.state;

<span className="euiSwitch__body">
<span className="euiSwitch__thumb" />
<span className="euiSwitch__track">
<EuiIcon
type="cross"
size="m"
className="euiSwitch__icon"
/>
const classes = classNames('euiSwitch', className);

<EuiIcon
type="check"
size="m"
className="euiSwitch__icon euiSwitch__icon--checked"
/>
return (
<div className={classes}>
<input
className="euiSwitch__input"
name={name}
id={id}
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>
</span>

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

EuiSwitch.propTypes = {
name: PropTypes.string,
Expand Down

0 comments on commit 3eb1abd

Please sign in to comment.