textarea ขนาดเล็กที่ปรับแต่งได้สำหรับ React to colorize ไฮไลต์ตกแต่งข้อความเสนอการเติมข้อความอัตโนมัติและอื่น ๆ อีกมากมาย
บันทึก
คุณอาจชอบ https://github.com/inokawa/edix






https://inokawa.github.io/rich-textarea/
บางครั้งเราต้องการตัวแก้ไขข้อความที่กำหนดเองในเว็บ อย่างไรก็ตามการสร้างมันด้วยความพึงพอใจแบบดิบนั้นยากที่จะทำอย่างถูกต้องและกรอบการแก้ไขมักจะหนักเกินไป ... บางทีคุณอาจต้องการเป็นเพียง textarea ที่มีการเน้นและเมนูที่ลอยอยู่บางส่วน แต่ Textarea ดั้งเดิม ห้องสมุดนี้มีเป้าหมายที่จะแก้ปัญหา
npm install rich-textarea หากคุณใช้ ESM และ WebPack 5 ให้ใช้ React> = 18 เพื่อหลีกเลี่ยงการแก้ไขข้อผิดพลาด react/jsx-runtime
import { useState } from "react" ;
import { RichTextarea } from "rich-textarea" ;
export const App = ( ) => {
const [ text , setText ] = useState ( "Lorem ipsum" ) ;
return (
< RichTextarea
value = { text }
style = { { width : "600px" , height : "400px" } }
onChange = { ( e ) => setText ( e . target . value ) }
>
{ ( v ) => {
return v . split ( "" ) . map ( ( t , i ) => (
< span key = { i } style = { { color : i % 2 === 0 ? "red" : undefined } } >
{ t }
</ span >
) ) ;
} }
</ RichTextarea >
) ;
} ; import { RichTextarea } from "rich-textarea" ;
export const App = ( ) => {
return (
< RichTextarea
defaultValue = "Lorem ipsum"
style = { { width : "600px" , height : "400px" } }
>
{ ( v ) => {
return v . split ( "" ) . map ( ( t , i ) => (
< span key = { i } style = { { color : i % 2 === 0 ? "red" : undefined } } >
{ t }
</ span >
) ) ;
} }
</ RichTextarea >
) ;
} ;คุณสามารถใช้ Helper สำหรับ Regex หากคุณไม่ต้องการสร้างฟังก์ชั่นการเรนเดอร์ของคุณเอง
import { useState } from "react" ;
import { RichTextarea , createRegexRenderer } from "rich-textarea" ;
const renderer = createRegexRenderer ( [
[ / [A-Z][a-z]+ / g , { borderRadius : "3px" , backgroundColor : "#d0bfff" } ] ,
] ) ;
export const App = ( ) => {
const [ text , setText ] = useState ( "Lorem ipsum" ) ;
return (
< RichTextarea
value = { text }
style = { { width : "600px" , height : "400px" } }
onChange = { ( e ) => setText ( e . target . value ) }
>
{ renderer }
</ RichTextarea >
) ;
} ; // _components/Textarea.tsx
"use client" ;
import { RichTextarea , RichTextareaProps } from "rich-textarea" ;
export const Textarea = (
props : Omit < RichTextareaProps , "children" | "ref" >
) => {
return (
< RichTextarea { ... props } >
{ ( v ) => {
return v . split ( "" ) . map ( ( t , i ) => (
< span key = { i } style = { { color : i % 2 === 0 ? "red" : undefined } } >
{ t }
</ span >
) ) ;
} }
</ RichTextarea >
) ;
} ;
// page.tsx in App Router of Next.js
import { Textarea } from "../_components/Textarea" ;
async function hello ( formData : FormData ) {
"use server" ;
console . log ( formData . get ( "hello" ) ) ;
}
export default ( ) => {
return (
< div >
< div > this is server! </ div >
< form action = { hello } >
< Textarea defaultValue = "Lorem ipsum" name = "hello" />
< button type = "submit" > submit </ button >
</ form >
</ div >
) ;
} ; import type { ActionFunction } from "@remix-run/node" ;
import { Form } from "@remix-run/react" ;
import { RichTextarea , RichTextareaProps } from "rich-textarea" ;
const Textarea = ( props : Omit < RichTextareaProps , "children" | "ref" > ) => {
return (
< RichTextarea { ... props } >
{ ( v ) => {
return v . split ( "" ) . map ( ( t , i ) => (
< span key = { i } style = { { color : i % 2 === 0 ? "red" : undefined } } >
{ t }
</ span >
) ) ;
} }
</ RichTextarea >
) ;
} ;
export const action : ActionFunction = async ( { request } ) => {
console . log ( ( await request . formData ( ) ) . get ( "hello" ) ) ;
} ;
export default ( ) => {
return (
< Form method = "post" >
< Textarea defaultValue = "Lorem ipsum" name = "hello" />
< button type = "submit" > submit </ button >
</ Form >
) ;
} ; ยินดีต้อนรับการมีส่วนร่วมทั้งหมด หากคุณพบปัญหาอย่าลังเลที่จะสร้างปัญหาหรือการประชาสัมพันธ์
npm install