Controlled inputs don't work
I've been tring to implement a controlled input with a limit of max 5 characters. I expect no letters after the 5th one to be rendered, however, input acts as if it's an uncontrolled one. It updates after ~5 seconds. Documentation here is misleading and should clearly state that controlled inputs don't work at all. Here is my code:
import { useState } from "preact/hooks"
import { render, h } from 'preact'
render( <App />, document.getElementById('root'))
function App() {
let [name, setName] = useState('')
return <input type='text' value={name} onInput={handleInput} />
function handleInput(ev) {
// only first 5 letters should be passed to setName here
setName( ev.target.value.substr(0, 5) )
}
}
Controlled inputs do work, I put up this minimal example of your use case: https://codesandbox.io/s/strange-glitter-8ml51
This is due to on your fifth update the value will stop updating and your browser will decide to switch this to uncontrolled. You have to tell your input that it will only reach a max length of 5
So for you:
return (
<input
type="text"
value={value}
onInput={e => setValue(e.target.value.substr(0, 5))}
maxLength={5}
/>
);
Will fix it, in that case you can also do:
return (
<input
type="text"
value={value}
onInput={e => setValue(e.target.value)}
maxLength={5}
/>
);
Since you are sure you will never exceed that 5 threshold
This solution works for this particular case, but it's useless in more complicated reallife situations, like in my real case: max length < 5 characters, and filter all characters outside the range 32 <= charCode <= 126. How would you approach this one?
import { render } from 'react-dom'
import React, {useState } from 'react'
render( <App />, document.getElementById('root'))
function App() {
let [name, setName] = useState('')
return <input
type='text'
value={name}
onChange={handleInput}
maxLength={ 5 /* suggested fix */ }
/>
function handleInput(ev) {
// only first 5 letters should be passed to setName here
// letters with charCode outside range 32 <= charCode <= 126 should be filtered
setName( ev.target.value.substr(0, 5).split('').filter(l => l.charCodeAt() >= 32 && l.charCodeAt() <= 126).join('') )
}
}
Can't that be fixed with this
Gist of this being that uncontrolled/controlled both work in Preact it just doesn't rely on 100% Javascript for the less common scenario's
I confirm that the following solves the problem. Thanks for quick responses. Reposting here, just in case the link above gets removed. I will close the issue, but I think this should be somehow documented in the "forms" section of your guide. This is such a common scenario and you wouldn't want every user to look for this solution here on github.
import { useState } from "preact/hooks";
import { render } from "preact";
const App = () => {
const [value, setValue] = useState("");
return (
<input
type="text"
value={value}
onInput={e => {
// control the input. In this case, check if length<5 and all characters are in range 32 <= charCode <= 126
const newValue = e.target.value.substr(0, 5).split('').filter(l => l.charCodeAt() >= 32 && l.charCodeAt() <= 126).join('');
// if value has changed, modify e.target.value
if (newValue !== e.target.value) {
e.target.value = newValue;
}
// then trigger state change so that the change is visible on neighboring components
setValue(newValue);
}}
/>
);
};
render(<App />, document.getElementById("root"));