chatscope / chat-ui-kit-react

Build your own chat UI with React components in few minutes. Chat UI Kit from chatscope is an open source UI toolkit for developing web chat applications.
https://chatscope.io
MIT License
1.34k stars 116 forks source link

Refocus MessageInput after it was disabled #54

Closed chloe-schoreisz closed 2 years ago

chloe-schoreisz commented 3 years ago

I set the disabled prop of messages to true while sending. This caused the input box to not be refocused once reenabled. Would it be possible to refocus the message once the prop disabled is false ?

Screen Shot 2021-08-18 at 5 42 50 PM Screen Shot 2021-08-18 at 5 42 56 PM Screen Shot 2021-08-18 at 5 41 36 PM
supersnager commented 3 years ago

Hi @chloe-schoreisz, thank you for your question.

The focus can be set using the focus() method provided by the <MessageInput /> component.

e.g.

const inputRef = useRef();
const [disabled, setDisabled] = useState(false);

const sendMessage = () => {
 setDisabled(true);
 senToWire().then( () => {
  inputRef.current.focus();
  setDisabled(false);
});

<MessageInput ref={inputRef} onSend={sendMessage} disabled={disabled} />
chloe-schoreisz commented 3 years ago

Thanks for the solution ! Would this be considered a common use case ? I wonder if it this behaviour should be native to the component. What other common use cases for the disable props do you have ?

supersnager commented 3 years ago

@chloe-schoreisz I'm not sure if it is a common use case. Why do you want disable the input, when a message is sending? If you do it, the user will not be able to write the next message untill the previous one is sent.

What exactly behavior do you consider native, disabling while sending or refocus after sent?

BTW, sorry I forgot about one property activateAfterChange it's meant for a different use case, but you can try setting it to true to automagically set the focus after sending.

Other common use cases for the disabled prop are for example when you have no active user selected, or when connection to server has not been established. Depending on the application, you may also want to block messages from being sent to the user when they are not connected.