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

对于当前的飞书卡片构建 实现了一部份扩展 #76

Merged
merged 1 commit into from
Oct 21, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions card/button.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var _ Element = (*ButtonBlock)(nil)
// ButtonBlock 按钮元素
type ButtonBlock struct {
text *TextBlock
name string
url string
multiURL *URLBlock
btnType string
Expand All @@ -15,6 +16,7 @@ type ButtonBlock struct {
type buttonRenderer struct {
ElementTag
Text Renderer `json:"text"`
Name string `json:"name,omitempty"`
URL string `json:"url,omitempty"`
MultiURL Renderer `json:"multi_url,omitempty"`
Type string `json:"type,omitempty"`
Expand All @@ -28,6 +30,7 @@ func (b *ButtonBlock) Render() Renderer {
ElementTag: ElementTag{
Tag: "button",
},
Name: b.name,
Text: b.text.Render(),
URL: b.url,
Type: b.btnType,
Expand All @@ -47,6 +50,12 @@ func Button(text *TextBlock) *ButtonBlock {
return (&ButtonBlock{text: text}).Default()
}

// Name 按钮的标识
func (b *ButtonBlock) Name(n string) *ButtonBlock {
b.name = n
return b
}

// URL 按钮的跳转链接
func (b *ButtonBlock) URL(u string) *ButtonBlock {
b.url = u
Expand Down
47 changes: 47 additions & 0 deletions card/form.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package card

var _ Element = (*FormBlock)(nil)

// FormBlock 表单元素
type FormBlock struct {
name string
elements []Element
value map[string]interface{}
}

type formRenderer struct {
ElementTag
Name string `json:"name"`
Elements []Renderer `json:"elements,omitempty"`
Value map[string]interface{} `json:"value,omitempty"`
}

// Render 渲染为 Renderer
func (b *FormBlock) Render() Renderer {
ret := formRenderer{
ElementTag: ElementTag{
Tag: "form",
},
Name: b.name,
Elements: renderElements(b.elements),
Value: b.value,
}
return ret
}

// Form 表单交互元素
func Form(name string, el ...Element) *FormBlock {
ret := &FormBlock{
name: name,
elements: make([]Element, len(el))}
for i, v := range el {
ret.elements[i] = v
}
return ret
}

// Value 点击后发送给业务方的数据
func (b *FormBlock) Value(v map[string]interface{}) *FormBlock {
b.value = v
return b
}
55 changes: 55 additions & 0 deletions card/input.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package card

var _ Element = (*InputBlock)(nil)

// InputBlock 输入元素
type InputBlock struct {
name string
tag string
placeholder string
}

type InputBlockRenderer struct {
ElementTag
Name string `json:"name"`
Placeholder Renderer `json:"placeholder,omitempty"`
LabelPosition string `json:"label_position"`
Label label `json:"label,omitempty"`
MaxLength int `json:"max_length"`
}

Check warning on line 19 in card/input.go

View workflow job for this annotation

GitHub Actions / run

exported type InputBlockRenderer should have comment or be unexported

type label struct {
Tag string `json:"tag"`
Content string `json:"content"`
}

// Render 渲染为 Renderer
func (s *InputBlock) Render() Renderer {
ret := InputBlockRenderer{
ElementTag: ElementTag{
Tag: "input",
},
Name: s.name,
LabelPosition: "left",
MaxLength: 120,
Label: label{
Tag: "plain_text",
Content: "Custom Input:",
},
Placeholder: Text(s.placeholder).Render(),
}
return ret
}

// Input 输入组件
func Input(name string) *InputBlock {
return &InputBlock{
name: name,
}
}

// Placeholder 默认展示内容
func (s *InputBlock) Placeholder(str string) *InputBlock {
s.placeholder = str
return s
}
80 changes: 80 additions & 0 deletions card/multi_select_static.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package card

var _ Element = (*MultiSelectMenuBlock)(nil)

// MultiSelectMenuBlock 菜单元素
type MultiSelectMenuBlock struct {
name string
tag string
placeholder string
options []Element
required bool
disabled bool
}

type multiselectMenuRenderer struct {
ElementTag
Name string `json:"name,omitempty"`
Placeholder Renderer `json:"placeholder,omitempty"`
Options []Renderer `json:"options,omitempty"`
SelectedValue []interface{} `json:"selected_value"`
Required bool `json:"required"`
Disabled bool `json:"disabled"`
Width string `json:"width,omitempty"`
}

// Render 渲染为 Renderer
func (s *MultiSelectMenuBlock) Render() Renderer {
ret := multiselectMenuRenderer{
ElementTag: ElementTag{
Tag: s.tag,
},
Name: s.name,
Required: s.required,
Disabled: s.disabled,
SelectedValue: []interface{}{},
Width: "fill",
Options: renderElements(s.options),
Placeholder: Text(s.placeholder).Render(),
}
return ret
}

// MultiSelectMenu 菜单组件
func MultiSelectMenu(name string, opt ...*OptionBlock) *MultiSelectMenuBlock {
ret := &MultiSelectMenuBlock{
tag: "multi_select_static",
name: name,
options: make([]Element, len(opt)),
required: false,
disabled: false,
}
for i, v := range opt {
ret.options[i] = v
}
return ret
}

// Placeholder 未选中时展示的内容,无默认选项时必须设置
func (s *MultiSelectMenuBlock) Placeholder(p string) *MultiSelectMenuBlock {
s.placeholder = p
return s
}

// Required 是否必选
func (s *MultiSelectMenuBlock) Required(r bool) *MultiSelectMenuBlock {
s.required = r
return s
}

// Disabled 是否禁用
func (s *MultiSelectMenuBlock) Disabled(d bool) *MultiSelectMenuBlock {
s.disabled = d
return s
}

// Name 按钮的标识
func (s *MultiSelectMenuBlock) Name(n string) *MultiSelectMenuBlock {
s.name = n
return s
}
67 changes: 67 additions & 0 deletions card/picker_datetime.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package card

var _ Element = (*PickerDatetimeBlock)(nil)

// PickerDatetimeBlock 输入元素
type PickerDatetimeBlock struct {
name string
width string
placeholder string
initialDatetime string
value map[string]interface{}
}

type PickerDatetimeBlockRenderer struct {
ElementTag
Name string `json:"name,omitempty"`
Width string `json:"width,omitempty"`
Placeholder Renderer `json:"placeholder,omitempty"`
InitialDatetime string `json:"initialDatetime,omitempty"`
Value map[string]interface{} `json:"value,omitempty"`
}

Check warning on line 21 in card/picker_datetime.go

View workflow job for this annotation

GitHub Actions / run

exported type PickerDatetimeBlockRenderer should have comment or be unexported

// Render 渲染为 Renderer
func (s *PickerDatetimeBlock) Render() Renderer {
ret := PickerDatetimeBlockRenderer{
ElementTag: ElementTag{
Tag: "picker_datetime",
},
Name: s.name,
Placeholder: Text(s.placeholder).Render(),
InitialDatetime: s.initialDatetime,
Value: s.value,
Width: "fill",
}
return ret
}

// PickerDatetime 时间组件
func PickerDatetime(name string) *PickerDatetimeBlock {
return &PickerDatetimeBlock{
name: name,
}
}

// Value 点击后发送给业务方的数据
func (s *PickerDatetimeBlock) Value(v map[string]interface{}) *PickerDatetimeBlock {
s.value = v
return s
}

// Placeholder 未选中值时展示的内容,无默认值时必填
func (s *PickerDatetimeBlock) Placeholder(p string) *PickerDatetimeBlock {
s.placeholder = p
return s
}

// InitialDatetime 默认选中值
func (s *PickerDatetimeBlock) InitialDatetime(d string) *PickerDatetimeBlock {
s.initialDatetime = d
return s
}

// Name 名字
func (s *PickerDatetimeBlock) Name(n string) *PickerDatetimeBlock {
s.name = n
return s
}
Loading