Add character length tracking for address and description fields in employee management forms

This commit is contained in:
Vaibhav Surve 2025-04-18 17:40:47 +05:30
parent 7308bd8d03
commit 4a16e27a1e
4 changed files with 85 additions and 37 deletions

View File

@ -30,6 +30,8 @@ const ManageEmployee = () => {
const [isloading, setLoading] = useState(false); const [isloading, setLoading] = useState(false);
const navigation = useNavigate(); const navigation = useNavigate();
const [currentEmployee, setCurrentEmployee] = useState(); const [currentEmployee, setCurrentEmployee] = useState();
const [currentAddressLength, setCurrentAddressLength] = useState(0);
const [permanentAddressLength, setPermanentAddressLength] = useState(0);
const userSchema = z.object({ const userSchema = z.object({
...(employeeId ? { Id: z.number().optional() } : {}), ...(employeeId ? { Id: z.number().optional() } : {}),
@ -217,6 +219,8 @@ const ManageEmployee = () => {
} }
: {} // Empty object resets the form : {} // Empty object resets the form
); );
setCurrentAddressLength(currentEmployee?.currentAddress?.length || 0);
setPermanentAddressLength(currentEmployee?.permanentAddress?.length || 0);
}, [currentEmployee, reset]); }, [currentEmployee, reset]);
return ( return (
@ -430,12 +434,17 @@ const ManageEmployee = () => {
aria-label="Current Address" aria-label="Current Address"
aria-describedby="basic-icon-default-message2" aria-describedby="basic-icon-default-message2"
{...register("CurrentAddress")} {...register("CurrentAddress")}
onChange={(e) => {
setCurrentAddressLength(e.target.value.length);
// let react-hook-form still handle it
register("CurrentAddress").onChange(e);
}}
></textarea> ></textarea>
<div className="text-end small">
{500 - currentAddressLength} characters remaining
</div>
{errors.CurrentAddress && ( {errors.CurrentAddress && (
<div <div className="danger-text text-start" style={{ fontSize: "12px" }}>
className="danger-text text-start"
style={{ fontSize: "12px" }}
>
{errors.CurrentAddress.message} {errors.CurrentAddress.message}
</div> </div>
)} )}
@ -452,12 +461,16 @@ const ManageEmployee = () => {
aria-label="Permanent Address" aria-label="Permanent Address"
aria-describedby="basic-icon-default-message2" aria-describedby="basic-icon-default-message2"
{...register("PermanentAddress")} {...register("PermanentAddress")}
onChange={(e) => {
setPermanentAddressLength(e.target.value.length);
register("PermanentAddress").onChange(e);
}}
></textarea> ></textarea>
<div className="text-end small">
{500 - permanentAddressLength} characters remaining
</div>
{errors.PermanentAddress && ( {errors.PermanentAddress && (
<div <div className="danger-text text-start" style={{ fontSize: "12px" }}>
className="danger-text text-start"
style={{ fontSize: "12px" }}
>
{errors.PermanentAddress.message} {errors.PermanentAddress.message}
</div> </div>
)} )}

View File

@ -54,18 +54,20 @@ const CreateJobRole = ({onClose}) => {
}; };
const resetForm =()=>{ const resetForm = () => {
reset({ reset({
role:"", role: "",
description:"" description: ""
}) });
setDescriptionLength(0);
} }
useEffect(()=>{ useEffect(()=>{
return ()=>resetForm() return ()=>resetForm()
},[]) },[])
const [descriptionLength, setDescriptionLength] = useState(0);
const maxDescriptionLength = 255;
return (<> return (<>
<form className="row g-2" onSubmit={handleSubmit(onSubmit)}> <form className="row g-2" onSubmit={handleSubmit(onSubmit)}>
@ -79,11 +81,18 @@ const CreateJobRole = ({onClose}) => {
</div> </div>
<div className="col-12 col-md-12"> <div className="col-12 col-md-12">
<label className="form-label" htmlFor="description">Description</label> <label className="form-label" htmlFor="description">Description</label>
<textarea <textarea
rows="3" rows="3"
{...register("description")} {...register("description")}
className={`form-control ${errors.description ? 'is-invalids' : ''}`} className={`form-control ${errors.description ? 'is-invalids' : ''}`}
></textarea> onChange={(e) => {
setDescriptionLength(e.target.value.length);
register("description").onChange(e);
}}
></textarea>
<div className="text-end small text-muted">
{maxDescriptionLength - descriptionLength} characters left
</div>
{errors.description && ( {errors.description && (
<p className="text-danger">{errors.description.message}</p> <p className="text-danger">{errors.description.message}</p>
)} )}

View File

@ -73,6 +73,11 @@ const onSubmit = (values) => {
}; };
const [descriptionLength, setDescriptionLength] = useState(0);
const maxDescriptionLength = 255;
useEffect(() => {
setDescriptionLength(0);
}, []);
return ( return (
@ -91,11 +96,20 @@ const onSubmit = (values) => {
<div className="col-12 col-md-12"> <div className="col-12 col-md-12">
<label className="form-label" htmlFor="description">Description</label> <label className="form-label" htmlFor="description">Description</label>
<textarea <textarea
rows="3" rows="3"
{...register("description")} {...register("description")}
className={`form-control ${errors.description ? 'is-invalids' : ''}`} className={`form-control ${errors.description ? 'is-invalids' : ''}`}
></textarea> onChange={(e) => {
setDescriptionLength(e.target.value.length);
// Also update react-hook-form's value
register("description").onChange(e);
}}
></textarea>
<div className="text-end small text-muted">
{maxDescriptionLength - descriptionLength} characters left
</div>
{errors.description && ( {errors.description && (
<p className="text-danger">{errors.description.message}</p> <p className="text-danger">{errors.description.message}</p>
)} )}

View File

@ -121,13 +121,18 @@ const EditMaster=({master,onClose})=> {
}; };
useEffect(()=>{ useEffect(() => {
reset({ reset({
role: master?.item?.role, role: master?.item?.role,
description: master?.item?.description, description: master?.item?.description,
permissions: initialPermissions, permissions: initialPermissions,
}) });
},[master,reset]) setDescriptionLength(master?.item?.description?.length || 0);
}, [master, reset]);
const [descriptionLength, setDescriptionLength] = useState(master?.item?.description?.length || 0);
const maxDescriptionLength = 255;
return ( return (
@ -144,14 +149,21 @@ const EditMaster=({master,onClose})=> {
<div className="col-12 col-md-12"> <div className="col-12 col-md-12">
<label className="form-label" htmlFor="description">Description</label> <label className="form-label" htmlFor="description">Description</label>
<textarea <textarea
rows="3" rows="3"
{...register("description")} {...register("description")}
className={`form-control ${errors.description ? 'is-invalids' : ''}`} className={`form-control ${errors.description ? 'is-invalids' : ''}`}
></textarea> onChange={(e) => {
{errors.description && ( setDescriptionLength(e.target.value.length);
<p className="text-danger">{errors.description.message}</p> register("description").onChange(e);
)} }}
></textarea>
<div className="text-end small text-muted">
{maxDescriptionLength - descriptionLength} characters left
</div>
{errors.description && (
<p className="text-danger">{errors.description.message}</p>
)}
</div> </div>
<div className="col-12 col-md-12 mx-2s " > <div className="col-12 col-md-12 mx-2s " >