最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

reactjs - How to save value in textField mui - Stack Overflow

programmeradmin4浏览0评论

I have a field and there is a state:

const [inputValue, setInputValue] = useState('');

when I enter text in this field and click save, it is not saved, to save it is necessary to press the problem or enter button, but I need to make it so that I write something in the field, click the save button, without pressing the space or enter the value is added:

<TextField
    {...params}
    label="label"
    autoFocus
    onChange={(e) => setInputValue(e.target.value.trim())}
    onKeyDown={(e) => {
        const value = (e.target as HTMLInputElement).value.trim();
        if ((e.code === "Space" || e.code === "Enter") && value) {
            if (!passportIds.includes(value)) {
                field.onChange(field.value.concat(value));
            }
            setInputValue("");
        }
    }}
/>

I changed onChange to onInput but I got problem that every letter is saving like a chip

I have a field and there is a state:

const [inputValue, setInputValue] = useState('');

when I enter text in this field and click save, it is not saved, to save it is necessary to press the problem or enter button, but I need to make it so that I write something in the field, click the save button, without pressing the space or enter the value is added:

<TextField
    {...params}
    label="label"
    autoFocus
    onChange={(e) => setInputValue(e.target.value.trim())}
    onKeyDown={(e) => {
        const value = (e.target as HTMLInputElement).value.trim();
        if ((e.code === "Space" || e.code === "Enter") && value) {
            if (!passportIds.includes(value)) {
                field.onChange(field.value.concat(value));
            }
            setInputValue("");
        }
    }}
/>

I changed onChange to onInput but I got problem that every letter is saving like a chip

Share Improve this question edited Mar 22 at 17:27 Basheer Jarrah 5983 silver badges16 bronze badges asked Mar 19 at 15:12 HrenmorzhovyiHrenmorzhovyi 212 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I would try tying it to onBlur which will trigger the input value to be saved to state when the input box loses focus.

发布评论

评论列表(0)

  1. 暂无评论