这是Options和FormatOptionLabel函数
const brandOptions = films.map((film) => ({
value: film.brand,
label: film.brand,
}))
const formatOptionLabel = ({ value, label, isSelected }) => (
<div style={{ display: 'flex', alignItems: 'center' }}>
{isSelected ? null : (
<img
src={films.find((film) => film.brand === value)?.brandpic}
alt={value}
style={{ marginRight: '8px', width: '20px', height: '20px' }}
/>
)}
{label}
</div>
)
这是下拉列表
<ReactSelect
value={brandOptions.find((option) => option.value === selectedBrand)}
onChange={handleBrandChange}
options={brandOptions}
placeholder="choose the brand"
formatOptionLabel={formatOptionLabel}
className="bg-white ml-5 rounded-[40px] border-[#EBEBEB] shadow-sm w-[278px]"
/>
这些选项既有品牌名称,也有形象.但问题是,当我单击以 Select 该选项时,我只希望在控件中显示品牌名称.这个代码既给了我形象也给了我品牌名称.
const formatOptionLabel = ({ value, label, isSelected }) => (
<div style={{ display: 'flex', alignItems: 'center' }}>
{isSelected ? null : (
<img
src={films.find((film) => film.brand === value)?.brandpic}
alt={value}
style={{ marginRight: '8px', width: '20px', height: '20px' }}
/>
)}
{label}
</div>
)
{被选中了吗?空:(...部分似乎没有效果