Rating input
Create rating inputs in your forms.

Use the ratingInput()
function to create rating inputs in your forms. It allows users to select a rating value using star or heart icons.
Basic usage
import { Composer } from "formsmd";
const composer = new Composer({
id: "my-form"
});
composer.ratingInput("rating", {
question: "How would you rate your experience?"
});
Generates the following Markdown-like syntax:
#! id = my-form
rating = RatingInput(
| question = How would you rate your experience?
)
Required
Add the required
parameter to make the field mandatory:
composer.ratingInput("rating", {
question: "How would you rate your experience?",
required: true
});
Generates the following Markdown-like syntax:
rating* = RatingInput(
| question = How would you rate your experience?
)
Function overview
The following is the overview of the function:
ratingInput(name: string, params: object)
Arguments
name
string
A unique name for your form field that you'll use to identify the user's response.*
params
object
An object containing all the configuration parameters for your rating input field (see the parameters section below for the full list of options).
*Avoid values for the name
argument which may be the names of HTML attributes, such as "name"
, "role"
, "id"
, etc. This is because by default, the form's template string is first sanitized using DOMPurify, and these values may be removed to prevent DOM clobbering.
Parameters
Shared parameters
These parameters are common to all form fields:
question
(required)
string
The main question or label of the form field.
required
true
(boolean
)
When set, the field becomes required.
description
string
Any extra information that the user may need to fill out the form. Appears right below the question.
fieldSize
"sm"
When set to "sm"
, the font sizes of the question, description, and answer are made smaller.
labelStyle
"classic"
When set to "classic"
, the question and description of the form field are made smaller.
subfield
true
(boolean
)
When set, the question and description of the form field are made smaller. Functionally the same as setting labelStyle
to "classic"
.
disabled
true
(boolean
)
When set, the input is disabled.
autofocus
true
(boolean
)
When set, the input will be automatically focused when the parent slide becomes active, or immediately after page load.
id
string
The id
attribute of the form field container.
attrs
Array<{ name: string, value: string }>
Other HTML attributes of the form field. Each attribute has a name
and value
property.
displayCondition
{ dependencies: string[], condition: string }
Controls when the field is shown. The dependencies
lists the fields to watch, and condition
is the expression that must be true to show the field. The condition
must be a valid Nunjucks expression. See example.
Rating input specific parameters
outOf
number
Number of rating options (1-10). Defaults to 5
.
icon
"star"
| "heart"
| "hearts"
Icon to use for rating. Defaults to "star"
.
value
number
Pre-selected rating value.
hideLabels
true
(boolean
)
Whether to hide the numeric labels.
Examples
Rating input out of 10
composer.ratingInput("satisfaction", {
question: "How satisfied are you with our service?",
description: "Please rate your overall satisfaction level",
outOf: 10,
icon: "star",
required: true
});
Generates the following Markdown-like syntax:
satisfaction* = RatingInput(
| question = How satisfied are you with our service?
| description = Please rate your overall satisfaction level
| outOf = 10
| icon = star
)
Rating input with hearts and hidden labels
composer.ratingInput("experience", {
question: "Rate your experience",
icon: "hearts",
hideLabels: true,
outOf: 5
});
Generates the following Markdown-like syntax:
experience = RatingInput(
| question = Rate your experience
| icon = hearts
| hideLabels
| outOf = 5
)
Styled rating input with custom attributes
Add CSS classes and other HTML attributes using the classNames
and attrs
parameters. Please note, these class names and attributes are added to the <div>
or <fieldset>
container that contains the actual input field(s).
composer.ratingInput("userRating", {
question: "Rate this content",
classNames: ["col-6", "xs:col-6"],
attrs: [
{ name: "style", value: "font-size: 18px;" }
]
});
Generates the following Markdown-like syntax:
[.col-6 .xs:col-6 style="font-size: 18px;"]
userRating = RatingInput(
| question = Rate this content
)
Please see the available CSS utility classes.
Conditional display
Conditionally show or hide a rating input field using the displayCondition
parameter. It works as follows:
dependencies
lists the fields to watch.condition
is the expression that must be true to show the field. This must be a valid Nunjucks expression.
For instance, in the example below, the detailed rating will only show up if the user indicates they want to provide one.
composer.choiceInput("wantToRate", {
question: "Would you like to rate our service?",
required: true,
choices: ["Yes", "No"]
})
composer.ratingInput("serviceRating", {
question: "Rate our service",
displayCondition: {
dependencies: ["wantToRate"],
condition: "wantToRate == 'Yes'"
}
});
Generates the following Markdown-like syntax:
wantToRate* = ChoiceInput(
| question = Would you like to rate our service?
| choices = Yes, No
)
::: [{$ wantToRate $}]
{% if wantToRate == "Yes" %}
serviceRating = RatingInput(
| question = Rate our service
)
{% endif %}
:::
Last updated
Was this helpful?