1- import React , { Component , Fragment } from 'react' ;
1+ import React , { Fragment , useEffect , useState } from 'react' ;
22import PropTypes from 'prop-types' ;
33import Editor from 'react-simple-code-editor' ;
44import Highlight , { Prism } from 'prism-react-renderer' ;
55import { theme as liveTheme } from '../../constants/theme' ;
66
7- class CodeEditor extends Component {
8- static propTypes = {
9- code : PropTypes . string ,
10- disabled : PropTypes . bool ,
11- language : PropTypes . string ,
12- onChange : PropTypes . func ,
13- style : PropTypes . object ,
14- theme : PropTypes . object
15- } ;
7+ const CodeEditor = props => {
8+ const [ state , setState ] = useState ( {
9+ code : props . code || ''
10+ } ) ;
1611
17- static getDerivedStateFromProps ( props , state ) {
18- if ( props . code !== state . prevCodeProp ) {
19- return { code : props . code , prevCodeProp : props . code } ;
12+ useEffect ( ( ) => {
13+ if ( state . prevCodeProp && props . code !== state . prevCodeProp ) {
14+ setState ( { code : props . code , prevCodeProp : props . code } ) ;
2015 }
16+ } , [ props . code ] ) ;
2117
22- return null ;
23- }
24-
25- state = {
26- code : ''
18+ const updateContent = code => {
19+ setState ( { code } ) ;
2720 } ;
2821
29- updateContent = code => {
30- this . setState ( { code } , ( ) => {
31- if ( this . props . onChange ) {
32- this . props . onChange ( this . state . code ) ;
33- }
34- } ) ;
35- } ;
22+ useEffect ( ( ) => {
23+ if ( props . onChange ) {
24+ props . onChange ( state . code ) ;
25+ }
26+ } , [ state . code ] ) ;
3627
37- highlightCode = code => (
28+ const highlightCode = code => (
3829 < Highlight
3930 Prism = { Prism }
4031 code = { code }
41- theme = { this . props . theme || liveTheme }
42- language = { this . props . language }
32+ theme = { props . theme || liveTheme }
33+ language = { props . language }
4334 >
4435 { ( { tokens, getLineProps, getTokenProps } ) => (
4536 < Fragment >
@@ -57,30 +48,36 @@ class CodeEditor extends Component {
5748 </ Highlight >
5849 ) ;
5950
60- render ( ) {
61- // eslint-disable-next-line no-unused-vars
62- const { style, theme, onChange, ...rest } = this . props ;
63- const { code } = this . state ;
51+ // eslint-disable-next-line no-unused-vars
52+ const { style, theme, onChange, ...rest } = props ;
53+ const { code } = state ;
54+
55+ const baseTheme = theme && typeof theme . plain === 'object' ? theme . plain : { } ;
6456
65- const baseTheme =
66- theme && typeof theme . plain === 'object' ? theme . plain : { } ;
57+ return (
58+ < Editor
59+ value = { code }
60+ padding = { 10 }
61+ highlight = { highlightCode }
62+ onValueChange = { updateContent }
63+ style = { {
64+ whiteSpace : 'pre' ,
65+ fontFamily : 'monospace' ,
66+ ...baseTheme ,
67+ ...style
68+ } }
69+ { ...rest }
70+ />
71+ ) ;
72+ } ;
6773
68- return (
69- < Editor
70- value = { code }
71- padding = { 10 }
72- highlight = { this . highlightCode }
73- onValueChange = { this . updateContent }
74- style = { {
75- whiteSpace : 'pre' ,
76- fontFamily : 'monospace' ,
77- ...baseTheme ,
78- ...style
79- } }
80- { ...rest }
81- />
82- ) ;
83- }
84- }
74+ CodeEditor . propTypes = {
75+ code : PropTypes . string ,
76+ disabled : PropTypes . bool ,
77+ language : PropTypes . string ,
78+ onChange : PropTypes . func ,
79+ style : PropTypes . object ,
80+ theme : PropTypes . object
81+ } ;
8582
8683export default CodeEditor ;
0 commit comments