1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-01-10 00:46:15 +01:00

74 lines
2.5 KiB
TypeScript
Raw Normal View History

2022-02-03 15:32:02 +01:00
import React, { ReactNode } from 'react';
import { FieldErrors, FieldPathValue, UseFormRegister, Validate } from 'react-hook-form';
import { FieldValues } from 'react-hook-form/dist/types/fields';
import { FieldPath } from 'react-hook-form/dist/types/path';
2022-02-03 15:32:02 +01:00
type inputType = string|number|readonly string [];
type ruleTypes<TFieldValues> = {
2022-02-03 15:32:02 +01:00
required?: boolean | string,
pattern?: RegExp | {value: RegExp, message: string},
minLenght?: number,
maxLenght?: number,
min?: number,
max?: number,
validate?: Validate<TFieldValues>;
2022-02-03 15:32:02 +01:00
};
interface RHFInputProps<TFieldValues> {
2022-02-03 15:32:02 +01:00
id: string,
register: UseFormRegister<TFieldValues>,
2022-02-03 15:32:02 +01:00
label?: string,
tooltip?: string,
defaultValue?: inputType,
icon?: ReactNode,
addOn?: ReactNode,
addOnClassName?: string,
classes?: string,
rules?: ruleTypes<TFieldValues>,
2022-02-03 15:32:02 +01:00
readOnly?: boolean,
disabled?: boolean,
placeholder?: string,
error?: FieldErrors,
type?: 'text' | 'date' | 'password' | 'url' | 'time' | 'tel' | 'search' | 'number' | 'month' | 'email' | 'datetime-local' | 'week',
step?: number | 'any'
}
/**
* This component is a template for an input component to use within React Hook Form
*/
export const RHFInput = <TFieldValues extends FieldValues>({ id, register, label, tooltip, defaultValue, icon, classes, rules, readOnly, disabled, type, addOn, addOnClassName, placeholder, error, step }: RHFInputProps<TFieldValues>) => {
2022-02-03 15:32:02 +01:00
// Compose classnames from props
const classNames = `
rhf-input ${classes || ''}
${error && error[id] ? 'is-incorrect' : ''}
${rules && rules.required ? 'is-required' : ''}
${readOnly ? 'is-readOnly' : ''}
${disabled ? 'is-disabled' : ''}`;
return (
<label className={classNames}>
{label && <div className='rhf-input-header'>
<p>{label}</p>
{/* TODO: Create tooltip component */}
{tooltip && <span>{tooltip}</span>}
</div>}
<div className='rhf-input-field'>
{icon && <span className="icon">{icon}</span>}
<input id={id}
{...register(id as FieldPath<TFieldValues>, {
2022-02-03 15:32:02 +01:00
...rules,
valueAsNumber: type === 'number',
value: defaultValue as FieldPathValue<TFieldValues, FieldPath<TFieldValues>>
2022-02-03 15:32:02 +01:00
})}
type={type}
step={step}
disabled={disabled}
readOnly={readOnly}
placeholder={placeholder} />
{addOn && <span className={`addon ${addOnClassName || ''}`}>{addOn}</span>}
</div>
{(error && error[id]) && <div className="rhf-input-error">{error[id].message}</div> }
</label>
);
};