How To Enter Text In A Text-input Inside An Atomic Block
I have an atomic block rendered in the following manner:
const blockRendererFn = (block) => {
if (block.getType() === 'atomic') {
return {
component: (props) => (
<AtomicBlockRenderer {...{
...props,
getEditorState: () => editorState,
setEditorState: (editorState) => dispatchSetEditorState(editorState),
editorEl
}}/>
),
editable: true
};
}
return null;
};
const AtomicBlockRenderer = (props) => {
const { contentState, block } = props;
const entity = contentState.getEntity(block.getEntityAt(0));
const type = entity.getType();
switch (type) {
case 'video': return <VideoInput {...{...props, entity}} />;
default: return null;
}
};
import React, { useRef } from 'react';
export const VideoInput = (props) => {
const inputEl = useRef(null);
return (
<div
className="video-input"
>
<input
ref={inputEl}
type="text"
className="form-control"
placeholder="paste youtube or vimeo url"
/>
</div>
);
};
I am not able to focus on the input
element inside VideoInput
. Even if I hook up focus somehow using ref
, the editor crashes as my content-state becomes malformed. How can I set up the input element to receive URLs from users?
Answer
You need to switch the editor to read only mode when the user tries to interact with the input field. You can do that by adding an onFocus
handler to the input field that sets the editor to read only.
To demonstrate that this is what you need, try the following steps:
* Add a video element
* Open the React debugger and flip the readOnly
flag to true
on the DraftEditor
component
* Try to type something in the input field
See https://draftjs.org/docs/api-reference-editor#readonly for further reference.
Related Questions
- → Import statement and Babel
- → should I choose reactjs+f7 or f7+vue.js?
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → .tsx webpack compile fails: Unexpected token <
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → React Native with visual studio 2015 IDE
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
- → How do I determine if a new ReactJS session and/or Browser session has started?
- → Alt @decorators in React-Native
- → How to dynamically add class to parent div of focused input field?