Skip to content
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

[EuiSelectable] Fix text truncation when a scrollbar is present #7392

Merged
merged 5 commits into from
Dec 3, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
3 changes: 3 additions & 0 deletions changelogs/upcoming/7392.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
**Bug fixes**

- Fixed a bug with `EuiSelectable`s with custom `truncationProps`, where scrollbar widths were not being accounted for
1 change: 1 addition & 0 deletions src-docs/src/views/selectable/selectable_truncation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ export default () => {
searchable={true}
options={options}
onChange={(updatedOptions) => setOptions(updatedOptions)}
height={100}
listProps={{
isVirtualized: textWrap !== 'wrap',
textWrap,
Expand Down
17 changes: 17 additions & 0 deletions src/components/selectable/selectable.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,23 @@ describe('EuiSelectable', () => {
);
});

it('correctly accounts for scrollbar width', () => {
const multipleOptions = Array.from({ length: 5 }).map(
() => sharedProps.options[0]
);
cy.realMount(
<EuiSelectableListboxOnly
{...truncationProps}
height={100}
options={multipleOptions}
/>
);

cy.get('[data-test-subj="truncatedText"]')
.first()
.should('have.text', 'Lorem ipsum …iscing elit.');
});

it('correctly accounts for the keyboard focus badge', () => {
cy.realMount(<EuiSelectableListboxOnly {...truncationProps} />);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,13 @@ describe('EuiSelectableListItem', () => {
});

describe('truncation performance optimization', () => {
// Mock requestAnimationFrame
beforeEach(() => {
jest
.spyOn(window, 'requestAnimationFrame')
.mockImplementation((cb: Function) => cb());
});

it('does not render EuiTextTruncate if not virtualized and text is wrapping', () => {
const { container } = render(
<EuiSelectableList
Expand Down Expand Up @@ -395,6 +402,12 @@ describe('EuiSelectableListItem', () => {
});

it('attempts to use a default optimized option width calculated from the wrapping EuiAutoSizer', () => {
// jsdom doesn't return valid element offsetWidths, so we have to mock it here
Object.defineProperty(HTMLElement.prototype, 'offsetWidth', {
configurable: true,
value: 600,
});

const { container } = render(
<EuiSelectableList
options={options}
Expand All @@ -409,6 +422,9 @@ describe('EuiSelectableListItem', () => {
expect(
container.querySelector('[data-resize-observer]')
).not.toBeInTheDocument();

// Reset jsdom mock
Object.defineProperty(HTMLElement.prototype, 'offsetWidth', { value: 0 });
});

it('falls back to individual resize observers if options have append/prepend nodes', () => {
Expand Down
24 changes: 16 additions & 8 deletions src/components/selectable/selectable_list/selectable_list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -479,15 +479,23 @@ export class EuiSelectableList<T> extends Component<
const checkedIconOffset = this.props.showIcons === false ? 0 : 28; // Defaults to true
this.focusBadgeOffset = this.props.onFocusBadge === false ? 0 : 46;

this.setState({
defaultOptionWidth: containerWidth - paddingOffset - checkedIconOffset,
});
// Wait a tick for the listbox ref to update before proceeding
requestAnimationFrame(() => {
const scrollbarOffset = this.listBoxRef
? containerWidth - this.listBoxRef.offsetWidth
: 0;

// Potentially force list rows to rerender on dynamic resize as well,
// but try to do it as lightly as possible
if (truncationProps || (searchable && searchValue)) {
this.forceVirtualizedListRowRerender();
}
this.setState({
defaultOptionWidth:
containerWidth - scrollbarOffset - paddingOffset - checkedIconOffset,
});

// Potentially force list rows to rerender on dynamic resize as well,
// but try to do it as lightly as possible
if (truncationProps || (searchable && searchValue)) {
this.forceVirtualizedListRowRerender();
}
});
};

getTruncationProps = (option: EuiSelectableOption, isFocused: boolean) => {
Expand Down
Loading