# Template
@@ -26,8 +27,8 @@ const DemoComponent = {
type="primary"
onClick={() => {
state.name = new Date().getTime().toString()
- routeContext.collapsed = !routeContext.collapsed
- routeContext.menuData = [
+ context.collapsed = !context.collapsed
+ context.menuData = [
{
path: `/dashboard/${state.name}`,
name: `${state.name}`,
@@ -42,11 +43,11 @@ const DemoComponent = {
state.name: { JSON.stringify(state.name) }
routeContext:
-
{ JSON.stringify(routeContext, null, 4) }
+
{ JSON.stringify(context, null, 4) }
-
+
diff --git a/examples/side-menu.tsx b/examples/side-menu.tsx
index 4a48eb13..1d59a2d3 100644
--- a/examples/side-menu.tsx
+++ b/examples/side-menu.tsx
@@ -4,76 +4,94 @@ import 'ant-design-vue/dist/antd.less';
import './side-menu.less';
import { Layout, Input, Space, Switch, message } from 'ant-design-vue';
import { menus } from './menus';
+import { RouterLink } from './mock-router';
import { default as SiderMenuWrapper } from '../src/SiderMenu';
-import { useMenu } from '../src/hooks/useMenu';
+import { createRouteContext, RouteContextProps } from '../src/RouteContext';
import * as Icon from '@ant-design/icons-vue';
import { MenuTheme } from '../src/typings';
const DemoComponent = {
setup() {
- const [menuState] = useMenu({
+ const state = reactive
({
collapsed: false,
- openKeys: [''],
+
+ openKeys: ['/dashboard', '/form'],
+ setOpenKeys: (keys: string[]) => (state.openKeys = keys),
selectedKeys: ['/welcome'],
- });
- const state = reactive({
+ setSelectedKeys: (keys: string[]) => (state.selectedKeys = keys),
+
+ isMobile: false,
+ fixSiderbar: false,
+ fixedHeader: false,
+ menuData: [...menus],
+ sideWidth: 208,
+ hasSideMenu: true,
+ hasHeader: true,
+ hasFooterToolbar: false,
+ setHasFooterToolbar: (has: boolean) => (state.hasFooterToolbar = has),
+ })
+ const [ RouteContextProvider ] = createRouteContext();
+
+ const myState = reactive({
theme: 'light',
});
const handleCollapse = (collapsed: boolean) => {
- menuState.collapsed = collapsed;
+ state.collapsed = collapsed;
}
return () => (
# SideMenu
-
+
- { (state.theme = state.theme === 'dark' ? 'light' : 'dark')}} />
+ { (myState.theme = myState.theme === 'dark' ? 'light' : 'dark')}} />
@@ -90,4 +108,4 @@ Object.keys(Icon)
app.component(Icon[k].displayName, Icon[k]);
});
-app.mount('#__vue-content>div');
+app.use(RouterLink).mount('#__vue-content>div');
diff --git a/src/RouteContext.tsx b/src/RouteContext.tsx
index 8e7f7db5..dc3218d5 100644
--- a/src/RouteContext.tsx
+++ b/src/RouteContext.tsx
@@ -2,11 +2,13 @@ import { InjectionKey, VNodeChild } from 'vue';
import { createContext, useContext } from './hooks/context';
import { MenuDataItem } from './typings';
import { PureSettings } from './defaultSettings';
+
export interface Route {
path: string;
breadcrumbName: string;
children?: Omit
[];
}
+
export interface BreadcrumbProps {
prefixCls?: string;
routes?: Route[];
@@ -18,10 +20,10 @@ export interface BreadcrumbProps {
export type BreadcrumbListReturn = Pick>;
export interface MenuState {
- selectedKeys?: string[];
- openKeys?: string[];
- onSelectedKeys: (key: string[]) => void;
- onOpenKeys: (key: string[]) => void;
+ selectedKeys: string[];
+ openKeys: string[];
+ setSelectedKeys?: (key: string[]) => void;
+ setOpenKeys?: (key: string[]) => void;
}
export interface RouteContextProps extends Partial, MenuState {
@@ -42,8 +44,8 @@ export interface RouteContextProps extends Partial, MenuState {
const routeContextInjectKey: InjectionKey = Symbol();
-export const createRouteContext = (context: RouteContextProps) =>
- createContext(context, routeContextInjectKey);
+export const createRouteContext = () =>
+ createContext(routeContextInjectKey);
export const useRouteContext = () =>
useContext(routeContextInjectKey);
diff --git a/src/SiderMenu/BaseMenu.tsx b/src/SiderMenu/BaseMenu.tsx
index 597a335b..b0a15f67 100644
--- a/src/SiderMenu/BaseMenu.tsx
+++ b/src/SiderMenu/BaseMenu.tsx
@@ -167,10 +167,8 @@ export default defineComponent({
emits: ['update:openKeys', 'update:selectedKeys'],
setup(props, { emit }) {
const { mode, i18n } = toRefs(props);
- const state = useMenuState();
const isInline = computed(() => mode.value === 'inline');
const handleOpenChange: OpenEventHandler = (openKeys: string[]): void => {
- state?.setOpenKeys(openKeys);
emit('update:openKeys', openKeys);
};
const handleSelect = (params: {
@@ -180,7 +178,6 @@ export default defineComponent({
domEvent: MouseEvent;
selectedKeys: string[];
}): void => {
- state?.setSelectedKeys(params.selectedKeys);
emit('update:selectedKeys', params.selectedKeys);
};
return () => (
@@ -190,8 +187,8 @@ export default defineComponent({
inlineIndent={16}
mode={props.mode}
theme={props.theme as 'dark' | 'light'}
- openKeys={props.openKeys || state?.openKeys.value || []}
- selectedKeys={props.selectedKeys || state?.selectedKeys.value || []}
+ openKeys={props.openKeys || []}
+ selectedKeys={props.selectedKeys || []}
onOpenChange={handleOpenChange}
onSelect={handleSelect}
>
diff --git a/src/SiderMenu/SiderMenu.tsx b/src/SiderMenu/SiderMenu.tsx
index 84b26169..c2ef2ecc 100644
--- a/src/SiderMenu/SiderMenu.tsx
+++ b/src/SiderMenu/SiderMenu.tsx
@@ -133,10 +133,10 @@ const SiderMenu: FunctionalComponent = (props: SiderMenuProps) =
class={`${baseClassName}-menu`}
{...{
'onUpdate:openKeys': ($event: string[]) => {
- context.onOpenKeys($event);
+ context?.setOpenKeys($event);
},
'onUpdate:selectedKeys': ($event: string[]) => {
- context.onSelectedKeys($event);
+ context?.setSelectedKeys($event);
},
}}
/>
diff --git a/src/hooks/context/index.ts b/src/hooks/context/index.ts
index 891eb91b..5f74976c 100644
--- a/src/hooks/context/index.ts
+++ b/src/hooks/context/index.ts
@@ -3,47 +3,50 @@ import {
InjectionKey,
provide,
inject,
- reactive,
readonly,
SetupContext,
- UnwrapRef,
VNode,
+ PropType,
DefineComponent,
- toRaw,
} from 'vue';
export type ContextType = any;
export type CreateContext = [
- UnwrapRef | T,
+ // UnwrapRef | T,
DefineComponent<{}, () => VNode | VNode[] | undefined, any>,
];
export const createContext = (
- context: ContextType,
+ // context: ContextType,
contextInjectKey: InjectionKey> = Symbol(),
): CreateContext => {
- const state = reactive>({
- ...toRaw(context),
- });
+ // const state = reactive>({
+ // ...toRaw(context),
+ // });
const ContextProvider = defineComponent({
name: 'ContextProvider',
- inheritAttrs: false,
- setup(props, { slots }: SetupContext) {
- provide(contextInjectKey, readonly(state));
+ props: {
+ value: {
+ type: Object as PropType>,
+ required: true,
+ },
+ },
+ setup(props: { value: ContextType }, { slots }: SetupContext) {
+ provide(contextInjectKey, readonly(props.value));
return () => slots.default?.();
},
});
- return [state, ContextProvider];
+ return [ContextProvider];
};
export const useContext = (
contextInjectKey: InjectionKey> = Symbol(),
defaultValue?: ContextType,
): T => {
- return readonly(inject(contextInjectKey, defaultValue || ({} as T)));
+ return inject(contextInjectKey, defaultValue || ({} as T));
};
// :: examples ::