Skip to content

Commit

Permalink
fix(Dialog): update position when change container & append to body w…
Browse files Browse the repository at this point in the history
…hen container has been changed to null, close #935
  • Loading branch information
Javey committed Dec 27, 2023
1 parent 0844dc5 commit 81e2b62
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 9 deletions.
30 changes: 30 additions & 0 deletions components/dialog/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,36 @@ describe('Dialog', () => {
expect(dialog2.querySelector('.k-dialog-body')!.textContent).to.eql('test');
});

it('should update position when change container', async () => {
class Demo extends Component<{show: boolean, container: any}> {
static template = `
var Dialog = this.Dialog;
<Dialog value={true} container={this.get('container')} ref="dialog">test</Dialog>
`;

private Dialog = Dialog;

static defaults() {
return {
container: (parentDom: HTMLElement) => parentDom,
};
}
}

const [instance, element] = mount(Demo);

await wait();
instance.set('container', undefined);
await wait();
const dialogDom = instance.refs.dialog.dialogRef.value;
const style = dialogDom.style;
expect(style.left).not.eql('');
expect(style.top).not.eql('');

// should append to body
expect(dialogDom.closest('.k-dialog-wrapper').parentElement).to.eql(document.body);
});

// it('should handle v-if and v-model at the same time correctly in Vue', async () => {
// const Test = {
// template: `<Dialog v-model="show" v-if="show" ref="dialog">test</Dialog>`,
Expand Down
4 changes: 4 additions & 0 deletions components/dialog/usePosition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ export function usePosition(elementRef: RefObject<HTMLDivElement>) {

instance.on(SHOW, center);
instance.on('afterClose', onAfterLeave);
instance.watch('container', () => {
if (!instance.get('value')) return;
center();
}, { presented: true, inited: true });

function center() {
position(elementRef.value!, {
Expand Down
28 changes: 19 additions & 9 deletions components/dropdown/usePosition.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import {useInstance, findDomFromVNode} from 'intact';
import type {Dropdown} from './';
import {Options, position, Feedback} from '../position';
import {noop} from 'intact-shared';
import {isObject} from 'intact-shared';
import {noop, isObject, isFunction} from 'intact-shared';
import { isEqualObject } from '../utils';

export type FeedbackCallback = (feedback: Feedback) => void;
Expand All @@ -19,21 +18,32 @@ export function usePosition() {

(['of', 'position'] as const).forEach(item => {
instance.watch(item, (newValue, oldValue) => {
// return if object is the same
if (
instance.get('value') ||
// return if object is the same
isObject(newValue) && isObject(oldValue) &&
// is not event object
!(newValue instanceof Event) &&
isEqualObject(newValue, oldValue)
) {
return;
}
if (instance.get('value')) {
handle(noop);
}
) return;

handle(noop);
}, {presented: true, inited: true});
});

// watch container, it is not commonly used
instance.watch('container', (newValue, oldValue) => {
if (
!instance.get('value') ||
// return if function is the same. Not rigorous!
isFunction(newValue) &&
isFunction(oldValue) &&
newValue.toString() === oldValue.toString()
) return;

handle(noop);
}, { presented: true, inited: true });

// if the dropdown is nested, we must show child after parent has positioned
function p(
ofElement: HTMLElement | MouseEvent | undefined,
Expand Down
4 changes: 4 additions & 0 deletions components/portal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,11 @@ export class Portal<T extends PortalProps = PortalProps> extends Component<T> {
} else {
this.container = container(parentDom, anchor);
}
} else {
// let below logic to set container to default if container does not exist.
this.container = null;
}

if (!this.container) {
if (this.$senior instanceof BaseDialog) {
// Dialog and Drawer must be inserted into document.body
Expand Down

0 comments on commit 81e2b62

Please sign in to comment.