basic.md 3.61 KB
Newer Older
ddcat1115's avatar
ddcat1115 committed
1 2
---
order: 0
3 4 5
title:
  zh-CN: 标准登录
  en-US: Standard Login
ddcat1115's avatar
ddcat1115 committed
6 7
---

8
Support login with account and mobile number.
ddcat1115's avatar
ddcat1115 committed
9 10 11 12 13 14 15 16 17 18 19 20

````jsx
import Login from 'ant-design-pro/lib/Login';
import { Alert, Checkbox } from 'antd';

const { Tab, UserName, Password, Mobile, Captcha, Submit } = Login;

class LoginDemo extends React.Component {
  state = {
    notice: '',
    type: 'tab2',
    autoLogin: true,
21
  };
ddcat1115's avatar
ddcat1115 committed
22
  onSubmit = (err, values) => {
23 24 25 26
    console.log('value collected ->', {
      ...values,
      autoLogin: this.state.autoLogin,
    });
ddcat1115's avatar
ddcat1115 committed
27
    if (this.state.type === 'tab1') {
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
      this.setState(
        {
          notice: '',
        },
        () => {
          if (
            !err &&
            (values.username !== 'admin' || values.password !== '888888')
          ) {
            setTimeout(() => {
              this.setState({
                notice:
                  'The combination of username and password is incorrect!',
              });
            }, 500);
          }
        },
      );
ddcat1115's avatar
ddcat1115 committed
46
    }
47
  };
ddcat1115's avatar
ddcat1115 committed
48 49 50
  onTabChange = (key) => {
    this.setState({
      type: key,
ddcat1115's avatar
ddcat1115 committed
51
    });
52
  };
ddcat1115's avatar
ddcat1115 committed
53 54 55
  changeAutoLogin = (e) => {
    this.setState({
      autoLogin: e.target.checked,
ddcat1115's avatar
ddcat1115 committed
56
    });
57
  };
ddcat1115's avatar
ddcat1115 committed
58 59
  render() {
    return (
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
      <div className="login-warp">
        <Login
          defaultActiveKey={this.state.type}
          onTabChange={this.onTabChange}
          onSubmit={this.onSubmit}
        >
          <Tab key="tab1" tab="Account">
            {this.state.notice && (
              <Alert
                style={{ marginBottom: 24 }}
                message={this.state.notice}
                type="error"
                showIcon
                closable
              />
            )}
            <UserName name="username" />
            <Password name="password" />
          </Tab>
          <Tab key="tab2" tab="Mobile">
            <Mobile name="mobile" />
            <Captcha
              onGetCaptcha={() => console.log('Get captcha!')}
              name="captcha"
            />
          </Tab>
          <div>
            <Checkbox
              checked={this.state.autoLogin}
              onChange={this.changeAutoLogin}
            >
              Keep me logged in
            </Checkbox>
            <a style={{ float: 'right' }} href="">
              Forgot password
            </a>
          </div>
          <Submit>Login</Submit>
          <div>
            Other login methods
            <span className="icon icon-alipay" />
            <span className="icon icon-taobao" />
            <span className="icon icon-weibo" />
            <a style={{ float: 'right' }} href="">
              Register
            </a>
          </div>
        </Login>
      </div>
ddcat1115's avatar
ddcat1115 committed
109
    );
ddcat1115's avatar
ddcat1115 committed
110 111 112 113 114 115 116
  }
}

ReactDOM.render(<LoginDemo />, mountNode);
````

<style>
117 118 119 120
#scaffold-src-components-Login-demo-basic .login-warp{
  max-width: 360px;
  margin: auto;
}
ddcat1115's avatar
ddcat1115 committed
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
#scaffold-src-components-Login-demo-basic .icon {
  display: inline-block;
  width: 24px;
  height: 24px;
  background: url('https://gw.alipayobjects.com/zos/rmsportal/itDzjUnkelhQNsycranf.svg');
  margin-left: 16px;
  vertical-align: middle;
  cursor: pointer;
}
#scaffold-src-components-Login-demo-basic .icon-alipay {
  background-position: -24px 0;
}
#scaffold-src-components-Login-demo-basic .icon-alipay:hover {
  background-position: 0 0;
}
#scaffold-src-components-Login-demo-basic .icon-taobao {
  background-position: -24px -24px;
}
#scaffold-src-components-Login-demo-basic .icon-taobao:hover {
  background-position: 0 -24px;
}
#scaffold-src-components-Login-demo-basic .icon-weibo {
  background-position: -24px -48px;
}
#scaffold-src-components-Login-demo-basic .icon-weibo:hover {
  background-position: 0 -48px;
}
</style>