Back to

Package vgform

Latest Go to latest

The latest major version is .

Published: Sep 13, 2020 | License: MIT | Module:


Package vgform has Vugu components that wrap HTML form controls for convenience.

NOTES: It would be tempting to wrap every HTML form control that exists. However, I think we're going to find as things move forward that there is a high value in keeping things as much "just HTML" as possible. For a button, for instance, I can't think of anything a wrapper component can do that a regular HTML button tag can't. In this case, we don't provide a component because it doesn't do anything. The core idea is that keeping things simple and not wrapping things that don't need wrapping is more important than keeping things "consistent" by wrapping everything. The aim here is to provide components that are as close to the regular HTML tag as possible while still providing useful functionality as it relates to integrating it with the data in your Go program. If a component doesn't do that, it should not be included here. That's the general idea anyway. If it turns out that this ends up wrapping 90% of the form controls anyway, then maybe we just do 100% and the "consistency" argument wins. Nonetheless, I still think this concern about not unnecessarily abstracting things is important to Vugu component design. Using forms (and hopefully other components) should read as "it's basically HTML with this additional functionality" rather than an entirely new language to learn. This will also come heavily into play in the design of things like a library that works with Bootstrap CSS.



var SimpleTitle = TextMapperFunc(func(key string) string {
	return strings.Title(strings.NewReplacer("-", " ", "_", " ").Replace(key))

SimpleTitle implements TextMapper by replacing '-' and '_' with a space and calling strings.Title.

type Input

type Input struct {
	Value   StringValuer // get/set the currently selected value
	AttrMap vugu.AttrMap

Input corresponds to an input HTML element. What you provide for the `type` attribute can trigger behavioral differences appropriate for specific input types.

type="checkbox" type="radio" (list them out)

func (*Input) Build

func (c *Input) Build(vgin *vugu.BuildIn) (vgout *vugu.BuildOut)

type KeyLister

type KeyLister interface {
	KeyList() []string

KeyLister provides a list keys as a string slice. Keys are used in the `value` attribute of HTML option tags (with a select).

type KeyListerFunc

type KeyListerFunc func() []string

KeyListerFunc implements KeyLister as a function.

func (KeyListerFunc) KeyList

func (f KeyListerFunc) KeyList() []string

KeyList implements the KeyLister interface.

type MapOptions

type MapOptions map[string]string

MapOptions implements the Options interface on a map[string]string. The keys will be returned in alphanumeric sequence (using sort.Strings), or you can call SortFunc to assign a custom sort function.

func (MapOptions) KeyList

func (m MapOptions) KeyList() []string

KeyList implements KeyLister by returning the map keys sorted with sort.Strings().

func (MapOptions) SortFunc

func (m MapOptions) SortFunc(sf func(i, j int) bool) Options

SortFunc returns an Options instance that uses this map for keys and text and sorts according to the order specified by this function.

func (MapOptions) TextMap

func (m MapOptions) TextMap(key string) string

TextMap implements TextMapper by returning `m[key]`.

type Options

type Options interface {

Options is an interface with KeyList and TextMap. It is used to express the options for a select element. It intentionally does not support option groups or other advanced behavior as that can be accomplished using slots (TO BE IMPLEMENTED). Options is provided to make it easy for the common case of adapting a slice or map to be used as select options.

type Select

type Select struct {
	Value StringValuer // get/set the currently selected value

	Options Options // provide KeyLister and TextMapper in one

	AttrMap vugu.AttrMap // regular HTML attributes like id and class
	// contains filtered or unexported fields

Select wraps an HTML select element.

For clarity in naming: The word "value" refers to the value of the currently selected option. The word "key" refers to the text inside the value attribute of an option tag, and the word "text" refers to the HTML content of an option tag, i.e. `<option value="some-key">some text</option>`.

The Value field is a StringValuer which is used to get the current value upon render, and update it when changed. The StringPtr type provides a simple adapter for *string.

NOTE: When slots are supported this will be updated to support providing your own option tags so you can do things like option groups.

For the common case of simple lists of texts, set the Options as appropriate. See SliceOptions and MapOptions for convenient adapters for []string and map[string]string.

func (*Select) Build

func (c *Select) Build(vgin *vugu.BuildIn) (vgout *vugu.BuildOut)

type SliceOptions

type SliceOptions []string

SliceOptions implements the Options interface on a []string. The slice specifies the sequence and these exact string keys are also used as the text. You can also call Title() to use the SimpleTitle mapper or use TextFunc to assign a custom text mapper.

func (SliceOptions) KeyList

func (s SliceOptions) KeyList() []string

KeyList implements KeyLister with a type conversion ([]string(s)).

func (SliceOptions) TextFunc

func (s SliceOptions) TextFunc(tmf TextMapperFunc) Options

TextFunc returns an Options instance that uses this slice as the key list and the specified function for text mapping.

func (SliceOptions) TextMap

func (s SliceOptions) TextMap(key string) string

TextMap implements TextMapper by returning the key as the text.

func (SliceOptions) Title

func (s SliceOptions) Title() Options

Title is shorthand for s.TextFunc(SimpleTitle).

type StringPtr

type StringPtr struct {
	Value *string

StringPtr implements StringValuer on a string pointer.

func StringPtrDefault

func StringPtrDefault(p *string, def string) StringPtr

StringPtrDefault returns a StringPtr and sets the underlying string to def if it empty.

func (StringPtr) SetStringValue

func (s StringPtr) SetStringValue(v string)

SetStringValue implements StringValuer

func (StringPtr) StringValue

func (s StringPtr) StringValue() string

StringValue implements StringValuer

type StringValuer

type StringValuer interface {
	StringValue() string

StringValuer is a string that can be gotten and set.

type TextMapper

type TextMapper interface {
	TextMap(key string) string

TextMapper provides mapping from a key to the corresponding text. Text is used inside the contents of an HTML option tag (with a select). Text values are always HTML escaped.

type TextMapperFunc

type TextMapperFunc func(key string) string

TextMapperFunc implements TextMapper as a function.

func (TextMapperFunc) TextMap

func (f TextMapperFunc) TextMap(key string) string

TextMap implements the TextMapper interface.

type Textarea

type Textarea struct {
	Value   StringValuer // get/set the currently selected value
	AttrMap vugu.AttrMap

Textarea corresponds to a textarea HTML element.

func (*Textarea) Build

func (c *Textarea) Build(vgin *vugu.BuildIn) (vgout *vugu.BuildOut)

Package Files

Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to identifier