How to build an inline edit component in React
Inline editing allows users to edit content without navigating to a separate edit screen. In this tutorial, we’ll be building an accessible inline edit component in React.
Here’s the final product:
We’ll also learn how to write some unit tests with React Testing Library. Let’s get started!
This tutorial assumes a basic understanding of React, including hooks.
If you want to jump straight to the full code, check out the React inline edit example on Codepen.
Inline editing and accessibility
When creating any React component, keep accessibility in mind. For example, your component should:
- Work with only a keyboard
- Use the correct HTML elements and other attributes to provide the most context to users
One way to approach writing an inline edit component is to have two separate components. One for a “view mode” and one for a “edit mode”:
When a user clicks on the view mode component, it will disappear and the edit mode will appear.
The second approach (and the one we will be implementing below) is to always use an input element. We can use CSS to make it look as though it has begun editing when a user focuses on it.
By always using an input element, we get behaviours like tabbing and focusing for free. It also makes more explicit what the purpose of the component is.
Create your inline edit component with an input
Let’s get started by creating a React component that uses the HTML input
tag:
The aria-label
tells screen reader users the purpose of the input. For instance, if it was the name of a list, you could use “List name”.
Then, let’s render our new InlineEdit
component, and pass in a value
and setValue
props:
In a real-life app, the setValue
function would make an endpoint call to store the value in a database somewhere. For this tutorial though, we’ll store the value in a useState
hook.
Add CSS to make it “click to edit”
We’ll then add some CSS to remove the input styling. This makes it look as though the user needs to click or focus on the input to start editing.
We’ll also add some styling to show that the component is editable when a user hovers over it:
Allow users to save when they press Enter or Escape
If a user clicks away from the input, it will lose focus and return to “view” mode. To keep things keyboard-friendly, we’ll want the escape and enter keys to achieve the same affect.
Only save on exit
Currently we call the setValue
prop on each key press. In a real-life situation, where setValue
was making an endpoint call, it would be making an endpoint call per keypress.
We want to prevent this from happening until a user exits the input.
Let’s create a local state variable called editingValue
. This is where we’ll store the value of the input when it is in a “editing” phase.
A user exiting the input will call the onBlur
handler. So we can use this to call setValue
.
Adding validation on empty strings
Finally, you don’t want users to be able to save an empty string or spaces as a value. In that case, we’ll cancel the edit and use the original value.
You’ll now have a complete single-line inline edit component. Here’s the full code:
Creating a multiline inline edit
If you want your inline edit component to be multiline, we can use the textarea
element instead:
The one difference with textarea is that you pass in a rows
value. This specifies the height of your textarea.
By default, textareas aren’t dynamic. Luckily, over on StackOverflow I found a solution to this problem.
If you add the following CSS to your text area:
And then pass in an onInput
handler, you’ll be able to achieve a “dynamic” look.
Note you may need to fiddle around with some of the values in the onInput
depending on the height and font size of your text area.
The one other thing you’ll need to add is a focus ring - the blue outline around a focused element. We can do this with some CSS:
And you’re done! Here’s the full code for a multiline inline edit:
Ensure your component’s functionality with unit tests
Before we finish, let’s write a couple of unit tests to ensure the functionality of our component. We’ll be using React Testing Library:
We can ensure that pressing enter causes the input to lose focus:
If you haven’t used React Testing Library before, let’s break this test down:
- The
render
function will render your component into a container. You can access it using thescreen
variable - We search for the input component via its aria role,
"textbox"
- We can use the
userEvent.type()
function to simulate a user typing. If you want to type special keys like space or enter, you can do it with curly braces around it (e.g{space}
and{enter}
)
Similarly, we can write two more unit tests:
And finally, we can use a cool library called jest-axe. You can use it to assert that your component doesn’t have any accessibility violations:
If we had forgotten to include an aria-label
, for instance, then this test would have failed.
And that’s it! Now you should be able to create inline-editable components for your React app, complete with unit tests.