mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-09-08 00:39:46 +08:00
feat(ActionBar): use relation
This commit is contained in:
parent
1f82bcf5f0
commit
ff3a5d66ab
@ -1,13 +1,13 @@
|
|||||||
|
import { ref, computed } from 'vue';
|
||||||
import { createNamespace } from '../utils';
|
import { createNamespace } from '../utils';
|
||||||
import { route, routeProps } from '../utils/router';
|
import { route, routeProps } from '../utils/router';
|
||||||
import { ChildrenMixin } from '../mixins/relation';
|
import { useChildren } from '../api/use-relation';
|
||||||
|
import { ACTION_BAR_KEY } from '../action-bar';
|
||||||
import Button from '../button';
|
import Button from '../button';
|
||||||
|
|
||||||
const [createComponent, bem] = createNamespace('action-bar-button');
|
const [createComponent, bem] = createNamespace('action-bar-button');
|
||||||
|
|
||||||
export default createComponent({
|
export default createComponent({
|
||||||
mixins: [ChildrenMixin('vanActionBar')],
|
|
||||||
|
|
||||||
props: {
|
props: {
|
||||||
...routeProps,
|
...routeProps,
|
||||||
type: String,
|
type: String,
|
||||||
@ -19,23 +19,38 @@ export default createComponent({
|
|||||||
},
|
},
|
||||||
|
|
||||||
setup(props, { slots }) {
|
setup(props, { slots }) {
|
||||||
|
const { children, index } = useChildren(ACTION_BAR_KEY, ref(true));
|
||||||
|
|
||||||
|
const isFirst = computed(() => {
|
||||||
|
if (children) {
|
||||||
|
const prev = children.value[index.value - 1];
|
||||||
|
return !(prev && prev.value);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const isLast = computed(() => {
|
||||||
|
if (children) {
|
||||||
|
const next = children.value[index.value + 1];
|
||||||
|
return !(next && next.value);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
return (vm) => {
|
return (vm) => {
|
||||||
const { type, icon, text, color, loading, disabled } = props;
|
const { type, icon, text, color, loading, disabled } = props;
|
||||||
|
|
||||||
const { parent } = vm;
|
|
||||||
const { name } = vm.$options;
|
|
||||||
const prev = parent && parent.children[vm.index - 1];
|
|
||||||
const next = parent && parent.children[vm.index + 1];
|
|
||||||
const last = !next || name !== next.$options.name;
|
|
||||||
const first = !prev || name !== prev.$options.name;
|
|
||||||
|
|
||||||
const onClick = () => {
|
const onClick = () => {
|
||||||
route(vm.$router, vm);
|
route(vm.$router, vm);
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Button
|
<Button
|
||||||
class={bem([type, { last, first }])}
|
class={bem([
|
||||||
|
type,
|
||||||
|
{
|
||||||
|
last: isLast.value,
|
||||||
|
first: isFirst.value,
|
||||||
|
},
|
||||||
|
])}
|
||||||
size="large"
|
size="large"
|
||||||
type={type}
|
type={type}
|
||||||
icon={icon}
|
icon={icon}
|
||||||
|
@ -1,14 +1,14 @@
|
|||||||
|
import { ref } from 'vue';
|
||||||
import { createNamespace } from '../utils';
|
import { createNamespace } from '../utils';
|
||||||
import { route, routeProps } from '../utils/router';
|
import { route, routeProps } from '../utils/router';
|
||||||
import { ChildrenMixin } from '../mixins/relation';
|
import { useChildren } from '../api/use-relation';
|
||||||
|
import { ACTION_BAR_KEY } from '../action-bar';
|
||||||
import Icon from '../icon';
|
import Icon from '../icon';
|
||||||
import Badge from '../badge';
|
import Badge from '../badge';
|
||||||
|
|
||||||
const [createComponent, bem] = createNamespace('action-bar-icon');
|
const [createComponent, bem] = createNamespace('action-bar-icon');
|
||||||
|
|
||||||
export default createComponent({
|
export default createComponent({
|
||||||
mixins: [ChildrenMixin('vanActionBar')],
|
|
||||||
|
|
||||||
props: {
|
props: {
|
||||||
...routeProps,
|
...routeProps,
|
||||||
dot: Boolean,
|
dot: Boolean,
|
||||||
@ -20,6 +20,8 @@ export default createComponent({
|
|||||||
},
|
},
|
||||||
|
|
||||||
setup(props, { slots }) {
|
setup(props, { slots }) {
|
||||||
|
useChildren(ACTION_BAR_KEY, ref());
|
||||||
|
|
||||||
function genIcon() {
|
function genIcon() {
|
||||||
const { dot, badge, icon, color, iconClass } = props;
|
const { dot, badge, icon, color, iconClass } = props;
|
||||||
|
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
import { createNamespace } from '../utils';
|
import { createNamespace } from '../utils';
|
||||||
import { ParentMixin } from '../mixins/relation';
|
import { useParent } from '../api/use-relation';
|
||||||
|
|
||||||
const [createComponent, bem] = createNamespace('action-bar');
|
const [createComponent, bem] = createNamespace('action-bar');
|
||||||
|
|
||||||
export default createComponent({
|
export const ACTION_BAR_KEY = 'vanActionBar';
|
||||||
mixins: [ParentMixin('vanActionBar')],
|
|
||||||
|
|
||||||
|
export default createComponent({
|
||||||
props: {
|
props: {
|
||||||
safeAreaInsetBottom: {
|
safeAreaInsetBottom: {
|
||||||
type: Boolean,
|
type: Boolean,
|
||||||
@ -14,6 +14,8 @@ export default createComponent({
|
|||||||
},
|
},
|
||||||
|
|
||||||
setup(props, { slots }) {
|
setup(props, { slots }) {
|
||||||
|
useParent(ACTION_BAR_KEY);
|
||||||
|
|
||||||
return () => (
|
return () => (
|
||||||
<div class={bem({ unfit: !props.safeAreaInsetBottom })}>
|
<div class={bem({ unfit: !props.safeAreaInsetBottom })}>
|
||||||
{slots.default?.()}
|
{slots.default?.()}
|
||||||
|
@ -16,7 +16,8 @@ export function useParent(key: string) {
|
|||||||
|
|
||||||
export function useChildren(key: string, child: unknown) {
|
export function useChildren(key: string, child: unknown) {
|
||||||
const parent = inject<Parent>(key, null);
|
const parent = inject<Parent>(key, null);
|
||||||
const index = computed(() => parent?.children.value.indexOf(child));
|
const children = parent?.children;
|
||||||
|
const index = computed(() => children?.value.indexOf(child));
|
||||||
|
|
||||||
if (parent) {
|
if (parent) {
|
||||||
parent.children.value.push(child);
|
parent.children.value.push(child);
|
||||||
@ -24,6 +25,6 @@ export function useChildren(key: string, child: unknown) {
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
index,
|
index,
|
||||||
parent,
|
children,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user