Skip to content

Commit

Permalink
fix(fuselage-ui-kit): MultiStaticSelectElement not working as expec…
Browse files Browse the repository at this point in the history
…ted (#32999)

Co-authored-by: Tasso Evangelista <[email protected]>
  • Loading branch information
tiagoevanp and tassoevan committed Sep 5, 2024
1 parent 3ba1854 commit 78e6ba4
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 7 deletions.
6 changes: 6 additions & 0 deletions .changeset/mighty-drinks-hide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@rocket.chat/meteor": patch
"@rocket.chat/fuselage-ui-kit": patch
---

Fixes multiple selection for MultiStaticSelectElement in UiKit
2 changes: 1 addition & 1 deletion apps/meteor/tests/e2e/apps/apps-modal.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { expect, test } from '../utils/test';

test.use({ storageState: Users.user1.state });

test.describe.serial('Apps > ContextualBar', () => {
test.describe.serial('Apps > Modal', () => {
let poHomeChannel: HomeChannel;
let poModal: Modal;

Expand Down
69 changes: 69 additions & 0 deletions packages/fuselage-ui-kit/src/hooks/useUiKitState.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import {
BlockContext,
type MultiStaticSelectElement,
} from '@rocket.chat/ui-kit';
import { act, renderHook } from '@testing-library/react';

import { useUiKitState } from './useUiKitState';

describe('state function', () => {
const context = BlockContext.NONE;

it('should handle arrays', async () => {
const element: MultiStaticSelectElement = {
type: 'multi_static_select',
placeholder: { type: 'plain_text', text: '' },
options: [],
initialValue: ['A', 'B'],
appId: 'app-id',
blockId: 'block-id',
actionId: 'action-id',
};

const { result } = renderHook(() => useUiKitState(element, context), {
legacyRoot: true,
});

await act(async () => {
const [, state] = result.current;
await state({
target: {
value: ['C', 'D'],
},
});
});

expect(result.current[0].value).toEqual(['C', 'D']);
});
});

describe('action function', () => {
const context = BlockContext.ACTION;

it('should handle arrays', async () => {
const element: MultiStaticSelectElement = {
type: 'multi_static_select',
placeholder: { type: 'plain_text', text: '' },
options: [],
initialValue: ['A', 'B'],
appId: 'app-id',
blockId: 'block-id',
actionId: 'action-id',
};

const { result } = renderHook(() => useUiKitState(element, context), {
legacyRoot: true,
});

await act(async () => {
const [, action] = result.current;
await action({
target: {
value: ['C', 'D'],
},
});
});

expect(result.current[0].value).toEqual(['C', 'D']);
});
});
17 changes: 11 additions & 6 deletions packages/fuselage-ui-kit/src/hooks/useUiKitState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const useUiKitState = <TElement extends UiKit.ActionableElement>(
| Event
| { target: EventTarget }
| { target: { value: UiKit.ActionOf<TElement> } }
) => void
) => Promise<void>
] => {
const { blockId, actionId, appId, dispatchActionConfig } = element;
const {
Expand Down Expand Up @@ -70,15 +70,20 @@ export const useUiKitState = <TElement extends UiKit.ActionableElement>(
const {
target: { value: elValue },
} = e;

setLoading(true);

if (Array.isArray(value)) {
const idx = value.findIndex((value) => value === elValue);

if (idx > -1) {
setValue(value.filter((_, i) => i !== idx));
if (Array.isArray(elValue)) {
setValue(elValue);
} else {
setValue([...value, elValue]);
const idx = value.findIndex((value) => value === elValue);

if (idx > -1) {
setValue(value.filter((_, i) => i !== idx));
} else {
setValue([...value, elValue]);
}
}
} else {
setValue(elValue);
Expand Down

0 comments on commit 78e6ba4

Please sign in to comment.