File tree Expand file tree Collapse file tree
lib/components/contenteditable Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1- <!doctype html>
1+ <!DOCTYPE html>
22< html lang ="en ">
33 < head >
44 < meta charset ="UTF-8 " />
55 < link rel ="icon " type ="image/svg+xml " href ="/vite.svg " />
66 < meta name ="viewport " content ="width=device-width, initial-scale=1.0 " />
7- < title > Vite + React + TS </ title >
7+ < title > react-basic-contenteditable </ title >
88 </ head >
99 < body >
1010 < div id ="root "> </ div >
Original file line number Diff line number Diff line change @@ -7,7 +7,7 @@ interface ContentEditableProps {
77
88const ContentEditable : React . FC < ContentEditableProps > = ( {
99 placeholder,
10- // emitContent ,
10+ onChange ,
1111} ) => {
1212 const [ content , setContent ] = useState ( "" )
1313 const divRef = useRef < HTMLDivElement | null > ( null )
@@ -16,8 +16,9 @@ const ContentEditable: React.FC<ContentEditableProps> = ({
1616 if ( divRef . current ) {
1717 divRef . current . style . height = "auto"
1818 // divRef.current.style.height = divRef.current.scrollHeight + "px"
19+ onChange ( content )
1920 }
20- } , [ content ] )
21+ } , [ content , onChange ] )
2122
2223 /**
2324 * Check if the caret is on the last line of an element
Original file line number Diff line number Diff line change 1- import { useState } from 'react'
2- import reactLogo from './assets/react.svg'
3- import viteLogo from '/vite.svg'
4- import './App.css'
1+ import "./App.css"
2+ import ContentEditable from "../lib/components/contenteditable"
3+ import { useState } from "react"
54
65function App ( ) {
7- const [ count , setCount ] = useState ( 0 )
8-
6+ const [ content , setContent ] = useState ( "" )
97 return (
10- < >
11- < div >
12- < a href = "https://vitejs.dev" target = "_blank" >
13- < img src = { viteLogo } className = "logo" alt = "Vite logo" />
14- </ a >
15- < a href = "https://react.dev" target = "_blank" >
16- < img src = { reactLogo } className = "logo react" alt = "React logo" />
17- </ a >
18- </ div >
19- < h1 > Vite + React</ h1 >
20- < div className = "card" >
21- < button onClick = { ( ) => setCount ( ( count ) => count + 1 ) } >
22- count is { count }
23- </ button >
24- < p >
25- Edit < code > src/App.tsx</ code > and save to test HMR
26- </ p >
27- </ div >
28- < p className = "read-the-docs" >
29- Click on the Vite and React logos to learn more
30- </ p >
31- </ >
8+ < div >
9+ < div style = { { whiteSpace : "pre-wrap" } } > { content } </ div >
10+ < ContentEditable
11+ placeholder = "Type here"
12+ onChange = { ( content ) => setContent ( content ) }
13+ />
14+ </ div >
3215 )
3316}
3417
You can’t perform that action at this time.
0 commit comments