From 39d508670d7a9ae42614ed5f658b43d20787aee6 Mon Sep 17 00:00:00 2001 From: Janvo Aldred Date: Mon, 10 Aug 2020 06:25:25 +0000 Subject: [PATCH] Refactored PostTextEditor to use React Hook (#23897) * Refactored PostTextEditor to use React Hook * use useInstanceId hook instead of withInstanceId * Replaced withSelect and withDispatch with useSelect and useDispatch * Replaced withSelect and withDispatch with useSelect and useDispatch * Remove external prop support * Refactored edit function to onChange * Removed onPersist function Co-authored-by: Janvo Aldred --- .../src/components/post-text-editor/index.js | 124 +++++-------- .../components/post-text-editor/test/index.js | 174 +++++++++++------- 2 files changed, 158 insertions(+), 140 deletions(-) diff --git a/packages/editor/src/components/post-text-editor/index.js b/packages/editor/src/components/post-text-editor/index.js index d0e0626a75f162..eb8488332ee81a 100644 --- a/packages/editor/src/components/post-text-editor/index.js +++ b/packages/editor/src/components/post-text-editor/index.js @@ -7,31 +7,26 @@ import Textarea from 'react-autosize-textarea'; * WordPress dependencies */ import { __ } from '@wordpress/i18n'; -import { Component } from '@wordpress/element'; +import { useState } from '@wordpress/element'; import { parse } from '@wordpress/blocks'; -import { withSelect, withDispatch } from '@wordpress/data'; -import { withInstanceId, compose } from '@wordpress/compose'; +import { useDispatch, useSelect } from '@wordpress/data'; +import { useInstanceId } from '@wordpress/compose'; import { VisuallyHidden } from '@wordpress/components'; -export class PostTextEditor extends Component { - constructor() { - super( ...arguments ); +export default function PostTextEditor() { + const postContent = useSelect( + ( select ) => select( 'core/editor' ).getEditedPostContent(), + [] + ); - this.edit = this.edit.bind( this ); - this.stopEditing = this.stopEditing.bind( this ); + const { editPost, resetEditorBlocks } = useDispatch( 'core/editor' ); - this.state = {}; - } - - static getDerivedStateFromProps( props, state ) { - if ( state.isDirty ) { - return null; - } + const [ value, setValue ] = useState( postContent ); + const [ isDirty, setIsDirty ] = useState( false ); + const instanceId = useInstanceId( PostTextEditor ); - return { - value: props.value, - isDirty: false, - }; + if ( ! isDirty && value !== postContent ) { + setValue( postContent ); } /** @@ -45,68 +40,47 @@ export class PostTextEditor extends Component { * * @param {Event} event Change event. */ - edit( event ) { - const value = event.target.value; - this.props.onChange( value ); - this.setState( { value, isDirty: true } ); - } + const onChange = ( event ) => { + const newValue = event.target.value; + + editPost( { content: newValue } ); + + setValue( newValue ); + setIsDirty( true ); + }; /** * Function called when the user has completed their edits, responsible for * ensuring that changes, if made, are surfaced to the onPersist prop * callback and resetting dirty state. */ - stopEditing() { - if ( this.state.isDirty ) { - this.props.onPersist( this.state.value ); - this.setState( { isDirty: false } ); + const stopEditing = () => { + if ( isDirty ) { + const blocks = parse( value ); + resetEditorBlocks( blocks ); + + setIsDirty( false ); } - } + }; - render() { - const { value } = this.state; - const { instanceId } = this.props; - return ( - <> - - { __( 'Type text or HTML' ) } - -