The EditInPlace
component returns a div that allows users to modify its text directly within the browser.
Import the component:
import { EditInPlace } from '@newrelic/nr-labs-components';
Use the component in your code:
<EditInPlace
value="Initial Text"
setValue={saveHandler}
/>
value
(string) - the initial text to be displayed and editedsetValue
(function) - a callback function that will be called with the new edited text when the user saves their changesplaceholder
(string) - the text to display when the component is emptydisabled
(boolean) - set to true to disable editing
You can set a ref
on the component to access available methods. Currently, the only available method is:
focus()
- sets the focus on the component
The component inherits the styles of the container it's placed in.
import React, { useEffect, useRef, useState } from 'react';
import { EditInPlace } from '@newrelic/nr-labs-components';
function MyComponent() {
const [value, setValue] = useState('Hello, World!');
const ref = useRef();
useEffect(() => console.log('value:', value), [value]);
return (
<div>
<h1>Edit In Place</h1>
<div style={{
fontSize: '36px',
lineHeight: 1.2,
color: '#1c7baa',
padding: '8px'
}}>
<EditInPlace
value={value}
setValue={setValue}
ref={ref}
placeholder="Click to edit"
/>
</div>
<button onClick={() => ref.current.focus()}>
Set focus
</button>
</div>
);
}