yearSelect.jsx 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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 (nextProps.defValue) {
  25. this.state.Year = nextProps.defValue;
  26. };
  27. this.state.Year = nextProps.value;
  28. },
  29. render() {
  30. return (
  31. <Select value={this.state.Year} className="year-select"
  32. placeholder="请选择年份"
  33. style={{ width: 200 }}
  34. onChange={this.onChange}>
  35. {this.state.theOption}
  36. </Select>
  37. )
  38. }
  39. });
  40. export default Yearselect;