form

package module
v0.9.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 9, 2022 License: Apache-2.0 Imports: 5 Imported by: 11

README

Form 🤔

GoDoc Build Status Codecov Go Report Card Version

JSON to HTML (Forms for Go)

This expandable Go module generates HTML forms using JSON configurations. It is inspired by JSON-Forms but is not compatable with that standard.

DO NOT USE

This project is a work-in-progress, and should NOT be used by ANYONE, for ANY PURPOSE, under ANY CIRCUMSTANCES. It is WILL BE CHANGED UNDERNEATH YOU WITHOUT NOTICE OR HESITATION, and is expressly GUARANTEED to blow up your computer, send your cat into an infinite loop, and combine your hot and cold laundry into a single cycle.

Pull Requests Welcome

This library is a work in progress, and will benefit from your experience reports, use cases, and contributions. If you have an idea for making this library better, send in a pull request. We're all in this together! 🤔

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Form

type Form struct {
	Path        string         `json:"path"`                  // Path to the data value displayed in for this form element
	Kind        string         `json:"kind"`                  // The kind of form element
	ID          string         `json:"id,omitempty"`          // DOM ID to use for this element.
	Label       string         `json:"label,omitempty"`       // Short label to be displayed on the form element
	Description string         `json:"description,omitempty"` // Longer description text to be displayed on the form element
	CSSClass    string         `json:"cssClass,omitempty"`    // CSS Class override to apply to this widget.  This should be used sparingly
	Options     map[string]any `json:"options,omitempty"`     // Additional custom properties defined by individual widgets
	Children    []Form         `json:"children,omitempty"`    // Array of sub-form elements that may be displayed depending on the kind.
	Show        Rule           `json:"show"`                  // Rules for showing/hiding/disabling this element
}

Form defines a single form element, or a nested form layout. It can be serialized to and from a database.

func MustParse added in v0.4.0

func MustParse(data any) Form

MustParse guarantees that a value has been parsed into a Form, or else it panics the application.

func NewForm added in v0.4.0

func NewForm(kind string) Form

NewForm returns a fully initialized Form object

func Parse added in v0.3.9

func Parse(data any) (Form, error)

Parse attempts to convert any value into a Form.

func (*Form) AddChild added in v0.4.0

func (form *Form) AddChild(kind string) *Form

AddChild adds a new child of the designated type to this form element. It returns a reference to the newly created child.

func (Form) AllPaths added in v0.2.1

func (form Form) AllPaths() []Form

AllPaths returns pointers to all of the valid paths in this form

func (Form) HTML

func (form Form) HTML(library *Library, schema *schema.Schema, value any) (string, error)

HTML returns a populated HTML string for the provided form, schema, and value

func (*Form) SetCSSClass added in v0.4.0

func (form *Form) SetCSSClass(cssClass string) *Form

SetCSSClass sets the CSSClass value for this form item

func (*Form) SetDescription added in v0.4.0

func (form *Form) SetDescription(description string) *Form

SetDescription sets the description value for this form item

func (*Form) SetID added in v0.4.0

func (form *Form) SetID(id string) *Form

SetID sets the ID value for this form item.

func (*Form) SetLabel added in v0.4.0

func (form *Form) SetLabel(label string) *Form

SetLabel sets the Label value for this form item

func (*Form) SetOption added in v0.4.0

func (form *Form) SetOption(key string, value string) *Form

SetOption sets an individual option for this form item

func (*Form) SetPath added in v0.4.0

func (form *Form) SetPath(path string) *Form

SetPath sets the path value for this form item This DOES NOT implement the path.Setter interface

func (*Form) UnmarshalMap added in v0.3.8

func (form *Form) UnmarshalMap(data map[string]any) error

UnmarshalMap parses data from a generic structure (map[string]any) into a Form record.

func (Form) Write

func (form Form) Write(library *Library, schema *schema.Schema, value any, b *html.Builder) error

Write generates an HTML string for the fully populated form into the provided string builder

type Library

type Library struct {
	Provider  OptionProvider
	Renderers map[string]Renderer
}

Library stores all of the available Renderers, and can execute them on a set of data

func NewLibrary added in v0.4.0

func NewLibrary(provider OptionProvider) Library

NewLibrary returns a fully initialized Library

func (Library) Options added in v0.3.2

func (library Library) Options(form Form, element schema.Element) []OptionCode

func (*Library) Register

func (library *Library) Register(name string, renderer Renderer)

Register adds a new Renderer to the form.Library

func (Library) Renderer

func (library Library) Renderer(name string) (Renderer, error)

Renderer retrieves a renderer function from the library

type OptionCode added in v0.3.2

type OptionCode struct {
	Value       string // Internal value of the Option
	Label       string // Human-friendly label/name of the Option
	Description string // Optional long description of the Option
	Icon        string // Optional icon to use when displaying the Option
	Group       string // Optiional grouping to use when displaying the Option
}

OptionCode represents a single value/label pair to be used in place of Enums for optional lists.

type OptionProvider added in v0.3.2

type OptionProvider interface {
	OptionCodes(string) ([]OptionCode, error)
}

OptionProvider is an external object that can inject OptionCodes based on their URL.

type Renderer

type Renderer func(Form, *schema.Schema, any, *html.Builder) error

Renderer is a function signature that writes HTML for a fully populated widget into a string builder.

type Rule added in v0.6.0

type Rule struct {
	Path  string `json:"path"`
	Op    string `json:"op"`
	Value string `json:"value"`
}

func (Rule) HyperscriptRules added in v0.6.0

func (rule Rule) HyperscriptRules() string

func (Rule) IsEmpty added in v0.6.0

func (rule Rule) IsEmpty() bool

func (Rule) Operator added in v0.6.0

func (rule Rule) Operator() string

type UnmarshalMaper added in v0.6.4

type UnmarshalMaper interface {

	// UnmarshalMap returns a value in the format map[string]interface
	UnmarshalMap() map[string]any
}

UnmarshalMaper wraps the UnmarshalMap interface

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳