在react Select 中,我有一个很长的选项列表,如下所示,这使得我很难从最后一个选项中 Select 一个.用户必须向下滚动所有选项才能获得最后一个选项.

有没有办法从数组列表的中间开始 Select 菜单的选项?

例如从options\[20\]开始,用户可以上下滚动以获得第一个或最后一个选项

我有一系列这样的 Select :

const options = [
    {
        label : '04:45',
        value : '04:45'
    },
    {
        label : '05:00',
        value : '05:00'
    },
    {
        label : '05:15',
        value : '05:15'
    },
    {
        label : '05:30',
        value : '05:30'
    },
    {
        label : '05:45',
        value : '05:45'
    },
    {
        label : '06:00',
        value : '06:00'
    },
    {
        label : '06:15',
        value : '06:15'
    },
    {
        label : '06:30',
        value : '06:30'
    },
    {
        label : '06:45',
        value : '06:45'
    },
    {
        label : '07:00',
        value : '07:00'
    },
    {
        label : '07:15',
        value : '07:15'
    },
]

使用React Select

<Select 
    isRtl
    options={hours}
    unstyled
    isSearchable={false}
/>

我试了defaultValue次,但它的表现并不像我预期的那样.

推荐答案

默认情况下, Select 中间选项并使用custom option component. Select 选项后,请拨打scrollIntoView({ behavior: "instant", block: "center" }).

const hours = [
    {
        label: '04:45',
        value: '04:45'
    },
    {
        label: '05:00',
        value: '05:00'
    },
    {
        label: '05:15',
        value: '05:15'
    },
    {
        label: '05:30',
        value: '05:30'
    },
    {
        label: '05:45',
        value: '05:45'
    },
    {
        label: '06:00',
        value: '06:00'
    },
    {
        label: '06:15',
        value: '06:15'
    },
    {
        label: '06:30',
        value: '06:30'
    },
    {
        label: '06:45',
        value: '06:45'
    },
    {
        label: '07:00',
        value: '07:00'
    },
    {
        label: '07:15',
        value: '07:15'
    },
    {
        label: '07:30',
        value: '07:30'
    },
    {
        label: '07:45',
        value: '07:45'
    }
];
const Option = (props) => {
    const ref = useRef();

    useEffect(() => {
        props.isSelected && ref.current.scrollIntoView({ behavior: "instant", block: "center" });
    }, [props.isSelected]);

    return !props.isDisabled ? (
        <components.Option {...props} innerRef={ref} />) : null;
};

export default function () {
    const components = {
        Option: Option,
    }
    return (
        <Select
            components={components}
            isRtl
            defaultValue={hours[Math.floor(hours.length/2)]}
            options={hours}
            isSearchable={false}
        />
    )
}

Reactjs相关问答推荐

为什么安装FLOBIT后,所有的输入FIELD现在都有一个蓝色的轮廓?

如何在关闭和打开此 Select 输入时应用旋转效果?

使用useReducer时,props 未从父级传递给子或孙级

两条react 路由的行为不同.有没有人能解释一下,我已经找了好几天了

如何在MUI X数据网格列中显示IMG?

为多租户SSO登录配置Azure AD应用程序

在动态React组件中删除事件侦听器

太多的重新渲染 - React 中的无限循环.如何使用React.effect并使用fetch?

是的验证:使用 useFieldArray 访问表单值

React Router 6 点击链接在 React 18 应用中打开 flickr 页面

Formik onChange() 字段挂钩覆盖显示值和 Select 机制

React v6 中的公共和私有路由

为什么接受 useState 返回的函数的 setter 在使用 React 的单个调用中执行两次?

React CSSTransition 两次创建新页面并在这两个相同的页面上运行转换

useState 在生命周期中的位置

如何在props 更改时运行自定义挂钩?

在 Reactjs 中状态发生变化时渲染一个新组件而不替换旧组件

列表应该有一个唯一的关键props

将 set statehook 函数传递给子路由组件.解构错误

Rtk 查询无效标签不使数据无效