fix(Popover): allow to dynamically set offset prop (#10840)

This commit is contained in:
neverland 2022-07-23 18:26:15 +08:00 committed by GitHub
parent c628a66770
commit 794fd029a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 22 deletions

View File

@ -93,25 +93,31 @@ export default defineComponent({
const wrapperRef = ref<HTMLElement>();
const popoverRef = ref<ComponentInstance>();
const getPopoverOptions = () => ({
placement: props.placement,
modifiers: [
{
name: 'computeStyles',
options: {
adaptive: false,
gpuAcceleration: false,
},
},
extend({}, offsetModifier, {
options: {
offset: props.offset,
},
}),
],
});
const createPopperInstance = () => {
if (wrapperRef.value && popoverRef.value) {
return createPopper(wrapperRef.value, popoverRef.value.popupRef.value, {
placement: props.placement,
modifiers: [
{
name: 'computeStyles',
options: {
adaptive: false,
gpuAcceleration: false,
},
},
extend({}, offsetModifier, {
options: {
offset: props.offset,
},
}),
],
});
return createPopper(
wrapperRef.value,
popoverRef.value.popupRef.value,
getPopoverOptions()
);
}
return null;
};
@ -125,9 +131,7 @@ export default defineComponent({
if (!popper) {
popper = createPopperInstance();
} else {
popper.setOptions({
placement: props.placement,
});
popper.setOptions(getPopoverOptions());
}
});
};
@ -207,7 +211,7 @@ export default defineComponent({
}
});
watch(() => [props.show, props.placement], updateLocation);
watch(() => [props.show, props.offset, props.placement], updateLocation);
useClickAway(wrapperRef, onClickAway, { eventName: 'touchstart' });

View File

@ -85,6 +85,17 @@ const show = ref({
const showPicker = ref(false);
const currentPlacement = ref<PopoverPlacement>('top');
const onClickChoosePlacement = () => {
showPicker.value = true;
setTimeout(() => {
show.value = {
...show.value,
placement: true,
};
}, 300);
};
const onPickerChange = (value: PopoverPlacement) => {
setTimeout(() => {
show.value.placement = true;
@ -129,7 +140,7 @@ const onSelect = (action: { text: string }) => Toast(action.text);
readonly
name="picker"
:label="t('choosePlacement')"
@click="showPicker = true"
@click="onClickChoosePlacement"
/>
<van-popup