当前用户界面
预期行为
我该怎么做呢?
App.tsx
import React from "react";
import "./index.css";
import { Select } from "antd";
const { Option } = Select;
const countryNames = [
{
name: "Japan",
code: "+987"
},
{
name: "aVeryVeryVeryVeryLongCountryName",
code: "+123"
}
];
const handleChange = (value: string) => {
console.log(`selected ${value}`);
};
const App: React.FC = () => (
<>
<Select defaultValue="Japan" onChange={handleChange}>
{countryNames.map((country) => (
<Option key={country.name} value={country.name}>
{`${country.code} ${country.name}`}
</Option>
))}
</Select>
</>
);
export default App;
代码沙箱
https://codesandbox.io/s/basic-usage-antd-5-1-5-forked-mfw8fj?file=/demo.tsx
1条答案
按热度按时间iyzzxitl1#
根据
antd
document,Select
可以使用optionLabelProp
从Option
属性中指定标签值,在此用例中,optionLabelProp
可以从country.code
中获取值。为了根据
Option
的内容扩展其大小,可以将dropdownMatchSelectWidth
设置为false
,从而使Option
不限于Select
的大小。修改演示日期:codesandbox