Input
A single-line text control.
<input.Input placeholder="Email" />Installation
Run the command in the app module.
gx add inputThe command writes these files.
| File | Path in the app |
|---|---|
Input.gx |
ui/input/Input.gx |
Input.fixtures.go |
ui/input/Input.fixtures.go |
styles.go |
ui/input/styles.go |
Copy each file to its path in the app. Change each import of a registry package to the path of that package in the app.
package input
props {
// Id is the id of the root element. A label points at it.
Id string = ""
// Type is the type attribute. Empty means "text".
Type string = "text"
// Name is the name attribute. The form sends the value under this name.
Name string = ""
// Value is the value attribute: the start value of the input.
Value string = ""
// Placeholder is the placeholder attribute: the hint text of an empty control.
Placeholder string = ""
// Class adds classes to the root element.
Class string = ""
// Attrs adds HTML attributes to the root element.
Attrs gx.Attrs = nil
}
<input id={p.Id} type={p.inputType()} name={p.Name} value={p.Value} placeholder={p.Placeholder} data-slot="input" class={gx.Cx(Class, p.Class)} {...p.Attrs} />package input
import "github.com/alternayte/gx"
var InputFixtures = gx.Fixtures[InputProps]{
"Text": {Placeholder: "Email"},
"Filled": {Value: "ada@example.com", Attrs: gx.Attrs{{Key: "aria-label", Value: "Email"}}},
"Disabled": {Attrs: gx.Attrs{gx.Bool("disabled", true)}, Placeholder: "Disabled"},
"Invalid": {Value: "ada@", Attrs: gx.Attrs{{Key: "aria-label", Value: "Email"}, {Key: "aria-invalid", Value: "true"}}},
"File": {Type: "file", Attrs: gx.Attrs{{Key: "aria-label", Value: "File"}}},
}package input
// Control is the classes of an input without its focus ring. A group that
// draws the ring on its own border takes these classes for its control.
const Control = "h-9 w-full min-w-0 rounded-md border border-input bg-transparent px-3 py-1 text-base shadow-xs transition-[color,box-shadow] outline-none selection:bg-primary selection:text-primary-foreground file:inline-flex file:h-7 file:border-0 file:bg-transparent file:text-sm file:font-medium file:text-foreground placeholder:text-muted-foreground disabled:pointer-events-none disabled:cursor-not-allowed disabled:opacity-50 md:text-sm dark:bg-input/30 aria-invalid:border-destructive aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40"
// Class is the classes of an input. A control that looks like an input
// takes these classes.
const Class = Control + " focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50"
// inputType returns the type of the input; a zero value is text.
func (p InputProps) inputType() string {
if p.Type == "" {
return "text"
}
return p.Type
}The theme must define these tokens: --input, --ring, --muted-foreground, --primary, --primary-foreground, --foreground, --destructive.
Usage
<input.Input name="email" type="email" placeholder="Email" />For a form with rules and errors, use the generated gx.FormField control instead.
Set aria-invalid="true" through Attrs to show the error border.
input.Class holds the classes of the input for another control that must look the same.
Examples
Text
<input.Input placeholder="Email" />Filled
<input.Input value="ada@example.com" attrs={gx.Attrs{{Key: "aria-label", Value: "Email"}}} />Disabled
<input.Input attrs={gx.Attrs{gx.Bool("disabled", true)}} placeholder="Disabled" />Invalid
<input.Input value="ada@" attrs={gx.Attrs{{Key: "aria-label", Value: "Email"}, {Key: "aria-invalid", Value: "true"}}} />File
<input.Input type="file" attrs={gx.Attrs{{Key: "aria-label", Value: "File"}}} />API reference
A tag sets a prop by its name with a lower-case first letter: Class is class.
input.Input
| Prop | Type | Default | Description |
|---|---|---|---|
Id |
string |
"" |
Id is the id of the root element. A label points at it. |
Type |
string |
"text" |
Type is the type attribute. Empty means "text". |
Name |
string |
"" |
Name is the name attribute. The form sends the value under this name. |
Value |
string |
"" |
Value is the value attribute: the start value of the input. |
Placeholder |
string |
"" |
Placeholder is the placeholder attribute: the hint text of an empty control. |
Class |
string |
"" |
Class adds classes to the root element. |
Attrs |
gx.Attrs |
nil |
Attrs adds HTML attributes to the root element. |
Do and do not
Do
- Always pair an input with a label.
- Set
Typeto the data the field expects.
Don't
- Do not use placeholder text as a label.
- Do not build
valuefrom untrusted text without escaping. Gx escapes attribute values.
Keyboard
| Key | Action |
|---|---|
| Tab | Moves focus to the input. |
| Enter | Submits the form when the input is in a form. |