页面上点击按钮,自动插入 prefix 比如 @ 到 mentions 输入框中,但是无法触发选择列表,必须得手动再键盘输入内容才可以,不方便
prefix
@
能够有办法唤起下拉输入选择框
0x6upsns1#
用 mentionsRef.current.textarea?.dispatchEvent(ev); 可以实现,但是 textarea 被标记为 @deprecated It may not work as expected 不知道有没有其他更好的方法?
mentionsRef.current.textarea?.dispatchEvent(ev);
textarea
@deprecated It may not work as expected
s8vozzvw2#
可以用 open 和 onOpenChange 属性控制弹层,文档中居然没写,得补一下: https://ant.design/components/mentions
xkftehaa3#
不对,这个 open 貌似生产不能用:https://github.com/react-component/mentions/blob/49e98b0cae81e160d2b1a311fbc1ee9bdef5c0d0/src/Mentions.tsx#L177
643ylb084#
嗯,我也是看到 open 不能用,然后找不到合适方法感觉不太科学。。。
fjnneemd5#
Mentions 里的列表是实时计算的,所以光设置 open 没有列表,也就不会有东西显示出来。
open
p1tboqfb6#
那这里就只能先用 mentionsRef.current.textarea?.dispatchEvent(ev) 凑合实现了。
mentionsRef.current.textarea?.dispatchEvent(ev)
还有个问题,好像没有办法控制当过滤列表为空时直接隐藏列表容器, notFoundContent=null 只能控制容器里的默认 empty 替换为空
notFoundContent=null
empty
iq3niunx7#
翻了下 Mentions 底层依赖的 rc-mentions 源码,对于弹窗内容的可见性控制是 visiable 和内部状态 mergedMeasuring 共同决定的,没有对外暴露,我是用如下比较黑的手动去绕开处理的。
Mentions
rc-mentions
useEffect(() => { const mentionsContainer = getContainer()?.querySelector<HTMLDivElement>('.ant-mentions-dropdown') const mentionsDropdownEl = mentionsContainer?.querySelector<HTMLUListElement>('ul'); if (!mentionsContainer || !mentionsDropdownEl) { return; } const observer = new MutationObserver(([record]) => { if (record.type === 'childList') { if (record.target.childNodes.length === 1 && record.target.firstChild.textContent === 'No Data') { mentionsContainer.style.opacity = '0'; } else { mentionsContainer.style.opacity = '1'; } } }); observer.observe(mentionsDropdownEl, { childList: true, }) return () => { observer.disconnect(); } }, [input]);
useEffect(() => {
const mentionsContainer = getContainer()?.querySelector<HTMLDivElement>('.ant-mentions-dropdown')
const mentionsDropdownEl = mentionsContainer?.querySelector<HTMLUListElement>('ul');
if (!mentionsContainer || !mentionsDropdownEl) {
return;
}
const observer = new MutationObserver(([record]) => {
if (record.type === 'childList') {
if (record.target.childNodes.length === 1 && record.target.firstChild.textContent === 'No Data') {
mentionsContainer.style.opacity = '0';
} else {
mentionsContainer.style.opacity = '1';
});
observer.observe(mentionsDropdownEl, {
childList: true,
})
return () => {
observer.disconnect();
}, [input]);
7条答案
按热度按时间0x6upsns1#
用
mentionsRef.current.textarea?.dispatchEvent(ev);
可以实现,但是textarea
被标记为@deprecated It may not work as expected
不知道有没有其他更好的方法?s8vozzvw2#
可以用 open 和 onOpenChange 属性控制弹层,文档中居然没写,得补一下: https://ant.design/components/mentions
xkftehaa3#
不对,这个 open 貌似生产不能用:https://github.com/react-component/mentions/blob/49e98b0cae81e160d2b1a311fbc1ee9bdef5c0d0/src/Mentions.tsx#L177
643ylb084#
嗯,我也是看到 open 不能用,然后找不到合适方法感觉不太科学。。。
fjnneemd5#
Mentions 里的列表是实时计算的,所以光设置
open
没有列表,也就不会有东西显示出来。p1tboqfb6#
Mentions 里的列表是实时计算的,所以光设置
open
没有列表,也就不会有东西显示出来。那这里就只能先用
mentionsRef.current.textarea?.dispatchEvent(ev)
凑合实现了。还有个问题,好像没有办法控制当过滤列表为空时直接隐藏列表容器,
notFoundContent=null
只能控制容器里的默认empty
替换为空iq3niunx7#
还有个问题,好像没有办法控制当过滤列表为空时直接隐藏列表容器,
notFoundContent=null
只能控制容器里的默认empty
替换为空翻了下
Mentions
底层依赖的rc-mentions
源码,对于弹窗内容的可见性控制是 visiable 和内部状态 mergedMeasuring 共同决定的,没有对外暴露,我是用如下比较黑的手动去绕开处理的。