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

fix(Combobox): Improve performance #1771

Merged
merged 53 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
24fa39f
fix(Combobox): Re-renders
Barsnes Apr 8, 2024
1f2a6b4
some more optimizations
Barsnes Apr 8, 2024
76a741c
options as object to lookup faster
Barsnes Apr 8, 2024
2c9be2a
update tests, some misc
Barsnes Apr 8, 2024
b5fdded
`selectedOptions` is an array
Barsnes Apr 8, 2024
3c69ad5
build errors
Barsnes Apr 8, 2024
98d44be
add consolelogs to figure out why options renders so much
Barsnes Apr 8, 2024
b81e9a7
misc work for options and lookup
Barsnes Apr 8, 2024
18d87f2
more console logs, add `useComboboxOption`
Barsnes Apr 8, 2024
5e266d2
move filter to useCombobox
Barsnes Apr 8, 2024
a9de233
more hooks (remember; add reducer to context)
Barsnes Apr 8, 2024
b5776a0
activeIndex working
Barsnes Apr 9, 2024
45d086c
fix keydown Enter
Barsnes Apr 9, 2024
e746f30
add missing test for enter
Barsnes Apr 9, 2024
e3a4e08
keyboard func hook
Barsnes Apr 9, 2024
955e8f7
start with adding context
Barsnes Apr 9, 2024
69236f4
more work on context, and moving around
Barsnes Apr 9, 2024
9f228e2
fix keyboard navigation
Barsnes Apr 9, 2024
b7f24f9
more keyboard
Barsnes Apr 9, 2024
03a6d50
more keyboard that comes from listnav
Barsnes Apr 9, 2024
4dffec5
refactor active descendant
Barsnes Apr 9, 2024
1ffac0b
more list optimization
Barsnes Apr 9, 2024
32c402c
remove console logs, cleanup
Barsnes Apr 9, 2024
dc91645
more options on story
Barsnes Apr 9, 2024
6c8e829
fix custom children
Barsnes Apr 10, 2024
3fb48b1
refactor filtering
Barsnes Apr 10, 2024
1009446
fix build errors
Barsnes Apr 10, 2024
e12537d
typescript enchancement
Barsnes Apr 10, 2024
7053ef2
fix length check
Barsnes Apr 10, 2024
a51278b
fix build error, update floating-ui
Barsnes Apr 22, 2024
ab8232d
check length equal or greater than 0
Barsnes Apr 22, 2024
0006fe3
Publish
mimarz Apr 24, 2024
8e2e8eb
only run children once
Barsnes Apr 25, 2024
ca44956
reducer for children
Barsnes Apr 25, 2024
b3defbe
early returns
Barsnes Apr 25, 2024
40b8216
fix single input reset
Barsnes Apr 25, 2024
cff4b8b
types
Barsnes Apr 25, 2024
368ac9e
Merge branch 'main' into test-fix/combobox
Barsnes Apr 25, 2024
8481123
Merge branch 'main' into test-fix/combobox
Barsnes Apr 25, 2024
3bbb62d
remove old prop
Barsnes Apr 25, 2024
b1f4458
Merge branch 'main' into test-fix/combobox
Barsnes Apr 26, 2024
226fc5e
Remove focus state from `ComboboxInput`
Barsnes Apr 26, 2024
c60908d
remove duplicate code
Barsnes Apr 26, 2024
43ead37
rework handle value changing
Barsnes Apr 26, 2024
a8d7c59
move props, and remove unused
Barsnes Apr 26, 2024
a3debda
some more work on props
Barsnes Apr 26, 2024
00619b1
remove console logs
Barsnes Apr 26, 2024
7dd6017
built css
mimarz Apr 26, 2024
7e9e9ba
Publish
mimarz Apr 26, 2024
f44dc06
Merge branch 'main' into test-fix/combobox
Barsnes May 7, 2024
4a1886b
Merge branch 'main' into test-fix/combobox
mimarz May 7, 2024
1031476
fix closed when typing
Barsnes May 7, 2024
1f8b784
Merge branch 'main' into test-fix/combobox
Barsnes May 8, 2024
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
10 changes: 10 additions & 0 deletions packages/css/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

## [0.6.1-alpha.1](https://github.com/digdir/designsystemet/compare/@digdir/[email protected]...@digdir/[email protected]) (2024-04-26)

### Bug Fixes

- **Modal:** fix close button position ([#1877](https://github.com/digdir/designsystemet/issues/1877)) ([c866710](https://github.com/digdir/designsystemet/commit/c866710cc00760a8f1a4f1676e2c8a5eda235a72))

## [0.6.1-alpha.0](https://github.com/digdir/designsystemet/compare/@digdir/[email protected]...@digdir/[email protected]) (2024-04-24)

**Note:** Version bump only for package @digdir/designsystemet-css

# [0.6.0](https://github.com/digdir/designsystemet/compare/@digdir/[email protected]...@digdir/[email protected]) (2024-04-23)

### Bug Fixes
Expand Down
2 changes: 1 addition & 1 deletion packages/css/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@digdir/designsystemet-css",
"version": "0.6.0",
"version": "0.6.1-alpha.1",
"description": "CSS for Designsystemet",
"author": "Designsystemet team",
"repository": "https://github.com/digdir/designsystemet",
Expand Down
2 changes: 1 addition & 1 deletion packages/css/react-css-modules.css
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@
/**
* Apply a focus outline on an element when it is focused with keyboard
*/
.fds-combobox-inFocus-249a725c {
.fds-combobox-inputWrapper-249a725c:has(input:focus) {
--fds-focus-border-width: 3px;

outline: var(--fds-focus-border-width) solid var(--fds-semantic-border-focus-outline);
Expand Down
8 changes: 8 additions & 0 deletions packages/react-old/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

## [0.53.10-alpha.1](https://github.com/digdir/designsystemet/compare/@digdir/[email protected]...@digdir/[email protected]) (2024-04-26)

**Note:** Version bump only for package @digdir/design-system-react

## [0.53.10-alpha.0](https://github.com/digdir/designsystemet/compare/@digdir/[email protected]...@digdir/[email protected]) (2024-04-24)

**Note:** Version bump only for package @digdir/design-system-react

## [0.53.9](https://github.com/digdir/designsystemet/compare/@digdir/[email protected]...@digdir/[email protected]) (2024-04-23)

**Note:** Version bump only for package @digdir/design-system-react
Expand Down
2 changes: 1 addition & 1 deletion packages/react-old/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@digdir/design-system-react",
"version": "0.53.9",
"version": "0.53.10-alpha.1",
"description": "Legacy React components for Designsystemet",
"author": "Designsystemet team",
"repository": "https://github.com/digdir/designsystemet",
Expand Down
12 changes: 12 additions & 0 deletions packages/react/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

## [0.59.1-alpha.1](https://github.com/digdir/designsystemet/compare/@digdir/[email protected]...@digdir/[email protected]) (2024-04-26)

### Bug Fixes

- **Modal:** fix close button position ([#1877](https://github.com/digdir/designsystemet/issues/1877)) ([c866710](https://github.com/digdir/designsystemet/commit/c866710cc00760a8f1a4f1676e2c8a5eda235a72))

## [0.59.1-alpha.0](https://github.com/digdir/designsystemet/compare/@digdir/[email protected]...@digdir/[email protected]) (2024-04-24)

### Bug Fixes

- **Combobox:** Re-renders ([24fa39f](https://github.com/digdir/designsystemet/commit/24fa39f5124c9fc0b01590ad3f1e7960c54e5f35))

# [0.59.0](https://github.com/digdir/designsystemet/compare/@digdir/[email protected]...@digdir/[email protected]) (2024-04-23)

### Bug Fixes
Expand Down
4 changes: 2 additions & 2 deletions packages/react/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@digdir/designsystemet-react",
"version": "0.59.0",
"version": "0.59.1-alpha.1",
"description": "React components for Designsystemet",
"author": "Designsystemet team",
"repository": "https://github.com/digdir/designsystemet",
Expand Down Expand Up @@ -28,7 +28,7 @@
"access": "public"
},
"dependencies": {
"@floating-ui/react": "0.26.4",
"@floating-ui/react": "0.26.12",
"@navikt/aksel-icons": "^5.12.2",
"@radix-ui/react-slot": "^1.0.2",
"@tanstack/react-virtual": "^3.2.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@
/**
* Apply a focus outline on an element when it is focused with keyboard
*/
.inFocus {
.inputWrapper:has(input:focus) {
--fds-focus-border-width: 3px;

outline: var(--fds-focus-border-width) solid var(--fds-semantic-border-focus-outline);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -597,3 +597,79 @@ CustomNewValue.args = {
size: 'medium',
label: 'Hvor går reisen?',
};

const items = Array.from({ length: 2000 }, (_, index) => ({
name: `Option ${index}`,
value: `option-${index}`,
}));

export const ThousandsOfOptions: StoryFn<typeof Combobox> = (args) => {
return (
<Combobox {...args}>
<Combobox.Empty>Fant ingen treff</Combobox.Empty>
{items.map((item, index) => (
<Combobox.Option
key={index}
value={item.value}
>
{item.name}
</Combobox.Option>
))}
</Combobox>
);
};

ThousandsOfOptions.args = {
virtual: true,
};

export const RemoveAllOptions: StoryFn<typeof Combobox> = (args) => {
const [selectedValues, setSelectedValues] = React.useState<string[]>([
'test1',
'test2',
]);
const [values, setValues] = React.useState<string[]>(['test1', 'test2']);

const handleComboboxChange = (values: string[]) => {
setSelectedValues(values);
};

const changeAllValues = (deleteValues: boolean) =>
setValues(deleteValues ? [] : ['test1', 'test2']);

const changeSomeValues = (removeTest2: boolean) =>
setValues(removeTest2 ? ['test1'] : ['test1', 'test2']);

const currentSelectedValues = selectedValues.filter((id) =>
values.includes(id),
);

return (
<>
<Combobox
{...args}
multiple
value={currentSelectedValues}
onValueChange={handleComboboxChange}
>
{values.map((attachment) => {
return (
<Combobox.Option
key={attachment}
value={attachment}
description={attachment}
displayValue={attachment}
/>
);
})}
</Combobox>
<Switch onChange={(event) => changeAllValues(event.target.checked)}>
Remove Values (Selected values remain unchanged as the combobox does not
update when options are empty.)
</Switch>
<Switch onChange={(event) => changeSomeValues(event.target.checked)}>
Remove test2 (this works)
</Switch>
</>
);
};
31 changes: 26 additions & 5 deletions packages/react/src/components/form/Combobox/Combobox.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,22 @@ describe('Combobox', () => {
expect(screen.queryByText('Leikanger')).not.toBeInTheDocument();
});

it('should select when we click Enter', async () => {
const onValueChange = vi.fn();
const { user } = await render({ onValueChange });
const combobox = screen.getByRole('combobox');
expect(screen.queryByText('Leikanger')).not.toBeInTheDocument();

await userEvent.click(combobox);
expect(screen.getByText('Leikanger')).toBeInTheDocument();

await user.type(combobox, '{Enter}');

await wait(500);

expect(onValueChange).toHaveBeenCalledWith(['leikanger']);
});

it('should set call `onValueChange` on the Combobox when we click and option', async () => {
const onValueChange = vi.fn();
await render({ onValueChange });
Expand Down Expand Up @@ -165,8 +181,12 @@ describe('Combobox', () => {
await wait(100);
await userEvent.click(screen.getByText('Leikanger'));
await wait(500);
expect(onValueChange).toHaveBeenCalledWith(['leikanger']);
await wait(500);
await userEvent.click(screen.getByText('Oslo'));
await wait(500);
expect(onValueChange).toHaveBeenCalledWith(['leikanger', 'oslo']);
await wait(500);
await user.click(document.body);
await wait(500);
expect(screen.getByText('Leikanger')).toBeInTheDocument();
Expand All @@ -181,12 +201,13 @@ describe('Combobox', () => {
throw new Error('Could not find clear button');
}
await userEvent.click(clearButton);
await userEvent.click(document.body);

setTimeout(() => {
expect(screen.queryByText('Leikanger')).not.toBeInTheDocument();
expect(screen.queryByText('Oslo')).not.toBeInTheDocument();
expect(onValueChange).toHaveBeenCalledWith([]);
}, 1000);
await wait(500);

expect(screen.queryByText('Leikanger')).not.toBeInTheDocument();
expect(screen.queryByText('Oslo')).not.toBeInTheDocument();
expect(onValueChange).toHaveBeenCalledWith([]);
});

it('should show "Fant ingen treff", when input does not match any values', async () => {
Expand Down
Loading
Loading