--- name: n-rate description: A rating component for collecting user feedback with customizable icons, colors, and half-star support in Naive UI. Ideal for product reviews, feedback forms, and rating systems. metadata: author: jiaiyan version: 1.0.0 --- # n-rate Component The `n-rate` component allows users to provide ratings, typically represented as stars or custom icons. It supports half-star ratings, custom colors, and various sizes. ## When to Use Use `n-rate` when you need to: - **Product Reviews**: Allow users to rate products or services - **Feedback Collection**: Gather user satisfaction ratings - **Content Rating**: Rate articles, videos, or other content - **Skill Assessment**: Display or collect skill level ratings - **Quick Evaluation**: Provide a simple, intuitive rating interface ## Basic Usage ### Basic Rate A simple 5-star rating component. ```vue ``` ### Different Sizes Rate supports `small`, `medium`, `large` sizes, or custom numeric sizes. ```vue ``` ### Custom Color Customize the color of active icons. ```vue ``` ### Custom Icon Use custom icons via the default slot. ```vue ``` ### Half Star Rating Enable half-star precision with `allow-half`. ```vue ``` ### Readonly Mode Display rating without user interaction. ```vue ``` ### Clearable Allow users to clear the rating by clicking on the current value. ```vue ``` ### Custom Count Set a custom maximum rating value. ```vue ``` ## API Reference ### n-rate Props | Name | Type | Default | Description | |------|------|---------|-------------| | `allow-half` | `boolean` | `false` | Allow activating half of the icon | | `clearable` | `boolean` | `false` | Whether the rate is clearable. Clicking on current value resets to `null` | | `color` | `string` | `undefined` | Activated icon color. Supports `#FFF`, `#FFFFFF`, `yellow`, `rgb(0, 0, 0)` formats | | `count` | `number` | `5` | Number of icons (maximum rating) | | `default-value` | `number \| null` | `null` | Default value of activated icons | | `readonly` | `boolean` | `false` | Readonly state | | `size` | `'small' \| 'medium' \| 'large' \| number` | `'medium'` | Icon size | | `value` | `number \| null` | `undefined` | Value of activated icons (controlled mode) | ### Events | Name | Parameters | Description | |------|------------|-------------| | `on-clear` | `() => void` | Callback when value is cleared | | `on-update:value` | `(value: number) => void` | Callback when the value (rating) changes | ### Slots | Name | Parameters | Description | |------|------------|-------------| | `default` | `(info: { index: number })` | Custom icon for the rating. `index` is the position (0-based) | ## Common Patterns ### Rate in Form ```vue ``` ### Display Rating with Text ```vue ``` ### Emoji Rating ```vue ``` ### Rating with Feedback ```vue ``` ## Best Practices ### 1. Use Half-Star for Precise Ratings Enable `allow-half` when users need to provide more precise ratings: ```vue ``` ### 2. Provide Clear Feedback Show the current rating value alongside the component: ```vue ``` ### 3. Use Readonly for Display When displaying existing ratings, use `readonly` mode: ```vue ``` ### 4. Consider Clearable Option Allow users to reset their rating when appropriate: ```vue ``` ### 5. Use Custom Colors Wisely Choose colors that match your design system and are accessible: ```vue ``` ### 6. Match Size with Context Use appropriate sizes based on the context: ```vue ``` ### 7. Validate in Forms Add validation rules when using in forms: ```javascript const rules = { rating: { required: true, type: 'number', min: 1, message: 'Please provide a rating', trigger: 'change' } } ```