From d7a6948619a1834c12b7f6c9188e41ad3f17a900 Mon Sep 17 00:00:00 2001 From: Tulsi Prasad Date: Tue, 19 Oct 2021 09:53:39 +0530 Subject: [PATCH] Add default value to RichTextEditor component (#1115) - Update DraftEditor component only when default value is changed --- frontend/src/Editor/Components/DraftEditor.jsx | 16 ++++++++++++++-- .../src/Editor/Components/RichTextEditor.jsx | 9 ++++++++- frontend/src/Editor/Components/components.js | 4 +++- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/frontend/src/Editor/Components/DraftEditor.jsx b/frontend/src/Editor/Components/DraftEditor.jsx index d18254b07c..a54498f5c9 100644 --- a/frontend/src/Editor/Components/DraftEditor.jsx +++ b/frontend/src/Editor/Components/DraftEditor.jsx @@ -1,6 +1,6 @@ /* eslint-disable react/no-string-refs */ import React from 'react'; -import { Editor, EditorState, RichUtils, getDefaultKeyBinding } from 'draft-js'; +import { Editor, EditorState, RichUtils, getDefaultKeyBinding, ContentState } from 'draft-js'; import 'draft-js/dist/Draft.css'; import { stateToHTML } from 'draft-js-export-html'; @@ -127,7 +127,7 @@ const InlineStyleControls = (props) => { class DraftEditor extends React.Component { constructor(props) { super(props); - this.state = { editorState: EditorState.createEmpty() }; + this.state = { editorState: EditorState.createWithContent(ContentState.createFromText(this.props.defaultValue)) }; this.focus = () => this.refs.editor.focus(); this.onChange = (editorState) => { @@ -142,6 +142,18 @@ class DraftEditor extends React.Component { this.toggleInlineStyle = this._toggleInlineStyle.bind(this); } + componentDidUpdate(prevProps) { + if (prevProps.defaultValue !== this.props.defaultValue) { + const newContentState = ContentState.createFromText(this.props.defaultValue); + const newEditorState = EditorState.createWithContent(newContentState); + const newEditorStateWithFocus = EditorState.moveFocusToEnd(newEditorState); + const html = stateToHTML(newContentState); + + this.props.handleChange(html); + this.setState({ editorState: newEditorStateWithFocus }); + } + } + _handleKeyCommand(command, editorState) { const newState = RichUtils.handleKeyCommand(editorState, command); if (newState) { diff --git a/frontend/src/Editor/Components/RichTextEditor.jsx b/frontend/src/Editor/Components/RichTextEditor.jsx index 8193fef0ad..b205ac2f6f 100644 --- a/frontend/src/Editor/Components/RichTextEditor.jsx +++ b/frontend/src/Editor/Components/RichTextEditor.jsx @@ -15,6 +15,7 @@ export const RichTextEditor = function RichTextEditor({ const placeholder = component.definition.properties.placeholder.value; const widgetVisibility = component.definition.styles?.visibility?.value ?? true; const disabledState = component.definition.styles?.disabledState?.value ?? false; + const defaultValue = component.definition.properties?.defaultValue?.value ?? ''; const parsedDisabledState = typeof disabledState !== 'boolean' ? resolveWidgetFieldValue(disabledState, currentState) : disabledState; @@ -40,7 +41,13 @@ export const RichTextEditor = function RichTextEditor({ onComponentClick(id, component); }} > - + ); }; diff --git a/frontend/src/Editor/Components/components.js b/frontend/src/Editor/Components/components.js index 57a87bc3a1..8f9aac71bd 100644 --- a/frontend/src/Editor/Components/components.js +++ b/frontend/src/Editor/Components/components.js @@ -852,6 +852,7 @@ export const componentTypes = [ }, properties: { placeholder: { type: 'code', displayName: 'Placeholder' }, + defaultValue: { type: 'code', displayName: 'Default Value' }, }, events: {}, styles: { @@ -859,7 +860,7 @@ export const componentTypes = [ disabledState: { type: 'code', displayName: 'Disable' }, }, exposedVariables: { - value: {}, + value: '', }, definition: { others: { @@ -868,6 +869,7 @@ export const componentTypes = [ }, properties: { placeholder: { value: 'Placeholder text' }, + defaultValue: { value: '' }, }, events: [], styles: {