2021-09-13 20:41:05 +00:00
|
|
|
import React, { useEffect, useState } from 'react';
|
2022-07-13 19:31:58 +00:00
|
|
|
import { Text, TextStyle } from 'react-native';
|
2022-05-26 14:07:17 +00:00
|
|
|
import { BlockContext } from '@rocket.chat/ui-kit';
|
2021-09-13 20:41:05 +00:00
|
|
|
|
|
|
|
import Button from '../../Button';
|
2022-03-29 20:06:50 +00:00
|
|
|
import { useTheme } from '../../../theme';
|
2022-05-26 14:07:17 +00:00
|
|
|
import { IText } from '../interfaces';
|
2021-09-13 20:41:05 +00:00
|
|
|
import Chips from './Chips';
|
|
|
|
import Input from './Input';
|
|
|
|
import styles from './styles';
|
2022-07-13 19:31:58 +00:00
|
|
|
import { useActionSheet } from '../../ActionSheet';
|
|
|
|
import { MultiSelectContent } from './MultiSelectContent';
|
2021-09-13 20:41:05 +00:00
|
|
|
|
2022-03-29 20:06:50 +00:00
|
|
|
export interface IItemData {
|
2022-04-08 22:53:48 +00:00
|
|
|
value: any;
|
2022-03-29 20:06:50 +00:00
|
|
|
text: { text: string };
|
|
|
|
imageUrl?: string;
|
|
|
|
}
|
|
|
|
|
2023-08-03 19:37:14 +00:00
|
|
|
interface IMultiSelectWithMultiSelect extends IMultiSelect {
|
|
|
|
multiselect: true;
|
|
|
|
onChange: ({ value }: { value: string[] }) => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface IMultiSelectWithoutMultiSelect extends IMultiSelect {
|
|
|
|
multiselect?: false;
|
|
|
|
onChange: ({ value }: { value: any }) => void;
|
|
|
|
}
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
interface IMultiSelect {
|
2022-03-29 20:06:50 +00:00
|
|
|
options?: IItemData[];
|
|
|
|
placeholder?: IText;
|
|
|
|
context?: BlockContext;
|
2021-09-15 19:58:14 +00:00
|
|
|
loading?: boolean;
|
2022-07-13 19:31:58 +00:00
|
|
|
onSearch?: (keyword: string) => IItemData[] | Promise<IItemData[] | undefined>;
|
2022-02-07 15:18:37 +00:00
|
|
|
onClose?: () => void;
|
2022-03-29 20:06:50 +00:00
|
|
|
inputStyle?: TextStyle;
|
2021-09-15 19:58:14 +00:00
|
|
|
value?: any[];
|
2023-05-18 21:09:33 +00:00
|
|
|
disabled?: boolean;
|
2021-09-22 17:29:26 +00:00
|
|
|
innerInputStyle?: object;
|
2021-09-13 20:41:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const MultiSelect = React.memo(
|
|
|
|
({
|
|
|
|
options = [],
|
|
|
|
onChange,
|
|
|
|
placeholder = { text: 'Search' },
|
|
|
|
context,
|
|
|
|
loading,
|
|
|
|
value: values,
|
|
|
|
multiselect = false,
|
|
|
|
onSearch,
|
|
|
|
onClose = () => {},
|
|
|
|
disabled,
|
|
|
|
inputStyle,
|
2021-09-22 17:29:26 +00:00
|
|
|
innerInputStyle
|
2023-08-03 19:37:14 +00:00
|
|
|
}: IMultiSelectWithMultiSelect | IMultiSelectWithoutMultiSelect) => {
|
2022-07-13 19:31:58 +00:00
|
|
|
const { colors } = useTheme();
|
2023-08-03 19:37:14 +00:00
|
|
|
const [selected, select] = useState<IItemData[]>(Array.isArray(values) ? values : []);
|
2021-09-13 20:41:05 +00:00
|
|
|
const [currentValue, setCurrentValue] = useState('');
|
2022-07-13 19:31:58 +00:00
|
|
|
|
|
|
|
const { showActionSheet, hideActionSheet } = useActionSheet();
|
2021-09-13 20:41:05 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (Array.isArray(values)) {
|
|
|
|
select(values);
|
|
|
|
}
|
2023-08-03 19:37:14 +00:00
|
|
|
}, []);
|
2021-09-13 20:41:05 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (values && values.length && !multiselect) {
|
|
|
|
setCurrentValue(values[0].text);
|
|
|
|
}
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const onShow = () => {
|
2022-07-13 19:31:58 +00:00
|
|
|
showActionSheet({
|
|
|
|
children: (
|
|
|
|
<MultiSelectContent
|
|
|
|
options={options}
|
|
|
|
onSearch={onSearch}
|
|
|
|
select={select}
|
|
|
|
onChange={onChange}
|
|
|
|
setCurrentValue={setCurrentValue}
|
|
|
|
onHide={onHide}
|
|
|
|
multiselect={multiselect}
|
|
|
|
selectedItems={selected}
|
|
|
|
/>
|
|
|
|
),
|
|
|
|
onClose,
|
|
|
|
headerHeight: 275
|
|
|
|
});
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
|
|
|
const onHide = () => {
|
|
|
|
onClose();
|
2022-07-13 19:31:58 +00:00
|
|
|
hideActionSheet();
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
|
|
|
|
2022-03-29 20:06:50 +00:00
|
|
|
const onSelect = (item: IItemData) => {
|
2021-09-13 20:41:05 +00:00
|
|
|
const {
|
|
|
|
value,
|
|
|
|
text: { text }
|
|
|
|
} = item;
|
|
|
|
if (multiselect) {
|
|
|
|
let newSelect = [];
|
2023-08-03 19:37:14 +00:00
|
|
|
if (!selected.find(s => s.value === value)) {
|
|
|
|
newSelect = [...selected, item];
|
2021-09-13 20:41:05 +00:00
|
|
|
} else {
|
2023-08-03 19:37:14 +00:00
|
|
|
newSelect = selected.filter((s: any) => s.value !== value);
|
2021-09-13 20:41:05 +00:00
|
|
|
}
|
|
|
|
select(newSelect);
|
2023-08-03 19:37:14 +00:00
|
|
|
onChange({ value: newSelect.map(s => s.value) });
|
2021-09-13 20:41:05 +00:00
|
|
|
} else {
|
|
|
|
onChange({ value });
|
|
|
|
setCurrentValue(text);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let button = multiselect ? (
|
2022-05-20 16:37:57 +00:00
|
|
|
<Button title={`${selected.length} selecteds`} onPress={onShow} loading={loading} />
|
2021-09-13 20:41:05 +00:00
|
|
|
) : (
|
2022-07-13 19:31:58 +00:00
|
|
|
<Input onPress={onShow} loading={loading} disabled={disabled} inputStyle={inputStyle} innerInputStyle={innerInputStyle}>
|
|
|
|
<Text style={[styles.pickerText, { color: currentValue ? colors.titleText : colors.auxiliaryText }]}>
|
2021-09-13 20:41:05 +00:00
|
|
|
{currentValue || placeholder.text}
|
|
|
|
</Text>
|
|
|
|
</Input>
|
|
|
|
);
|
|
|
|
|
2022-05-26 14:07:17 +00:00
|
|
|
if (context === BlockContext.FORM) {
|
2021-09-13 20:41:05 +00:00
|
|
|
button = (
|
2022-07-13 19:31:58 +00:00
|
|
|
<Input onPress={onShow} loading={loading} disabled={disabled} inputStyle={inputStyle} innerInputStyle={innerInputStyle}>
|
2023-08-03 19:37:14 +00:00
|
|
|
{selected.length ? (
|
|
|
|
<Chips items={selected} onSelect={(item: any) => (disabled ? {} : onSelect(item))} />
|
2021-09-13 20:41:05 +00:00
|
|
|
) : (
|
2022-07-13 19:31:58 +00:00
|
|
|
<Text style={[styles.pickerText, { color: colors.auxiliaryText }]}>{placeholder.text}</Text>
|
2021-09-13 20:41:05 +00:00
|
|
|
)}
|
|
|
|
</Input>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-07-13 19:31:58 +00:00
|
|
|
return <>{button}</>;
|
2021-09-13 20:41:05 +00:00
|
|
|
}
|
|
|
|
);
|