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 @@ -3,7 +3,7 @@ import { InternalDropdownItemProps, InternalDropdownItem } from './InternalDropd
import { DropdownArrowContext } from './dropdownConstants';
import { useOUIAProps, OUIAProps } from '../../helpers';

export interface DropdownItemProps extends InternalDropdownItemProps, OUIAProps {
export interface DropdownItemProps extends Omit<InternalDropdownItemProps, 'tabIndex'>, OUIAProps {
/** Anything which can be rendered as dropdown item */
children?: React.ReactNode;
/** Classes applied to root element of dropdown item */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class DropdownToggleCheckbox extends React.Component<DropdownToggleCheckb
}

handleChange = (event: React.FormEvent<HTMLInputElement>) => {
this.props.onChange((event.target as HTMLInputElement).checked, event);
this.props.onChange?.((event.target as HTMLInputElement).checked, event);
};

calculateChecked = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export class InternalDropdownItem extends React.Component<InternalDropdownItemPr
componentDidMount() {
const { context, index, isDisabled, role, customChild, autoFocus } = this.props;
const customRef = customChild ? this.getInnerNode(this.ref.current) : this.ref.current;
context.sendRef(
context?.sendRef?.(
index,
[customRef, customChild ? customRef : this.additionalRef.current],
isDisabled,
Expand All @@ -99,7 +99,7 @@ export class InternalDropdownItem extends React.Component<InternalDropdownItemPr
componentDidUpdate() {
const { context, index, isDisabled, role, customChild } = this.props;
const customRef = customChild ? this.getInnerNode(this.ref.current) : this.ref.current;
context.sendRef(
context?.sendRef?.(
index,
[customRef, customChild ? customRef : this.additionalRef.current],
isDisabled,
Expand All @@ -116,21 +116,21 @@ export class InternalDropdownItem extends React.Component<InternalDropdownItemPr
event.preventDefault();
}
if (event.key === 'ArrowUp') {
this.props.context.keyHandler(this.props.index, innerIndex, KEYHANDLER_DIRECTION.UP);
this.props.context?.keyHandler?.(this.props.index, innerIndex, KEYHANDLER_DIRECTION.UP);
event.stopPropagation();
} else if (event.key === 'ArrowDown') {
this.props.context.keyHandler(this.props.index, innerIndex, KEYHANDLER_DIRECTION.DOWN);
this.props.context?.keyHandler?.(this.props.index, innerIndex, KEYHANDLER_DIRECTION.DOWN);
event.stopPropagation();
} else if (event.key === 'ArrowRight') {
this.props.context.keyHandler(this.props.index, innerIndex, KEYHANDLER_DIRECTION.RIGHT);
this.props.context?.keyHandler?.(this.props.index, innerIndex, KEYHANDLER_DIRECTION.RIGHT);
event.stopPropagation();
} else if (event.key === 'ArrowLeft') {
this.props.context.keyHandler(this.props.index, innerIndex, KEYHANDLER_DIRECTION.LEFT);
this.props.context?.keyHandler?.(this.props.index, innerIndex, KEYHANDLER_DIRECTION.LEFT);
event.stopPropagation();
} else if (event.key === 'Enter' || event.key === ' ') {
event.target.click();
this.props.enterTriggersArrowDown &&
this.props.context.keyHandler(this.props.index, innerIndex, KEYHANDLER_DIRECTION.DOWN);
this.props.context?.keyHandler?.(this.props.index, innerIndex, KEYHANDLER_DIRECTION.DOWN);
}
};

Expand Down Expand Up @@ -264,13 +264,13 @@ export class InternalDropdownItem extends React.Component<InternalDropdownItemPr

return (
<li
className={listItemClassName || null}
{...(listItemClassName && { className: listItemClassName })}
role="none"
onKeyDown={this.onKeyDown}
onClick={(event: any) => {
if (!isDisabled && !isAriaDisabled) {
onClick(event);
onSelect(event);
onClick?.(event);
onSelect?.(event);
}
}}
id={id}
Expand Down
16 changes: 8 additions & 8 deletions packages/react-core/src/components/Dropdown/Toggle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export class Toggle extends React.Component<ToggleProps> {
const clickedOnToggle = parentRef && parentRef.current && parentRef.current.contains(event.target as Node);
const clickedWithinMenu = menuRef && menuRef.contains && menuRef.contains(event.target as Node);
if (isOpen && !(clickedOnToggle || clickedWithinMenu)) {
onToggle(false, event);
onToggle?.(false, event);
}
};

Expand All @@ -103,8 +103,8 @@ export class Toggle extends React.Component<ToggleProps> {
(event.key === KeyTypes.Escape || event.key === 'Tab') &&
(escFromToggle || escFromWithinMenu)
) {
this.props.onToggle(false, event);
this.buttonRef.current.focus();
this.props.onToggle?.(false, event);
this.buttonRef.current?.focus();
}
};

Expand All @@ -118,15 +118,15 @@ export class Toggle extends React.Component<ToggleProps> {
}
event.preventDefault();

this.props.onToggle(!this.props.isOpen, event);
this.props.onToggle?.(!this.props.isOpen, event);
} else if ((event.key === 'Enter' || event.key === ' ') && !this.props.isOpen) {
if (!this.props.bubbleEvent) {
event.stopPropagation();
}
event.preventDefault();

this.props.onToggle(!this.props.isOpen, event);
this.props.onEnter();
this.props.onToggle?.(!this.props.isOpen, event);
this.props.onEnter?.();
}
};

Expand Down Expand Up @@ -167,11 +167,11 @@ export class Toggle extends React.Component<ToggleProps> {
isPlain && styles.modifiers.plain,
isText && styles.modifiers.text,
isPrimary && styles.modifiers.primary,
buttonVariantStyles[toggleVariant],
toggleVariant && buttonVariantStyles[toggleVariant],
className
)}
type={type || 'button'}
onClick={event => onToggle(!isOpen, event)}
onClick={event => onToggle?.(!isOpen, event)}
aria-expanded={isOpen}
aria-haspopup={ariaHasPopup}
onKeyDown={event => this.onKeyDown(event)}
Expand Down
6 changes: 3 additions & 3 deletions packages/react-core/src/components/Modal/Modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export interface ModalProps extends React.HTMLProps<HTMLDivElement>, OUIAProps {
/** Optional title label text for screen readers */
titleLabel?: string;
/** Id to use for Modal Box label */
'aria-labelledby'?: string | null;
'aria-labelledby'?: string;
/** Accessible descriptor of modal */
'aria-label'?: string;
/** Id to use for Modal Box descriptor */
Expand Down Expand Up @@ -121,7 +121,7 @@ export class Modal extends React.Component<ModalProps, ModalState> {
handleEscKeyClick = (event: KeyboardEvent): void => {
const { onEscapePress } = this.props;
if (event.key === KeyTypes.Escape && this.props.isOpen) {
onEscapePress ? onEscapePress(event) : this.props.onClose();
onEscapePress ? onEscapePress(event) : this.props.onClose?.();
}
};

Expand All @@ -143,7 +143,7 @@ export class Modal extends React.Component<ModalProps, ModalState> {
}
};

isEmpty = (value: string | null) => value === null || value === undefined || value === '';
isEmpty = (value: string | null | undefined) => value === null || value === undefined || value === '';

componentDidMount() {
const {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-core/src/components/Modal/ModalBoxTitle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const ModalBoxTitle: React.FunctionComponent<ModalBoxTitleProps> = ({
...props
}: ModalBoxTitleProps) => {
const [hasTooltip, setHasTooltip] = React.useState(false);
const h1 = React.useRef<HTMLHeadingElement>();
const h1 = React.useRef<HTMLHeadingElement>(null);
const label = titleLabel || (isVariantIcon(titleIconVariant) ? `${capitalize(titleIconVariant)} alert:` : titleLabel);
const variantIcons = {
success: <CheckCircleIcon />,
Expand Down
2 changes: 1 addition & 1 deletion packages/react-core/src/components/TreeView/TreeView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export const TreeView: React.FunctionComponent<TreeViewProps> = ({
<TreeViewList isNested={isNested} toolbar={toolbar}>
{data.map(item => (
<TreeViewListItem
key={item.id?.toString() || item.name.toString()}
key={item.id?.toString() || item.name?.toString()}
name={item.name}
title={item.title}
id={item.id}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { TreeViewDataItem } from './TreeView';
import { Badge } from '../Badge';
import { GenerateId } from '../../helpers/GenerateId/GenerateId';

export interface TreeViewCheckProps extends Partial<React.InputHTMLAttributes<HTMLInputElement>> {
export interface TreeViewCheckProps extends Omit<Partial<React.InputHTMLAttributes<HTMLInputElement>>, 'checked'> {
checked?: boolean | null;
}

Expand Down
54 changes: 29 additions & 25 deletions packages/react-core/src/components/TreeView/TreeViewRoot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ export class TreeViewRoot extends React.Component<TreeViewRootProps> {
window.addEventListener('keydown', this.props.hasChecks ? this.handleKeysCheckbox : this.handleKeys);
}
if (this.props.hasChecks) {
const firstToggle = this.treeRef.current.getElementsByClassName('pf-c-tree-view__node-toggle')[0] as HTMLElement;
const firstToggle = this.treeRef.current?.getElementsByClassName('pf-c-tree-view__node-toggle')[0] as HTMLElement;
if (firstToggle) {
firstToggle.tabIndex = 0;
}
const firstInput = this.treeRef.current.getElementsByTagName('INPUT')[0] as HTMLElement;
const firstInput = this.treeRef.current?.getElementsByTagName('INPUT')[0] as HTMLElement;
if (firstInput) {
firstInput.tabIndex = 0;
}
} else {
(this.treeRef.current.getElementsByClassName('pf-c-tree-view__node')[0] as HTMLElement).tabIndex = 0;
(this.treeRef.current?.getElementsByClassName('pf-c-tree-view__node')[0] as HTMLElement).tabIndex = 0;
}
}

Expand All @@ -51,7 +51,7 @@ export class TreeViewRoot extends React.Component<TreeViewRootProps> {
}
const activeElement = document.activeElement;
const key = event.key;
const treeItems = Array.from(this.treeRef.current.getElementsByClassName('pf-c-tree-view__node')).filter(
const treeItems = Array.from(this.treeRef.current?.getElementsByClassName('pf-c-tree-view__node')).filter(
el => !el.classList.contains('pf-m-disabled')
);

Expand All @@ -72,32 +72,36 @@ export class TreeViewRoot extends React.Component<TreeViewRootProps> {
);

if (['ArrowLeft', 'ArrowRight'].includes(key)) {
const isExpandable = activeElement.firstElementChild.firstElementChild.classList.contains(
const isExpandable = activeElement?.firstElementChild?.firstElementChild?.classList.contains(
'pf-c-tree-view__node-toggle'
);
const isExpanded = activeElement.closest('li').classList.contains('pf-m-expanded');
const isExpanded = activeElement?.closest('li')?.classList.contains('pf-m-expanded');
if (key === 'ArrowLeft') {
if (isExpandable && isExpanded) {
(activeElement as HTMLElement).click();
} else {
const parentList = activeElement.closest('ul').parentElement;
if (parentList.tagName !== 'DIV') {
const parentButton = parentList.querySelector('button');
const parentList = activeElement?.closest('ul')?.parentElement;
if (parentList?.tagName !== 'DIV') {
const parentButton = parentList?.querySelector('button');
(activeElement as HTMLElement).tabIndex = -1;
parentButton.tabIndex = 0;
parentButton.focus();
if (parentButton) {
parentButton.tabIndex = 0;
parentButton.focus();
}
}
}
} else {
if (isExpandable && !isExpanded) {
(activeElement as HTMLElement).tabIndex = -1;
(activeElement as HTMLElement).click();
const childElement = activeElement
.closest('li')
.querySelector('ul > li')
.querySelector('button');
childElement.tabIndex = 0;
childElement.focus();
?.closest('li')
?.querySelector('ul > li')
?.querySelector('button');
if (childElement) {
childElement.tabIndex = 0;
childElement.focus();
}
}
}
event.preventDefault();
Expand All @@ -116,7 +120,7 @@ export class TreeViewRoot extends React.Component<TreeViewRootProps> {
event.preventDefault();
}

const treeNodes = Array.from(this.treeRef.current.getElementsByClassName('pf-c-tree-view__node'));
const treeNodes = Array.from(this.treeRef.current?.getElementsByClassName('pf-c-tree-view__node'));

handleArrows(
event,
Expand All @@ -131,21 +135,21 @@ export class TreeViewRoot extends React.Component<TreeViewRootProps> {

if (['ArrowLeft', 'ArrowRight'].includes(key)) {
if (key === 'ArrowLeft') {
if (activeElement.tagName === 'INPUT') {
activeElement.parentElement.previousSibling &&
if (activeElement?.tagName === 'INPUT') {
activeElement?.parentElement?.previousSibling &&
(activeElement.parentElement.previousSibling as HTMLElement).focus();
} else if (activeElement.previousSibling) {
if (activeElement.previousElementSibling.tagName === 'SPAN') {
} else if (activeElement?.previousSibling) {
if (activeElement.previousElementSibling?.tagName === 'SPAN') {
(activeElement.previousSibling.firstChild as HTMLElement).focus();
} else {
(activeElement.previousSibling as HTMLElement).focus();
}
}
} else {
if (activeElement.tagName === 'INPUT') {
activeElement.parentElement.nextSibling && (activeElement.parentElement.nextSibling as HTMLElement).focus();
} else if (activeElement.nextSibling) {
if (activeElement.nextElementSibling.tagName === 'SPAN') {
if (activeElement?.tagName === 'INPUT') {
activeElement.parentElement?.nextSibling && (activeElement.parentElement.nextSibling as HTMLElement).focus();
} else if (activeElement?.nextSibling) {
if (activeElement.nextElementSibling?.tagName === 'SPAN') {
(activeElement.nextSibling.firstChild as HTMLElement).focus();
} else {
(activeElement.nextSibling as HTMLElement).focus();
Expand Down