Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { css } from '@patternfly/react-styles';
import { CheckIcon } from '@patternfly/react-icons';
import { getUniqueId } from '../../helpers/util';
import { Omit } from '../../helpers/typeUtils';
import { isOUIAEnvironment, getUniqueId as getOUIAUniqueId } from '../../helpers/ouia';

export interface SwitchProps extends Omit<React.HTMLProps<HTMLInputElement>, 'type' | 'onChange' | 'disabled' | 'label'> {
/** id for the label. */
Expand All @@ -24,6 +25,7 @@ export interface SwitchProps extends Omit<React.HTMLProps<HTMLInputElement>, 'ty

export class Switch extends React.Component<SwitchProps> {
id = '';
ouiaId = getOUIAUniqueId();

static defaultProps = {
id: '',
Expand All @@ -47,7 +49,14 @@ export class Switch extends React.Component<SwitchProps> {
render() {
const { className, label, isChecked, isDisabled, onChange, ...props } = this.props;
return (
<label className={css(styles.switch, className)} htmlFor={this.id}>
<label
className={css(styles.switch, className)}
htmlFor={this.id}
{...isOUIAEnvironment() && {
'data-ouia-component-type': 'Switch',
'data-ouia-component-id': this.ouiaId
}}
>
<input
{...props}
id={this.id}
Expand Down
4 changes: 4 additions & 0 deletions packages/patternfly-4/react-core/src/helpers/ouia.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const isOUIAEnvironment = (): boolean => typeof window !== 'undefined' && window.localStorage.ouia;

let id = 0;
export const getUniqueId = (): number => id++;