我正在开发一个Reaction网站,我试图根据我自己的图设计定制 Select 选项.

enter image description here

这是我的Figma设计,我是这样做的:

const CustomDatePicker = ({ birthdate, handle }) => {
    const selectRef = useRef(null);

    const [month, setMonth] = useState('0');
    const [day, setDay] = useState('0');
    const [year, setYear] = useState('0');

    const months = [
        { value: '01', label: 'January' },
        { value: '02', label: 'February' },
        { value: '03', label: 'March' },
        { value: '04', label: 'April' },
        { value: '05', label: 'May' },
        { value: '06', label: 'June' },
        { value: '07', label: 'July' },
        { value: '08', label: 'August' },
        { value: '09', label: 'September' },
        { value: '10', label: 'Octorber' },
        { value: '11', label: 'November' },
        { value: '12', label: 'December' },
    ];

    const days = Array.from({ length: 31 }, (_, i) => i + 1).map((day) => ({
        value: day < 10 ? `0${day}` : `${day}`,
        label: day.toString(),
    }));

    const years = Array.from({ length: 125 }, (_, i) => 2024 - i).map((year) => ({
        value: year.toString(),
        label: year.toString(),
    }));

    const handleMonthChange = (event: any) => {
        setMonth(event.target.value);
        handle({ ...birthdate, month: parseInt(event.target.value) });
    };

    const handleDayChange = (event: any) => {
        setDay(event.target.value);
        handle({ ...birthdate, day: parseInt(event.target.value) });
    };

    const handleYearChange = (event: any) => {
        setYear(event.target.value);
        handle({ ...birthdate, year: parseInt(event.target.value) });
    };

    const handleIconClick = () => {
        // Trigger click event on select element
        selectRef.current.click();
    };

    return (
        <div style={{ display: 'flex', gap: '8px' }}>
            <div style={{ flex: 1, position: 'relative' }}>
                <select ref={selectRef} value={month} onChange={handleMonthChange} className={month == '0' ? styles.date_selector_placeholder : styles.date_selector}>
                    <option value="0" disabled className={styles.date_selector_option_item_placeholder}>Month</option>
                    {months.map((month) => (
                        <option key={month.value} value={month.value} className={styles.date_selector_option_item}>
                            {month.label}
                        </option>
                    ))}
                </select>
                <CustomIcon onClick={handleIconClick} name='arrowDownGrey' width={23} height={17} style={{ position: 'absolute', top: '14px', right: '12px' }} />
            </div>
            <div style={{ flex: 0.5, position: 'relative' }}>
                <select value={day} onChange={handleDayChange} className={day == '0' ? styles.date_selector_placeholder : styles.date_selector} style={{ flex: 0.5 }}>
                    <option value="0" disabled className={styles.date_selector_option_item_placeholder}>Day</option>
                    {days.map((day) => (
                        <option key={day.value} value={day.value} className={styles.date_selector_option_item}>
                            {day.label}
                        </option>
                    ))}
                </select>
                <CustomIcon onClick={handleIconClick} name='arrowDownGrey' width={23} height={17} style={{ position: 'absolute', top: '14px', right: '12px' }} />
            </div>
            <div style={{ flex: 0.5, position: 'relative' }}>
                <select value={year} onChange={handleYearChange} className={year == '0' ? styles.date_selector_placeholder : styles.date_selector} style={{ flex: 0.5 }}>
                    <option value="0" disabled className={styles.date_selector_option_item_placeholder}>Year</option>
                    {years.map((year) => (
                        <option key={year.value} value={year.value} className={styles.date_selector_option_item}>
                            {year.label}
                        </option>
                    ))}
                </select>
                <CustomIcon onClick={handleIconClick} name='arrowDownGrey' width={23} height={17} style={{ position: 'absolute', top: '14px', right: '12px' }} />
            </div>
        </div>
    );
};

正如您所看到的,当我单击CustomIcon时,我想打开 Select 选项菜单,但它不起作用.我试图找到解决办法,但目前还没有结果.请帮帮我,谢谢大家.

为了解决这个问题,我使用了useRef,但它没有工作.还有其他方法吗?

推荐答案

请参考此仓库. https://github.com/santiagourregobotero/customized-select

为了实现 Select 元素的自定义外观,一种常见的技术包括将 Select 的不透明度设置为0,有效地隐藏它,然后使用替代元素自定义其外观.这种方法允许为所选元素提供更专业和定制的外观和感觉.

通过将 Select 元素的不透明度设置为0,它将在视觉上隐藏,同时仍然保留其功能.随后,可以将自定义样式应用于其他元素,如div或span,以创建 Select 元素的视觉吸引力和个性化表示.这种方法在设计方面提供了更大的灵活性,并允许与网页或应用程序的整体外观和感觉进行更无缝的集成.

enter image description here

Typescript相关问答推荐

webpack错误:模块找不到.当使用我的其他库时,

具有泛型类型和缺省值的键

是否使用非显式名称隔离在对象属性上声明的接口的内部类型?

带switch 的函数的返回类型的类型缩小

打字类型保护递归判断

在类型脚本中创建泛型类型以动态追加属性后缀

如何将定义模型(在Vue 3.4中)用于输入以外的其他用途

(TypeScript)TypeError:无法读取未定义的属性(正在读取映射)"

界面中数组中的混合类型导致打字错误

带投掷的收窄类型

如何使用泛型函数参数定义类型脚本函数

如何扩展RxJS?

来自枚举的<;复选框和组件列表

typescript如何从映射类型推断

Vite+Chrome扩展 list v3-;不能在模块外使用import语句;对于inpage脚本

记录的子类型<;字符串,X>;没有索引签名

基于参数的 TS 返回类型

递归类型名称

如何判断路由或其嵌套路由是否处于活动状态?

是否有解释为什么默认情况下在泛型类型上访问的属性不是索引访问