HeaderClass.tsx 4.44 KB
Newer Older
mayi's avatar
mayi committed
1 2
//类组件
import * as React from "react";
3
// import { Component } from "react";
mayi's avatar
mayi committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
import logo from "../../assets/logo.svg";
import styles from "./Header.module.css";
import { withTranslation, WithTranslation } from "react-i18next";
import { connect } from "react-redux";
import { Dispatch } from "redux";
import {
  changeLanguageActionCreator,
  addLanguageActionCreator,
} from "../../redux/language/languageActions";
import {
  Layout,
  Typography,
  Input,
  Menu,
  Button,
  Dropdown,
20
  // MenuItemProps,
mayi's avatar
mayi committed
21 22 23 24 25 26 27
} from "antd";
import { GlobalOutlined } from "@ant-design/icons";
import { withRouter, RouteComponentProps } from "react-router-dom";
import  { RootState } from "../../redux/store";

const mapStateToProps = (state: RootState) => {
  return {
mayi's avatar
mayi committed
28 29
    language: state.language.language,
    languageList: state.language.languageList,
mayi's avatar
mayi committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
  };
};
const mapDispatchToProps =(dispatch:Dispatch) =>{
  return{
    changeLanguage:(code:'en'|'zh')=>{
      const action =changeLanguageActionCreator(code)
      dispatch(action)
    },
  
    addLanguage:(name:string,code:string)=>{
      const action = addLanguageActionCreator(name,code)
      dispatch(action)
    }
  }
}
class HeaderComponent extends React.Component<
  RouteComponentProps & WithTranslation & ReturnType<typeof mapStateToProps>&ReturnType<typeof mapDispatchToProps>
> {

  changeLanguage = (e) => {
    if (e.key === "new") {
      this.props.addLanguage("新语言", "新语言");
    
    } else {
      this.props.changeLanguage(e.key);
    
    }
  };
 
  render() {
    const { t } = this.props;
    console.log(this.props);
    const { history } = this.props;
    return (
      <>
        <div className={styles["app-header"]}>
          {/* top-header */}
          <div className={styles["top-header"]}>
            <div className={styles["inner"]}>
              <Typography.Text>{t("header.slogan")}</Typography.Text>
              <Dropdown.Button
                style={{ marginLeft: 15 }}
                overlay={
                  <Menu onClick={this.changeLanguage}>
                    {this.props.languageList.map((item, index) => (
                      <Menu.Item key={item.code}>{item.name}</Menu.Item>
                    ))}
                    <Menu.Item key={"new"}>{t("header.slogan")}</Menu.Item>
                  </Menu>
                }
                icon={<GlobalOutlined />}
              >
                {this.props.language === "zh" ? "中文" : "English"}
              </Dropdown.Button>
              <Button.Group className={styles["button-group"]}>
                <Button onClick={() => history.push("/register")}>
                  {t("header.register")}
                </Button>
                <Button onClick={() => history.push("/signin")}>登陆</Button>
              </Button.Group>
            </div>
          </div>
          <Layout.Header className={styles["main-header"]}>
            <span>
              <img src={logo} alt="logo" className={styles["App-logo"]} />
              <Typography.Title level={3} className={styles.title}>
                {t("header.title")}
              </Typography.Title>
            </span>

            <Input.Search
              placeholder={"请输入旅游目的地、主题、或关键字"}
              className={styles["search-input"]}
            />
          </Layout.Header>
          <Menu mode={"horizontal"} className={styles["main-menu"]}>
            <Menu.Item key={1}>旅游首页</Menu.Item>
            <Menu.Item key={2}>周末游</Menu.Item>
            <Menu.Item key={3}>跟团游</Menu.Item>
            <Menu.Item key="4"> 自由行 </Menu.Item>
            <Menu.Item key="5"> 私家团 </Menu.Item>
            <Menu.Item key="6"> 邮轮 </Menu.Item>
            <Menu.Item key="7"> 酒店+景点 </Menu.Item>
            <Menu.Item key="8"> 当地玩乐 </Menu.Item>
            <Menu.Item key="9"> 主题游 </Menu.Item>
            <Menu.Item key="10"> 定制游 </Menu.Item>
            <Menu.Item key="11"> 游学 </Menu.Item>
            <Menu.Item key="12"> 签证 </Menu.Item>
            <Menu.Item key="13"> 企业游 </Menu.Item>
            <Menu.Item key="14"> 高端游 </Menu.Item>
            <Menu.Item key="15"> 爱玩户外 </Menu.Item>
            <Menu.Item key="16"> 保险 </Menu.Item>
          </Menu>
        </div>
      </>
    );
  }
}
export const Header = connect(mapStateToProps,mapDispatchToProps)(
  withTranslation()(withRouter(HeaderComponent))
);
131 132
//6.4日在函数组建里面用connect
//重学redux封装部分