Skip to content

Commit

Permalink
Merge pull request #26 from ldabiralai/master
Browse files Browse the repository at this point in the history
Update to es6 class syntax and prop-types
  • Loading branch information
afc163 committed Apr 27, 2017
2 parents 2f9cf00 + 8752948 commit f0a936c
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 55 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Expand Up @@ -7,7 +7,7 @@ notifications:
- yiminghe@gmail.com

node_js:
- 4.0.0
- 6.0.0

before_install:
- |
Expand All @@ -34,4 +34,4 @@ env:

matrix:
allow_failures:
- env: "TEST_TYPE=saucelabs"
- env: "TEST_TYPE=saucelabs"
31 changes: 15 additions & 16 deletions examples/multiple.js
Expand Up @@ -2,33 +2,32 @@
import Dropdown from 'rc-dropdown';
import Menu, { Item as MenuItem, Divider } from 'rc-menu';
import 'rc-dropdown/assets/index.less';
import React from 'react';
import React, { Component } from 'react';
import ReactDOM from 'react-dom';

const Test = React.createClass({
getInitialState() {
this.selected = [];
return {
visible: false,
};
},
class Test extends Component {
state = {
visible: false,
};

onVisibleChange(visible) {
onVisibleChange = (visible) => {
this.setState({
visible,
});
},
}

selected = [];

saveSelected({ selectedKeys }) {
saveSelected = ({ selectedKeys }) => {
this.selected = selectedKeys;
},
}

confirm() {
confirm = () => {
console.log(this.selected);
this.setState({
visible: false,
});
},
}

render() {
const menu = (
Expand Down Expand Up @@ -67,8 +66,8 @@ const Test = React.createClass({
<button>open</button>
</Dropdown>
);
},
});
}
}

ReactDOM.render(<div style={{ margin: 20 }}>
<Test/>
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -55,6 +55,7 @@
"lint"
],
"dependencies": {
"prop-types": "^15.5.8",
"rc-trigger": "1.x"
}
}
76 changes: 39 additions & 37 deletions src/Dropdown.jsx
@@ -1,4 +1,5 @@
import React, { PropTypes } from 'react';
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import ReactDOM from 'react-dom';
import Trigger from 'rc-trigger';
import placements from './placements';
Expand All @@ -13,8 +14,8 @@ import placements from './placements';
</DropDown>
*/

const Dropdown = React.createClass({
propTypes: {
class Dropdown extends Component {
static propTypes = {
minOverlayWidthMatchTrigger: PropTypes.bool,
onVisibleChange: PropTypes.func,
prefixCls: PropTypes.string,
Expand All @@ -29,45 +30,46 @@ const Dropdown = React.createClass({
showAction: PropTypes.array,
hideAction: PropTypes.array,
getPopupContainer: PropTypes.func,
},
visible: PropTypes.bool,
defaultVisible: PropTypes.bool,
};

getDefaultProps() {
return {
minOverlayWidthMatchTrigger: true,
prefixCls: 'rc-dropdown',
trigger: ['hover'],
showAction: [],
hideAction: [],
overlayClassName: '',
overlayStyle: {},
defaultVisible: false,
onVisibleChange() {
},
placement: 'bottomLeft',
};
},
static defaultProps = {
minOverlayWidthMatchTrigger: true,
prefixCls: 'rc-dropdown',
trigger: ['hover'],
showAction: [],
hideAction: [],
overlayClassName: '',
overlayStyle: {},
defaultVisible: false,
onVisibleChange() {
},
placement: 'bottomLeft',
}

getInitialState() {
const props = this.props;
constructor(props) {
super(props);
if ('visible' in props) {
return {
this.state = {
visible: props.visible,
};
} else {
this.state = {
visible: props.defaultVisible,
};
}
return {
visible: props.defaultVisible,
};
},
}

componentWillReceiveProps({ visible }) {
if (visible !== undefined) {
this.setState({
visible,
});
}
},
}

onClick(e) {
onClick = (e) => {
const props = this.props;
const overlayProps = props.overlay.props;
// do no call onVisibleChange, if you need click to hide, use onClick and control visible
Expand All @@ -79,39 +81,39 @@ const Dropdown = React.createClass({
if (overlayProps.onClick) {
overlayProps.onClick(e);
}
},
}

onVisibleChange(visible) {
onVisibleChange = (visible) => {
const props = this.props;
if (!('visible' in props)) {
this.setState({
visible,
});
}
props.onVisibleChange(visible);
},
}

getMenuElement() {
const props = this.props;
return React.cloneElement(props.overlay, {
prefixCls: `${props.prefixCls}-menu`,
onClick: this.onClick,
});
},
}

getPopupDomNode() {
return this.refs.trigger.getPopupDomNode();
},
}

afterVisibleChange(visible) {
afterVisibleChange = (visible) => {
if (visible && this.props.minOverlayWidthMatchTrigger) {
const overlayNode = this.getPopupDomNode();
const rootNode = ReactDOM.findDOMNode(this);
if (rootNode.offsetWidth > overlayNode.offsetWidth) {
overlayNode.style.width = `${rootNode.offsetWidth}px`;
}
}
},
}

render() {
const {
Expand Down Expand Up @@ -142,7 +144,7 @@ const Dropdown = React.createClass({
onPopupVisibleChange={this.onVisibleChange}
getPopupContainer={getPopupContainer}
>{children}</Trigger>);
},
});
}
}

export default Dropdown;

0 comments on commit f0a936c

Please sign in to comment.