File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -278,12 +278,18 @@ const ContentEditable: React.FC<ContentEditableProps> = ({
278278
279279 function handleKeyDown ( e : React . KeyboardEvent ) {
280280 if ( onKeyDown ) onKeyDown ( e )
281- if ( ( e . key === "Delete" || e . key === "Backspace" ) && isAllTextSelected ( ) ) {
281+ if ( ! divRef . current ) return
282+ if (
283+ ( [ "Delete" , "Backspace" ] . includes ( e . key ) && isAllTextSelected ( ) ) ||
284+ ( e . key === "Backspace" && content . length === 1 ) ||
285+ ( e . key === "Delete" &&
286+ getCaretPosition ( divRef . current ) === 0 &&
287+ content . length === 1 )
288+ ) {
282289 e . preventDefault ( )
283- if ( divRef . current ) {
284- divRef . current . innerText = ""
285- setContent ( "" )
286- }
290+
291+ divRef . current . innerText = ""
292+ setContent ( "" )
287293 }
288294 }
289295
Original file line number Diff line number Diff line change 4040 "react-dom" : " ^18.2.0"
4141 },
4242 "devDependencies" : {
43- "@types/node" : " ^22.10.2 " ,
44- "@types/react" : " ^19.0.2 " ,
45- "@types/react-dom" : " ^19.0.2 " ,
46- "@typescript-eslint/eslint-plugin" : " ^8.18.2 " ,
47- "@typescript-eslint/parser" : " ^8.18.2 " ,
48- "@vitejs/plugin-react-swc" : " ^3.7.2 " ,
43+ "@types/node" : " ^22.13.4 " ,
44+ "@types/react" : " ^19.0.8 " ,
45+ "@types/react-dom" : " ^19.0.3 " ,
46+ "@typescript-eslint/eslint-plugin" : " ^8.24.0 " ,
47+ "@typescript-eslint/parser" : " ^8.24.0 " ,
48+ "@vitejs/plugin-react-swc" : " ^3.8.0 " ,
4949 "eslint" : " ^8.56.0" ,
5050 "eslint-plugin-react-hooks" : " ^4.6.0" ,
5151 "eslint-plugin-react-refresh" : " ^0.4.16" ,
52- "glob" : " ^11.0.0 " ,
52+ "glob" : " ^11.0.1 " ,
5353 "react" : " ^19.0.0" ,
5454 "react-dom" : " ^19.0.0" ,
55- "typescript" : " ^5.7.2 " ,
56- "vite" : " ^6.0.9 " ,
57- "vite-plugin-dts" : " ^4.4 .0"
55+ "typescript" : " ^5.7.3 " ,
56+ "vite" : " ^6.1.0 " ,
57+ "vite-plugin-dts" : " ^4.5 .0"
5858 },
5959 "bugs" : {
6060 "url" : " https://github.com/ChrisUser/react-basic-contenteditable/issues"
You can’t perform that action at this time.
0 commit comments