v3.0.0StarRatingInputRHF + ZodRatingDistributionuseRatingField

v3 — Form Integration

StarRatingInput · StarRatingTooltip · RatingDistribution · useRatingField

43
01

StarRatingInput — Form Field

v3Label + helper text
Your feedback helps us improve
import { StarRatingInput } from "star-rating-x";

<StarRatingInput
  label="Rate your experience"
  helperText="Your feedback helps us improve"
  required
  theme="gold"
/>
v3Error state validation
Please leave a rating.
<StarRatingInput
  label="Product quality"
  required
  errorMessage="Please leave a rating."
  theme="fire"
/>
02

useRatingField — Standalone Validation

v3Two validated fields in a form
const r1 = useRatingField({ required: true });
const r2 = useRatingField({ required: true, minValue: 3 });

<form onSubmit={handleSubmit}>
  <StarRatingInput
    {...r1.field}
    label="Overall"
    required
    errorMessage={r1.errorMessage}
  />
  <StarRatingInput
    {...r2.field}
    label="Would recommend?"
    required
    helperText="Min 3 stars"
    errorMessage={r2.errorMessage}
  />
  <button type="submit">Submit</button>
</form>
03

React Hook Form Integration

RHFController + Zod pattern
// Zod schema
z.object({
  rating: z.number()
    .min(1,"Select 1+ star"),
  quality: z.number()
    .min(3,"Quality ≥ 3 stars"),
})
import { useForm, Controller } from "react-hook-form";
import { z } from "zod";
import { zodResolver } from "@hookform/resolvers/zod";
import { StarRatingInput } from "star-rating-x";

const schema = z.object({
  rating: z.number().min(1,"Select at least 1 star"),
});

const { control } = useForm({
  resolver: zodResolver(schema),
  defaultValues: { rating: 0 },
});

<Controller
  name="rating"
  control={control}
  render={({ field, fieldState }) => (
    <StarRatingInput
      {...field}
      label="Product rating"
      required
      errorMessage={fieldState.error?.message}
    />
  )}
/>
04

StarRatingTooltip — Popup on Hover

v3Rich tooltip with custom renderer
import { StarRatingTooltip } from "star-rating-x";

const labels = ["Terrible 😡","Bad 😕","Okay 😐","Good 😊","Amazing! 🤩"];

<StarRatingTooltip
  tooltips={labels}
  tooltipRenderer={({ value, label }) => (
    <span><strong>{value}★</strong> — {label}</span>
  )}
  theme="gold"
  size={34}
/>
05

RatingDistribution — Bar Chart

v3Clickable filter rows
5
312
4
128
3
45
2
18
1
9

Click a row to filter

import { RatingDistribution } from "star-rating-x";

const data = { 5:312, 4:128, 3:45, 2:18, 1:9 };

<RatingDistribution
  data={data}
  theme="gold"
  showCount
  onFilter={(star) => setFilter(star)}
  activeFilter={filter}
/>

star-rating-x v3

MIT · Abdelrahman Ayman

v4 →npmGitHub