mirror of
https://github.com/mattermost/mattermost-mobile.git
synced 2026-11-05 10:28:54 +00:00
* Mattermost Blocks * Move block renderer to its own components folder * Sync translation layer * UX fixes * Refactor button to use the button component * Code review * Fix CI * Add animations to collapse blocks * Add unit tests * Add e2e tests and related fixes * Address coderabbit comments * Address UX comments * Address feedback * Address coderabbit comments * Fix snapshots * Sync with webapp translation * Add node limit to button markdown node * Address feedback * i18n-extract * Fix test * Move image utils to the utils folder * Remove unneeded style * Revert build script change --------- Co-authored-by: Mattermost Build <build@mattermost.com>
60 lines
2.2 KiB
TypeScript
60 lines
2.2 KiB
TypeScript
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
import React, {useCallback, useContext, useMemo, useState} from 'react';
|
|
|
|
import AutocompleteSelector from '@components/autocomplete_selector';
|
|
import {usePreventDoubleTap} from '@hooks/utils';
|
|
import {filterOptions} from '@utils/message_attachment';
|
|
|
|
import {MmBlocksRenderContext} from './context';
|
|
|
|
import type {ActionHandler} from './types';
|
|
|
|
type StaticSelectElementProps = {
|
|
element: MmStaticSelectBlock;
|
|
onAction: ActionHandler;
|
|
};
|
|
|
|
export const StaticSelectElement = ({element, onAction}: StaticSelectElementProps) => {
|
|
const renderContext = useContext(MmBlocksRenderContext);
|
|
const filteredOptions = useMemo(() => filterOptions(element.options), [element.options]);
|
|
const [selected, setSelected] = useState(() => {
|
|
if (element.initial_option && element.options) {
|
|
return element.options.find((o) => o.value === element.initial_option)?.value;
|
|
}
|
|
return undefined;
|
|
});
|
|
|
|
const isDynamicSource = element.data_source === 'users' || element.data_source === 'channels';
|
|
const optionCount = element.options?.length ?? 0;
|
|
const isValid = Boolean(element.action_id && (isDynamicSource || optionCount > 0));
|
|
|
|
const handleSelect = usePreventDoubleTap(useCallback(async (selectedItem: SelectedDialogOption) => {
|
|
if (!selectedItem || Array.isArray(selectedItem) || !element.action_id) {
|
|
return;
|
|
}
|
|
await onAction(element.action_id, selectedItem.value, element.query, element.cookie);
|
|
setSelected(selectedItem.value);
|
|
}, [element.action_id, element.cookie, element.query, onAction]));
|
|
|
|
if (!isValid || !renderContext) {
|
|
return null;
|
|
}
|
|
|
|
return (
|
|
<AutocompleteSelector
|
|
placeholder={element.placeholder}
|
|
dataSource={element.data_source}
|
|
isMultiselect={false}
|
|
options={filteredOptions}
|
|
selected={selected}
|
|
onSelected={handleSelect}
|
|
disabled={element.disabled}
|
|
testID={`mm_blocks.static_select.${element.action_id}`}
|
|
location={renderContext.location}
|
|
omitMargins={true}
|
|
/>
|
|
);
|
|
};
|