forked from elastic/eui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert
EuiSwitch
to TS (elastic#2243)
* convert euiswitch to ts * remove test file * euiswitchevent; store id in state * change interface to type * CL
- Loading branch information
1 parent
68093b1
commit 39dbcf6
Showing
9 changed files
with
107 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { EuiSwitch, EuiSwitchEvent, EuiSwitchProps } from './switch'; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import React, { | ||
ButtonHTMLAttributes, | ||
FunctionComponent, | ||
ReactNode, | ||
useState, | ||
} 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 EuiSwitchEvent = React.BaseSyntheticEvent< | ||
React.MouseEvent<HTMLButtonElement>, | ||
HTMLButtonElement, | ||
EventTarget & { | ||
checked: boolean; | ||
} | ||
>; | ||
|
||
export type EuiSwitchProps = CommonProps & | ||
Omit<ButtonHTMLAttributes<HTMLButtonElement>, 'onChange'> & { | ||
label: ReactNode; | ||
checked: boolean; | ||
onChange: (event: EuiSwitchEvent) => void; | ||
disabled?: boolean; | ||
compressed?: boolean; | ||
}; | ||
|
||
export const EuiSwitch: FunctionComponent<EuiSwitchProps> = ({ | ||
label, | ||
id, | ||
name, | ||
checked, | ||
disabled, | ||
compressed, | ||
onChange, | ||
className, | ||
...rest | ||
}) => { | ||
const [switchId] = useState(id || makeId()); | ||
|
||
const onClick = (e: React.MouseEvent<HTMLButtonElement>) => { | ||
const event = (e as unknown) as EuiSwitchEvent; | ||
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> | ||
); | ||
}; |