Skip to main content

RHFColorPicker

RHFColorPicker is derived from the ColorPicker component of react-color-palette package.

npm install react-color-palette
yarn add react-color-palette

Usage

import { RHFColorPicker, RHFColorPickerProps } from '@nish1896/rhf-mui-components';

The color-picker accepts hex, rgb, hsl or text format, such as:

  • #561ecb
  • rgb(86 30 203)
  • hsl(120 100% 50% / .5)
  • cyan

If an invalid value is provided, the default color #000000(black) will be selected.

<RHFColorPicker
fieldName='favouriteColor'
value={getValues('favouriteColor')}
onValueChange={color => setValue('favouriteColor', color.hex)}
/>
note

The onValueChange prop is mandatory and allows you to choose the format of the selected color value based on your preference.

Examples

Alternatives

If this package doesn't meet your requirements, consider the react-color package.

API

RHFColorPickerProps extends the props of ColorPicker and accepts the following additional props:

NameTypeRequiredDescription
fieldNamestringReact Hook Form requires name as a key for the registration process.
valuestringSelected color in RHFColorPicker component. Default is #000000 (black).
onValueChange(color: IColor) => voidCallback function to get the selected color. Update form state by calling the setValue function, and passing the color value in preffered format.
disabledbooleanA boolean value to enable or disable editing of the form field.
labelReactNodeThe text to render in FormLabel component. By default, the value of fieldName such as firstName will be transformed to display "First Name".
showLabelAboveFormFieldbooleanRender form label above the form field in FormLabel component.
formLabelPropsFormLabelPropsFormLabelProps to customise FormLabel component for a field. Multiple fields can be configured using the ConfigProvider component.
helperTextReactNodeOptional helperText to render under RHFSelect or RHFNativeSelect field.
errorMessageReactNodeError message to be shown for a field in FormHelperText component.
hideErrorMessagebooleanPrevent replacing of form HelperText by error message during validation trigger.
formHelperTextPropsFormHelperTextPropsFormHelperTextProps to customise FormHelperText component for a field. Multiple fields can be configured using the ConfigProvider component.