UNPKG

2.52 kBPlain TextView Raw
1/**
2 * A Field describes a single field in an action or form.
3 *
4 * Fields can be used to automatically render forms or other UIs based on
5 * hypermedia actions.
6 */
7export interface BaseField<TType extends string, TValue> {
8 /**
9 * Name of the field.
10 *
11 * Typically this is the property that will get sent to a server.
12 */
13 name: string;
14
15 /**
16 * Type describes the type of the property.
17 *
18 * This is similar to the HTML5 "type" attribute on forms.
19 */
20 type: TType,
21
22 /**
23 * The current (pre-filed) value on the form.
24 */
25 value?: TValue;
26
27 /**
28 * This could be used to describe a sample value.
29 */
30 placeholder?: TValue;
31
32 /**
33 * Whether this field is required for submitting the form.
34 */
35 required: boolean;
36
37 /**
38 * Render the field as read-only.
39 */
40 readOnly: boolean;
41
42 /**
43 * A human-readable label for the field.
44 */
45 label?: string;
46}
47
48
49/**
50 * A checkbox basically behaves like a boolean.
51 */
52export type Checkbox = BaseField<'checkbox', boolean>
53
54/**
55 * A color picker.
56 */
57export type Color = BaseField<'color', string>
58
59/**
60 * A 'date' field.
61 */
62export type Date = BaseField<'date', string>
63
64/**
65 * @deprecated
66 */
67export type DateTime = BaseField<'datetime', Date>
68
69export type DateTimeLocal = BaseField<'datetime-local', Date>
70
71export type Email = BaseField<'email', string>
72
73export type File = BaseField<'file', never>
74
75export type Hidden = BaseField<'hidden', string>
76
77export interface Number extends BaseField<'number', number> {
78 max?: number;
79 min?: number;
80 step?: number;
81}
82
83export type Month = BaseField<'month', string>
84
85export type Password = BaseField<'password', string>
86
87export interface Radio extends BaseField<'radio', string> {
88 options?: Map<string, string>;
89}
90
91export interface Range extends BaseField<'range', number> {
92 max?: number;
93 min?: number;
94 step?: number;
95}
96
97export type Search = BaseField<'search', string>
98
99export type Tel = BaseField<'tel', string>
100
101export interface Text extends BaseField<'text', string> {
102 minLength?: number;
103 maxLength?: number;
104 pattern?: RegExp,
105 options?: Map<string, string>;
106}
107
108export type Time = BaseField<'time', string>
109
110export type Url = BaseField<'url', string>
111
112export type Week = BaseField<'week', string>
113
114// eslint will want to fix the number type here
115export type Field = Checkbox | Color | Date | DateTime | DateTimeLocal | Email
116// eslint-disable-next-line
117 | File | Hidden | Number | Month | Password | Radio | Range | Search | Tel
118 | Text | Time | Url | Week;