1- import React , { useCallback , useRef } from "react" ;
1+ import React , { useCallback , useEffect , useRef } from "react" ;
22import { createDecorators , type EventEmitter } from "@next-core/element" ;
33import { ReactNextElement , wrapBrick } from "@next-core/react-element" ;
4- import { TextareaAutoResize } from "@next-shared/form" ;
4+ import {
5+ TextareaAutoResize ,
6+ type TextareaAutoResizeRef ,
7+ } from "@next-shared/form" ;
58import "@next-core/theme" ;
69import { initializeI18n } from "@next-core/i18n" ;
7- import type { Button , ButtonProps } from "@next-bricks/basic/button" ;
10+ import type {
11+ GeneralIcon ,
12+ GeneralIconProps ,
13+ } from "@next-bricks/icons/general-icon" ;
814import { K , NS , locales , t } from "./i18n.js" ;
915import styleText from "./styles.shadow.css" ;
1016
1117initializeI18n ( NS , locales ) ;
1218
13- const WrappedButton = wrapBrick < Button , ButtonProps > ( "eo-button" ) ;
14-
15- const SEND_ICON : ButtonProps [ "icon" ] = {
16- lib : "fa" ,
17- prefix : "fas" ,
18- icon : "arrow-up" ,
19- } ;
19+ const WrappedIcon = wrapBrick < GeneralIcon , GeneralIconProps > ( "eo-icon" ) ;
2020
2121const { defineElement, property, event } = createDecorators ( ) ;
2222
2323export interface ChatBoxProps {
2424 disabled ?: boolean ;
25+ placeholder ?: string ;
26+ autoFocus ?: boolean ;
2527}
2628
2729/**
@@ -35,6 +37,12 @@ class ChatBox extends ReactNextElement implements ChatBoxProps {
3537 @property ( { type : Boolean } )
3638 accessor disabled : boolean | undefined ;
3739
40+ @property ( )
41+ accessor placeholder : string | undefined ;
42+
43+ @property ( { type : Boolean } )
44+ accessor autoFocus : boolean | undefined ;
45+
3846 @event ( { type : "message.submit" } )
3947 accessor #messageSubmit! : EventEmitter < string > ;
4048
@@ -46,6 +54,8 @@ class ChatBox extends ReactNextElement implements ChatBoxProps {
4654 return (
4755 < ChatBoxComponent
4856 disabled = { this . disabled }
57+ placeholder = { this . placeholder }
58+ autoFocus = { this . autoFocus }
4959 onSubmit = { this . #handleMessageSubmit}
5060 />
5161 ) ;
@@ -59,9 +69,12 @@ export interface ChatBoxComponentProps extends ChatBoxProps {
5969
6070export function ChatBoxComponent ( {
6171 disabled,
72+ placeholder,
73+ autoFocus,
6274 onSubmit,
6375} : ChatBoxComponentProps ) {
6476 const containerRef = useRef < HTMLDivElement > ( null ) ;
77+ const textareaRef = useRef < TextareaAutoResizeRef > ( null ) ;
6578 const valueRef = useRef ( "" ) ;
6679
6780 const handleSubmit = useCallback (
@@ -82,26 +95,31 @@ export function ChatBoxComponent({
8295 onSubmit ?.( valueRef . current ) ;
8396 } , [ onSubmit ] ) ;
8497
98+ useEffect ( ( ) => {
99+ if ( autoFocus ) {
100+ Promise . resolve ( ) . then ( ( ) => {
101+ textareaRef . current ?. focus ( ) ;
102+ } ) ;
103+ }
104+ } , [ ] ) ;
105+
85106 return (
86107 < div className = "container" ref = { containerRef } >
87108 < TextareaAutoResize
88109 containerRef = { containerRef }
110+ ref = { textareaRef }
89111 minRows = { 5 }
90- paddingSize = { 24 }
112+ paddingSize = { 20 }
91113 autoResize
92114 disabled = { disabled }
93- placeholder = { t ( K . HOW_CAN_I_HELP ) }
115+ placeholder = { placeholder ?? t ( K . ASK_ANY_THING ) }
94116 submitWhen = "enter-without-shift"
95117 onSubmit = { handleSubmit }
96118 onChange = { handleChange }
97119 />
98- < WrappedButton
99- className = "btn-send"
100- shape = "circle"
101- icon = { SEND_ICON }
102- disabled = { disabled }
103- onClick = { handleSubmitClick }
104- />
120+ < button className = "btn-send" onClick = { handleSubmitClick } >
121+ < WrappedIcon lib = "fa" prefix = "fas" icon = "arrow-up" />
122+ </ button >
105123 </ div >
106124 ) ;
107125}
0 commit comments