Skip to content

Rate Rating ​

Rules ​

Basic Example ​

js
const rule = {
    type:"rate",
    field:"rate",
    title:"Recommendation Level",
    value:3.5,
    props:{
        max: 10,
    },
    validate:[
        {required:true,type:'number',min:3, message: 'Select more than 3 stars',trigger:'change'}
    ]
}

Props Configuration Examples ​

Allow Half Selection ​

js
const rule = {
    type:"rate",
    field:"rating",
    title:"Product Rating",
    value:4.5,
    props:{
        allowHalf: true,
        max: 5,
    },
}

Show Text ​

js
const rule = {
    type:"rate",
    field:"satisfaction",
    title:"Satisfaction",
    value:3,
    props:{
        showText: true,
        texts: ['Very Poor', 'Disappointed', 'Average', 'Satisfied', 'Surprised'],
    },
}

Show Score ​

js
const rule = {
    type:"rate",
    field:"score",
    title:"Rating",
    value:4,
    props:{
        showScore: true,
        scoreTemplate: '{value} points',
    },
}

Custom Colors ​

js
const rule = {
    type:"rate",
    field:"rating",
    title:"Product Rating",
    value:4,
    props:{
        colors: {
            2: '#99A9BF',
            4: '#F7BA2A',
            5: '#FF9900'
        },
    },
}

Clearable ​

js
const rule = {
    type:"rate",
    field:"rating",
    title:"Product Rating",
    value:0,
    props:{
        clearable: true,
        allowHalf: true,
    },
}

Events Examples ​

Listen to Rating Changes ​

js
const rule = {
    type:"rate",
    field:"rating",
    title:"Product Rating",
    value:0,
    props:{
        allowHalf: true,
    },
    on: {
        change: (value) => {
            console.log('Rating changed:', value);
        },
    },
}

Show Feedback After Rating ​

js
const rule = {
    type:"rate",
    field:"rating",
    title:"Product Rating",
    value:0,
    props:{
        showText: true,
        texts: ['Very Poor', 'Disappointed', 'Average', 'Satisfied', 'Surprised'],
    },
    inject: true,
    on: {
        change: ($inject, value) => {
            // Auto-fill comment content based on rating
            const comments = {
                1: 'Product quality is very poor, not recommended',
                2: 'Product is average, needs improvement',
                3: 'Product is acceptable, basically satisfied',
                4: 'Product is good, worth recommending',
                5: 'Product is excellent, highly recommended',
            };
            if (value >= 1 && value <= 5) {
                $inject.api.setValue('comment', comments[Math.ceil(value)]);
            }
        },
    },
}

Full configuration: Element_Rate

value :Number

Props ​

Attribute NameDescriptionTypeDefault Value
maxMaximum scorenumber5
sizeSizeenum—
disabledWhether it is read-onlybooleanfalse
allowHalfWhether half selection is allowedbooleanfalse
lowThresholdBoundary value between low and medium scores. The value itself is classified as low scorenumber2
highThresholdBoundary value between high and medium scores. The value itself is classified as high scorenumber4
colorsColors of icons. If an array is passed, there are 3 elements corresponding to colors for 3 segments; if an object is passed, segments can be customized, with keys as boundary values of segments and values as corresponding colorsobject['#f7ba2a', '#f7ba2a', '#f7ba2a']
voidColorColor of unselected iconstring#c6d1de
disabledVoidColorColor of unselected icon when read-onlystring#eff2f7
iconsIcon components. If an array is passed, 3 elements are required, corresponding to class names for 3 parts; if an object is passed, segments can be customized, with keys as boundary values of segments and values as corresponding class namesobject[StarFilled, StarFilled, StarFilled]
voidIconIcon component for unselectedstring / ComponentStar
disabledVoidIconUnselected icon in disabled statestring / ComponentStarFilled
showTextWhether to display auxiliary text. If true, text content corresponding to current score will be selected from texts arraybooleanfalse
showScoreWhether to display current score. show-score and show-text cannot both be truebooleanfalse
textColorColor of auxiliary textstring''
textsAuxiliary text arrayarray['Extremely bad', 'Disappointed', 'Fair', 'Satisfied', 'Surprise']
scoreTemplateScore display templatestring
clearableWhether value can be reset to 0booleanfalse
idNative id attributestring—
ariaLabelConsistent with Rate's aria-label attributestring—

Events ​

Event NameDescriptionType
changeTriggered when score changesFunction

FormCreate is an open-source project released under the MIT License. Free for personal and commercial use.