From 914e9aaaffa7198d0be9294ccc0b34c0b6d6254c Mon Sep 17 00:00:00 2001 From: Hussachai Puripunpinyo Date: Mon, 19 Dec 2022 11:37:41 -0800 Subject: [PATCH 1/2] add datetimepicker component --- block.go | 1 + block_conv.go | 5 +++++ block_element.go | 25 +++++++++++++++++++++++++ block_element_test.go | 6 ++++++ 4 files changed, 37 insertions(+) diff --git a/block.go b/block.go index 240f55279..18222bd2b 100644 --- a/block.go +++ b/block.go @@ -50,6 +50,7 @@ type BlockAction struct { SelectedConversations []string `json:"selected_conversations"` SelectedDate string `json:"selected_date"` SelectedTime string `json:"selected_time"` + SelectedDateTime int64 `json:"selected_date_time"` InitialOption OptionBlockObject `json:"initial_option"` InitialUser string `json:"initial_user"` InitialChannel string `json:"initial_channel"` diff --git a/block_conv.go b/block_conv.go index 555e48117..4ab58de3f 100644 --- a/block_conv.go +++ b/block_conv.go @@ -110,6 +110,8 @@ func (b *InputBlock) UnmarshalJSON(data []byte) error { e = &DatePickerBlockElement{} case "timepicker": e = &TimePickerBlockElement{} + case "datetimepicker": + e = &DateTimePickerBlockElement{} case "plain_text_input": e = &PlainTextInputBlockElement{} case "email_text_input": @@ -190,6 +192,8 @@ func (b *BlockElements) UnmarshalJSON(data []byte) error { blockElement = &DatePickerBlockElement{} case "timepicker": blockElement = &TimePickerBlockElement{} + case "datetimepicker": + blockElement = &DateTimePickerBlockElement{} case "plain_text_input": blockElement = &PlainTextInputBlockElement{} case "email_text_input": @@ -233,6 +237,7 @@ func (a *Accessory) MarshalJSON() ([]byte, error) { // UnmarshalJSON implements the Unmarshaller interface for Accessory, so that any JSON // unmarshalling is delegated and proper type determination can be made before unmarshal +// Note: datetimepicker is not supported in Accessory func (a *Accessory) UnmarshalJSON(data []byte) error { var r json.RawMessage diff --git a/block_element.go b/block_element.go index aba29c6ba..bee65879d 100644 --- a/block_element.go +++ b/block_element.go @@ -9,6 +9,7 @@ const ( METOverflow MessageElementType = "overflow" METDatepicker MessageElementType = "datepicker" METTimepicker MessageElementType = "timepicker" + METDatetimepicker MessageElementType = "datetimepicker" METPlainTextInput MessageElementType = "plain_text_input" METRadioButtons MessageElementType = "radio_buttons" METEmailTextInput MessageElementType = "email_text_input" @@ -392,6 +393,30 @@ func NewTimePickerBlockElement(actionID string) *TimePickerBlockElement { } } +// DateTimePickerBlockElement defines an element that allows the selection of both +// a date and a time of day formatted as a UNIX timestamp. +// More Information: https://api.slack.com/reference/messaging/block-elements#datetimepicker +type DateTimePickerBlockElement struct { + Type MessageElementType `json:"type"` + ActionID string `json:"action_id,omitempty"` + Placeholder *TextBlockObject `json:"placeholder,omitempty"` + InitialDateTime int64 `json:"initial_date_time,omitempty"` + Confirm *ConfirmationBlockObject `json:"confirm,omitempty"` +} + +// ElementType returns the type of the Element +func (s DateTimePickerBlockElement) ElementType() MessageElementType { + return s.Type +} + +// NewDatePickerBlockElement returns an instance of a datetime picker element +func NewDateTimePickerBlockElement(actionID string) *DateTimePickerBlockElement { + return &DateTimePickerBlockElement{ + Type: METDatetimepicker, + ActionID: actionID, + } +} + // EmailTextInputBlockElement creates a field where a user can enter email // data. // email-text-input elements are currently only available in modals. diff --git a/block_element_test.go b/block_element_test.go index 164f8e2f7..0c47affc3 100644 --- a/block_element_test.go +++ b/block_element_test.go @@ -133,6 +133,12 @@ func TestNewTimePickerBlockElement(t *testing.T) { assert.Equal(t, timepickerElement.ActionID, "test") } +func TestNewDateTimePickerBlockElement(t *testing.T) { + datetimepickerElement := NewDateTimePickerBlockElement("test") + assert.Equal(t, string(datetimepickerElement.Type), "datetimepicker") + assert.Equal(t, datetimepickerElement.ActionID, "test") +} + func TestNewPlainTextInputBlockElement(t *testing.T) { plainTextInputElement := NewPlainTextInputBlockElement(nil, "test") From 68bc30c07ba0fb3f5ae4dc71ecee60fd1b858db9 Mon Sep 17 00:00:00 2001 From: Hussachai Puripunpinyo Date: Wed, 21 Dec 2022 10:14:38 -0800 Subject: [PATCH 2/2] remove unsupported field `Placeholder` from Datetimepicker --- block_element.go | 1 - 1 file changed, 1 deletion(-) diff --git a/block_element.go b/block_element.go index bee65879d..a70d8f2de 100644 --- a/block_element.go +++ b/block_element.go @@ -399,7 +399,6 @@ func NewTimePickerBlockElement(actionID string) *TimePickerBlockElement { type DateTimePickerBlockElement struct { Type MessageElementType `json:"type"` ActionID string `json:"action_id,omitempty"` - Placeholder *TextBlockObject `json:"placeholder,omitempty"` InitialDateTime int64 `json:"initial_date_time,omitempty"` Confirm *ConfirmationBlockObject `json:"confirm,omitempty"` }