UNPKG

@commercetools-uikit/radio-field

Version:

A controlled radio input component with validation states and a label.

143 lines (142 loc) 5.16 kB
import { Component, type ChangeEventHandler, type FocusEventHandler, type ReactElement, type ReactNode, type MouseEvent, type KeyboardEvent } from 'react'; import { type TStackProps } from '@commercetools-uikit/spacings-stack'; import { type TIconProps } from '@commercetools-uikit/field-label'; type TErrorRenderer = (key: string, error?: boolean) => ReactNode; type TFieldErrors = Record<string, boolean>; type TFieldWarnings = Record<string, boolean>; type TCustomFormErrors<Values> = { [K in keyof Values]?: TFieldErrors; }; export type TRadioFieldProps = { /** * Used as HTML id property. An id is auto-generated when it is not specified. */ id?: string; /** * Horizontal size limit of the input fields. */ horizontalConstraint?: 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 'scale' | 'auto'; /** * A map of errors. Error messages for known errors are rendered automatically. Unknown errors will be forwarded to `renderError`. */ errors?: TFieldErrors; /** * A map of warnings. Warning messages for known warnings are rendered automatically. * <br/> * Unknown warnings will be forwarded to renderWarning. */ warnings?: TFieldWarnings; /** * Called with custom errors. * <br/> * This function can return a message which will be wrapped in an ErrorMessage. It can also return null to show no error. */ renderError?: TErrorRenderer; /** * Called with custom warnings, as renderWarning(key, warning). This function can return a message which will be wrapped in a WarningMessage. * <br /> * It can also return null to show no warning. */ renderWarning?: (key: string, warning?: boolean) => ReactNode; /** * Indicates if the value is required. Shows an the "required asterisk" if so. */ isRequired?: boolean; /** * Indicates whether the field was touched. Errors will only be shown when the field was touched. */ touched?: boolean; /** * Used as HTML name of the input component. */ name?: string; /** * Value of the input component. */ value: string | boolean; /** * Called with an event containing the new value. Required when input is not read only. Parent should pass it back as value. */ onChange?: ChangeEventHandler<HTMLInputElement>; /** * Called when input is blurred */ onBlur?: FocusEventHandler<HTMLLabelElement>; /** * Called when input is focused */ onFocus?: FocusEventHandler<HTMLLabelElement>; /** * Indicates that the input cannot be modified (e.g not authorized, or changes currently saving). */ isDisabled?: boolean; /** * Indicates that the field is displaying read-only content */ isReadOnly?: boolean; /** * Rendering direction of the radio `RadioInput.Option`s */ direction?: 'stack' | 'inline'; /** * Passes props of the `Spacings.Stack` or `Spacings.Inline`, depending on the chosen direction */ directionProps?: Partial<TStackProps>; /** * At least one `RadioInput.Option` component or another node (mixed children are allowed) */ children: ReactNode; /** * Title of the label */ title: string | ReactNode; /** * Hint for the label. Provides a supplementary but important information regarding the behaviour of the input (e.g warn about uniqueness of a field, when it can only be set once), whereas description can describe it in more depth. * <br /> * Can also receive a `hintIcon`. */ hint?: string | ReactNode; /** * Provides a description for the title. */ description?: string | ReactNode; /** * Function called when info button is pressed. * <br /> * Info button will only be visible when this prop is passed. */ onInfoButtonClick?: (event: MouseEvent<HTMLButtonElement> | KeyboardEvent<HTMLButtonElement>) => void; /** * Icon to be displayed beside the hint text. * <br /> * Will only get rendered when `hint` is passed as well. */ hintIcon?: ReactElement<TIconProps>; /** * Badge to be displayed beside the label. * <br /> * Might be used to display additional information about the content of the field (E.g verified email) */ badge?: ReactNode; }; type TRadioFieldStates = Pick<TRadioFieldProps, 'id'>; declare class RadioField extends Component<TRadioFieldProps, TRadioFieldStates> { static displayName: string; static defaultProps: { horizontalConstraint: string; }; state: { id: string | undefined; }; static getDerivedStateFromProps: (props: TRadioFieldProps, state: TRadioFieldStates) => { id: string; }; /** * Use this function to convert the Formik `errors` object type to * our custom field errors type. * This is primarly useful when using TypeScript. */ static toFieldErrors<FormValues>(errors: unknown): TCustomFormErrors<FormValues>; render(): import("@emotion/react/jsx-runtime").JSX.Element; } export default RadioField;