fix: fix problems caused by uncontrolled components

This commit is contained in:
robin 2022-10-14 14:10:45 +08:00
parent 5dd8d1246f
commit 9deb8ceb78
6 changed files with 34 additions and 23 deletions

View File

@ -195,7 +195,7 @@ export interface PostAnswerReq {
}
export interface PageUser {
id;
id?;
displayName;
userName?;
avatar_url?;

View File

@ -29,7 +29,9 @@ const Form = ({
const handleChange = (e) => {
setValue(e.target.value);
};
const handleSelected = (val) => {
setValue(val);
};
return (
<div
className={classNames(
@ -37,7 +39,7 @@ const Form = ({
className,
)}>
<div>
<Mentions pageUsers={pageUsers.getUsers()}>
<Mentions pageUsers={pageUsers.getUsers()} onSelected={handleSelected}>
<TextArea size="sm" value={value} onChange={handleChange} />
</Mentions>
<div className="form-text">{t(`tip_${mode}`)}</div>

View File

@ -13,13 +13,18 @@ const Form = ({ userName, onSendReply, onCancel, mode }) => {
const handleChange = (e) => {
setValue(e.target.value);
};
const handleSelected = (val) => {
setValue(val);
};
return (
<div className="mb-2">
<div className="fs-14 mb-2">Reply to {userName}</div>
<div className="d-flex mb-1 align-items-start flex-column flex-md-row">
<div>
<Mentions pageUsers={pageUsers.getUsers()}>
<Mentions
pageUsers={pageUsers.getUsers()}
onSelected={handleSelected}>
<TextArea size="sm" value={value} onChange={handleChange} />
</Mentions>
<div className="form-text">{t(`tip_${mode}`)}</div>

View File

@ -6,11 +6,12 @@ import * as Types from '@answer/common/interface';
interface IProps {
children: React.ReactNode;
pageUsers;
onSelected: (val: string) => void;
}
const MAX_RECODE = 5;
const Mentions: FC<IProps> = ({ children, pageUsers }) => {
const Mentions: FC<IProps> = ({ children, pageUsers, onSelected }) => {
const menuRef = useRef<HTMLDivElement>(null);
const dropdownRef = useRef<HTMLDivElement>(null);
const [val, setValue] = useState('');
@ -71,23 +72,17 @@ const Mentions: FC<IProps> = ({ children, pageUsers }) => {
if (!selectionStart) {
return;
}
const str = value.substring(
value.substring(0, selectionStart).lastIndexOf('@'),
selectionStart,
);
const text = `@${item?.displayName}[${item?.userName}] `;
element.value = `${value.substring(
0,
value.substring(0, selectionStart).lastIndexOf('@'),
)}${text}${value.substring(selectionStart)}`;
onSelected(
`${value.substring(
0,
value.substring(0, selectionStart).lastIndexOf('@'),
)}${text}${value.substring(selectionStart)}`,
);
setUsers([]);
setValue('');
const newSelectionStart = selectionStart + text.length - str.length;
element.setSelectionRange(newSelectionStart, newSelectionStart);
element.focus();
};
const filterData = val
? users.filter(
(item) =>

View File

@ -14,11 +14,11 @@ const usePageUsers = () => {
getUsers,
setUsers: (data: Types.PageUser | Types.PageUser[]) => {
if (data instanceof Array) {
setUsers(uniqBy([...users, ...data], 'name'));
globalUsers = uniqBy([...globalUsers, ...data], 'name');
setUsers(uniqBy([...users, ...data], 'userName'));
globalUsers = uniqBy([...globalUsers, ...data], 'userName');
} else {
setUsers(uniqBy([...users, data], 'name'));
globalUsers = uniqBy([...globalUsers, data], 'name');
setUsers(uniqBy([...users, data], 'userName'));
globalUsers = uniqBy([...globalUsers, data], 'userName');
}
},
};

View File

@ -55,7 +55,16 @@ const Index = () => {
}
res.list.forEach((item) => {
setUsers([item.user_info, item?.update_user_info]);
setUsers([
{
displayName: item.user_info.display_name,
userName: item.user_info.username,
},
{
displayName: item?.update_user_info?.display_name,
userName: item?.update_user_info?.username,
},
]);
});
}
};