Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 32 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,27 @@ function App() {

## 🔧 Props

| Prop | Type | Default | Description |
| -------------------- | ----------------------- | -------------- | ------------------------------------ |
| value | string | "" | Text to display and edit |
| isEditing | boolean | false | Initial editing state |
| inputType | string | "text" | Type of input field |
| label | string | "" | Label for the input |
| className | string | "" | Container class name |
| inputClassName | string | "" | Input field class name |
| editButtonClassName | string | "" | Edit button class name |
| saveButtonClassName | string | "" | Save button class name |
| editWrapperClassName | string | "" | Edit mode wrapper class name |
| saveButtonLabel | React.ReactNode | "Save" | Custom save button label |
| editButtonLabel | React.ReactNode | "Edit" | Custom edit button label |
| showIcons | boolean | false | Show icons in buttons |
| editIcon | React.ReactNode | `<LuPencil />` | Custom edit icon |
| saveIcon | React.ReactNode | `<LuCheck />` | Custom save icon |
| iconPosition | "left" \| "right" | "left" | Position of icons in buttons |
| onEditButtonClick | () => void | () => {} | Callback when edit button is clicked |
| onInputChange | (value: string) => void | () => {} | Callback when input value changes |
| onSaveButtonClick | () => void | () => {} | Callback when save button is clicked |
| Prop | Type | Default | Description |
| -------------------- | ----------------------- | -------------- | ------------------------------------- |
| value | string | "" | Text to display and edit |
| isEditing | boolean | false | Initial editing state |
| inputType | string | "text" | Type of input field |
| label | string | "" | Label for the input |
| className | string | "" | Container class name |
| inputClassName | string | "" | Input field class name |
| editButtonClassName | string | "" | Edit button class name |
| saveButtonClassName | string | "" | Save button class name |
| editWrapperClassName | string | "" | Edit mode wrapper class name |
| saveButtonLabel | React.ReactNode | "Save" | Custom save button label |
| editButtonLabel | React.ReactNode | "Edit" | Custom edit button label |
| showIcons | boolean | false | Show icons in buttons |
| editIcon | React.ReactNode | `<LuPencil />` | Custom edit icon |
| saveIcon | React.ReactNode | `<LuCheck />` | Custom save icon |
| iconPosition | "left" \| "right" | "left" | Position of icons in buttons |
| onEditButtonClick | () => void | () => {} | Callback when edit button is clicked |
| onInputChange | (value: string) => void | () => {} | Callback when input value changes |
| onSaveButtonClick | () => void | () => {} | Callback when save button is clicked |
| iconsOnly | boolean | false | Show only icons without button labels |

## 💡 Examples

Expand Down Expand Up @@ -81,6 +82,17 @@ function App() {
/>
```

### Icons Only (No Text Labels)

```tsx
<InputClickEdit
value="Icons only buttons"
iconsOnly
editIcon={<span>✎</span>}
saveIcon={<span>✓</span>}
/>
```

## 📄 License

MIT
2 changes: 2 additions & 0 deletions playground/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ function App() {
<h1>Vite + React</h1>
<div className="card">
<InputClickEdit onInputChange={handleChange} value={value} showIcons />
<br />
<InputClickEdit onInputChange={handleChange} value={value} justIcons />
<p>
Edit <code>src/App.tsx</code> and save to test HMR
</p>
Expand Down
12 changes: 8 additions & 4 deletions src/InputClickEdit/InputClickEdit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type InputClickEditProps = {
editIcon?: React.ReactNode;
saveIcon?: React.ReactNode;
iconPosition?: "left" | "right";
iconsOnly?: boolean;
onEditButtonClick?: () => void;
onInputChange?: (value: string) => void;
onSaveButtonClick?: () => void;
Expand All @@ -41,6 +42,7 @@ const InputClickEdit = ({
showIcons = false,
saveIcon = <LuCheck />,
editIcon = <LuPencil />,
iconsOnly = false,
iconPosition = "left",
onEditButtonClick = () => {},
onInputChange = () => {},
Expand Down Expand Up @@ -87,9 +89,10 @@ const InputClickEdit = ({
<button
className={cn(buttonBaseClassName, saveButtonClassName)}
onClick={handleSave}
aria-label={iconsOnly ? saveButtonLabel?.toString() : undefined}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix potential aria-label type issues.

The current aria-label implementation has potential issues:

  1. toString() on ReactNode might not be safe as ReactNode can be null/undefined
  2. Explicitly setting undefined is unnecessary as omitting the prop achieves the same result

Apply this fix:

-            aria-label={iconsOnly ? saveButtonLabel?.toString() : undefined}
+            aria-label={iconsOnly && typeof saveButtonLabel === 'string' ? saveButtonLabel : undefined}

-            aria-label={iconsOnly ? editButtonLabel?.toString() : undefined}
+            aria-label={iconsOnly && typeof editButtonLabel === 'string' ? editButtonLabel : undefined}

Also applies to: 104-104

>
{showIcons && saveIcon}
{saveButtonLabel}
{(showIcons || iconsOnly) && saveIcon}
{!iconsOnly && saveButtonLabel}
</button>
</div>
) : (
Expand All @@ -98,9 +101,10 @@ const InputClickEdit = ({
<button
className={cn(buttonBaseClassName, editButtonClassName)}
onClick={onEditClick}
aria-label={iconsOnly ? editButtonLabel?.toString() : undefined}
>
{showIcons && editIcon}
{editButtonLabel}
{(showIcons || iconsOnly) && editIcon}
{!iconsOnly && editButtonLabel}
</button>
</div>
)}
Expand Down
Loading