fit-select
Version:
选择框
50 lines (49 loc) • 1.9 kB
JSX
"use strict";
const React = require('react');
const classNames = require('classnames');
const _ = require('lodash');
const module = require('./module');
require('./index.scss');
const reg = (input) => {
let flags = 'g';
return new RegExp(input, flags);
};
class Option extends React.Component {
constructor(...args) {
super(...args);
this.state = new module.State();
}
componentWillMount() {
// 如果当前value和select的value相同,传递给父级
if (this.props.value === this.props['activeValue']) {
this.props['setLabelValue'](this.props.children);
}
}
handleClick() {
if (this.props.disabled)
return;
this.props['onClick'](this.props.value, this.props.children, this.props['optChildren'], this.props.zIndex);
}
render() {
const classes = classNames({
'_namespace': true,
'active': this.props['active'],
'disabled': this.props.disabled,
[this.props['className']]: !!this.props['className']
});
if (!_.isEmpty(this.props['searchValue'])) {
let regex = reg(_.escape(this.props['searchValue']));
if (regex.test(this.props['children'].toString())) {
let matchedString = _.escape(this.props['children'].toString()).replace(regex, '<span class="active">' + this.props['searchValue'] + '</span>');
return (<li onClick={this.handleClick.bind(this)} className={classes} dangerouslySetInnerHTML={{ __html: matchedString }}/>);
}
else {
return null;
}
}
return (<li onClick={this.handleClick.bind(this)} className={classes}>{this.props['children']}</li>);
}
}
Option.defaultProps = new module.Props();
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = Option;