react 可搜索的下拉列表

js代码

import React, { PropTypes, Component, findNodeHandle, UIManager } from 'react';
import qs from 'qs';
import request from '../../utils/request';
import styles from './patrol.less'
import { Tabs, WhiteSpace, List, Icon, Steps, WingBlank, Flex, Button, TextareaItem, Toast, InputItem } from 'antd-mobile';

class DropDownList extends Component {
    constructor(props) {
        super(props);
        this.state = {
            showValues: false,
            value: undefined,
            placeholder: "请选择",
            editable: true,
            disabled: false,
            data: [],
            boxWidth: 100,
            showDataList: true,
            pageNo: 1,
            pageSize: 15
        }
        this.loadData = this.loadData.bind(this)
    }
    componentDidMount() {
        if (this.refs.box) {
            this.setState({ boxWidth: this.refs.box.offsetWidth })
        }
    }
    render() {
        const { data, value, editable, disabled, placeholder, showDataList, boxWidth } = this.state
        return (
            <div style={{ position: "relative" }} ref="box">
                <List>
                    <InputItem className={styles.searchBox} ref="inputBox"
                        value={value} editable={editable} disabled={disabled}
                        extra={
                            <Icon type={data && data.length > 0 && showDataList ? "up" : "down"} size="md" onClick={() => {
                                if (data && data.length > 0 && showDataList) {
                                    this.setState({ showDataList: false })
                                } else {
                                    this.refs.inputBox.focus()
                                }
                            }} />
                        }
                        clear={true}
                        placeholder={placeholder}
                        onChange={this.onChange.bind(this)}
                        onFocus={this.onFocus.bind(this)}
                        onBlur={() => [
                            setTimeout(() => {
                                this.setState({ showDataList: false })
                            }, 50)
                        ]}
                    />
                    {data && data.length > 0 && showDataList ?
                        <ul
                            className={styles.menuList}
                            style={{ width: boxWidth }}
                            onScroll={event => { this.onScrollEvent(event) }}
                        >
                            {
                                this.state.data.map((item, index) => {
                                    return (
                                        <li className={styles.menuItem} key={index} onClick={this.onItemClick.bind(this, item)}>{item.text}</li>
                                    )
                                })
                            }
                        </ul>
                        : ""}
                </List>
            </div>
        );
    }
    //加载数据
    async loadData(isClearOld, text) {
        const { url, filter,isCache, textField, valueField} = this.props
        const { data, value, pageNo, pageSize } = this.state
        if (url) {
            if (text) {
                param={col:textField,value:text}
            }
            let { data: reserveDate } = await request(url, param, { method: "POST", isPost: true })
            if (reserveDate.status == 0) {
                let newData = reserveDate.rows.map(item => {
                    return {
                        text: item[textField],
                        value: item[valueField]
                    }
                })
                if (isClearOld) {
                    this.setState({ data: newData })
                    this.pageNo = 1
                } else {
                    let oldData = data
                    this.setState({ data: oldData.concat(newData) })
                    this.pageNo++;
                }
            }
        }
    }
    //滚动
    onScrollEvent(e) {
        const { clientHeight, scrollHeight, scrollTop } = event.target;
        const isBottom = scrollTop + clientHeight == scrollHeight;
        //滑动到底部
        if (isBottom) {
            this.loadData(false,this.state.value)
        }
    }
    //list 点击事件
    onItemClick(data) {
        this.setState({
            value: data.text,
            showDataList: false,
            selectData: data
        })
        if (this.props.onChange) {
            this.props.onChange(data.value)
        }
    }
    //input onchange
    onChange(value) {
        this.setState({ value: value })
        if (this.props.onChange) {
            this.props.onChange(value)
        }
        this.loadData(true, value)
    }
    //input得到焦点
    onFocus() {
        //input获取到焦点时调用
        if (this.props.onFocus) {
            const focusRes = this.props.onFocus()
            if (focusRes) {
                return
            }
        }
        const { data } = this.state
        this.setState({ showDataList: true })
        if (data && data.length == 0) {
            this.loadData(true)
        }
    }
    // 点击图标
    iconClick() {
        this.setState({})
    }
}

export default DropDownList;

css样式

.searchBox {
    width: 100%;
    height: 35px;
    border: 1px solid #eeeeee;
    border-radius: 5px;
}

.searchInput {
    height: 32px;
    border: none;
    padding: 3px;
}

.searchIcon {
    width: 30px;
    height: 30px;
    // background-image: url("../images/search.svg");
    background-size: 30px 30px;
    float: right;
    cursor: pointer;
    margin-right: 5px;
}

.menuList {
    // width: 300px;
    // border: 1px solid #eee;
    max-height: 200px;
    overflow-y: scroll;
    margin: 0;
    padding: 0;
    position: fixed;
    background: #fff;
    z-index: 999;
    list-style: none;
    box-shadow: 2px 2px 2px 2px #eee,-2px 2px 2px 2px #eeefff;
    border-radius: 5px;
}
.hiddenMenuList{
    display: none;
}
.menuItem{
    line-height:40px;
    text-align:left;
    padding-left: 5px;
    // border-bottom:1px solid #eee;
}
.menuItem:hover{
    background: #d9e2f3;
    font-weight: bold;
}

使用

                                <DropDownList
                                    url="api/YtRole/GetData"
                                    moduleCode="PMS_ROLE_MNG"
                                    textField="Role_Name"
                                    valueField="Id"
                                    onChange={value => {
                                        debugger
                                    }}
                                />

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值