From c28a81353726b953af7b7e7de9da428016358d1a Mon Sep 17 00:00:00 2001 From: Kazuhiro Sera Date: Tue, 3 Oct 2023 14:16:42 +0900 Subject: [PATCH] Add RichTextInputElement to slack_sdk.models (#1406) --- slack_sdk/models/blocks/__init__.py | 2 ++ slack_sdk/models/blocks/block_elements.py | 41 ++++++++++++++++++++++- tests/slack_sdk/models/test_elements.py | 21 ++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) diff --git a/slack_sdk/models/blocks/__init__.py b/slack_sdk/models/blocks/__init__.py index 3dfd87d3f..87febae70 100644 --- a/slack_sdk/models/blocks/__init__.py +++ b/slack_sdk/models/blocks/__init__.py @@ -32,6 +32,7 @@ from .block_elements import InteractiveElement from .block_elements import LinkButtonElement from .block_elements import OverflowMenuElement +from .block_elements import RichTextInputElement from .block_elements import PlainTextInputElement from .block_elements import EmailInputElement from .block_elements import UrlInputElement @@ -81,6 +82,7 @@ "InteractiveElement", "LinkButtonElement", "OverflowMenuElement", + "RichTextInputElement", "PlainTextInputElement", "EmailInputElement", "UrlInputElement", diff --git a/slack_sdk/models/blocks/block_elements.py b/slack_sdk/models/blocks/block_elements.py index 4cf1519c2..c65bd7f8a 100644 --- a/slack_sdk/models/blocks/block_elements.py +++ b/slack_sdk/models/blocks/block_elements.py @@ -3,7 +3,7 @@ import re import warnings from abc import ABCMeta -from typing import Iterator, List, Optional, Set, Type, Union, Sequence +from typing import Iterator, List, Optional, Set, Type, Union, Sequence, Dict, Any from slack_sdk.models import show_unknown_key_warning from slack_sdk.models.basic_objects import ( @@ -1331,6 +1331,45 @@ def __init__( self.max_selected_items = max_selected_items +# ------------------------------------------------- +# Rich Text Input Element +# ------------------------------------------------- + + +class RichTextInputElement(InputInteractiveElement): + type = "rich_text_input" + + @property + def attributes(self) -> Set[str]: + return super().attributes.union( + { + "initial_value", + "dispatch_action_config", + } + ) + + def __init__( + self, + *, + action_id: Optional[str] = None, + placeholder: Optional[Union[str, dict, TextObject]] = None, + initial_value: Optional[Dict[str, Any]] = None, # TODO: Add rich_text block class and its element classes + dispatch_action_config: Optional[Union[dict, DispatchActionConfig]] = None, + focus_on_load: Optional[bool] = None, + **others: dict, + ): + super().__init__( + type=self.type, + action_id=action_id, + placeholder=TextObject.parse(placeholder, PlainTextObject.type), + focus_on_load=focus_on_load, + ) + show_unknown_key_warning(self, others) + + self.initial_value = initial_value + self.dispatch_action_config = dispatch_action_config + + # ------------------------------------------------- # Plain Text Input Element # ------------------------------------------------- diff --git a/tests/slack_sdk/models/test_elements.py b/tests/slack_sdk/models/test_elements.py index 1b246b0a6..fe63d5fc0 100644 --- a/tests/slack_sdk/models/test_elements.py +++ b/tests/slack_sdk/models/test_elements.py @@ -34,6 +34,7 @@ NumberInputElement, UrlInputElement, WorkflowButtonElement, + RichTextInputElement, ) from . import STRING_3001_CHARS, STRING_301_CHARS @@ -1013,6 +1014,26 @@ def test_document(self): # ------------------------------------------------- +class RichTextInputElementTests(unittest.TestCase): + def test_simple(self): + input = { + "type": "rich_text_input", + "action_id": "rich_input", + "placeholder": {"type": "plain_text", "text": "Enter some plain text"}, + } + self.assertDictEqual(input, RichTextInputElement(**input).to_dict()) + + def test_document(self): + input = { + "type": "rich_text_input", + "action_id": "rich_text_input-action", + "dispatch_action_config": {"trigger_actions_on": ["on_character_entered"]}, + "focus_on_load": True, + "placeholder": {"type": "plain_text", "text": "Enter text"}, + } + self.assertDictEqual(input, RichTextInputElement(**input).to_dict()) + + class PlainTextInputElementTests(unittest.TestCase): def test_document_1(self): input = {