new: Extensions v0
This commit is contained in:
42
extension/ui_elements/abstract.go
Normal file
42
extension/ui_elements/abstract.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package ui_elements
|
||||
|
||||
// // Field is an interface that all specific field types implement.
|
||||
// type Field interface {
|
||||
// GetType() string
|
||||
// }
|
||||
|
||||
// // GenericField holds common field properties.
|
||||
// const (
|
||||
// Select string = "Select"
|
||||
// Email string = "Email"
|
||||
// Input string = "Input"
|
||||
// Password string = "Password"
|
||||
// TextArea string = "TextArea"
|
||||
// Switch string = "Switch"
|
||||
// Checkbox string = "Checkbox"
|
||||
// RadioButton string = "RadioButton"
|
||||
// DigitsOnly string = "digitsOnly"
|
||||
// )
|
||||
|
||||
// // FormField extends GenericField with additional common properties.
|
||||
// type FormField struct {
|
||||
// Key string `json:"key"`
|
||||
// Type string `json:"type"`
|
||||
// Label string `json:"label,omitempty"`
|
||||
// LabelHidden bool `json:"labelHidden"`
|
||||
// Required bool `json:"required,omitempty"`
|
||||
// Placeholder string `json:"placeholder,omitempty"`
|
||||
// Readonly bool `json:"readonly,omitempty"`
|
||||
// Value string `json:"value"`
|
||||
// Validator string `json:"validator,omitempty"`
|
||||
// Items []SelectItem `json:"items,omitempty"`
|
||||
// Lines int `json:"lines,omitempty"`
|
||||
// VerticalScroll bool `json:"verticalScroll,omitempty"`
|
||||
// HorizontalScroll bool `json:"horizontalScroll,omitempty"`
|
||||
// Monospace bool `json:"monospace,omitempty"`
|
||||
// }
|
||||
|
||||
// // GetType returns the type of the field.
|
||||
// func (gf FormField) GetType() string {
|
||||
// return gf.Type
|
||||
// }
|
||||
75
extension/ui_elements/all_test.go
Normal file
75
extension/ui_elements/all_test.go
Normal file
@@ -0,0 +1,75 @@
|
||||
package ui_elements
|
||||
|
||||
// import (
|
||||
// "encoding/json"
|
||||
// "testing"
|
||||
// )
|
||||
|
||||
// // Test UnmarshalJSON for different field types
|
||||
// func TestFormUnmarshalJSON(t *testing.T) {
|
||||
// formJSON := `{
|
||||
// "title": "Form Example",
|
||||
// "description": "This is a sample form.",
|
||||
// "fields": [
|
||||
// {
|
||||
// "key": "inputKey",
|
||||
// "type": "Input",
|
||||
// "label": "Hi Group",
|
||||
// "placeholder": "Hi Group flutter",
|
||||
// "required": true,
|
||||
// "value": "D"
|
||||
// },
|
||||
// {
|
||||
// "key": "passwordKey",
|
||||
// "type": "Password",
|
||||
// "label": "Password",
|
||||
// "required": true,
|
||||
// "value": "secret"
|
||||
// },
|
||||
// {
|
||||
// "key": "emailKey",
|
||||
// "type": "Email",
|
||||
// "label": "Email Label",
|
||||
// "placeholder": "Enter your email",
|
||||
// "required": true,
|
||||
// "value": "example@example.com"
|
||||
// }
|
||||
// ]
|
||||
// }`
|
||||
|
||||
// var form Form
|
||||
// err := json.Unmarshal([]byte(formJSON), &form)
|
||||
// if err != nil {
|
||||
// t.Fatalf("Error unmarshaling form JSON: %v", err)
|
||||
// }
|
||||
|
||||
// if form.Title != "Form Example" {
|
||||
// t.Errorf("Expected Title to be 'Form Example', got '%s'", form.Title)
|
||||
// }
|
||||
// if form.Description != "This is a sample form." {
|
||||
// t.Errorf("Expected Description to be 'This is a sample form.', got '%s'", form.Description)
|
||||
// }
|
||||
|
||||
// if len(form.Fields) != 3 {
|
||||
// t.Fatalf("Expected 3 fields, got %d", len(form.Fields))
|
||||
// }
|
||||
|
||||
// for i, field := range form.Fields {
|
||||
// switch f := field.(type) {
|
||||
// case InputField:
|
||||
// if f.Type != "Input" {
|
||||
// t.Errorf("Field %d: Expected Type to be 'Input', got '%s'", i+1, f.Type)
|
||||
// }
|
||||
// case PasswordField:
|
||||
// if f.Type != "Password" {
|
||||
// t.Errorf("Field %d: Expected Type to be 'Password', got '%s'", i+1, f.Type)
|
||||
// }
|
||||
// case EmailField:
|
||||
// if f.Type != "Email" {
|
||||
// t.Errorf("Field %d: Expected Type to be 'Email', got '%s'", i+1, f.Type)
|
||||
// }
|
||||
// default:
|
||||
// t.Errorf("Field %d: Unexpected field type %T", i+1, f)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
84
extension/ui_elements/base.go
Normal file
84
extension/ui_elements/base.go
Normal file
@@ -0,0 +1,84 @@
|
||||
package ui_elements
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// Field is an interface that all specific field types implement.
|
||||
type Field interface {
|
||||
GetType() string
|
||||
}
|
||||
|
||||
// GenericField holds common field properties.
|
||||
const (
|
||||
FieldSelect string = "Select"
|
||||
FieldEmail string = "Email"
|
||||
FieldInput string = "Input"
|
||||
FieldPassword string = "Password"
|
||||
FieldTextArea string = "TextArea"
|
||||
FieldSwitch string = "Switch"
|
||||
FieldCheckbox string = "Checkbox"
|
||||
FieldRadioButton string = "RadioButton"
|
||||
ValidatorDigitsOnly string = "digitsOnly"
|
||||
Button_SubmitCancel string = "SubmitCancel"
|
||||
Button_Cancel string = "Cancel"
|
||||
)
|
||||
|
||||
// FormField extends GenericField with additional common properties.
|
||||
type FormField struct {
|
||||
Key string `json:"key"`
|
||||
Type string `json:"type"`
|
||||
Label string `json:"label,omitempty"`
|
||||
LabelHidden bool `json:"labelHidden"`
|
||||
Required bool `json:"required,omitempty"`
|
||||
Placeholder string `json:"placeholder,omitempty"`
|
||||
Readonly bool `json:"readonly,omitempty"`
|
||||
Value string `json:"value"`
|
||||
Validator string `json:"validator,omitempty"`
|
||||
Items []SelectItem `json:"items,omitempty"`
|
||||
Lines int `json:"lines,omitempty"`
|
||||
VerticalScroll bool `json:"verticalScroll,omitempty"`
|
||||
HorizontalScroll bool `json:"horizontalScroll,omitempty"`
|
||||
Monospace bool `json:"monospace,omitempty"`
|
||||
}
|
||||
|
||||
// GetType returns the type of the field.
|
||||
func (gf FormField) GetType() string {
|
||||
return gf.Type
|
||||
}
|
||||
|
||||
type InputField struct {
|
||||
FormField
|
||||
Validator string `json:"validator,omitempty"`
|
||||
}
|
||||
|
||||
type SelectItem struct {
|
||||
Label string `json:"label"`
|
||||
Value string `json:"value"`
|
||||
}
|
||||
|
||||
type Form struct {
|
||||
Title string `json:"title"`
|
||||
Description string `json:"description"`
|
||||
Fields []FormField `json:"fields"`
|
||||
ButtonMode string `json:"buttonMode"`
|
||||
}
|
||||
|
||||
func (f *Form) ToJSON() string {
|
||||
formJson, err := json.MarshalIndent(f, "", " ")
|
||||
if err != nil {
|
||||
fmt.Println("Error encoding to JSON:", err)
|
||||
return ""
|
||||
}
|
||||
return (string(formJson))
|
||||
}
|
||||
|
||||
// UnmarshalJSON custom unmarshals JSON data into a Form.
|
||||
func (f *Form) UnmarshalJSON(data []byte) error {
|
||||
if err := json.Unmarshal(data, &f); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
25
extension/ui_elements/content.go
Normal file
25
extension/ui_elements/content.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package ui_elements
|
||||
|
||||
// // ContentField represents a label with additional properties.
|
||||
// type ContentField struct {
|
||||
// GenericField
|
||||
// Lines int `json:"lines,omitempty"`
|
||||
// VerticalScroll bool `json:"verticalScroll,omitempty"`
|
||||
// HorizontalScroll bool `json:"horizontalScroll,omitempty"`
|
||||
// Monospace bool `json:"monospace,omitempty"`
|
||||
// }
|
||||
|
||||
// // NewContentField creates a new ContentField.
|
||||
// func NewContentField(key, label string, lines int, monospace, horizontalScroll, verticalScroll bool) ContentField {
|
||||
// return ContentField{
|
||||
// GenericField: GenericField{
|
||||
// Key: key,
|
||||
// Type: "Content",
|
||||
// Label: label,
|
||||
// },
|
||||
// Lines: lines,
|
||||
// VerticalScroll: verticalScroll,
|
||||
// HorizontalScroll: horizontalScroll,
|
||||
// Monospace: monospace,
|
||||
// }
|
||||
// }
|
||||
244
extension/ui_elements/form.go
Normal file
244
extension/ui_elements/form.go
Normal file
@@ -0,0 +1,244 @@
|
||||
package ui_elements
|
||||
|
||||
// import (
|
||||
// "encoding/json"
|
||||
// "fmt"
|
||||
// )
|
||||
|
||||
// // InputField represents a text input field.
|
||||
// type InputField struct {
|
||||
// FormField
|
||||
// Validator string `json:"validator,omitempty"`
|
||||
|
||||
// }
|
||||
|
||||
// // // NewInputField creates a new InputField.
|
||||
// // func NewInputField(key, label, placeholder string, required bool, value string) InputField {
|
||||
// // return InputField{
|
||||
// // FormField: FormField{
|
||||
// // GenericField: GenericField{
|
||||
// // Key: key,
|
||||
// // Type: "Input",
|
||||
// // Label: label,
|
||||
// // },
|
||||
// // Placeholder: placeholder,
|
||||
// // Required: required,
|
||||
// // Value: value,
|
||||
// // },
|
||||
// // }
|
||||
// // }
|
||||
|
||||
// // // PasswordField represents a password field.
|
||||
// // type PasswordField struct {
|
||||
// // FormField
|
||||
// // }
|
||||
|
||||
// // // NewPasswordField creates a new PasswordField.
|
||||
// // func NewPasswordField(key, label string, required bool, value string) PasswordField {
|
||||
// // return PasswordField{
|
||||
// // FormField: FormField{
|
||||
// // GenericField: GenericField{
|
||||
// // Key: key,
|
||||
// // Type: "Password",
|
||||
// // Label: label,
|
||||
// // },
|
||||
// // Required: required,
|
||||
// // Value: value,
|
||||
// // },
|
||||
// // }
|
||||
// // }
|
||||
|
||||
// // // EmailField represents an email field.
|
||||
// // type EmailField struct {
|
||||
// // FormField
|
||||
// // }
|
||||
|
||||
// // // NewEmailField creates a new EmailField.
|
||||
// // func NewEmailField(key, label, placeholder string, required bool, value string) EmailField {
|
||||
// // return EmailField{
|
||||
// // FormField: FormField{
|
||||
// // GenericField: GenericField{
|
||||
// // Key: key,
|
||||
// // Type: "Email",
|
||||
// // Label: label,
|
||||
// // },
|
||||
// // Placeholder: placeholder,
|
||||
// // Required: required,
|
||||
// // Value: value,
|
||||
// // },
|
||||
// // }
|
||||
// // }
|
||||
|
||||
// // // TextAreaField represents a multi-line text area field.
|
||||
// // type TextAreaField struct {
|
||||
// // FormField
|
||||
// // }
|
||||
|
||||
// // // NewTextAreaField creates a new TextAreaField.
|
||||
// // func NewTextAreaField(key, label, placeholder string, required bool, value string) TextAreaField {
|
||||
// // return TextAreaField{
|
||||
// // FormField: FormField{
|
||||
// // GenericField: GenericField{
|
||||
// // Key: key,
|
||||
// // Type: "TextArea",
|
||||
// // Label: label,
|
||||
// // },
|
||||
// // Placeholder: placeholder,
|
||||
// // Required: required,
|
||||
// // Value: value,
|
||||
// // },
|
||||
// // }
|
||||
// // }
|
||||
|
||||
// // // SelectField represents a dropdown selection field.
|
||||
// // type SelectField struct {
|
||||
// // FormField
|
||||
// // Items []SelectItem `json:"items"`
|
||||
// // }
|
||||
|
||||
// // // SelectItem represents an item in a dropdown.
|
||||
// type SelectItem struct {
|
||||
// Label string `json:"label"`
|
||||
// Value string `json:"value"`
|
||||
// }
|
||||
|
||||
// // // NewSelectField creates a new SelectField.
|
||||
// // func NewSelectField(key, label, value string, items []SelectItem) SelectField {
|
||||
// // return SelectField{
|
||||
// // FormField: FormField{
|
||||
// // GenericField: GenericField{
|
||||
// // Key: key,
|
||||
// // Type: "Select",
|
||||
// // Label: label,
|
||||
// // },
|
||||
// // Value: value,
|
||||
// // },
|
||||
// // Items: items,
|
||||
// // }
|
||||
// // }
|
||||
|
||||
// // Form represents a collection of fields with metadata.
|
||||
// type Form struct {
|
||||
// Title string `json:"title"`
|
||||
// Description string `json:"description"`
|
||||
// Fields []FormField `json:"fields"`
|
||||
// }
|
||||
|
||||
// func (f *Form) ToJSON() string {
|
||||
// formJson, err := json.MarshalIndent(f, "", " ")
|
||||
// if err != nil {
|
||||
// fmt.Println("Error encoding to JSON:", err)
|
||||
// return ""
|
||||
// }
|
||||
// return (string(formJson))
|
||||
// }
|
||||
|
||||
// // UnmarshalJSON custom unmarshals JSON data into a Form.
|
||||
// func (f *Form) UnmarshalJSON(data []byte) error {
|
||||
// if err := json.Unmarshal(data, &f); err != nil {
|
||||
// return err
|
||||
// }
|
||||
|
||||
// // f.Title = raw.Title
|
||||
// // f.Description = raw.Description
|
||||
|
||||
// // for _, fieldData := range raw.Fields {
|
||||
// // var base FormField
|
||||
// // if err := json.Unmarshal(fieldData, &base); err != nil {
|
||||
// // return err
|
||||
// // }
|
||||
|
||||
// // var field Field
|
||||
// // switch base.Type {
|
||||
// // case "Input":
|
||||
// // var inputField InputField
|
||||
// // if err := json.Unmarshal(fieldData, &inputField); err != nil {
|
||||
// // return err
|
||||
// // }
|
||||
// // field = inputField
|
||||
// // case "Password":
|
||||
// // var passwordField PasswordField
|
||||
// // if err := json.Unmarshal(fieldData, &passwordField); err != nil {
|
||||
// // return err
|
||||
// // }
|
||||
// // field = passwordField
|
||||
// // case "Email":
|
||||
// // var emailField EmailField
|
||||
// // if err := json.Unmarshal(fieldData, &emailField); err != nil {
|
||||
// // return err
|
||||
// // }
|
||||
// // field = emailField
|
||||
// // case "TextArea":
|
||||
// // var textAreaField TextAreaField
|
||||
// // if err := json.Unmarshal(fieldData, &textAreaField); err != nil {
|
||||
// // return err
|
||||
// // }
|
||||
// // field = textAreaField
|
||||
// // case "Select":
|
||||
// // var selectField SelectField
|
||||
// // if err := json.Unmarshal(fieldData, &selectField); err != nil {
|
||||
// // return err
|
||||
// // }
|
||||
// // field = selectField
|
||||
// // case "Content":
|
||||
// // var contentField ContentField
|
||||
// // if err := json.Unmarshal(fieldData, &contentField); err != nil {
|
||||
// // return err
|
||||
// // }
|
||||
// // field = contentField
|
||||
// // default:
|
||||
// // return fmt.Errorf("unsupported field type: %s", base.Type)
|
||||
// // }
|
||||
|
||||
// // f.Fields = append(f.Fields, field)
|
||||
// // }
|
||||
|
||||
// return nil
|
||||
// }
|
||||
|
||||
// // func main() {
|
||||
// // // Example form JSON
|
||||
// // formJSON := `{
|
||||
// // "title": "Form Example",
|
||||
// // "description": "",
|
||||
// // "fields": [
|
||||
// // {
|
||||
// // "key": "inputKey",
|
||||
// // "type": "Input",
|
||||
// // "label": "Hi Group",
|
||||
// // "placeholder": "Hi Group flutter",
|
||||
// // "required": true,
|
||||
// // "value": "D"
|
||||
// // },
|
||||
// // {
|
||||
// // "key": "passwordKey",
|
||||
// // "type": "Password",
|
||||
// // "label": "Password",
|
||||
// // "required": true,
|
||||
// // "value": "secret"
|
||||
// // },
|
||||
// // {
|
||||
// // "key": "emailKey",
|
||||
// // "type": "Email",
|
||||
// // "label": "Email Label",
|
||||
// // "placeholder": "Enter your email",
|
||||
// // "required": true,
|
||||
// // "value": "example@example.com"
|
||||
// // }
|
||||
// // ]
|
||||
// // }`
|
||||
|
||||
// // var form Form
|
||||
|
||||
// // // Decode the form JSON
|
||||
// // if err := json.Unmarshal([]byte(formJSON), &form); err != nil {
|
||||
// // fmt.Println("Error decoding form:", err)
|
||||
// // return
|
||||
// // }
|
||||
|
||||
// // // Print decoded form fields
|
||||
// // fmt.Println("Form Title:", form.Title)
|
||||
// // for i, field := range form.Fields {
|
||||
// // fmt.Printf("Field %d: %T\n", i+1, field)
|
||||
// // }
|
||||
// // }
|
||||
Reference in New Issue
Block a user