React-hook-form: how to add logic to the onChange property in a custom input

I am working in a project using react with typescript, and I’m using react-hook-form and the form component that comes with shadcn/ui: https://ui.shadcn.com/docs/components/input#form
For my case, I created a custom Input component which has a button inside of it (with a pen icon), which will toggle on and off the **readonly **property of the input, but i want to add another functionality:

  • When the user clicks on the pen button to modify the value of the input, I want to replace that button with a submit button once the initial value of that input changes, and only when he clicks on the submit button and the form submits can the input return to being **readonly **again and so on for every input.

The problem is that the onChange property doesn’t work on the input to get the value or just to set a state indicating that the value has been changed to replace the buttons, and when I tried to isolate the input by using react-hook-form’s hooks such as useFormContext to get the **control ** prop and then pass it as well as the name of the input to the useWatch or useFormState, it triggers countless re-renders.
If anyone has any idea or suggestion please help me.
Here is the code of my component if anyone wants to modify it:

import * as React from "react";
import { Button } from "@/components/ui/button";
import { Input, type InputProps } from "@/components/ui/input";
import { cn } from "@/lib/utils";
import { Pencil, PencilOff } from "lucide-react";

interface EditableInputProps extends InputProps {
     editPermission?: boolean;
}

const EditableInput = React.forwardRef<HTMLInputElement, EditableInputProps>(
({ className, editPermission = true, ...props }, ref) => {
    const [canEdit, setCanEdit] = React.useState<boolean>(false);
    const disabled = props.disabled || !editPermission;

    const handleEdit = () => {
        if (!disabled) {
            setCanEdit((prev) => !prev);
        }
    };

    return (
        <div className="relative">
            <Input
                ref={ref}
                type="text"
                readOnly={!canEdit ? true : false}
                className={cn("pr-10", className)}
                {...props}
            />
            <Button
                type="button"
                variant="ghost"
                size="sm"
                className="absolute right-0 top-0 h-full px-3 py-2 hover:bg-transparent"
                disabled={disabled}
                onClick={() => handleEdit()}
            >
                {canEdit && !disabled ? (
                    <Pencil className="h-4 w-4" aria-hidden="true" />
                ) : (
                    <PencilOff className="h-4 w-4" aria-hidden="true" />
                )}
                <span className="sr-only">
                    {canEdit ? "Can edit input" : "Cannot edit input"}
                </span>
            </Button>
        </div>
    );
}
);

EditableInput.displayName = "EditableInput";
export { EditableInput };

// usage in my form
<FormField
control={form.control}
name="order_total"
render={({ field }) => (
    <FormItem>
        <FormLabel>
            Order Total
        </FormLabel>
        <FormControl>
            <EditableInput
                {...field}
                placeholder="Order Total"
            />
        </FormControl>
        <FormMessage />
    </FormItem>
)}

/>

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

React-hook-form: how to add logic to the onChange property in a custom input

I am working in a project using react with typescript, and I’m using react-hook-form and the form component that comes with shadcn/ui: https://ui.shadcn.com/docs/components/input#form
For my case, I created a custom Input component which has a button inside of it (with a pen icon), which will toggle on and off the **readonly **property of the input, but i want to add another functionality:

  • When the user clicks on the pen button to modify the value of the input, I want to replace that button with a submit button once the initial value of that input changes, and only when he clicks on the submit button and the form submits can the input return to being **readonly **again and so on for every input.

The problem is that the onChange property doesn’t work on the input to get the value or just to set a state indicating that the value has been changed to replace the buttons, and when I tried to isolate the input by using react-hook-form’s hooks such as useFormContext to get the **control ** prop and then pass it as well as the name of the input to the useWatch or useFormState, it triggers countless re-renders.
If anyone has any idea or suggestion please help me.
Here is the code of my component if anyone wants to modify it:

import * as React from "react";
import { Button } from "@/components/ui/button";
import { Input, type InputProps } from "@/components/ui/input";
import { cn } from "@/lib/utils";
import { Pencil, PencilOff } from "lucide-react";

interface EditableInputProps extends InputProps {
     editPermission?: boolean;
}

const EditableInput = React.forwardRef<HTMLInputElement, EditableInputProps>(
({ className, editPermission = true, ...props }, ref) => {
    const [canEdit, setCanEdit] = React.useState<boolean>(false);
    const disabled = props.disabled || !editPermission;

    const handleEdit = () => {
        if (!disabled) {
            setCanEdit((prev) => !prev);
        }
    };

    return (
        <div className="relative">
            <Input
                ref={ref}
                type="text"
                readOnly={!canEdit ? true : false}
                className={cn("pr-10", className)}
                {...props}
            />
            <Button
                type="button"
                variant="ghost"
                size="sm"
                className="absolute right-0 top-0 h-full px-3 py-2 hover:bg-transparent"
                disabled={disabled}
                onClick={() => handleEdit()}
            >
                {canEdit && !disabled ? (
                    <Pencil className="h-4 w-4" aria-hidden="true" />
                ) : (
                    <PencilOff className="h-4 w-4" aria-hidden="true" />
                )}
                <span className="sr-only">
                    {canEdit ? "Can edit input" : "Cannot edit input"}
                </span>
            </Button>
        </div>
    );
}
);

EditableInput.displayName = "EditableInput";
export { EditableInput };

// usage in my form
<FormField
control={form.control}
name="order_total"
render={({ field }) => (
    <FormItem>
        <FormLabel>
            Order Total
        </FormLabel>
        <FormControl>
            <EditableInput
                {...field}
                placeholder="Order Total"
            />
        </FormControl>
        <FormMessage />
    </FormItem>
)}

/>

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

React-hook-form: how to add logic to the onChange property in a custom input

I am working in a project using react with typescript, and I’m using react-hook-form and the form component that comes with shadcn/ui: https://ui.shadcn.com/docs/components/input#form
For my case, I created a custom Input component which has a button inside of it (with a pen icon), which will toggle on and off the **readonly **property of the input, but i want to add another functionality:

  • When the user clicks on the pen button to modify the value of the input, I want to replace that button with a submit button once the initial value of that input changes, and only when he clicks on the submit button and the form submits can the input return to being **readonly **again and so on for every input.

The problem is that the onChange property doesn’t work on the input to get the value or just to set a state indicating that the value has been changed to replace the buttons, and when I tried to isolate the input by using react-hook-form’s hooks such as useFormContext to get the **control ** prop and then pass it as well as the name of the input to the useWatch or useFormState, it triggers countless re-renders.
If anyone has any idea or suggestion please help me.
Here is the code of my component if anyone wants to modify it:

import * as React from "react";
import { Button } from "@/components/ui/button";
import { Input, type InputProps } from "@/components/ui/input";
import { cn } from "@/lib/utils";
import { Pencil, PencilOff } from "lucide-react";

interface EditableInputProps extends InputProps {
     editPermission?: boolean;
}

const EditableInput = React.forwardRef<HTMLInputElement, EditableInputProps>(
({ className, editPermission = true, ...props }, ref) => {
    const [canEdit, setCanEdit] = React.useState<boolean>(false);
    const disabled = props.disabled || !editPermission;

    const handleEdit = () => {
        if (!disabled) {
            setCanEdit((prev) => !prev);
        }
    };

    return (
        <div className="relative">
            <Input
                ref={ref}
                type="text"
                readOnly={!canEdit ? true : false}
                className={cn("pr-10", className)}
                {...props}
            />
            <Button
                type="button"
                variant="ghost"
                size="sm"
                className="absolute right-0 top-0 h-full px-3 py-2 hover:bg-transparent"
                disabled={disabled}
                onClick={() => handleEdit()}
            >
                {canEdit && !disabled ? (
                    <Pencil className="h-4 w-4" aria-hidden="true" />
                ) : (
                    <PencilOff className="h-4 w-4" aria-hidden="true" />
                )}
                <span className="sr-only">
                    {canEdit ? "Can edit input" : "Cannot edit input"}
                </span>
            </Button>
        </div>
    );
}
);

EditableInput.displayName = "EditableInput";
export { EditableInput };

// usage in my form
<FormField
control={form.control}
name="order_total"
render={({ field }) => (
    <FormItem>
        <FormLabel>
            Order Total
        </FormLabel>
        <FormControl>
            <EditableInput
                {...field}
                placeholder="Order Total"
            />
        </FormControl>
        <FormMessage />
    </FormItem>
)}

/>

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật