Radio group
Choose one submitted value from an accessible typed radio group.
Default
open System
open FSharp.ViewEngine
open Acme.Components
open type Html
open type Svg
open type Datastar
let basicRadioGroup =
fveRadioGroup {
fveFieldLegend { "Contact method" }
for value, caption in [ "email", "Email"; "phone", "Phone" ] do
fveField {
_layout FieldLayout.Horizontal
fveRadio {
_id ("contact-" + value)
_name "contactMethod"
_value value
}
fveLabel {
_for ("contact-" + value)
caption
}
}
}On this page
Installation
Copy this component and its required dependencies into the consumer-owned Components project.
dotnet fve add radio-group --config src/Acme.Components/fve.jsonAdd to src/Acme.Web/js/app.js once, alongside your other component imports:
import '../../Acme.Components/Components/Assets/radio.js';The CLI resolves dependencies and preserves consumer-authored files. See Installation for project initialization, Tailwind, the shared runtime, and building/loading your app entry.
Usage
open System
open FSharp.ViewEngine
open Acme.Components
open type Html
open type Svg
open type Datastar
let basicRadioGroup =
fveRadioGroup {
fveFieldLegend { "Contact method" }
for value, caption in [ "email", "Email"; "phone", "Phone" ] do
fveField {
_layout FieldLayout.Horizontal
fveRadio {
_id ("contact-" + value)
_name "contactMethod"
_value value
}
fveLabel {
_for ("contact-" + value)
caption
}
}
}Option descriptions
open System
open FSharp.ViewEngine
open Acme.Components
open type Html
open type Svg
open type Datastar
let radioGroupWithHelp =
fveRadioGroup {
_ariaDescribedby "delivery-help"
fveFieldLegend { "Delivery" }
fveFieldDescription {
_id "delivery-help"
"Choose how you would like your order delivered."
}
for value, caption, description in
[ "standard", "Standard", "Arrives in three to five business days."
"express", "Express", "Arrives the next business day." ] do
fveField {
_layout FieldLayout.Horizontal
fveRadio {
_id ("shipping-" + value)
_name "delivery"
_value value
_ariaDescribedby ("shipping-" + value + "-help")
}
fveFieldContent {
fveLabel {
_for ("shipping-" + value)
caption
}
fveFieldDescription {
_id ("shipping-" + value + "-help")
description
}
}
}
}Horizontal orientation
open System
open FSharp.ViewEngine
open Acme.Components
open type Html
open type Svg
open type Datastar
let horizontalRadioGroup =
fveRadioGroup {
_orientation Orientation.Horizontal
fveFieldLegend { "Table density" }
for value, caption in [ "compact", "Compact"; "comfortable", "Comfortable"; "spacious", "Spacious" ] do
fveField {
_layout FieldLayout.Horizontal
fveRadio {
_id ("density-" + value)
_name "density"
_value value
if value = "comfortable" then
_checked true
}
fveLabel {
_for ("density-" + value)
caption
}
}
}Required choice with validation
open System
open FSharp.ViewEngine
open Acme.Components
open type Html
open type Svg
open type Datastar
let choiceSubmitButton id (label: string) =
fveButton {
_color Color.Primary
_variant ButtonVariant.Solid
_type "submit"
_id id
label
}
let choiceResult id result =
output {
_id id
_role "status"
_class "block min-h-5 text-sm text-[var(--fve-muted-text)]"
defaultArg result "Submit the form to see the result."
}
let private updateModeOptions =
[ "automatic", "Automatic", false
"manual", "Manual review", false
"scheduled", "Scheduled", true ]
let radioGroupFormRegion selected (validation: string option) result =
div {
_id "components-radio-form-region"
_class "grid max-w-sm gap-3"
form {
_novalidate true
_dataOn ("submit", "@post('/components/choices/radio', {contentType: 'form'})")
_class "grid gap-3"
fveRadioGroup {
_id "components-update-mode"
_ariaRequired true
_ariaInvalid validation.IsSome
_ariaDescribedby (
String.concat
" "
[ "update-mode-help"
if validation.IsSome then
"update-mode-error" ]
)
fveFieldLegend { "Update mode" }
fveFieldDescription {
_id "update-mode-help"
"Choose how updates are applied."
}
for value, caption, disabled in updateModeOptions do
fveField {
_layout FieldLayout.Horizontal
fveRadio {
_id ("update-mode-" + value)
_name "updateMode"
_value value
if selected = Some value then
_checked true
_disabled disabled
_required true
_ariaInvalid validation.IsSome
_ariaDescribedby (
String.concat
" "
[ "update-mode-help"
if validation.IsSome then
"update-mode-error" ]
)
}
fveLabel {
_for ("update-mode-" + value)
caption
}
}
match validation with
| Some message ->
fveFieldError {
_id "update-mode-error"
message
}
| None -> ()
}
choiceSubmitButton "components-radio-submit" "Validate choice"
}
choiceResult "components-radio-result" result
}
let updateMode = radioGroupFormRegion None None NoneThis example also uses Button. See their Installation sections for any additional setup.
Pending
open System
open FSharp.ViewEngine
open Acme.Components
open type Html
open type Svg
open type Datastar
let private updateModeOptions =
[ "automatic", "Automatic", false
"manual", "Manual review", false
"scheduled", "Scheduled", true ]
let pendingUpdateMode =
fveRadioGroup {
_ariaBusy true
fveFieldLegend { "Saving update mode" }
for value, caption, disabled in updateModeOptions do
fveField {
_layout FieldLayout.Horizontal
fveRadio {
_id ("pending-mode-" + value)
_name "pendingUpdateMode"
_value value
if value = "automatic" then
_checked true
_disabled disabled
_readonly true
}
fveLabel {
_for ("pending-mode-" + value)
caption
}
}
}Disabled
open System
open FSharp.ViewEngine
open Acme.Components
open type Html
open type Svg
open type Datastar
let private updateModeOptions =
[ "automatic", "Automatic", false
"manual", "Manual review", false
"scheduled", "Scheduled", true ]
let disabledUpdateMode =
fveRadioGroup {
_disabled true
fveFieldLegend { "Update mode unavailable" }
for value, caption, _ in updateModeOptions do
fveField {
_layout FieldLayout.Horizontal
fveRadio {
_id ("disabled-mode-" + value)
_name "disabledUpdateMode"
_value value
if value = "manual" then
_checked true
}
fveLabel {
_for ("disabled-mode-" + value)
caption
}
}
}API reference
module RadioGroupDsl =
val fveRadioGroup : ComponentElementBuilder
val fveRadio : ComponentElementBuilder