yearSelect.jsx 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import React from 'react';
  2. import { Select } from 'antd';
  3. const Yearselect = React.createClass({
  4. getInitialState: function () {
  5. return { theOption: [] };
  6. },
  7. onChange(e) {
  8. this.props.handleyear(e);
  9. this.setState({
  10. Year: e
  11. });
  12. },
  13. componentWillMount() {
  14. const Option = Select.Option;
  15. const d = new Date();
  16. const thisYear = d.getFullYear();
  17. this.state.thisYear = d.getFullYear();
  18. for (let i = thisYear; i >= 1950; i--) {
  19. this.state.theOption.push(<Option key={i.toString()}>{i}</Option>)
  20. };
  21. this.state.defYear = this.state.thisYear.toString();
  22. },
  23. componentWillUpdate(nextProps) {
  24. if (this.props.defValue != nextProps.defValue) {
  25. this.state.Year = nextProps.defValue;
  26. };
  27. },
  28. render() {
  29. return (
  30. <Select value={this.state.Year} className="year-select"
  31. placeholder="请选择年份"
  32. style={{ width: 200 }}
  33. onChange={this.onChange}>
  34. {this.state.theOption}
  35. </Select>
  36. )
  37. }
  38. });
  39. export default Yearselect;