Skip to content

Commit

Permalink
Added message variables button for Webhook body form field (elastic#6…
Browse files Browse the repository at this point in the history
…0174) (elastic#60340)

* Added message variables button for Webhook body form field

* Fixed test issue
  • Loading branch information
YulNaumenko committed Mar 17, 2020
1 parent effb428 commit 5142d8c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ describe('WebhookParamsFields renders', () => {
.first()
.prop('value')
).toStrictEqual('test message');
expect(wrapper.find('[data-test-subj="webhookAddVariableButton"]').length > 0).toBeTruthy();
});

test('params validation fails when body is not valid', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ import {
EuiCodeEditor,
EuiSwitch,
EuiButtonEmpty,
EuiContextMenuItem,
EuiPopover,
EuiContextMenuPanel,
} from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import {
Expand Down Expand Up @@ -454,10 +457,24 @@ const WebhookParamsFields: React.FunctionComponent<ActionParamsProps<WebhookActi
actionParams,
editAction,
index,
messageVariables,
errors,
}) => {
const { body } = actionParams;

const [isVariablesPopoverOpen, setIsVariablesPopoverOpen] = useState<boolean>(false);
const messageVariablesItems = messageVariables?.map((variable: string, i: number) => (
<EuiContextMenuItem
key={variable}
data-test-subj={`variableMenuButton-${i}`}
icon="empty"
onClick={() => {
editAction('body', (body ?? '').concat(` {{${variable}}}`), index);
setIsVariablesPopoverOpen(false);
}}
>
{`{{${variable}}}`}
</EuiContextMenuItem>
));
return (
<Fragment>
<EuiFormRow
Expand All @@ -471,6 +488,30 @@ const WebhookParamsFields: React.FunctionComponent<ActionParamsProps<WebhookActi
isInvalid={errors.body.length > 0 && body !== undefined}
fullWidth
error={errors.body}
labelAppend={
// TODO: replace this button with a proper Eui component, when it will be ready
<EuiPopover
button={
<EuiButtonIcon
data-test-subj="webhookAddVariableButton"
onClick={() => setIsVariablesPopoverOpen(true)}
iconType="indexOpen"
aria-label={i18n.translate(
'xpack.triggersActionsUI.components.builtinActionTypes.webhookAction.addVariablePopoverButton',
{
defaultMessage: 'Add variable',
}
)}
/>
}
isOpen={isVariablesPopoverOpen}
closePopover={() => setIsVariablesPopoverOpen(false)}
panelPaddingSize="none"
anchorPosition="downLeft"
>
<EuiContextMenuPanel items={messageVariablesItems} />
</EuiPopover>
}
>
<EuiCodeEditor
mode="json"
Expand Down

0 comments on commit 5142d8c

Please sign in to comment.