Skip to content

Rate

Rules

Basic Example

js
const rule = {
    type:"rate",
    field:"rate",
    title:"Recommendation Level",
    value:3.5,
    props:{
        count: 10,
    },
    validate:[
        {required:true,type:'number',min:3, message: 'Please rate at least 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,
        count: 5,
    },
}

Custom Character

js
const rule = {
    type:"rate",
    field:"rating",
    title:"Product Rating",
    value:4,
    props:{
        character: "❤",
        count: 5,
    },
}

Custom Tooltip

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

Disabled State

js
const rule = {
    type:"rate",
    field:"rating",
    title:"Product Rating",
    value:4,
    props:{
        disabled: 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);
        },
        hoverChange: (value) => {
            console.log('Mouse hover:', value);
        },
        focus: () => {
            console.log('Component focused');
        },
        blur: () => {
            console.log('Component blurred');
        },
    },
}

Display Feedback After Rating

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

Complete configuration items: Ant-desing-vue_Rate

value :Number

Props

PropertyDescriptionTypeDefault
allowClearWhether to allow clearing after clicking againbooleantrue
allowHalfWhether to allow half selectionbooleanfalse
autofocusAuto focusbooleanfalse
characterCustom characterstring | slot<StarOutlined />
countTotal number of starsnumber5
disabledRead-only, cannot interactbooleanfalse
tooltipsCustom tooltip for each itemstring[]-

Events

Event NameDescriptionCallback Parameters
blurCallback when losing focusFunction()
changeCallback when selectingFunction(value: number)
focusCallback when gaining focusFunction()
hoverChangeCallback when value changes on mouse hoverFunction(value: number)
keydownKey press callbackFunction(event)

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