import { html, Component, createRef } from "../lib/index.js";
export default class Composer extends Component {
state = {
text: "",
};
textInput = createRef();
constructor(props) {
super(props);
this.handleInput = this.handleInput.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
this.handleInputKeyDown = this.handleInputKeyDown.bind(this);
this.handleWindowKeyDown = this.handleWindowKeyDown.bind(this);
}
handleInput(event) {
this.setState({ [event.target.name]: event.target.value });
if (this.props.readOnly && event.target.name === "text" && !event.target.value) {
event.target.blur();
}
}
handleSubmit(event) {
event.preventDefault();
this.props.onSubmit(this.state.text);
this.setState({ text: "" });
}
handleInputKeyDown(event) {
if (!this.props.autocomplete || event.key !== "Tab") {
return;
}
let text = this.state.text;
let i;
for (i = text.length - 1; i >= 0; i--) {
if (text[i] === " ") {
break;
}
}
let prefix = text.slice(i + 1);
if (!prefix) {
return;
}
event.preventDefault();
let repl = this.props.autocomplete(prefix);
if (!repl) {
return;
}
text = text.slice(0, i + 1) + repl;
this.setState({ text });
}
handleWindowKeyDown(event) {
// If an or