Sam*_*mmy 8 reactjs material-ui
这是一个沙箱:https : //codesandbox.io/s/wild-sea-h2i0m
这是该沙箱中自动完成的代码:
import React from "react";
import Autocomplete from "@material-ui/lab/Autocomplete";
import Chip from "@material-ui/core/Chip";
import CloseIcon from "@material-ui/icons/Close";
import TextField from "@material-ui/core/TextField";
import { FieldProps } from "formik";
const isEmailValid = (email: string) =>
/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);
const EmailsField = ({
field,
form: { errors, touched, setTouched, setFieldValue },
...props
}: FieldProps) => {
const name = field.name;
const [value, setValue] = React.useState<string[]>([]);
const [inputValue, setInputValue] = React.useState("");
const handleChange = (event: React.ChangeEvent<{}>, emails: string[]) => {
setTouched({ ...touched, [name]: true });
setValue(emails);
event.persist();
setFieldValue(name, emails);
};
const handleInputChange = (
event: React.ChangeEvent<{}>,
newInputValue: string
) => {
const options = newInputValue.split(/[ ,]+/);
const fieldValue = value
.concat(options)
.map(x => x.trim())
.filter(x => x);
if (options.length > 1) {
handleChange(event, fieldValue);
} else {
setInputValue(newInputValue);
}
};
return (
<Autocomplete<string>
multiple
disableClearable={true}
options={[]}
freeSolo
renderTags={(emails, getTagProps) =>
emails.map((email, index) => (
<Chip
deleteIcon={<CloseIcon />}
variant="default"
label={email}
color={isEmailValid(email) ? "primary" : "secondary"}
{...getTagProps({ index })}
/>
))
}
value={value}
inputValue={inputValue}
onChange={handleChange}
onInputChange={handleInputChange}
renderInput={params => (
<TextField
{...params}
name={name}
error={touched[name] && Boolean(errors.emails)}
helperText={touched[name] && errors.emails}
variant="outlined"
InputProps={{ ...params.InputProps }}
{...props}
/>
)}
/>
);
};
export default EmailsField;
Run Code Online (Sandbox Code Playgroud)
在用户键入并按下选项卡键盘按钮移至提交按钮后,我无法使用户键入的值成为单个选项。
知道怎么做吗?
您可以通过为以下对象添加处理程序来完成此操作onKeyDown:
const handleKeyDown = (event: React.KeyboardEvent) => {
switch (event.key) {
case "Tab": {
if (inputValue.length > 0) {
handleChange(event, value.concat([inputValue]));
}
break;
}
default:
}
};
Run Code Online (Sandbox Code Playgroud)
然后通过以下方式将其添加到文本输入中inputProps:
renderInput={params => {
params.inputProps.onKeyDown = handleKeyDown;
return (
<TextField
{...params}
name={name}
error={touched[name] && Boolean(errors.emails)}
helperText={touched[name] && errors.emails}
variant="outlined"
{...props}
/>
);
}}
Run Code Online (Sandbox Code Playgroud)
这是EmailsField沙箱中组件的完整新代码:
import React from "react";
import Autocomplete from "@material-ui/lab/Autocomplete";
import Chip from "@material-ui/core/Chip";
import CloseIcon from "@material-ui/icons/Close";
import TextField from "@material-ui/core/TextField";
import { FieldProps } from "formik";
const isEmailValid = (email: string) =>
/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);
const EmailsField = ({
field,
form: { errors, touched, setTouched, setFieldValue },
...props
}: FieldProps) => {
const name = field.name;
const [value, setValue] = React.useState<string[]>([]);
const [inputValue, setInputValue] = React.useState("");
const handleChange = (event: React.ChangeEvent<{}>, emails: string[]) => {
setTouched({ ...touched, [name]: true });
setValue(emails);
event.persist();
setFieldValue(name, emails);
};
const handleInputChange = (
event: React.ChangeEvent<{}>,
newInputValue: string
) => {
const options = newInputValue.split(/[ ,]+/);
const fieldValue = value
.concat(options)
.map(x => x.trim())
.filter(x => x);
if (options.length > 1) {
handleChange(event, fieldValue);
} else {
setInputValue(newInputValue);
}
};
const handleKeyDown = (event: React.KeyboardEvent) => {
switch (event.key) {
case "Tab": {
if (inputValue.length > 0) {
handleChange(event, value.concat([inputValue]));
}
break;
}
default:
}
};
return (
<Autocomplete<string>
multiple
disableClearable={true}
options={[]}
freeSolo
renderTags={(emails, getTagProps) =>
emails.map((email, index) => (
<Chip
deleteIcon={<CloseIcon />}
variant="default"
label={email}
color={isEmailValid(email) ? "primary" : "secondary"}
{...getTagProps({ index })}
/>
))
}
value={value}
inputValue={inputValue}
onChange={handleChange}
onInputChange={handleInputChange}
renderInput={params => {
params.inputProps.onKeyDown = handleKeyDown;
return (
<TextField
{...params}
name={name}
error={touched[name] && Boolean(errors.emails)}
helperText={touched[name] && errors.emails}
variant="outlined"
{...props}
/>
);
}}
/>
);
};
export default EmailsField;
Run Code Online (Sandbox Code Playgroud)
相关答案:Material ui Autocomplete:可以在“Enter”事件之外的事件上创建标签吗?
| 归档时间: |
|
| 查看次数: |
1761 次 |
| 最近记录: |