Screen.Recording.2025-01-20.at.15.58.10.mov
A lightweight, easy-to-use React component that makes any text editable with a click!
- π― Simple and intuitive API
- π¨ Fully customizable styling
- π Controlled component
- π TypeScript support
- π¨ Custom icons support
- π Label support
- π€ Multiple input types
npm install @nobrainers/react-click-edit
The component requires its base CSS file to be imported. Add the following import to your application:
import "@nobrainers/react-click-edit/dist/style.css";
import { InputClickEdit } from "@nobrainers/react-click-edit";
function App() {
const [name, setName] = useState("John Doe");
return <InputClickEdit value={name} onChange={setName} />;
}
Prop | Type | Required | Default | Description |
---|---|---|---|---|
value | string | Yes* | - | Controlled text value to display and edit |
defaultValue | string | No | - | Initial uncontrolled value |
type | string | No | "text" | HTML input type attribute |
onChange | ChangeEventHandler<HTMLInputElement> |
Yes* | - | HTML input onChange handler |
isEditing | boolean | No | false | Control the editing state |
label | string | No | "" | Label for the input field |
className | string | No | "" | Container class name |
editButtonLabel | React.ReactNode | No | "Edit" | Custom edit button label |
saveButtonLabel | React.ReactNode | No | "Save" | Custom save button label |
showIcons | boolean | No | false | Toggle button icons visibility |
iconsOnly | boolean | No | false | Show only icons without text labels |
editIcon | React.ElementType | No | LuPencil | Custom edit icon component |
saveIcon | React.ElementType | No | LuCheck | Custom save icon component |
iconPosition | "left" | "right" | No | "left" | Position of icons in buttons |
onEditButtonClick | () => void | No | () => {} | Callback when edit button is clicked |
onSaveButtonClick | () => void | No | () => {} | Callback when save button is clicked |
*Either value
+ onChange
(controlled) or defaultValue
(uncontrolled) must be provided.
function BasicExample() {
const [name, setName] = useState("John Doe");
return <InputClickEdit value={name} onChange={setName} />;
}
<InputClickEdit
label="Age"
type="number"
value="25"
onChange={(value) => console.log(value)}
/>
<InputClickEdit
value="Click me to edit"
showIcons
iconPosition="right"
className="container"
inputClassName="custom-input"
saveButtonClassName="save-btn"
editButtonClassName="edit-btn"
editWrapperClassName="edit-wrapper"
/>
import { FiEdit } from "react-icons/fi";
import { FiSave } from "react-icons/fi";
<InputClickEdit
value="Custom everything"
showIcons
editIcon={FiEdit}
saveIcon={FiSave}
editButtonLabel="Modify"
saveButtonLabel="Update"
/>;
function ControlledExample() {
const [isEditing, setIsEditing] = useState<boolean>(false);
const [value, setValue] = useState<string>("Control me");
return (
<InputClickEdit
value={value}
isEditing={isEditing}
onEditButtonClick={() => setIsEditing(true)}
onSaveButtonClick={() => setIsEditing(false)}
onChange={setValue}
/>
);
}
<InputClickEdit
value="Icons only"
showIcons
iconsOnly
editIcon={FiEdit}
saveIcon={FiSave}
/>
import { useForm, Controller } from "react-hook-form";
import { InputClickEdit } from "@nobrainers/react-click-edit";
function FormExample() {
const { control, handleSubmit } = useForm();
const onSubmit = (data) => console.log(data);
return (
<form onSubmit={handleSubmit(onSubmit)}>
<Controller
name="editableText"
control={control}
defaultValue="Edit me"
render={({ field }) => (
<InputClickEdit {...field} onChange={field.onChange} />
)}
/>
<button type="submit">Submit</button>
</form>
);
}
import { useForm } from "react-hook-form";
import { InputClickEdit } from "@nobrainers/react-click-edit";
function RegisterExample() {
const { register, handleSubmit } = useForm();
const onSubmit = (data) => console.log(data);
return (
<form onSubmit={handleSubmit(onSubmit)}>
<InputClickEdit {...register("editableText")} />
<button type="submit">Submit</button>
</form>
);
}
The component comes with minimal default styling through its base CSS file. You can override these styles or add additional styling using CSS classes. All main elements accept custom class names through props.
Import the default styles in your application:
import "@nobrainers/react-click-edit/dist/style.css";
Example with CSS modules:
import "@nobrainers/react-click-edit/dist/style.css"; // Base styles
import styles from "./styles.module.css"; // Your custom styles
<InputClickEdit
className={styles.wrapper}
inputClassName={styles.input}
editButtonClassName={styles.editButton}
saveButtonClassName={styles.saveButton}
editWrapperClassName={styles.editingWrapper}
/>;
MIT