Skip to content

Commit 900aae8

Browse files
juliajforestidougfabristassoevan
authored
feat(fuselage): MultiSelect improvements (#645)
* Adapt MulsiSelect to replace AutoComplete * Convert MultiSelect to tsx * Convert MultiSelectFiltered to tsx * fix * fix: review * Add addonIcon prop on MultiSelect * Adapt Select and SelectFiltered to replace AutoComplete * Fix `MultiStaticSelectElement` definition of initial value * Add utility type `ActionOf` * Fix state types * Replace `SelectOptions` * Fix failing test Co-authored-by: dougfabris <devfabris@gmail.com> Co-authored-by: Tasso Evangelista <tasso.evangelista@rocket.chat>
1 parent 5193110 commit 900aae8

29 files changed

+588
-423
lines changed

packages/fuselage-hooks/src/useAutoFocus.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { useEffect, useRef, Ref } from 'react';
77
* @param options - options of the focus request
88
* @returns the ref which holds the element
99
* @public
10-
* @deprecated
10+
* @deprecated in favor of focus provided by react-hook-form
1111
*/
1212
export const useAutoFocus = <
1313
T extends { focus: (options?: FocusOptions) => void }

packages/fuselage-ui-kit/src/elements/LinearScaleElement.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ const LinearScaleElement = ({
6060
{points.map((point, i) => (
6161
<Button
6262
key={i}
63-
className={point === value ? 'active' : undefined}
63+
className={point === String(value) ? 'active' : undefined}
6464
disabled={loading}
6565
danger={!!error}
6666
minWidth='4ch'

packages/fuselage-ui-kit/src/elements/MultiStaticSelectElement.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { MultiSelectFiltered } from '@rocket.chat/fuselage';
1+
import { MultiSelectFiltered, SelectOption } from '@rocket.chat/fuselage';
22
import * as UiKit from '@rocket.chat/ui-kit';
33
import React, { memo, ReactElement, useCallback, useMemo } from 'react';
44

@@ -15,7 +15,7 @@ const MultiStaticSelectElement = ({
1515
}: MultiStaticSelectElementProps): ReactElement => {
1616
const [{ loading, value, error }, action] = useUiKitState(block, context);
1717

18-
const options = useMemo<readonly [string, string][]>(
18+
const options = useMemo<SelectOption[]>(
1919
() =>
2020
block.options.map(({ value, text }, i) => [
2121
value,

packages/fuselage-ui-kit/src/elements/OverflowElement.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ const OverflowElement = ({
2323
const [{ loading }, action] = useUiKitState(block, context);
2424

2525
const fireChange = useCallback(
26-
([value]: [unknown, string]) => action({ target: { value } }),
26+
([value]: [UiKit.ActionOf<UiKit.OverflowElement>, string]) =>
27+
action({ target: { value } }),
2728
[action]
2829
);
2930

@@ -51,7 +52,7 @@ const OverflowElement = ({
5152

5253
const handleSelection = useCallback(
5354
([value]: OptionType) => {
54-
action({ target: { value } });
55+
action({ target: { value: String(value) } });
5556
reset();
5657
hide();
5758
},

packages/fuselage-ui-kit/src/hooks/useUiKitState.ts

+21-11
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,36 @@ import { useContext, useMemo, useState } from 'react';
44

55
import { kitContext, useUiKitStateValue } from '../contexts/kitContext';
66

7-
type UiKitState = {
7+
type UiKitState<
8+
TElement extends UiKit.ActionableElement = UiKit.ActionableElement
9+
> = {
810
loading: boolean;
911
setLoading: (loading: boolean) => void;
1012
error?: string;
11-
value: string | number | undefined;
13+
value: UiKit.ActionOf<TElement>;
1214
};
1315

14-
const hasInitialValue = <T extends UiKit.ActionableElement>(
15-
element: T
16-
): element is T & { initialValue: number | string } =>
16+
const hasInitialValue = <TElement extends UiKit.ActionableElement>(
17+
element: TElement
18+
): element is TElement & { initialValue: number | string } =>
1719
'initialValue' in element;
1820

19-
const hasInitialOption = <T extends UiKit.ActionableElement>(
20-
element: T
21-
): element is T & { initialOption: UiKit.Option } => 'initialOption' in element;
21+
const hasInitialOption = <TElement extends UiKit.ActionableElement>(
22+
element: TElement
23+
): element is TElement & { initialOption: UiKit.Option } =>
24+
'initialOption' in element;
2225

23-
export const useUiKitState: <T extends UiKit.ActionableElement>(
24-
element: T,
26+
export const useUiKitState: <TElement extends UiKit.ActionableElement>(
27+
element: TElement,
2528
context: UiKit.BlockContext
26-
) => [UiKitState, any] = (rest, context) => {
29+
) => [
30+
state: UiKitState<TElement>,
31+
action: (
32+
pseudoEvent?:
33+
| { target: EventTarget }
34+
| { target: { value: UiKit.ActionOf<TElement> } }
35+
) => void
36+
] = (rest, context) => {
2737
const { blockId, actionId, appId } = rest;
2838
const {
2939
action,

packages/fuselage/src/components/AutoComplete/AutoComplete.d.ts

+4
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,8 @@ type AutoCompleteProps = {
1515
error?: boolean;
1616
disabled?: boolean;
1717
};
18+
19+
/**
20+
* @deprecated in favor of Select and MultiSelect
21+
*/
1822
export const AutoComplete: FC<AutoCompleteProps>;

packages/fuselage/src/components/MultiSelect/MultiSelect.d.ts

-31
This file was deleted.

packages/fuselage/src/components/MultiSelect/MultiSelect.js

-177
This file was deleted.

packages/fuselage/src/components/MultiSelect/MultiSelect.stories.tsx

+9-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { ComponentStory, ComponentMeta } from '@storybook/react';
1212
import React from 'react';
1313

1414
import { MultiSelect, MultiSelectFiltered } from '../..';
15+
import { SelectOption } from '../Select';
1516

1617
export default {
1718
title: 'Inputs/MultiSelect',
@@ -35,7 +36,7 @@ export default {
3536
},
3637
} as ComponentMeta<typeof MultiSelect>;
3738

38-
const options: readonly (readonly [string, string, boolean?])[] = [
39+
const options: SelectOption[] = [
3940
['1', 'a teste 1'],
4041
['2', 'b teste 2', true],
4142
['3', 'c teste 3'],
@@ -58,6 +59,13 @@ Default.args = {
5859
options,
5960
};
6061

62+
export const WithValue: ComponentStory<typeof MultiSelect> = Template.bind({});
63+
WithValue.args = {
64+
placeholder: 'Placeholder here...',
65+
options,
66+
value: ['1', '2'],
67+
};
68+
6169
export const Error: ComponentStory<typeof MultiSelect> = Template.bind({});
6270
Error.args = {
6371
error: 'Error',

0 commit comments

Comments
 (0)