Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Select组件自定义候选项渲染添加renderOptionItem属性,按键盘上下方向键切换item, 滚动条不跟着移动 #2597

Open
1 task done
jin-qiaojin opened this issue Nov 28, 2024 · 0 comments
Assignees
Labels
bug Something isn't working

Comments

@jin-qiaojin
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Which Component

Select

Semi Version

all

Current Behavior

image

Expected Behavior

image

Steps To Reproduce

No response

ReproducibleCode

import React from 'react';
import { Select, Checkbox, Highlight } from '@douyinfe/semi-ui';

() => {
    const [inputValue, setInputValue] = useState('');
    const renderOptionItem = renderProps => {
        const {
            disabled,
            selected,
            label,
            value,
            focused,
            className,
            style,
            onMouseEnter,
            onClick,
            empty,
            emptyContent,
            ...rest
        } = renderProps;
        const optionCls = classNames({
            ['custom-option-render']: true,
            ['custom-option-render-focused']: focused,
            ['custom-option-render-disabled']: disabled,
            ['custom-option-render-selected']: selected,
            className
        });
        const searchWords = [inputValue];

        // Notice:
        // 1.props传入的style需在wrapper dom上进行消费,否则在虚拟化场景下会无法正常使用
        // 2.选中(selected)、聚焦(focused)、禁用(disabled)等状态的样式需自行加上,你可以从props中获取到相对的boolean值
        // 3.onMouseEnter、className需在wrapper dom上绑定,否则上下键盘操作时显示会有问题
        
        return (
            <div style={style} className={optionCls} onClick={() => onClick()} onMouseEnter={e => onMouseEnter()}>
                <Checkbox checked={selected} />
                <div className="option-right">
                    <Highlight sourceString={label} searchWords={searchWords} />
                </div>
            </div>
        );
    };

    const optionList = [
        { value: 'abc', label: '抖音', otherKey: 0 },
        { value: 'ulikecam', label: '轻颜相机', disabled: true, otherKey: 1 },
        { value: 'jianying', label: '剪映', otherKey: 2 },
        { value: 'toutiao', label: '今日头条', otherKey: 3 },
        { value: 'ulikecam1', label: '轻颜相机1', disabled: true, otherKey: 1 },
        { value: 'jianying1', label: '剪映1', otherKey: 2 },
        { value: 'toutiao1', label: '今日头条1', otherKey: 3 },
        { value: 'ulikecam2', label: '轻颜相机2', disabled: true, otherKey: 1 },
        { value: 'jianying2', label: '剪映2', otherKey: 2 },
        { value: 'toutiao2', label: '今日头条2', otherKey: 3 },
        { value: 'ulikecam3', label: '轻颜相机3', disabled: true, otherKey: 1 },
        { value: 'jianying3', label: '剪映3', otherKey: 2 },
        { value: 'toutiao3', label: '今日头条3', otherKey: 3 },
    ];

    return (
        <>
            <Select
                filter
                placeholder="多选"
                multiple
                onSearch={(v) => setInputValue(v)}
                dropdownClassName="components-select-demo-renderOptionItem"
                optionList={optionList}
                style={{ width: 320 }}
                renderOptionItem={renderOptionItem}
            />
        </>
    );
};

Environment

- OS:
- browser:

Anything else?

No response

@pointhalo pointhalo added the bug Something isn't working label Dec 2, 2024
@pointhalo pointhalo self-assigned this Dec 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants