Page Menu
Home
Sealhub
Search
Configure Global Search
Log In
Files
F22396937
dropdown.ts
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Authored By
Unknown
Size
2 KB
Referenced Files
None
Subscribers
None
dropdown.ts
View Options
import
{
Context
}
from
"koa"
;
import
{
FlatTemplatable
,
tempstream
}
from
"tempstream"
;
import
{
attribute
}
from
"../../sanitize.js"
;
import
{
FormField
}
from
"../fields/field.js"
;
import
{
PickFromListField
}
from
"../fields/pick-from-list.js"
;
import
{
FormDataValue
}
from
"../form-types.js"
;
import
{
FormControlContext
}
from
"./form-control.js"
;
import
{
SimpleInput
}
from
"./simple-input.js"
;
export
type
DropdownOptions
=
{
label
:
string
;
autosubmit
?:
boolean
;
autocomplete
?:
boolean
;
};
export
type
DropdownOption
=
{
value
:
string
;
label
:
string
};
export
class
FreeformDropdown
<
ParsedValue
>
extends
SimpleInput
{
constructor
(
public
field
:
FormField
<
boolean
,
ParsedValue
|
null
>
,
public
_generateOptions
:
(
ctx
:
Context
)
=>
Promise
<
DropdownOption
[]
>
,
public
options
:
DropdownOptions
=
{
label
:
field
.
label
||
field
.
name
,
autosubmit
:
false
,
autocomplete
:
true
,
}
)
{
super
(
field
,
options
);
}
async
generateOptions
(
ctx
:
Context
)
:
Promise
<
DropdownOption
[]
>
{
return
this
.
_generateOptions
(
ctx
);
}
async
getInputAttributes
(
fctx
:
FormControlContext
)
:
Promise
<
Record
<
string
,
string
|
boolean
>>
{
const
original
=
await
super
.
getInputAttributes
(
fctx
);
return
{
...
original
,
...
Object
.
fromEntries
([
...(
this
.
options
.
autosubmit
?
[
[
"onchange"
,
((
original
.
onchange
as
string
|
undefined
)
||
""
)
+
";this.form.submit();"
,
],
]
:
[]),
]),
}
as
Record
<
string
,
string
>
;
}
async
renderInput
(
ctx
:
Context
,
attributes_str
:
string
,
data
:
Record
<
string
,
FormDataValue
>
)
:
Promise
<
FlatTemplatable
>
{
const
{
parsed
:
picked_value
}
=
await
this
.
field
.
getParsedValue
(
ctx
,
data
);
return
/* HTML */
tempstream
`<select
${
attributes_str
}
autocomplete="off">
${
Promise
.
resolve
(
this
.
generateOptions
(
ctx
)).
then
((
options
)
=>
options
.
map
(
({
value
,
label
}
) =>
`
<
option
value
=
"${attribute(value)}"
$
{
(
value
||
""
)
==
picked_value
?
"selected"
:
""
}
>
$
{
label
}
<
/option>`
)
)}
<
/select>`;
}
}
export
class
Dropdown
<
Required
extends
boolean
,
>
extends
FreeformDropdown
<
string
>
{
constructor
(
public
field
:
PickFromListField
<
Required
>
,
public
options
:
DropdownOptions
=
{
label
:
field
.
label
||
field
.
name
,
autosubmit
:
false
,
autocomplete
:
true
,
}
)
{
super
(
field
,
(
ctx
)
=>
field
.
generateOptions
(
ctx
),
options
);
}
}
File Metadata
Details
Attached
Mime Type
application/javascript
Expires
Fri, Nov 27, 17:01 (1 d, 22 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1199959
Default Alt Text
dropdown.ts (2 KB)
Attached To
Mode
rSGEN sealgen
Attached
Detach File
Event Timeline