Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(drawer): no default config when using component mode #535

Merged
merged 4 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/stale-dots-speak.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@alauda/ui': patch
---

- fix(drawer): no default config when using component mode
37 changes: 25 additions & 12 deletions src/drawer/drawer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,6 @@ import { DrawerRef } from './drawer-ref';
import { DrawerOptions, DrawerSize } from './types';

const DRAWER_OVERLAY_CLASS = 'aui-drawer-overlay';
const DEFAULT_OPTIONS: DrawerOptions = {
size: DrawerSize.Medium,
offsetY: '0',
showClose: true,
hideOnClickOutside: false,
divider: true,
disposeWhenHide: true,
};

@Injectable()
export class DrawerService<
Expand All @@ -31,6 +23,15 @@ export class DrawerService<
DrawerInternalComponent<T, C>
>;

private readonly DEFAULT_OPTIONS: DrawerOptions<T, C> = {
JounQin marked this conversation as resolved.
Show resolved Hide resolved
size: DrawerSize.Medium,
offsetY: '0',
showClose: true,
hideOnClickOutside: false,
divider: true,
disposeWhenHide: true,
};

constructor(private readonly overlay: Overlay) {}

open(options: DrawerOptions<T, C>) {
Expand All @@ -46,10 +47,7 @@ export class DrawerService<
}

updateOptions(options: DrawerOptions<T, C>): void {
this.options = {
...(DEFAULT_OPTIONS as DrawerOptions<T, C>),
...options,
};
this.options = merge<DrawerOptions<T, C>>(this.DEFAULT_OPTIONS, options);
}

private createOverlay() {
Expand Down Expand Up @@ -135,3 +133,18 @@ export class DrawerService<
this.dispose();
}
}

function merge<T extends object>(target: T, source: T) {
return Object.keys(source).reduce(
(acc, _key) => {
const key = _key as keyof T;
if (source[key] !== undefined) {
acc[key] = source[key];
}
return acc;
},
{
...target,
},
);
}
Loading