Page Menu
Home
Sealhub
Search
Configure Global Search
Log In
Files
F3010796
checkboxed-list.ts
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
1 KB
Referenced Files
None
Subscribers
None
checkboxed-list.ts
View Options
import
{
is
,
predicates
}
from
"@sealcode/ts-predicates"
;
import
{
Context
}
from
"koa"
;
import
{
FormDataValue
}
from
"../form-types.js"
;
import
{
FieldParseResult
,
FormField
,
FormFieldValidationResponse
,
}
from
"./field.js"
;
export
type
CheckboxedListItem
=
{
value
:
string
;
label
?:
string
;
group
?:
string
;
};
export
class
CheckboxedListField
<
Required
extends
boolean
>
extends
FormField
<
Required
,
Record
<
string
,
"on"
>
>
{
constructor
(
public
required
:
Required
,
public
generateOptions
:
(
ctx
:
Context
)
=>
Promise
<
CheckboxedListItem
[]
>
,
public
isVisible
:
(
ctx
:
Context
)
=>
Promise
<
boolean
>
=
()
=>
Promise
.
resolve
(
true
)
)
{
super
(
required
);
}
predicate
=
predicates
.
array
(
predicates
.
string
);
// TODO: this is probably wrong
public
async
isValueValid
(
_
:
Context
,
value
:
unknown
)
:
Promise
<
FormFieldValidationResponse
>
{
if
(
is
(
value
,
predicates
.
string
))
{
return
{
valid
:
false
,
message
:
"you need an array"
};
}
if
(
is
(
value
,
predicates
.
null
))
{
return
{
valid
:
false
,
message
:
"you need an array"
};
}
return
{
valid
:
true
,
message
:
""
};
}
async
parse
(
_
:
Context
,
raw_value
:
FormDataValue
)
:
Promise
<
FieldParseResult
<
Record
<
string
,
"on"
>>>
{
if
(
is
(
raw_value
,
predicates
.
object
))
{
return
{
parsable
:
true
,
parsed_value
:
Object
.
fromEntries
(
Object
.
entries
(
raw_value
).
filter
(
([,
value
])
=>
value
===
"on"
)
)
as
Record
<
string
,
"on"
>
,
error
:
null
,
};
}
else
{
return
{
parsed_value
:
null
,
parsable
:
false
,
error
:
"Expected an object"
,
};
}
}
public
getEmptyValue
()
:
Record
<
string
,
"on"
>
{
return
{};
}
}
File Metadata
Details
Attached
Mime Type
text/x-java
Expires
Wed, May 7, 19:47 (1 d, 1 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
628504
Default Alt Text
checkboxed-list.ts (1 KB)
Attached To
Mode
rSGEN sealgen
Attached
Detach File
Event Timeline
Log In to Comment