Skip to content
This repository was archived by the owner on Oct 16, 2026. It is now read-only.
2 changes: 2 additions & 0 deletions packages/fuselage-ui-kit/src/contexts/kitContext.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { InputElementDispatchAction } from '@rocket.chat/ui-kit';
import { createContext, useContext } from 'react';

type ActionParams = {
Expand All @@ -6,6 +7,7 @@ type ActionParams = {
actionId: string;
value: unknown;
viewId?: string;
dispatchActionConfig?: InputElementDispatchAction[];
};

type UiKitContext = {
Expand Down
40 changes: 39 additions & 1 deletion packages/fuselage-ui-kit/src/hooks/useUiKitState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const useUiKitState: <TElement extends UiKit.ActionableElement>(
| { target: { value: UiKit.ActionOf<TElement> } }
) => void
] = (rest, context) => {
const { blockId, actionId, appId } = rest;
const { blockId, actionId, appId, dispatchActionConfig } = rest;
const {
action,
appId: appIdFromContext,
Expand Down Expand Up @@ -72,6 +72,27 @@ export const useUiKitState: <TElement extends UiKit.ActionableElement>(
setLoading(false);
});

// Used for triggering actions on text inputs. Removing the load state
// makes the text input field remain focused after running the action
const noLoadStateActionFunction = useMutableCallback(async (e) => {
const {
target: { value },
} = e;
setValue(value);
state && (await state({ blockId, appId, actionId, value, viewId }, e));
await action(
{
blockId,
appId: appId || appIdFromContext,
actionId,
value,
viewId,
dispatchActionConfig,
},
e
);
});

const stateFunction = useMutableCallback(async (e) => {
const {
target: { value },
Expand All @@ -94,6 +115,23 @@ export const useUiKitState: <TElement extends UiKit.ActionableElement>(
[loading, setLoading, error, value]
);

if (context === UiKit.BlockContext.FORM) {
if (
rest.type === 'plain_text_input' &&
Array.isArray(rest?.dispatchActionConfig) &&
rest.dispatchActionConfig.includes('on_character_entered')
) {
return [result, noLoadStateActionFunction];
}

if (
Array.isArray(rest?.dispatchActionConfig) &&
rest.dispatchActionConfig.includes('on_item_selected')
) {
return [result, actionFunction];
}
}

if (
context &&
[UiKit.BlockContext.SECTION, UiKit.BlockContext.ACTION].includes(context)
Expand Down
2 changes: 2 additions & 0 deletions packages/ui-kit/src/blocks/Actionable.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import type { ConfirmationDialog } from './ConfirmationDialog';
import type { InputElementDispatchAction } from './InputElementDispatchAction';

export type Actionable<Block> = Block & {
appId: string;
blockId: string;
actionId: string;
confirm?: ConfirmationDialog;
dispatchActionConfig?: InputElementDispatchAction[];
};
3 changes: 3 additions & 0 deletions packages/ui-kit/src/blocks/InputElementDispatchAction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export type InputElementDispatchAction =
| 'on_character_entered'
| 'on_item_selected';
2 changes: 2 additions & 0 deletions packages/ui-kit/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ export { InputBlock } from './blocks/layout/InputBlock';
export { SectionBlock } from './blocks/layout/SectionBlock';
export { LayoutBlock } from './blocks/LayoutBlock';

export { InputElementDispatchAction } from './blocks/InputElementDispatchAction';

export { Block } from './blocks/Block';
export { RenderableBlock } from './blocks/RenderableBlock';

Expand Down