refactor: breadcrumb
parent
6ad0f9dc9b
commit
0fa1581a99
|
@ -1,6 +1,6 @@
|
||||||
import { cloneVNode, defineComponent, PropType, ExtractPropTypes } from 'vue';
|
import { cloneVNode, defineComponent, PropType, ExtractPropTypes } from 'vue';
|
||||||
import PropTypes from '../_util/vue-types';
|
import PropTypes from '../_util/vue-types';
|
||||||
import { filterEmpty, getPropsSlot } from '../_util/props-util';
|
import { flattenChildren, getPropsSlot } from '../_util/props-util';
|
||||||
import warning from '../_util/warning';
|
import warning from '../_util/warning';
|
||||||
import BreadcrumbItem from './BreadcrumbItem';
|
import BreadcrumbItem from './BreadcrumbItem';
|
||||||
import Menu from '../menu';
|
import Menu from '../menu';
|
||||||
|
@ -17,7 +17,7 @@ const breadcrumbProps = {
|
||||||
prefixCls: PropTypes.string,
|
prefixCls: PropTypes.string,
|
||||||
routes: { type: Array as PropType<Route[]> },
|
routes: { type: Array as PropType<Route[]> },
|
||||||
params: PropTypes.any,
|
params: PropTypes.any,
|
||||||
separator: PropTypes.VNodeChild,
|
separator: PropTypes.any,
|
||||||
itemRender: {
|
itemRender: {
|
||||||
type: Function as PropType<
|
type: Function as PropType<
|
||||||
(opt: { route: Route; params: unknown; routes: Route[]; paths: string[] }) => VueNode
|
(opt: { route: Route; params: unknown; routes: Route[]; paths: string[] }) => VueNode
|
||||||
|
@ -53,6 +53,7 @@ function defaultItemRender(opt: {
|
||||||
export default defineComponent({
|
export default defineComponent({
|
||||||
name: 'ABreadcrumb',
|
name: 'ABreadcrumb',
|
||||||
props: breadcrumbProps,
|
props: breadcrumbProps,
|
||||||
|
slots: ['separator', 'itemRender'],
|
||||||
setup(props, { slots }) {
|
setup(props, { slots }) {
|
||||||
const { prefixCls, direction } = useConfigInject('breadcrumb', props);
|
const { prefixCls, direction } = useConfigInject('breadcrumb', props);
|
||||||
|
|
||||||
|
@ -122,10 +123,10 @@ export default defineComponent({
|
||||||
|
|
||||||
const { routes, params = {} } = props;
|
const { routes, params = {} } = props;
|
||||||
|
|
||||||
const children = filterEmpty(getPropsSlot(slots, props));
|
const children = flattenChildren(getPropsSlot(slots, props));
|
||||||
const separator = getPropsSlot(slots, props, 'separator');
|
const separator = getPropsSlot(slots, props, 'separator') ?? '/';
|
||||||
|
|
||||||
const itemRender = getPropsSlot(slots, props, 'itemRender') || defaultItemRender;
|
const itemRender = props.itemRender || slots.itemRender || defaultItemRender;
|
||||||
if (routes && routes.length > 0) {
|
if (routes && routes.length > 0) {
|
||||||
// generated by route
|
// generated by route
|
||||||
crumbs = genForRoutes({
|
crumbs = genForRoutes({
|
||||||
|
@ -153,5 +154,4 @@ export default defineComponent({
|
||||||
return <div class={breadcrumbClassName}>{crumbs}</div>;
|
return <div class={breadcrumbClassName}>{crumbs}</div>;
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
methods: {},
|
|
||||||
});
|
});
|
||||||
|
|
|
@ -8,8 +8,8 @@ import useConfigInject from '../_util/hooks/useConfigInject';
|
||||||
const breadcrumbItemProps = {
|
const breadcrumbItemProps = {
|
||||||
prefixCls: PropTypes.string,
|
prefixCls: PropTypes.string,
|
||||||
href: PropTypes.string,
|
href: PropTypes.string,
|
||||||
separator: PropTypes.VNodeChild.def('/'),
|
separator: PropTypes.any,
|
||||||
overlay: PropTypes.VNodeChild,
|
overlay: PropTypes.any,
|
||||||
};
|
};
|
||||||
|
|
||||||
export type BreadcrumbItemProps = Partial<ExtractPropTypes<typeof breadcrumbItemProps>>;
|
export type BreadcrumbItemProps = Partial<ExtractPropTypes<typeof breadcrumbItemProps>>;
|
||||||
|
@ -17,6 +17,7 @@ export default defineComponent({
|
||||||
name: 'ABreadcrumbItem',
|
name: 'ABreadcrumbItem',
|
||||||
__ANT_BREADCRUMB_ITEM: true,
|
__ANT_BREADCRUMB_ITEM: true,
|
||||||
props: breadcrumbItemProps,
|
props: breadcrumbItemProps,
|
||||||
|
slots: ['separator', 'overlay'],
|
||||||
setup(props, { slots }) {
|
setup(props, { slots }) {
|
||||||
const { prefixCls } = useConfigInject('breadcrumb', props);
|
const { prefixCls } = useConfigInject('breadcrumb', props);
|
||||||
/**
|
/**
|
||||||
|
@ -39,11 +40,11 @@ export default defineComponent({
|
||||||
};
|
};
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
const separator = getPropsSlot(slots, props, 'separator');
|
const separator = getPropsSlot(slots, props, 'separator') ?? '/';
|
||||||
const children = getPropsSlot(slots, props);
|
const children = getPropsSlot(slots, props);
|
||||||
let link: JSX.Element;
|
let link: JSX.Element;
|
||||||
|
|
||||||
if ('href' in props) {
|
if (props.href !== undefined) {
|
||||||
link = <a class={`${prefixCls.value}-link`}>{children}</a>;
|
link = <a class={`${prefixCls.value}-link`}>{children}</a>;
|
||||||
} else {
|
} else {
|
||||||
link = <span class={`${prefixCls.value}-link`}>{children}</span>;
|
link = <span class={`${prefixCls.value}-link`}>{children}</span>;
|
||||||
|
@ -54,9 +55,7 @@ export default defineComponent({
|
||||||
return (
|
return (
|
||||||
<span>
|
<span>
|
||||||
{link}
|
{link}
|
||||||
{separator && separator !== '' && (
|
{separator && <span class={`${prefixCls.value}-separator`}>{separator}</span>}
|
||||||
<span class={`${prefixCls.value}-separator`}>{separator}</span>
|
|
||||||
)}
|
|
||||||
</span>
|
</span>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import { defineComponent, ExtractPropTypes } from 'vue';
|
import { defineComponent, ExtractPropTypes } from 'vue';
|
||||||
import PropTypes from '../_util/vue-types';
|
import PropTypes from '../_util/vue-types';
|
||||||
import { getPropsSlot } from '../_util/props-util';
|
import { flattenChildren } from '../_util/props-util';
|
||||||
import useConfigInject from '../_util/hooks/useConfigInject';
|
import useConfigInject from '../_util/hooks/useConfigInject';
|
||||||
|
|
||||||
const breadcrumbSeparator = {
|
const breadcrumbSeparator = {
|
||||||
|
@ -18,8 +18,7 @@ export default defineComponent({
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
const { separator, class: className, ...restAttrs } = attrs;
|
const { separator, class: className, ...restAttrs } = attrs;
|
||||||
const children = getPropsSlot(slots, props) || [];
|
const children = flattenChildren(slots.default?.());
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<span class={[`${prefixCls.value}-separator`, className]} {...restAttrs}>
|
<span class={[`${prefixCls.value}-separator`, className]} {...restAttrs}>
|
||||||
{children.length > 0 ? children : '/'}
|
{children.length > 0 ? children : '/'}
|
||||||
|
|
|
@ -38,7 +38,8 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
&-link {
|
&-link {
|
||||||
> .@{iconfont-css-prefix} + span {
|
> .@{iconfont-css-prefix} + span,
|
||||||
|
> .@{iconfont-css-prefix} + a {
|
||||||
margin-left: 4px;
|
margin-left: 4px;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -49,3 +50,5 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@import './rtl';
|
||||||
|
|
|
@ -0,0 +1,29 @@
|
||||||
|
.@{breadcrumb-prefix-cls} {
|
||||||
|
&-rtl {
|
||||||
|
.clearfix();
|
||||||
|
direction: rtl;
|
||||||
|
|
||||||
|
> span {
|
||||||
|
float: right;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
&-link {
|
||||||
|
> .@{iconfont-css-prefix} + span,
|
||||||
|
> .@{iconfont-css-prefix} + a {
|
||||||
|
.@{breadcrumb-prefix-cls}-rtl & {
|
||||||
|
margin-right: 4px;
|
||||||
|
margin-left: 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
&-overlay-link {
|
||||||
|
> .@{iconfont-css-prefix} {
|
||||||
|
.@{breadcrumb-prefix-cls}-rtl & {
|
||||||
|
margin-right: 4px;
|
||||||
|
margin-left: 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue