mattermost-mobile-upstream/app/components/block_renderer/static_select_element.tsx
Daniel Espino García 84ea188366
Mattermost Blocks (#9764)
* 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>
2026-06-29 18:42:16 +02:00

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}
/>
);
};