Skip to content

feat(DualListSelector): enabled opt-in animations #11837

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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 @@ -17,27 +17,41 @@ export interface DualListSelectorProps {
isTree?: boolean;
/** Content to be rendered in the dual list selector. */
children?: React.ReactNode;
/** Flag indicating whether a tree dual list selector has animations. This will always render
* nested dual list selector items rather than dynamically rendering them. This prop will be removed in
* the next breaking change release in favor of defaulting to always-rendered items.
*/
hasAnimations?: boolean;
}

class DualListSelector extends Component<DualListSelectorProps> {
static displayName = 'DualListSelector';
static defaultProps: PickOptional<DualListSelectorProps> = {
children: '',
isTree: false
isTree: false,
hasAnimations: false
};

constructor(props: DualListSelectorProps) {
super(props);
}

render() {
const { className, children, id, isTree, ...props } = this.props;
const { className, children, id, isTree, hasAnimations, ...props } = this.props;

return (
<DualListSelectorContext.Provider value={{ isTree }}>
<DualListSelectorContext.Provider value={{ isTree, hasAnimations }}>
<GenerateId>
{(randomId) => (
<div className={css(styles.dualListSelector, className)} id={id || randomId} {...props}>
<div
className={css(
styles.dualListSelector,
hasAnimations && isTree && styles.modifiers.animateExpand,
className
)}
id={id || randomId}
{...props}
>
{children}
</div>
)}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { createContext } from 'react';
export const DualListSelectorContext = createContext<{
isTree?: boolean;
}>({ isTree: false });
hasAnimations?: boolean;
}>({ isTree: false, hasAnimations: false });

export const DualListSelectorListContext = createContext<{
setFocusedOption?: (id: string) => void;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,17 @@ export const DualListSelectorListWrapperBase: React.FunctionComponent<DualListSe
}
event.stopImmediatePropagation();
const validOptions = isTree
? (Array.from(
menuRef.current.querySelectorAll(
`.${styles.dualListSelectorItemToggle}, .${styles.dualListSelectorItemCheck} > input`
)
) as Element[])
? (
Array.from(
menuRef.current.querySelectorAll(
`.${styles.dualListSelectorItemToggle}, .${styles.dualListSelectorItemCheck} > input`
)
) as Element[]
).filter((item) => !item.closest(`.${styles.dualListSelectorList}[inert]`))
: (Array.from(menuRef.current.getElementsByTagName('LI')) as Element[]).filter(
(el) => !el.classList.contains('pf-m-disabled')
);

const activeElement = document.activeElement;
handleArrows(
event,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { useContext } from 'react';
import { css } from '@patternfly/react-styles';
import styles from '@patternfly/react-styles/css/components/DualListSelector/dual-list-selector';
import { DualListSelectorContext } from './DualListSelectorContext';
import { DualListSelectorTreeItem } from './DualListSelectorTreeItem';

export interface DualListSelectorTreeItemData {
Expand Down Expand Up @@ -68,11 +70,13 @@ export const DualListSelectorTree: React.FunctionComponent<DualListSelectorTreeP
isDisabled = false,
...props
}: DualListSelectorTreeProps) => {
const { hasAnimations } = useContext(DualListSelectorContext);
const dataToRender = typeof data === 'function' ? data() : data;
const tree = dataToRender.map((item) => (
<DualListSelectorTreeItem
key={item.id}
text={item.text}
hasAnimations={hasAnimations}
id={item.id}
defaultExpanded={item.defaultExpanded !== undefined ? item.defaultExpanded : defaultAllExpanded}
onOptionCheck={onOptionCheck}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { memo, useContext, useEffect, useRef, useState } from 'react';
import { memo, useContext, useEffect, useRef, useState, cloneElement, Children, isValidElement } from 'react';
import styles from '@patternfly/react-styles/css/components/DualListSelector/dual-list-selector';
import { css } from '@patternfly/react-styles';
import { DualListSelectorTreeItemData } from './DualListSelectorTree';
Expand Down Expand Up @@ -38,6 +38,11 @@ export interface DualListSelectorTreeItemProps extends React.HTMLProps<HTMLLIEle
isDisabled?: boolean;
/** Flag indicating the DualListSelector tree should utilize memoization to help render large data sets. */
useMemo?: boolean;
/** Flag indicating whether a tree dual list selector has animations. This will always render
* nested dual list selector items rather than dynamically rendering them. This prop will be removed in
* the next breaking change release in favor of defaulting to always-rendered items.
*/
hasAnimations?: boolean;
}

const DualListSelectorTreeItemBase: React.FunctionComponent<DualListSelectorTreeItemProps> = ({
Expand All @@ -53,6 +58,7 @@ const DualListSelectorTreeItemBase: React.FunctionComponent<DualListSelectorTree
badgeProps,
itemData,
isDisabled = false,
hasAnimations,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
useMemo,
...props
Expand All @@ -65,6 +71,15 @@ const DualListSelectorTreeItemBase: React.FunctionComponent<DualListSelectorTree
setIsExpanded(defaultExpanded);
}, [defaultExpanded]);

const clonedChildren = Children.map(
children,
(child) =>
isValidElement(child) &&
cloneElement(child as React.ReactElement<any>, {
inert: isExpanded ? undefined : ''
})
);

return (
<li
className={css(
Expand Down Expand Up @@ -156,7 +171,7 @@ const DualListSelectorTreeItemBase: React.FunctionComponent<DualListSelectorTree
</span>
</div>
</div>
{isExpanded && children}
{(isExpanded || hasAnimations) && clonedChildren}
</li>
);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,37 @@
import { render } from '@testing-library/react';
import { render, screen } from '@testing-library/react';
import styles from '@patternfly/react-styles/css/components/DualListSelector/dual-list-selector';
import { DualListSelector } from '../DualListSelector';
import { DualListSelectorPane } from '../DualListSelectorPane';
import { SearchInput } from '../../SearchInput';

// The following tests can be removed as part of https://github.com/patternfly/patternfly-react/issues/11838
describe('Opt-in animations', () => {
test(`Does not render with class ${styles.modifiers.animateExpand} by default`, () => {
render(<DualListSelector data-testid="test-id" />);

expect(screen.getByTestId('test-id')).not.toHaveClass(styles.modifiers.animateExpand);
});

test(`Does not render with class ${styles.modifiers.animateExpand} when hasAnimations is true and isTree is false`, () => {
render(<DualListSelector hasAnimations data-testid="test-id" />);

expect(screen.getByTestId('test-id')).not.toHaveClass(styles.modifiers.animateExpand);
});

test(`Does not render with class ${styles.modifiers.animateExpand} by default when isTree is true`, () => {
render(<DualListSelector isTree data-testid="test-id" />);

expect(screen.getByTestId('test-id')).not.toHaveClass(styles.modifiers.animateExpand);
});

test(`Renders with class ${styles.modifiers.animateExpand} when both isTree and hasAnimations are true`, () => {
render(<DualListSelector isTree hasAnimations data-testid="test-id" />);

expect(screen.getByTestId('test-id')).toHaveClass(styles.modifiers.animateExpand);
});
});

// Following tests should be moved to a separate DualListSelectorPane test file
describe('DualListSelector', () => {
test('basic', () => {
const { asFragment } = render(<DualListSelectorPane id="basicTest" />);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { render, screen } from '@testing-library/react';
import styles from '@patternfly/react-styles/css/components/DualListSelector/dual-list-selector';
import { DualListSelectorTreeItem } from '../DualListSelectorTreeItem';

// The following tests checking for children to not be/to be rendered will need to be refactored
// as part of https://github.com/patternfly/patternfly-react/issues/11838
test('Does not render children by default', () => {
render(
<DualListSelectorTreeItem id="item-id" text="Test text">
<div>Children content</div>
</DualListSelectorTreeItem>
);

expect(screen.queryByText('Children content')).not.toBeInTheDocument();
});

test('Renders children when defaultExpanded is true', () => {
render(
<DualListSelectorTreeItem defaultExpanded id="item-id" text="Test text">
<div>Children content</div>
</DualListSelectorTreeItem>
);

expect(screen.getByText('Children content')).toBeVisible();
});

test('Renders children when hasAnimations is true', () => {
render(
<DualListSelectorTreeItem hasAnimations id="item-id" text="Test text">
<div>Children content</div>
</DualListSelectorTreeItem>
);

expect(screen.getByText('Children content')).toBeVisible();
});

test('Renders children with inert attribute by default when hasAnimations is true', () => {
render(
<DualListSelectorTreeItem hasAnimations id="item-id" text="Test text">
<div>Children content</div>
</DualListSelectorTreeItem>
);

expect(screen.getByText('Children content')).toHaveAttribute('inert', '');
});

test('Does not render children with inert attribute when hasAnimations and defaultExpanded are true', () => {
render(
<DualListSelectorTreeItem hasAnimations defaultExpanded id="item-id" text="Test text">
<div>Children content</div>
</DualListSelectorTreeItem>
);

expect(screen.getByText('Children content')).not.toHaveAttribute('inert');
});
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ export const DualListSelectorComposableTree: React.FunctionComponent<ExampleProp
};

return (
<DualListSelector isTree>
<DualListSelector hasAnimations isTree>
{buildPane(false)}
<DualListSelectorControlsWrapper>
<DualListSelectorControl
Expand Down
4 changes: 2 additions & 2 deletions packages/react-core/src/helpers/KeyboardHandler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,12 @@ export const handleArrows = (
: nextSiblingMainElement.nextElementSibling;

if (nextSibling) {
if (validSiblingTags.includes(nextSibling.tagName)) {
if (validSiblingTags.includes(nextSibling?.tagName)) {
moveTarget = nextSibling;
break;
}
// For cases where the validSiblingTag is inside a div wrapper
if (validSiblingTags.includes(nextSibling.children[0].tagName)) {
if (validSiblingTags.includes(nextSibling.children[0]?.tagName)) {
moveTarget = nextSibling.children[0];
break;
}
Expand Down
Loading