Search 搜索
如果项目中使用的是 1.x 版本的基础组件(@alifd/next),请在左侧导航顶部切换组件版本。
安装方法
- 在命令行中执行以下命令
npm install @icedesign/base@latest -S
开发指南
何时使用
页面、表单数据搜索时使用
API
搜索
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
prefix | 样式前缀 | String | 'next-' |
size | 大小,可选 primary/secondary 可选择 medium/large ; normal 可选择 small/medium | String | 'medium' |
filter | 前置下拉框,default为默认选中项 [{text:'Products', value:'Products',default: true},{text:'Suppliers',valuse:'Suppliers'}] | Array | [] |
hasIcon | 搜索按钮图标 | Boolean | true |
searchText | 搜索按钮文案 | String | 'Search' |
combox | 定制下拉框,适合业务特殊定制 | ReactNode | false |
inputWidth | 搜索框宽度 | String/Number | 'auto' |
overlayVisible | 与combox配合使用,控制定制下拉框的展现 | Boolean | - |
dataSource | 下拉提示框:历史搜索/搜索建议 [{label:'',value:'',disabled:true}] | Array | [] |
type | 类型可选值:'primary', 'secondary', 'normal' | Enum | 'primary' |
value | 数值 | String/Number | - |
defaultValue | 搜索框默认值 (不适用于Combox) | String | - |
className | 样式名称 | String | - |
placeholder | 默认提示 | String | - |
onInputFocus | input获取焦点的时候触发的回调签名:Function() => void | Function | function() { } |
onInputBlur | input失去焦点的时候触发的回调签名:Function() => void | Function | function() { } |
onSearch | 点击搜索按钮触发的回调签名:Function(object: Object) => void参数:object: {Object} {filter:'',key:''} | Function | function() { } |
onChange | 输入关键字时的回掉签名:Function() => void | Function | function() { } |
onFilterChange | Filter改变时的回掉(filterValue)签名:Function() => void | Function | function() { } |
autoWidth | 搜索框100%自适应父容器 | Boolean | false |
filterAutoWidth | filter的下拉菜单是否与选择器对齐 | Boolean | true |
style | 自定义样式 | Object | - |
container | 指定渲染combox的容器 | ReactNode | - |
代码示例
自适应宽度
查看源码在线预览
import { Search } from "@icedesign/base";
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
filter: [
{
text: "Products",
value: "Products"
},
{
text: "Products1",
value: "Products1"
},
{
text: "Products2",
value: "Products2"
},
{
text: "Products3",
value: "Products3"
},
{
text: "Products4",
value: "Products4"
},
{
text: "Products5",
value: "Products5"
},
{
text: "Products6",
value: "Products6"
},
{
text: "Products7",
value: "Products7"
},
{
text: "Products8",
value: "Products8"
},
{
text: "Products9",
value: "Products9"
},
{
text: "Products10",
value: "Products10"
},
{
text: "Suppliers",
value: "Suppliers",
default: true
}
],
value: "",
dataSource: []
};
}
onSearch(value) {
console.log(value);
}
onChange(value) {
console.log("input is:", value);
this.setState({
value: value
});
}
onFilterChange(value) {
console.log("filter is:", value);
}
render() {
return (
<div style={{ width: 700 }}>
<Search
onSearch={this.onSearch.bind(this)}
onChange={this.onChange.bind(this)}
dataSource={this.state.dataSource}
autoWidth
/>
</div>
);
}
}
ReactDOM.render(<App />, mountNode);
自定义下拉框内容。
查看源码在线预览
import { Search, Menu } from "@icedesign/base";
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
overlayVisible: false,
value: "111222",
menuData: [
{
text: "Option 1",
value: "Option 1 Value",
index: "1"
},
{
text: "Option 2",
value: "Option 2 Value",
index: "2"
},
{
text: "Option 3",
value: "Option 3 Value",
index: "3"
},
{
text: "Option 4",
value: "Option 4 Value",
index: "4"
}
],
filter: [
{
text: "Products",
value: "Products"
},
{
text: "Products1",
value: "Products1"
},
{
text: "Products2",
value: "Products2"
},
{
text: "Products3",
value: "Products3"
},
{
text: "Products4",
value: "Products4"
},
{
text: "Products5",
value: "Products5"
},
{
text: "Products6",
value: "Products6"
},
{
text: "Products7",
value: "Products7"
},
{
text: "Products8",
value: "Products8"
},
{
text: "Products9",
value: "Products9"
},
{
text: "Products10",
value: "Products10"
},
{
text: "Suppliers",
value: "Suppliers",
default: true
}
]
};
}
renderMenu() {
const menuData = this.state.menuData;
return (
<Menu onClick={this.onClick.bind(this)} className="diy-menu">
<Menu.Group label="Recent" key="xxx">
{menuData.map(item => {
return (
<Menu.Item key={item.value}>
{" "}
{item.text}{" "}
<a
onClick={this.onDelete.bind(this)}
target="_self"
data-index={item.index}
>
Delete
</a>{" "}
</Menu.Item>
);
})}
</Menu.Group>
</Menu>
);
}
onSearch(value) {
console.log(value);
}
onChange(value) {
this.setState({
overlayVisible: value.length > 0,
value: value
});
}
onClick(selectedKeys) {
if (typeof selectedKeys === "string") {
this.setState({
overlayVisible: false,
value: selectedKeys
});
}
}
onDelete(e) {
e.stopPropagation();
const index = e.currentTarget.getAttribute("data-index");
const menuData = this.state.menuData;
const menuDataNew = [];
menuData.forEach(function(item) {
if (item.index !== index) {
menuDataNew.push(item);
}
});
this.setState({
menuData: menuDataNew
});
}
onInputFocus() {
this.setState({
overlayVisible: true
});
}
render() {
const overlayVisible = this.state.overlayVisible;
const value = this.state.value;
return (
<div style={{ width: 700 }}>
<Search
placeholder="123"
autoWidth
combox={<div>{this.renderMenu()}</div>}
overlayVisible={overlayVisible}
value={value}
onSearch={this.onSearch.bind(this)}
onChange={this.onChange.bind(this)}
onInputFocus={this.onInputFocus.bind(this)}
/>
</div>
);
}
}
ReactDOM.render(<App />, mountNode);
.diy-menu{
/*width: 275px*/;
}
.diy-menu .next-menu-item a{
display:none;
float: right;
cursor: pointer;
}
.diy-menu .next-menu-item:hover a{
display:inline-block;
}
onSearch, onChange, onInputBlur, onInputFocus事件。
查看源码在线预览
import { Search, Button } from "@icedesign/base";
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
dataSource: [
{
label: "Recent",
value: "Recent",
disabled: true
},
{
label: "连衣裙",
value: "连衣裙",
disabled: false
},
{
label: "墨镜",
value: "墨镜",
disabled: false
},
{
label: "短袖",
value: "短袖",
disabled: false
}
],
value: ""
};
}
onSearchClick() {
let time = new Date();
time = time.getTime();
this.setState({
value: time
});
}
onClearClick() {
this.setState({
value: ""
});
}
// 点击search按钮和在选中项上回车时触发
// 参数为obj:
// {
// filter: [],
// key: xx
// }
onSearch(obj) {
console.log(obj);
}
// input 输入时触发
onChange(value) {
this.setState({
value
});
}
onInputBlur(e, obj) {
console.log(e, obj);
}
onInputFocus(e, clickByUser, state) {
console.log(e, clickByUser, state);
}
render() {
return (
<div>
<div>
<Button onClick={this.onSearchClick.bind(this)}>
改变搜索关键字
</Button>
<Button onClick={this.onClearClick.bind(this)}>清除关键字</Button>
</div>
<br />
<Search
inputWidth={300}
value={this.state.value}
onSearch={this.onSearch.bind(this)}
onChange={this.onChange.bind(this)}
onInputBlur={this.onInputBlur.bind(this)}
onInputFocus={this.onInputFocus.bind(this)}
dataSource={this.state.dataSource}
placeholder="What are you looking for..."
name="textName"
/>
<br />
</div>
);
}
}
ReactDOM.render(<App />, mountNode);
带下拉框的用法。可以设置onFilterChange事件
查看源码在线预览
import { Search, Button } from "@icedesign/base";
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
filter: [
{
text: "Products",
value: "Products"
},
{
text: "Products1",
value: "Products1"
},
{
text: "Products2",
value: "Products2"
},
{
text: "Products3",
value: "Products3"
},
{
text: "Products4",
value: "Products4"
},
{
text: "Products5",
value: "Products5"
},
{
text: "Products6",
value: "Products6"
},
{
text: "Products7",
value: "Products7"
},
{
text: "Products8",
value: "Products8"
},
{
text: "Products9",
value: "Products9"
},
{
text: "Products10",
value: "Products10"
},
{
text: "Suppliers",
value: "Suppliers",
default: true
}
],
value: ""
};
}
onSearch(value) {
console.log(value);
}
onChange(value) {
console.log(`input is: ${value}`);
this.setState({
value: value
});
}
// value为filter的值,obj为search的全量值
onFilterChange(value, obj) {
console.log(`filter is: ${value}`);
console.log("fullData: ", obj);
}
onButtonClick() {
this.setState({
filter: [
{
text: "111111111111111111",
value: "111111111111111111"
},
{
text: "2222222",
value: "22222222",
default: true
}
]
});
}
render() {
return (
<div>
<div>
<Button onClick={this.onButtonClick.bind(this)}>修改filter</Button>
</div>
<br />
<Search
onChange={this.onChange.bind(this)}
onSearch={this.onSearch.bind(this)}
filter={this.state.filter}
onFilterChange={this.onFilterChange.bind(this)}
/>
<br />
<br />
<Search
size="large"
onChange={this.onChange.bind(this)}
onSearch={this.onSearch.bind(this)}
filter={this.state.filter}
onFilterChange={this.onFilterChange.bind(this)}
/>
<br />
</div>
);
}
}
ReactDOM.render(<App />, mountNode);
【注意】type为primary和secondary的时候size只能为'medium'和'large',type为normal是size只能为'medium'和'small'.type 切换,展示出不同的样式风格。size进行大小设置 .
查看源码在线预览
import { Search } from "@icedesign/base";
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
size: "medium",
normalSize: "small"
};
}
onButtonClick() {
this.setState({
size: this.state.size === "medium" ? "large" : "medium",
normalSize: this.state.normalSize === "medium" ? "small" : "medium"
});
}
render() {
return (
<div>
<h2>Primary</h2>
<Search
size="large"
inputWidth={300}
defaultValue="large"
placeholder="What are you looking for..."
/>
<br />
<Search
size="medium"
inputWidth={300}
defaultValue="medium"
placeholder="What are you looking for..."
/>
<h2>Secondary</h2>
<Search
size="large"
inputWidth={300}
defaultValue="large"
type="secondary"
placeholder="What are you looking for..."
/>
<br />
<Search
inputWidth={300}
size="medium"
defaultValue="medium"
type="secondary"
placeholder="What are you looking for..."
/>
<h2>normal</h2>
<Search
size="medium"
type="normal"
inputWidth={300}
defaultValue="medium"
placeholder="What are you looking for..."
/>
<br />
<Search
size="small"
type="normal"
inputWidth={300}
defaultValue="small"
placeholder="What are you looking for..."
/>
</div>
);
}
}
ReactDOM.render(<App />, mountNode);
简单用法
查看源码在线预览
import { Search } from "@icedesign/base";
ReactDOM.render(
<div>
<Search type="primary" inputWidth={300} placeholder="primary" />
<br />
<Search type="secondary" inputWidth={300} placeholder="Secondary" />
<br />
<Search type="normal" inputWidth={300} placeholder="normal" />
</div>,
mountNode
);