Closed pointhalo closed 1 month ago
修改建议:
用户在使用 renderOptionItem 时,从props中获取 classname并将其绑定在自定义 dom的最外层上
把option的render函数改成这样
if (typeof renderOptionItem === 'function') {
return (
<div className='semi-select-custom-option'>
{
renderOptionItem({
disabled,
focused,
selected,
style,
label,
value,
inputValue,
onMouseEnter: (e: React.MouseEvent) => onMouseEnter(e),
onClick: (e: React.MouseEvent) => this.onClick({ value, label, children, ...rest }, e),
className,
...rest
})
}
</div>
);
}
然后把index中的updateScrollTop函数改成这样
updateScrollTop: (index?: number) => {
let optionClassName = `.${prefixcls}-option-selected`;
let renderLocateClassName = `.semi-select-custom-option`;
if (index !== undefined) {
optionClassName = `.${prefixcls}-option:nth-child(${index})`;
}
let destNode = document.querySelector(`#${prefixcls}-${this.selectOptionListID} ${optionClassName}`) as HTMLDivElement;
if (this.props.renderOptionItem) {
destNode = document.querySelectorAll(`${renderLocateClassName}`)[index ? index : 0] as HTMLDivElement;
}
if (Array.isArray(destNode)) {
destNode = destNode[0];
}
console.log(destNode);
if (destNode) {
/**
* Scroll the first selected item into view.
* The reason why ScrollIntoView is not used here is that it may cause page to move.
*/
const destParent = destNode.parentNode as HTMLDivElement;
destParent.scrollTop = destNode.offsetTop -
destParent.offsetTop -
(destParent.clientHeight / 2) +
(destNode.clientHeight / 2);
}
},
效果:
这种改法可以吗?会不会影响之前的样式。
这种改法可以吗?会不会影响之前的样式。 @icwoker
你好,这个改法会对现有的用户造成影响(无论他是否需要这个滚动功能),在 renderOptionItem的情况下,它肯定是希望更彻底地掌控每个 Option的 Render结构的,否则它使用更简单的往 label传 reactNode也能做一些简单的定制渲染。所以额外多出一个 div wrapper不是很合适,可能会受到现在已经使用这个能力的用户的质疑。
这里我们倾向于将 classname在入参中传出去,并且强调,如果需要键盘滚动功能,那么它需要消费 renderOptionItem中传入的 classname,并且绑定到它的最外层容器上。
如果无需滚动功能的用户,它升级后无影响。 需要滚动功能的用户,毕竟它现在都是不work的,是一定需要改动的。那么升级后需要再改动一下它的用例代码即可,也是可以接受的。 这样可以把影响面降到最低。
+ const customRenderClassName = classNames(className,
+ {
+ [`${prefixCls}-custom-option`]: true,
+ [`${prefixCls}-custom-option-selected`]: selected
+ }
+ );
if (typeof renderOptionItem === 'function') {
return renderOptionItem({
disabled,
focused,
selected,
style,
label,
value,
inputValue,
onMouseEnter: (e: React.MouseEvent) => onMouseEnter(e),
onClick: (e: React.MouseEvent) => this.onClick({ value, label, children, ...rest }, e),
+ className: customRenderClassName
...rest
});
}
Is there an existing issue for this?
Which Component
Select
Semi Version
2.59.0
Current Behavior
Expected Behavior
No response
Steps To Reproduce
ReproducibleCode
No response
Environment
Anything else?
问题原因,使用特定classname 来做滚动,renderOptionItem时未考虑这一点。