Skip to content

Commit

Permalink
Merge pull request #970 from deepoil/add_field_to_inputblock
Browse files Browse the repository at this point in the history
Add field to inputblock
  • Loading branch information
kanata2 committed Jun 2, 2022
2 parents 8d2ed5e + 44ab244 commit b2bdf74
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 10 deletions.
3 changes: 2 additions & 1 deletion block_input.go
Expand Up @@ -19,11 +19,12 @@ func (s InputBlock) BlockType() MessageBlockType {
}

// NewInputBlock returns a new instance of an input block
func NewInputBlock(blockID string, label *TextBlockObject, element BlockElement) *InputBlock {
func NewInputBlock(blockID string, label, hint *TextBlockObject, element BlockElement) *InputBlock {
return &InputBlock{
Type: MBTInput,
BlockID: blockID,
Label: label,
Element: element,
Hint: hint,
}
}
4 changes: 2 additions & 2 deletions block_input_test.go
Expand Up @@ -9,8 +9,8 @@ import (
func TestNewInputBlock(t *testing.T) {
label := NewTextBlockObject("plain_text", "label", false, false)
element := NewDatePickerBlockElement("action_id")

inputBlock := NewInputBlock("test", label, element)
hint := NewTextBlockObject("plain_text", "hint", false, false)
inputBlock := NewInputBlock("test", label, hint, element)
assert.Equal(t, string(inputBlock.Type), "input")
assert.Equal(t, inputBlock.BlockID, "test")
assert.Equal(t, inputBlock.Label, label)
Expand Down
6 changes: 4 additions & 2 deletions examples/modal/modal.go
Expand Up @@ -31,15 +31,17 @@ func generateModalRequest() slack.ModalViewRequest {
headerSection := slack.NewSectionBlock(headerText, nil, nil)

firstNameText := slack.NewTextBlockObject("plain_text", "First Name", false, false)
firstNameHint := slack.NewTextBlockObject("plain_text", "First Name Hint", false, false)
firstNamePlaceholder := slack.NewTextBlockObject("plain_text", "Enter your first name", false, false)
firstNameElement := slack.NewPlainTextInputBlockElement(firstNamePlaceholder, "firstName")
// Notice that blockID is a unique identifier for a block
firstName := slack.NewInputBlock("First Name", firstNameText, firstNameElement)
firstName := slack.NewInputBlock("First Name", firstNameText, firstNameHint, firstNameElement)

lastNameText := slack.NewTextBlockObject("plain_text", "Last Name", false, false)
lastNameHint := slack.NewTextBlockObject("plain_text", "Last Name Hint", false, false)
lastNamePlaceholder := slack.NewTextBlockObject("plain_text", "Enter your first name", false, false)
lastNameElement := slack.NewPlainTextInputBlockElement(lastNamePlaceholder, "lastName")
lastName := slack.NewInputBlock("Last Name", lastNameText, lastNameElement)
lastName := slack.NewInputBlock("Last Name", lastNameText, lastNameHint, lastNameElement)

blocks := slack.Blocks{
BlockSet: []slack.Block{
Expand Down
11 changes: 6 additions & 5 deletions examples/modal_users/users.go
Expand Up @@ -20,20 +20,20 @@ func main() {
// Only the inputs in input blocks will be included in view_submission’s view.state.values: https://slack.dev/java-slack-sdk/guides/modals
// This means the inputs will not be interactive either because they do not trigger block_actions messages: https://api.slack.com/surfaces/modals/using#interactions
channelNameText := slack.NewTextBlockObject(slack.PlainTextType, "Channel Name", false, false)
channelNameHint := slack.NewTextBlockObject(slack.PlainTextType, "Channel names may only contain lowercase letters, numbers, hyphens, and underscores, and must be 80 characters or less", false, false)
channelPlaceholder := slack.NewTextBlockObject(slack.PlainTextType, "New channel name", false, false)
channelNameElement := slack.NewPlainTextInputBlockElement(channelPlaceholder, "channel_name")
// Slack channel names can be maximum 80 characters: https://api.slack.com/methods/conversations.create
channelNameElement.MaxLength = 80
channelNameBlock := slack.NewInputBlock("channel_name", channelNameText, channelNameElement)
channelNameBlock.Hint = slack.NewTextBlockObject(slack.PlainTextType, "Channel names may only contain lowercase letters, numbers, hyphens, and underscores, and must be 80 characters or less", false, false)
channelNameBlock := slack.NewInputBlock("channel_name", channelNameText, channelNameHint, channelNameElement)

// Provide a static list of users to choose from, those provided now are just made up user IDs
// Get user IDs by right clicking on them in Slack, select "Copy link", and inspect the last part of the link
// The user ID should start with "U" followed by 8 random characters
memberOptions := createOptionBlockObjects([]string{"U9911MMAA", "U2233KKNN", "U00112233"}, true)
inviteeText := slack.NewTextBlockObject(slack.PlainTextType, "Invitee from static list", false, false)
inviteeOption := slack.NewOptionsSelectBlockElement(slack.OptTypeStatic, nil, "invitee", memberOptions...)
inviteeBlock := slack.NewInputBlock("invitee", inviteeText, inviteeOption)
inviteeBlock := slack.NewInputBlock("invitee", inviteeText, nil, inviteeOption)

// Section with users select - this input will not be included in the view_submission's view.state.values,
// but instead be sent as a "block_actions" request
Expand All @@ -48,15 +48,16 @@ func main() {
checkboxTxt := slack.NewTextBlockObject(slack.PlainTextType, "Checkbox", false, false)
checkboxOptions := createOptionBlockObjects([]string{"option 1", "option 2", "option 3"}, false)
checkboxOptionsBlock := slack.NewCheckboxGroupsBlockElement("chkbox", checkboxOptions...)
checkboxBlock := slack.NewInputBlock("chkbox", checkboxTxt, checkboxOptionsBlock)
checkboxBlock := slack.NewInputBlock("chkbox", checkboxTxt, nil, checkboxOptionsBlock)

summaryText := slack.NewTextBlockObject(slack.PlainTextType, "Summary", false, false)
summaryHint := slack.NewTextBlockObject(slack.PlainTextType, "Summary Hint", false, false)
summaryPlaceholder := slack.NewTextBlockObject(slack.PlainTextType, "Summary of reason for creating channel", false, false)
summaryElement := slack.NewPlainTextInputBlockElement(summaryPlaceholder, "summary")
// Just set an arbitrary max length to avoid too prose summary
summaryElement.MaxLength = 200
summaryElement.Multiline = true
summaryBlock := slack.NewInputBlock("summary", summaryText, summaryElement)
summaryBlock := slack.NewInputBlock("summary", summaryText, summaryHint, summaryElement)

blocks := slack.Blocks{
BlockSet: []slack.Block{
Expand Down
2 changes: 2 additions & 0 deletions interactions_test.go
Expand Up @@ -256,6 +256,7 @@ func TestViewSubmissionCallback(t *testing.T) {
false,
false,
),
nil,
&PlainTextInputBlockElement{
Type: "plain_text_input",
ActionID: "ml-value",
Expand All @@ -270,6 +271,7 @@ func TestViewSubmissionCallback(t *testing.T) {
false,
false,
),
nil,
&SelectBlockElement{
Type: "conversations_select",
ActionID: "target_select",
Expand Down
16 changes: 16 additions & 0 deletions views_test.go
Expand Up @@ -173,6 +173,10 @@ func TestSlack_OpenView(t *testing.T) {
Type: PlainTextType,
Text: "A simple label",
},
&TextBlockObject{
Type: PlainTextType,
Text: "A simple hint",
},
NewPlainTextInputBlockElement(
&TextBlockObject{
Type: PlainTextType,
Expand Down Expand Up @@ -332,6 +336,10 @@ func TestSlack_View_PublishView(t *testing.T) {
Type: PlainTextType,
Text: "A simple label",
},
&TextBlockObject{
Type: PlainTextType,
Text: "A simple hint",
},
NewPlainTextInputBlockElement(
&TextBlockObject{
Type: PlainTextType,
Expand Down Expand Up @@ -504,6 +512,10 @@ func TestSlack_PushView(t *testing.T) {
Type: PlainTextType,
Text: "A simple label",
},
&TextBlockObject{
Type: PlainTextType,
Text: "A simple hint",
},
NewPlainTextInputBlockElement(
&TextBlockObject{
Type: PlainTextType,
Expand Down Expand Up @@ -680,6 +692,10 @@ func TestSlack_UpdateView(t *testing.T) {
Type: PlainTextType,
Text: "A simple label",
},
&TextBlockObject{
Type: PlainTextType,
Text: "A simple hint",
},
NewPlainTextInputBlockElement(
&TextBlockObject{
Type: PlainTextType,
Expand Down

0 comments on commit b2bdf74

Please sign in to comment.