fix: page-header

pull/2377/head^2
tangjinzhou 2020-06-10 23:06:43 +08:00
parent 723bd2832f
commit 36673e3d03
3 changed files with 45 additions and 35 deletions

View File

@ -96,7 +96,26 @@ const getSlotOptions = ele => {
return componentOptions ? componentOptions.Ctor.options || {} : {}; return componentOptions ? componentOptions.Ctor.options || {} : {};
}; };
const getOptionProps = instance => { const getOptionProps = instance => {
return (instance.$ && instance.$.vnode ? instance.$.vnode.props : instance.props) || {}; const res = {};
if (instance.$ && instance.$.vnode) {
const props = instance.$.vnode.props || {};
Object.keys(instance.$props).forEach(k => {
const v = instance.$props[k];
if (v !== undefined || k in props) {
res[k] = v;
}
});
} else if (isVNode(instance) && typeof instance.type === 'object') {
const props = instance.props || {};
const allProps = instance.type.props;
Object.keys(allProps).forEach(k => {
const v = allProps[k].default;
if (v !== undefined || k in props) {
res[k] = v;
}
});
}
return res;
}; };
const getComponent = (instance, prop, options = instance, execute = true) => { const getComponent = (instance, prop, options = instance, execute = true) => {
const temp = instance[prop]; const temp = instance[prop];

View File

@ -14,6 +14,8 @@ const inlineStyle = {
}; };
const TransButton = { const TransButton = {
name: 'TransButton',
inheritAttrs: false,
props: { props: {
noStyle: PropTypes.bool, noStyle: PropTypes.bool,
}, },
@ -34,18 +36,18 @@ const TransButton = {
}, },
setRef(btn) { setRef(btn) {
this.div = btn; this.$refs.div = btn;
}, },
focus() { focus() {
if (this.div) { if (this.$refs.div) {
this.div.focus(); this.$refs.div.focus();
} }
}, },
blur() { blur() {
if (this.div) { if (this.$refs.div) {
this.div.blur(); this.$refs.div.blur();
} }
}, },
}, },
@ -57,22 +59,13 @@ const TransButton = {
<div <div
role="button" role="button"
tabIndex={0} tabIndex={0}
{...{ ref="div"
directives: [ {...this.$attrs}
{ onKeydown={this.onKeyDown}
name: 'ant-ref', onKeyup={this.onKeyUp}
value: this.setRef,
},
],
on: {
...this.$listeners,
keydown: this.onKeyDown,
keyup: this.onKeyUp,
},
}}
style={{ ...(!noStyle ? inlineStyle : null) }} style={{ ...(!noStyle ? inlineStyle : null) }}
> >
{this.$slots.default} {this.$slots.default && this.$slots.default()}
</div> </div>
); );
}, },

View File

@ -22,14 +22,13 @@ export const PageHeaderProps = {
}; };
const renderBack = (instance, prefixCls, backIcon, onBack) => { const renderBack = (instance, prefixCls, backIcon, onBack) => {
// eslint-disable-next-line no-unused-vars
const h = instance.$createElement;
if (!backIcon || !onBack) { if (!backIcon || !onBack) {
return null; return null;
} }
return ( return (
<LocaleReceiver componentName="PageHeader"> <LocaleReceiver
{({ back }) => ( componentName="PageHeader"
children={({ back }) => (
<div class={`${prefixCls}-back`}> <div class={`${prefixCls}-back`}>
<TransButton <TransButton
onClick={e => { onClick={e => {
@ -42,15 +41,15 @@ const renderBack = (instance, prefixCls, backIcon, onBack) => {
</TransButton> </TransButton>
</div> </div>
)} )}
</LocaleReceiver> ></LocaleReceiver>
); );
}; };
const renderBreadcrumb = (h, breadcrumb) => { const renderBreadcrumb = breadcrumb => {
return <Breadcrumb {...breadcrumb} />; return <Breadcrumb {...breadcrumb} />;
}; };
const renderTitle = (h, prefixCls, instance) => { const renderTitle = (prefixCls, instance) => {
const { avatar } = instance; const { avatar } = instance;
const title = getComponent(instance, 'title'); const title = getComponent(instance, 'title');
const subTitle = getComponent(instance, 'subTitle'); const subTitle = getComponent(instance, 'subTitle');
@ -80,14 +79,14 @@ const renderTitle = (h, prefixCls, instance) => {
return null; return null;
}; };
const renderFooter = (h, prefixCls, footer) => { const renderFooter = (prefixCls, footer) => {
if (footer) { if (footer) {
return <div class={`${prefixCls}-footer`}>{footer}</div>; return <div class={`${prefixCls}-footer`}>{footer}</div>;
} }
return null; return null;
}; };
const renderChildren = (h, prefixCls, children) => { const renderChildren = (prefixCls, children) => {
return <div class={`${prefixCls}-content`}>{children}</div>; return <div class={`${prefixCls}-content`}>{children}</div>;
}; };
@ -99,13 +98,12 @@ const PageHeader = {
configProvider: inject('configProvider', ConfigConsumerProps), configProvider: inject('configProvider', ConfigConsumerProps),
}; };
}, },
render(h) { render() {
const { getPrefixCls, pageHeader } = this.configProvider; const { getPrefixCls, pageHeader } = this.configProvider;
const props = getOptionProps(this); const props = getOptionProps(this);
const { prefixCls: customizePrefixCls, breadcrumb } = props; const { prefixCls: customizePrefixCls, breadcrumb } = props;
const footer = getComponent(this, 'footer'); const footer = getComponent(this, 'footer');
const children = this.$slots.default && this.$slots.default(); const children = this.$slots.default && this.$slots.default();
let ghost = true; let ghost = true;
// Use `ghost` from `props` or from `ConfigProvider` instead. // Use `ghost` from `props` or from `ConfigProvider` instead.
@ -117,7 +115,7 @@ const PageHeader = {
const prefixCls = getPrefixCls('page-header', customizePrefixCls); const prefixCls = getPrefixCls('page-header', customizePrefixCls);
const breadcrumbDom = const breadcrumbDom =
breadcrumb && breadcrumb.props && breadcrumb.props.routes breadcrumb && breadcrumb.props && breadcrumb.props.routes
? renderBreadcrumb(h, breadcrumb) ? renderBreadcrumb(breadcrumb)
: null; : null;
const className = [ const className = [
prefixCls, prefixCls,
@ -131,9 +129,9 @@ const PageHeader = {
return ( return (
<div class={className}> <div class={className}>
{breadcrumbDom} {breadcrumbDom}
{renderTitle(h, prefixCls, this)} {renderTitle(prefixCls, this)}
{children && renderChildren(h, prefixCls, children)} {children && children.length && renderChildren(prefixCls, children)}
{renderFooter(h, prefixCls, footer)} {renderFooter(prefixCls, footer)}
</div> </div>
); );
}, },