Skip to content

Commit

Permalink
🎨 chore: 优化组件架构
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx committed Jun 6, 2021
1 parent 3345abb commit 0d19251
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 64 deletions.
40 changes: 40 additions & 0 deletions packages/user-panel/src/UserLogin.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import React from 'react';
import type { FC } from 'react';
import type { LayoutProps } from './layout';
import type { LoginParamsType } from './type';
import Layout from './layout';
import Login from './login';

export interface UserLoginProps extends Omit<LayoutProps, 'type'> {
/**
* 获取校验码方法
*/
onClickCaptcha?: (mobile: string) => Promise<boolean>;
/**
* 登录方法
*/
onLoginSubmit?: (values: LoginParamsType) => Promise<void>;
}

const UserLogin: FC<UserLoginProps> = ({
onClickCaptcha,
onLoginSubmit,
logo,
logoUrl,
wechatLogin,
showFooter,
}) => {
return (
<Layout
logoUrl={logoUrl}
logo={logo}
showFooter={showFooter}
wechatLogin={wechatLogin}
type={'login'}
>
<Login onClickCaptcha={onClickCaptcha} handleSubmit={onLoginSubmit} />
</Layout>
);
};

export default UserLogin;
14 changes: 11 additions & 3 deletions packages/user-panel/src/components/Footer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,31 @@ import { Typography, Divider } from 'antd';
import WechatLogin from '../WechatLogin';

import { useFormatMessage } from '../../hooks';
import type { PanelContentType } from '../../type';

import './style.less';

const { Text } = Typography;
interface FooterProps {
type: 'login' | 'register' | 'forgot';
/**
* 类型
*/
type: PanelContentType;
/**
* 微信登录方法
*/
wechatLogin?;
}

const Footer: FC<FooterProps> = ({ type }) => {
const Footer: FC<FooterProps> = ({ type, wechatLogin }) => {
const f = useFormatMessage();

return (
<div className="avx-user-panel-footer">
<Divider dashed className="avx-user-panel-footer-divider">
{f('login.or')}
</Divider>
<WechatLogin />
{wechatLogin ? <WechatLogin login={wechatLogin} /> : null}
<div className="avx-user-panel-footer-other">
<Text>
{f(type === 'login' ? 'login.no-account' : 'register.have-account')}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('WechatLogin 组件', () => {
const fn = jest.fn();
const { findByTestId } = render(
<IntlWrapper>
<WechatLogin onClick={fn} />
<WechatLogin login={fn} />
</IntlWrapper>,
);
const btn = await findByTestId('wechat-btn');
Expand Down
8 changes: 4 additions & 4 deletions packages/user-panel/src/components/WechatLogin/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import './style.less';

interface IWechatLoginProps {
disabled?: boolean;
onClick?: () => void;
login?: () => void;
}
const WechatLogin: FC<IWechatLoginProps> = ({ onClick }) => {
const WechatLogin: FC<IWechatLoginProps> = ({ login }) => {
const f = useFormatMessage();
const handleWeChatClick = () => {
if (onClick) {
onClick();
if (login) {
login();
} else {
const errorMsg = f('login.wechat-login.function.error');

Expand Down
7 changes: 7 additions & 0 deletions packages/user-panel/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import UserLogin from './UserLogin';

const UserPanel = {
Login: UserLogin,
};

export default UserPanel;
48 changes: 0 additions & 48 deletions packages/user-panel/src/index.tsx

This file was deleted.

49 changes: 49 additions & 0 deletions packages/user-panel/src/layout/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import React from 'react';
import type { FC } from 'react';
import { IntlWrapper, Header, Footer } from '../components';
import './style.less';
import type { PanelContentType } from '../type';

export interface LayoutProps {
/**
* 微信登录方法
*/
wechatLogin?: () => void;
/**
* logo 链接
*/
logo?: string;
/**
* 点击 logo 跳转的 URL
*/
logoUrl?: string;
/**
* 显示底部
*/
showFooter?: boolean;
/**
* 内容类型
*/
type: PanelContentType;
}

const Layout: FC<LayoutProps> = ({
logo,
logoUrl,
wechatLogin,
children,
showFooter,
type,
}) => {
return (
<IntlWrapper>
<div className="avx-user-panel-container">
<Header type={type} logo={logo} url={logoUrl} />
{children}
{showFooter ? <Footer type={type} wechatLogin={wechatLogin} /> : null}
</div>
</IntlWrapper>
);
};

export default Layout;
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
&-container {
z-index: 2;
width: 480px;
padding: 36px 64px;
padding: 48px 64px;
background: white;
border-radius: 4px;
box-shadow: 0 24px 36px hsla(210, 20%, 39%, 0.16);
Expand Down
19 changes: 12 additions & 7 deletions packages/user-panel/src/login/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export type LoginProps = {
onClickCaptcha?: (mobile: string) => Promise<boolean>;
handleSubmit?: (values: LoginParamsType) => Promise<void>;
captchaCountDown?: number;
forgotUrl?: string;
};

const Login: React.FC<LoginProps> = (props) => {
Expand All @@ -30,6 +31,7 @@ const Login: React.FC<LoginProps> = (props) => {
onClickCaptcha,
handleSubmit,
captchaCountDown,
forgotUrl,
} = props;
const { status, type: loginType } = userLogin;
const [type, setType] = useState<string>('account');
Expand Down Expand Up @@ -183,13 +185,16 @@ const Login: React.FC<LoginProps> = (props) => {
<ProFormCheckbox noStyle name="autoLogin">
{f('login.rememberMe')}
</ProFormCheckbox>
<a
style={{
float: 'right',
}}
>
{f('login.forgotPassword')}
</a>
{forgotUrl ? (
<a
style={{
float: 'right',
}}
href={forgotUrl}
>
{f('login.forgotPassword')}
</a>
) : null}
</div>
</ProForm>
</div>
Expand Down
2 changes: 2 additions & 0 deletions packages/user-panel/src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export type LoginParamsType = {
captcha: string;
};

export type PanelContentType = 'login' | 'register' | 'forgot';

export type LocalesType = 'en-US' | 'zh-CN';
export type LocaleMessages = typeof messages;
export type LocaleKey = keyof LocaleMessages;

0 comments on commit 0d19251

Please sign in to comment.