Skip to content
Open
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
10 changes: 10 additions & 0 deletions example/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ const App = () => {
editButtonProps={{ style: { marginLeft: '5px', width: 16 } }}
showEditButton
/>
<EditText
name='textbox4'
defaultValue='Double click me to edit my text'
editOnDoubleClick
/>
<EditTextarea placeholder='I am an editable textarea' />
</div>
</div>
Expand Down Expand Up @@ -171,6 +176,11 @@ const App = () => {
(default: false) are props that are passed to the edit button
element. This can include any valid DOM attribute.
</li>
<li>
<b>editOnDoubleClick</b> <sup className='new-badge'>NEW</sup>{' '}
(default: false) enables editing on double click instead of single
click.
</li>
</ul>
<p className='lead mb-0'>EditTextarea props</p>
<ul>
Expand Down
12 changes: 11 additions & 1 deletion src/EditText.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ export default function EditText({
showEditButton = false,
editButtonContent = <EditIcon />,
editButtonProps = {},
inputClassName
inputClassName,
editOnDoubleClick = false
}) {
const inputRef = React.useRef(null);
const [changeEvent, setChangeEvent] = React.useState({});
Expand All @@ -50,6 +51,14 @@ export default function EditText({

const handleClickDisplay = () => {
if (readonly || showEditButton) return;
if (editOnDoubleClick) return;
setEditMode(true);
onEditMode();
};

const handleDoubleClickDisplay = () => {
if (readonly || showEditButton) return;
if (!editOnDoubleClick) return;
setEditMode(true);
onEditMode();
};
Expand Down Expand Up @@ -120,6 +129,7 @@ export default function EditText({
className
)}
onClick={handleClickDisplay}
onDoubleClick={handleDoubleClickDisplay}
style={style}
aria-label='display component'
>
Expand Down
9 changes: 9 additions & 0 deletions src/EditText.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ test('clicking on the component should activate edit mode', async () => {
expect(screen.queryByLabelText(displayComponentLabel)).toBeNull();
expect(screen.queryByLabelText(inputComponentLabel)).toBeTruthy();
});
test('double clicking on the component should activate edit mode if editOnDoubleClick is true', async () => {
render(<EditText editOnDoubleClick />);
const div = await screen.findByLabelText(displayComponentLabel);
expect(div).toBeTruthy();
expect(screen.queryByLabelText(inputComponentLabel)).toBeNull();
await userEvent.dblClick(div);
expect(screen.queryByLabelText(displayComponentLabel)).toBeNull();
expect(screen.queryByLabelText(inputComponentLabel)).toBeTruthy();
});
test('pressing enter key should disable edit mode and trigger onSave', async () => {
const handleSave = jest.fn();
render(<EditText name='mockName' onSave={handleSave} />);
Expand Down
3 changes: 2 additions & 1 deletion src/propTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export const EditTextPropTypes = {
inline: PropTypes.bool,
showEditButton: PropTypes.bool,
editButtonContent: PropTypes.any,
editButtonProps: PropTypes.object
editButtonProps: PropTypes.object,
editOnDoubleClick: PropTypes.bool
};

export const EditTextareaPropTypes = {
Expand Down