Commit 0a386582 authored by jim's avatar jim Committed by ι™ˆεΈ…

chang to new context api

parent b3821d53
import React, { Component } from 'react'; import React, { Component } from 'react';
import PropTypes from 'prop-types'; import { Form, Input, Button, Row, Col } from 'antd';
import { Form, Button, Row, Col } from 'antd';
import omit from 'omit.js'; import omit from 'omit.js';
import styles from './index.less'; import styles from './index.less';
import map from './map'; import ItemMap from './map';
import LoginContext from './loginContext';
const FormItem = Form.Item; const FormItem = Form.Item;
function generator({ defaultProps, defaultRules, type }) { class WarpFormItem extends Component {
return WrappedComponent => {
return class BasicComponent extends Component {
static contextTypes = {
form: PropTypes.object,
updateActive: PropTypes.func,
};
constructor(props) { constructor(props) {
super(props); super(props);
this.state = { this.state = {
...@@ -21,8 +15,8 @@ function generator({ defaultProps, defaultRules, type }) { ...@@ -21,8 +15,8 @@ function generator({ defaultProps, defaultRules, type }) {
}; };
} }
componentDidMount() { componentDidMount() {
if (this.context.updateActive) { if (this.props.updateActive) {
this.context.updateActive(this.props.name); this.props.updateActive(this.props.name);
} }
} }
componentWillUnmount() { componentWillUnmount() {
...@@ -42,28 +36,46 @@ function generator({ defaultProps, defaultRules, type }) { ...@@ -42,28 +36,46 @@ function generator({ defaultProps, defaultRules, type }) {
} }
}, 1000); }, 1000);
}; };
render() { getFormItemOptions = ({ onChange, defaultValue, rules }) => {
const { getFieldDecorator } = this.context.form; const options = {
const options = {}; rules: rules || this.customprops.rules,
let otherProps = {}; };
const { onChange, defaultValue, rules, name, ...restProps } = this.props;
const { count } = this.state;
options.rules = rules || defaultRules;
if (onChange) { if (onChange) {
options.onChange = onChange; options.onChange = onChange;
} }
if (defaultValue) { if (defaultValue) {
options.initialValue = defaultValue; options.initialValue = defaultValue;
} }
otherProps = restProps || otherProps; return options;
if (type === 'Captcha') { };
render() {
const { count } = this.state;
const { getFieldDecorator } = this.props.form;
// θΏ™δΉˆε†™ζ˜―δΈΊδΊ†ι˜²ζ­’restPropsδΈ­ εΈ¦ε…₯ onChange, defaultValue, rules props
const {
onChange,
customprops,
defaultValue,
rules,
name,
updateActive,
...restProps
} = this.props;
// get getFieldDecorator props
const options = this.getFormItemOptions(this.props);
const otherProps = restProps || {};
if (this.props.type === 'Captcha') {
const inputProps = omit(otherProps, ['onGetCaptcha']); const inputProps = omit(otherProps, ['onGetCaptcha']);
return ( return (
<FormItem> <FormItem>
<Row gutter={8}> <Row gutter={8}>
<Col span={16}> <Col span={16}>
{getFieldDecorator(name, options)( {getFieldDecorator(name, options)(
<WrappedComponent {...defaultProps} {...inputProps} /> <Input {...this.props.customprops} {...inputProps} />
)} )}
</Col> </Col>
<Col span={8}> <Col span={8}>
...@@ -82,23 +94,31 @@ function generator({ defaultProps, defaultRules, type }) { ...@@ -82,23 +94,31 @@ function generator({ defaultProps, defaultRules, type }) {
} }
return ( return (
<FormItem> <FormItem>
{getFieldDecorator(name, options)( {getFieldDecorator(name, options)(<Input {...this.props.customprops} {...otherProps} />)}
<WrappedComponent {...defaultProps} {...otherProps} />
)}
</FormItem> </FormItem>
); );
} }
};
};
} }
const LoginItem = {}; const LoginItem = {};
Object.keys(map).forEach(item => { Object.keys(ItemMap).forEach(key => {
LoginItem[item] = generator({ const item = ItemMap[key];
defaultProps: map[item].props, LoginItem[key] = props => {
defaultRules: map[item].rules, return (
type: item, <LoginContext.Consumer>
})(map[item].component); {context => (
<WarpFormItem
customprops={item.props}
{...props}
rules={item.rules}
type={key}
updateActive={context.updateActive}
form={context.form}
/>
)}
</LoginContext.Consumer>
);
};
}); });
export default LoginItem; export default LoginItem;
import React, { Component } from 'react'; import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { Tabs } from 'antd'; import { Tabs } from 'antd';
import LoginContext from './loginContext';
const { TabPane } = Tabs; const { TabPane } = Tabs;
...@@ -12,21 +12,24 @@ const generateId = (() => { ...@@ -12,21 +12,24 @@ const generateId = (() => {
}; };
})(); })();
export default class LoginTab extends Component { class LoginTab extends Component {
static __ANT_PRO_LOGIN_TAB = true;
static contextTypes = {
tabUtil: PropTypes.object,
};
constructor(props) { constructor(props) {
super(props); super(props);
this.uniqueId = generateId('login-tab-'); this.uniqueId = generateId('login-tab-');
} }
componentWillMount() { componentDidMount() {
if (this.context.tabUtil) { this.props.tabUtil.addTab(this.uniqueId);
this.context.tabUtil.addTab(this.uniqueId);
}
} }
render() { render() {
return <TabPane {...this.props} />; return <TabPane {...this.props} />;
} }
} }
const warpContext = props => {
return (
<LoginContext.Consumer>
{value => <LoginTab tabUtil={value.tabUtil} {...props} />}
</LoginContext.Consumer>
);
};
export default warpContext;
...@@ -6,6 +6,7 @@ import LoginItem from './LoginItem'; ...@@ -6,6 +6,7 @@ import LoginItem from './LoginItem';
import LoginTab from './LoginTab'; import LoginTab from './LoginTab';
import LoginSubmit from './LoginSubmit'; import LoginSubmit from './LoginSubmit';
import styles from './index.less'; import styles from './index.less';
import LoginContext from './loginContext';
@Form.create() @Form.create()
class Login extends Component { class Login extends Component {
...@@ -21,17 +22,18 @@ class Login extends Component { ...@@ -21,17 +22,18 @@ class Login extends Component {
onTabChange: PropTypes.func, onTabChange: PropTypes.func,
onSubmit: PropTypes.func, onSubmit: PropTypes.func,
}; };
static childContextTypes = {
tabUtil: PropTypes.object,
form: PropTypes.object,
updateActive: PropTypes.func,
};
state = { state = {
type: this.props.defaultActiveKey, type: this.props.defaultActiveKey,
tabs: [], tabs: [],
active: {}, active: {},
}; };
getChildContext() { onSwitch = type => {
this.setState({
type,
});
this.props.onTabChange(type);
};
getContext = () => {
return { return {
tabUtil: { tabUtil: {
addTab: id => { addTab: id => {
...@@ -58,12 +60,6 @@ class Login extends Component { ...@@ -58,12 +60,6 @@ class Login extends Component {
}); });
}, },
}; };
}
onSwitch = type => {
this.setState({
type,
});
this.props.onTabChange(type);
}; };
handleSubmit = e => { handleSubmit = e => {
e.preventDefault(); e.preventDefault();
...@@ -83,17 +79,18 @@ class Login extends Component { ...@@ -83,17 +79,18 @@ class Login extends Component {
return; return;
} }
// eslint-disable-next-line // eslint-disable-next-line
if (item.type.__ANT_PRO_LOGIN_TAB) { if (item.type.name === 'warpContext') {
TabChildren.push(item); TabChildren.push(item);
} else { } else {
otherChildren.push(item); otherChildren.push(item);
} }
}); });
return ( return (
<LoginContext.Provider value={this.getContext()}>
<div className={classNames(className, styles.login)}> <div className={classNames(className, styles.login)}>
<Form onSubmit={this.handleSubmit}> <Form onSubmit={this.handleSubmit}>
{tabs.length ? ( {tabs.length ? (
<div> <>
<Tabs <Tabs
animated={false} animated={false}
className={styles.tabs} className={styles.tabs}
...@@ -103,12 +100,13 @@ class Login extends Component { ...@@ -103,12 +100,13 @@ class Login extends Component {
{TabChildren} {TabChildren}
</Tabs> </Tabs>
{otherChildren} {otherChildren}
</div> </>
) : ( ) : (
[...children] [...children]
)} )}
</Form> </Form>
</div> </div>
</LoginContext.Provider>
); );
} }
} }
......
import { createContext } from 'react';
const LoginContext = createContext();
export default LoginContext;
import React from 'react'; import React from 'react';
import { Input, Icon } from 'antd'; import { Icon } from 'antd';
import styles from './index.less'; import styles from './index.less';
const map = { export default {
UserName: { UserName: {
component: Input,
props: { props: {
size: 'large', size: 'large',
prefix: <Icon type="user" className={styles.prefixIcon} />, prefix: <Icon type="user" className={styles.prefixIcon} />,
...@@ -18,7 +17,6 @@ const map = { ...@@ -18,7 +17,6 @@ const map = {
], ],
}, },
Password: { Password: {
component: Input,
props: { props: {
size: 'large', size: 'large',
prefix: <Icon type="lock" className={styles.prefixIcon} />, prefix: <Icon type="lock" className={styles.prefixIcon} />,
...@@ -33,7 +31,6 @@ const map = { ...@@ -33,7 +31,6 @@ const map = {
], ],
}, },
Mobile: { Mobile: {
component: Input,
props: { props: {
size: 'large', size: 'large',
prefix: <Icon type="mobile" className={styles.prefixIcon} />, prefix: <Icon type="mobile" className={styles.prefixIcon} />,
...@@ -51,7 +48,6 @@ const map = { ...@@ -51,7 +48,6 @@ const map = {
], ],
}, },
Captcha: { Captcha: {
component: Input,
props: { props: {
size: 'large', size: 'large',
prefix: <Icon type="mail" className={styles.prefixIcon} />, prefix: <Icon type="mail" className={styles.prefixIcon} />,
...@@ -65,5 +61,3 @@ const map = { ...@@ -65,5 +61,3 @@ const map = {
], ],
}, },
}; };
export default map;
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment