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(fuselage-ui-kit): MultiStaticSelectElement not working as expected #32999

Merged
merged 14 commits into from
Sep 5, 2024
Merged
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
Loading