Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add datetimepicker component #1145

Merged
merged 2 commits into from
Dec 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions block.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
5 changes: 5 additions & 0 deletions block_conv.go
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down Expand Up @@ -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":
Expand Down Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Question] Is it correct to say that this is because datetimepicker does not work with Section?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe so, and the builder complains about this https://app.slack.com/block-kit-builder/T3E1TGDM3#%7B%22blocks%22:%5B%7B%22type%22:%22section%22,%22text%22:%7B%22type%22:%22mrkdwn%22,%22text%22:%22Pick%20a%20date%20for%20the%20deadline.%22%7D,%22accessory%22:%7B%22type%22:%22datetimepicker%22,%22action_id%22:%22datepicker-action%22%7D%7D%5D%7D

I assume that the space is not permitted for the layout. I hope Slack supports grid layout like Teams in future. Currently, everything seems to be limited to 2 columns at most.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might be wrong on this. Let me check one more time. Looks like it is supported
slackapi/java-slack-sdk@4176b91#diff-212bb028a409f158a94e9af3431b61e080b8bec0e72711ba99dc294e02fd5084L2866

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Working on it. Should be done soon. Thanks for asking this. The assumption needs to be tested.

Copy link
Contributor Author

@hussachai hussachai Dec 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. I tested it out, and my assumption is right.

0 = {string} "[ERROR] failed to match all allowed schemas [json-pointer:/view]"
1 = {string} "[ERROR] unsupported type: datetimepicker [json-pointer:/view/blocks/8/accessory/type]"

datetimepicker won't work in Section. That kinda makes sense to me because I don't see any room to put text on it. It will go overflow.

func (a *Accessory) UnmarshalJSON(data []byte) error {
var r json.RawMessage

Expand Down
24 changes: 24 additions & 0 deletions block_element.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -392,6 +393,29 @@ 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"`
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.
Expand Down
6 changes: 6 additions & 0 deletions block_element_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down