Skip to main content
Version: 2.0

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/misc/color-picker';

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

  • #00ffff
  • rgb(0 255 255)
  • hsl(180 100% 100% / 1)
  • cyan

If the field value is uninitialized, the default color Black(#000000) will be selected, which can be configured using the defaultColor prop.

<RHFColorPicker
fieldName='favouriteColor'
control={control}
value={getValues('favouriteColor')}
errorMessage={errors?.favouriteColor?.message}
/>

The returned color value from onValueChange prop is an object with the equivalent hex, rgb and hsv values.

{
hex: '#00ffff',
hsv: {
a: 1,
h: 180,
s: 100,
v: 100
},
rgb: {
a: 1,
b: 255,
g: 255,
r: 0
}
}

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. This is a required prop for all components.
controlUseFormControlThe control option yielded on calling the useForm hook.
registerOptionsRegisterOptionsRegister options for validation if using react-hook-form without any validation libraries like yup or Joi.
valuestringDefines the selected color in the RHFColorPicker component. Use getValues('fieldName') to set the value dynamically, else defaultColor is selected as the current color.
valueKeyhex or rgb or hsvReturns the hex, rgba or hsva string for the selected color. Returns hex code by default.
defaultColorstringThe default color to select when the field is not initialized. The default value Black(#000000) can be overridden by providing a valid color string, hex, rgb or hsv value.
excludeAlphabooleanSpecifies whether to exclude alpha from the color string when the valueKey is rgb or hsv. Alpha will only be excluded if its value is 1 or is undefined in the input color.
requiredbooleanIndicates that the field is mandatory by adding an asterisk symbol (*) to the formLabel. This visual cue helps users quickly identify required fields in the form.
onValueChange(color: IColor) => voidCallback function to get the selected color in hex, rgb or hsv format. The color format being set in field value can be configured by the valueKey prop.
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".
showLabelAboveFormFieldbooleanRenders the form label above the field by default. Set this prop to false to hide the label.
formLabelPropsFormLabelPropsFormLabelProps to customise FormLabel component for a field. Multiple fields can be configured using the ConfigProvider component.
helperTextReactNodeThe content to display within the FormHelperText component below the field. If the field validation fails, this content will be overridden by the corresponding error message.
errorMessageReactNodeError message to be shown for a field in FormHelperText component.
hideErrorMessagebooleanA flag to prevent replacement of helper text of a field by the error message when the validation is triggered.
formHelperTextPropsFormHelperTextPropsFormHelperTextProps to customise FormHelperText component for a field. Multiple fields can be configured using the ConfigProvider component.