Gecko UIGecko UI

RHFRating

Rating wired to React Hook Form

RHFRating

Rating wired to React Hook Form.

Installation

import { RHFRating } from '@geckoui/geckoui';

Basic Usage

The form holds 3

import { FormProvider, useForm } from 'react-hook-form';
import { RHFRating } from '@geckoui/geckoui';

function ReviewForm() {
  const methods = useForm({ defaultValues: { score: 0 } });

  return (
    <FormProvider {...methods}>
      <form onSubmit={methods.handleSubmit(save)}>
        <RHFRating name="score" rules={{ min: { value: 1, message: 'Pick a rating' } }} />
      </form>
    </FormProvider>
  );
}

The field holds a number.

Props API

PropTypeDefaultDescription
namestringrequiredThe field path
controlControl-Taken from FormProvider when left out
rulesRegisterOptions-Validation rules
defaultValuenumber0Where it starts when the form has nothing
onChange(value: number) => void-Called alongside the form update
...restRatingProps-Everything Rating takes, except value, onChange and name

name is handed to the rating so its radios are tied to the field.

Requiring one

Nothing picked is 0, not undefined, so required will not catch an untouched rating — 0 is a value. Use min instead:

<RHFRating name="score" rules={{ min: { value: 1, message: 'Pick a rating' } }} />
<RHFError name="score" />

Halves

The form holds 3.5

<RHFRating name="score" precision={0.5} />

precision is what a click lands on. The field still holds a number, 3.5 here.

  • Rating - The rating on its own
  • RHFRadio - For a one of many choice that is not a rating
  • RHFError - Shows the message