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 block element type FileInput #1249

Merged
merged 2 commits into from
Feb 10, 2024
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 @@ -39,6 +39,7 @@ type BlockAction struct {
Type ActionType `json:"type"`
Text TextBlockObject `json:"text"`
Value string `json:"value"`
Files []File `json:"files"`
ActionTs string `json:"action_ts"`
SelectedOption OptionBlockObject `json:"selected_option"`
SelectedOptions []OptionBlockObject `json:"selected_options"`
Expand Down
2 changes: 2 additions & 0 deletions block_conv.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ func (b *InputBlock) UnmarshalJSON(data []byte) error {
e = &RadioButtonsBlockElement{}
case "number_input":
e = &NumberInputBlockElement{}
case "file_input":
e = &FileInputBlockElement{}
default:
return errors.New("unsupported block element type")
}
Expand Down
38 changes: 38 additions & 0 deletions block_element.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const (
METEmailTextInput MessageElementType = "email_text_input"
METURLTextInput MessageElementType = "url_text_input"
METNumber MessageElementType = "number_input"
METFileInput MessageElementType = "file_input"

MixedElementImage MixedElementType = "mixed_image"
MixedElementText MixedElementType = "mixed_text"
Expand Down Expand Up @@ -591,3 +592,40 @@ func NewNumberInputBlockElement(placeholder *TextBlockObject, actionID string, i
IsDecimalAllowed: isDecimalAllowed,
}
}

// FileInputBlockElement creates a field where a user can upload a file.
//
// File input elements are currently only available in modals.
//
// More Information: https://api.slack.com/reference/block-kit/block-elements#file_input
type FileInputBlockElement struct {
Type MessageElementType `json:"type"`
ActionID string `json:"action_id,omitempty"`
FileTypes []string `json:"filetypes,omitempty"`
MaxFiles int `json:"max_files,omitempty"`
}

// ElementType returns the type of the Element
func (s FileInputBlockElement) ElementType() MessageElementType {
return s.Type
}

// NewFileInputBlockElement returns an instance of a file input element
func NewFileInputBlockElement(actionID string) *FileInputBlockElement {
return &FileInputBlockElement{
Type: METFileInput,
ActionID: actionID,
}
}

// WithFileTypes sets the file types that can be uploaded
func (s *FileInputBlockElement) WithFileTypes(fileTypes ...string) *FileInputBlockElement {
s.FileTypes = fileTypes
return s
}

// WithMaxFiles sets the maximum number of files that can be uploaded
func (s *FileInputBlockElement) WithMaxFiles(maxFiles int) *FileInputBlockElement {
s.MaxFiles = maxFiles
return s
}
16 changes: 16 additions & 0 deletions block_element_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,19 @@ func TestNewNumberInputBlockElement(t *testing.T) {
assert.Equal(t, numberInputElement.IsDecimalAllowed, true)

}

func TestNewFileInputBlockElement(t *testing.T) {

fileInputElement := NewFileInputBlockElement("test")

assert.Equal(t, string(fileInputElement.Type), "file_input")
assert.Equal(t, fileInputElement.ActionID, "test")

fileInputElement.WithFileTypes("jpg", "png")
assert.Equal(t, len(fileInputElement.FileTypes), 2)
assert.Contains(t, fileInputElement.FileTypes, "jpg")
assert.Contains(t, fileInputElement.FileTypes, "png")

fileInputElement.WithMaxFiles(10)
assert.Equal(t, fileInputElement.MaxFiles, 10)
}