Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"@rc-component/menu": "~1.1.0",
"@rc-component/textarea": "~1.0.0",
"@rc-component/trigger": "^3.0.0",
"@rc-component/util": "^1.2.0",
"@rc-component/util": "^1.3.0",
"classnames": "^2.2.6"
},
"devDependencies": {
Expand Down
18 changes: 9 additions & 9 deletions src/Mentions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { CommonInputProps } from '@rc-component/input/lib/interface';
import type { TextAreaProps, TextAreaRef } from '@rc-component/textarea';
import TextArea from '@rc-component/textarea';
import toArray from '@rc-component/util/lib/Children/toArray';
import useMergedState from '@rc-component/util/lib/hooks/useMergedState';
import useControlledState from '@rc-component/util/lib/hooks/useControlledState';
import KeyCode from '@rc-component/util/lib/KeyCode';
import React, {
forwardRef,
Expand Down Expand Up @@ -171,10 +171,10 @@ const InternalMentions = forwardRef<MentionsRef, MentionsProps>(
const [isFocus, setIsFocus] = useState(false);

// ============================== Value ===============================
const [mergedValue, setMergedValue] = useMergedState('', {
defaultValue,
value: value,
});
const [mergedValue, setMergedValue] = useControlledState(
defaultValue || '',
value,
);
Comment on lines +174 to +177

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

useMergedState 替换为 useControlledState 的重构是正确的。

然而,这里有一个潜在的先前就存在的问题。当 InternalMentionsMentions 组件内部使用时,此处的 value prop 始终是 undefined。这意味着 InternalMentions 实际上总是在非受控模式下运行,其内部的 mergedValue 状态与 Mentions 组件的 mergedValue 状态是分离的。

这会导致一个 bug:当 Mentions 组件作为受控组件并且其 value prop 从外部更新时,TextArea 中显示的值不会更新。

为了修复这个问题,Mentions 组件需要将其 mergedValue 传递给 InternalMentionsvalue prop。虽然修复超出了本次变更的范围,但由于你正在处理状态管理逻辑,这可能是一个值得考虑解决的问题。

一个可能的修复方案是在 Mentions 组件中:

<InternalMentions
  value={mergedValue}
  {...rest}
/>


// =============================== Open ===============================
const { open } = useContext(UnstableContext);
Expand Down Expand Up @@ -564,10 +564,10 @@ const Mentions = forwardRef<MentionsRef, MentionsProps>(
}));

// ============================== Value ===============================
const [mergedValue, setMergedValue] = useMergedState('', {
defaultValue,
value: customValue,
});
const [mergedValue, setMergedValue] = useControlledState(
defaultValue || '',
customValue,
);

// ============================== Change ==============================
const triggerChange = (currentValue: string) => {
Expand Down
Loading