From f524e2c449e638d68ee2b76af64ea0055a18432f Mon Sep 17 00:00:00 2001 From: Amelia Gapin Date: Tue, 29 Nov 2022 17:16:19 -0500 Subject: [PATCH 1/3] add block elements for email_text_input and url_text_input --- block_conv.go | 8 ++++++ block_element.go | 58 +++++++++++++++++++++++++++++++++++++++++++ block_element_test.go | 14 +++++++++++ 3 files changed, 80 insertions(+) diff --git a/block_conv.go b/block_conv.go index 1a2c57e9f..d01849cad 100644 --- a/block_conv.go +++ b/block_conv.go @@ -112,6 +112,10 @@ func (b *InputBlock) UnmarshalJSON(data []byte) error { e = &TimePickerBlockElement{} case "plain_text_input": e = &PlainTextInputBlockElement{} + case "email_text_input": + e = &EmailTextInputBlockElement{} + case "url_text_input": + e = &URLTextInputBlockElement{} case "static_select", "external_select", "users_select", "conversations_select", "channels_select": e = &SelectBlockElement{} case "multi_static_select", "multi_external_select", "multi_users_select", "multi_conversations_select", "multi_channels_select": @@ -186,6 +190,10 @@ func (b *BlockElements) UnmarshalJSON(data []byte) error { blockElement = &TimePickerBlockElement{} case "plain_text_input": blockElement = &PlainTextInputBlockElement{} + case "email_text_input": + blockElement = &EmailTextInputBlockElement{} + case "url_text_input": + blockElement = &URLTextInputBlockElement{} case "checkboxes": blockElement = &CheckboxGroupsBlockElement{} case "radio_buttons": diff --git a/block_element.go b/block_element.go index 643529ff0..019433e0e 100644 --- a/block_element.go +++ b/block_element.go @@ -11,6 +11,8 @@ const ( METTimepicker MessageElementType = "timepicker" METPlainTextInput MessageElementType = "plain_text_input" METRadioButtons MessageElementType = "radio_buttons" + METEmailTextInput MessageElementType = "email_text_input" + METURLTextInput MessageElementType = "url_text_input" MixedElementImage MixedElementType = "mixed_image" MixedElementText MixedElementType = "mixed_text" @@ -389,6 +391,62 @@ func NewTimePickerBlockElement(actionID string) *TimePickerBlockElement { } } +// EmailTextInputBlockElement creates a field where a user can enter email +// data. +// email-text-input elements are currently only available in modals. +// +// More Information: https://api.slack.com/reference/block-kit/block-elements#email +type EmailTextInputBlockElement struct { + Type MessageElementType `json:"type"` + ActionID string `json:"action_id,omitempty"` + Placeholder *TextBlockObject `json:"placeholder,omitempty"` + InitialValue string `json:"initial_value,omitempty"` + DispatchActionConfig *DispatchActionConfig `json:"dispatch_action_config,omitempty"` +} + +// ElementType returns the type of the Element +func (s EmailTextInputBlockElement) ElementType() MessageElementType { + return s.Type +} + +// NewEmailTextInputBlockElement returns an instance of a plain-text input +// element +func NewEmailTextInputBlockElement(placeholder *TextBlockObject, actionID string) *EmailTextInputBlockElement { + return &EmailTextInputBlockElement{ + Type: METEmailTextInput, + ActionID: actionID, + Placeholder: placeholder, + } +} + +// URLTextInputBlockElement creates a field where a user can enter url data. +// +// url-text-input elements are currently only available in modals. +// +// More Information: https://api.slack.com/reference/block-kit/block-elements#url +type URLTextInputBlockElement struct { + Type MessageElementType `json:"type"` + ActionID string `json:"action_id,omitempty"` + Placeholder *TextBlockObject `json:"placeholder,omitempty"` + InitialValue string `json:"initial_value,omitempty"` + DispatchActionConfig *DispatchActionConfig `json:"dispatch_action_config,omitempty"` +} + +// ElementType returns the type of the Element +func (s URLTextInputBlockElement) ElementType() MessageElementType { + return s.Type +} + +// NewURLTextInputBlockElement returns an instance of a plain-text input +// element +func NewURLTextInputBlockElement(placeholder *TextBlockObject, actionID string) *URLTextInputBlockElement { + return &URLTextInputBlockElement{ + Type: METURLTextInput, + ActionID: actionID, + Placeholder: placeholder, + } +} + // PlainTextInputBlockElement creates a field where a user can enter freeform // data. // Plain-text input elements are currently only available in modals. diff --git a/block_element_test.go b/block_element_test.go index 7da816eab..45e84b9f5 100644 --- a/block_element_test.go +++ b/block_element_test.go @@ -142,6 +142,20 @@ func TestNewPlainTextInputBlockElement(t *testing.T) { } +func TestNewEmailTextInputBlockElement(t *testing.T) { + emailTextInputElement := NewEmailTextInputBlockElement(nil, "test@test.com") + + assert.Equal(t, string(emailTextInputElement.Type), "email_text_input") + assert.Equal(t, emailTextInputElement.ActionID, "test@test.com") +} + +func TestNewURLTextInputBlockElement(t *testing.T) { + urlTextInputElement := NewURLTextInputBlockElement(nil, "www.test.com") + + assert.Equal(t, string(urlTextInputElement.Type), "url_text_input") + assert.Equal(t, urlTextInputElement.ActionID, "www.test.com") +} + func TestNewCheckboxGroupsBlockElement(t *testing.T) { // Build Text Objects associated with each option checkBoxOptionTextOne := NewTextBlockObject("plain_text", "Check One", false, false) From 23204b5d470e489e0f7b0b9dbb8290077c49c895 Mon Sep 17 00:00:00 2001 From: Amelia Gapin Date: Fri, 9 Dec 2022 11:33:51 -0500 Subject: [PATCH 2/3] add focus_on_load option for email/url text inputs --- block_element.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/block_element.go b/block_element.go index 019433e0e..ee42a5645 100644 --- a/block_element.go +++ b/block_element.go @@ -402,6 +402,7 @@ type EmailTextInputBlockElement struct { Placeholder *TextBlockObject `json:"placeholder,omitempty"` InitialValue string `json:"initial_value,omitempty"` DispatchActionConfig *DispatchActionConfig `json:"dispatch_action_config,omitempty"` + FocusOnLoad bool `json:"focus_on_load,omitempty"` } // ElementType returns the type of the Element @@ -430,6 +431,7 @@ type URLTextInputBlockElement struct { Placeholder *TextBlockObject `json:"placeholder,omitempty"` InitialValue string `json:"initial_value,omitempty"` DispatchActionConfig *DispatchActionConfig `json:"dispatch_action_config,omitempty"` + FocusOnLoad bool `json:"focus_on_load,omitempty"` } // ElementType returns the type of the Element From 6958897d79b5a31025a008c3e2a98acd672bc730 Mon Sep 17 00:00:00 2001 From: Amelia Gapin Date: Fri, 9 Dec 2022 11:34:12 -0500 Subject: [PATCH 3/3] use example.com in tests for email/url text inputs --- block_element_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/block_element_test.go b/block_element_test.go index 45e84b9f5..94ae574c5 100644 --- a/block_element_test.go +++ b/block_element_test.go @@ -143,17 +143,17 @@ func TestNewPlainTextInputBlockElement(t *testing.T) { } func TestNewEmailTextInputBlockElement(t *testing.T) { - emailTextInputElement := NewEmailTextInputBlockElement(nil, "test@test.com") + emailTextInputElement := NewEmailTextInputBlockElement(nil, "example@example.com") assert.Equal(t, string(emailTextInputElement.Type), "email_text_input") - assert.Equal(t, emailTextInputElement.ActionID, "test@test.com") + assert.Equal(t, emailTextInputElement.ActionID, "example@example.com") } func TestNewURLTextInputBlockElement(t *testing.T) { - urlTextInputElement := NewURLTextInputBlockElement(nil, "www.test.com") + urlTextInputElement := NewURLTextInputBlockElement(nil, "www.example.com") assert.Equal(t, string(urlTextInputElement.Type), "url_text_input") - assert.Equal(t, urlTextInputElement.ActionID, "www.test.com") + assert.Equal(t, urlTextInputElement.ActionID, "www.example.com") } func TestNewCheckboxGroupsBlockElement(t *testing.T) {