Skip to content

Rate

rate

Rule

Basic Example

js
const rule = {
    type: 'rate',
    title: 'Rating',
    field: 'rate',
    value: 5,
    props: {
        disabled: false,
    }
}

Props Configuration Examples

Allow Half Selection

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

Custom Icon and Color

js
const rule = {
    type: 'rate',
    title: 'Product Rating',
    field: 'rating',
    value: 4,
    props: {
        icon: 'like',
        voidIcon: 'like-o',
        color: '#ffd21e',
        voidColor: '#c8c9cc',
    }
}

Clearable

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

Custom Total Count

js
const rule = {
    type: 'rate',
    title: 'Recommendation Level',
    field: 'level',
    value: 3,
    props: {
        count: 10,
    }
}

Events Examples

Listen to Rating Changes

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

Show Feedback After Rating

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

Complete configuration items:Vant_Rate

value :Number

Props

ParameterDescriptionTypeDefault Value
countTotal number of iconsnumber | string5
sizeIcon size, default unit is pxnumber | string20px
gutterIcon spacing, default unit is pxnumber | string4px
colorColor when selectedstring#ee0a24
void-colorColor when not selectedstring#c8c9cc
disabled-colorColor when disabledstring#c8c9cc
iconIcon name or image link when selected, same as Icon component's name attributestringstar
void-iconIcon name or image link when not selected, same as Icon component's name attributestringstar-o
icon-prefixIcon class name prefix, same as Icon component's class-prefix attributestringvan-icon
allow-halfWhether to allow half selectionbooleanfalse
clearableWhether to allow clearing by clicking againbooleanfalse
readonlyWhether it is in readonly state, cannot modify rating in readonly statebooleanfalse
disabledWhether to disable ratingbooleanfalse
touchableWhether to allow selecting rating through swipe gesturebooleantrue

Events

Event NameDescriptionCallback Parameters
changeEvent triggered when current rating value changescurrentValue: number

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