Home > Blockchain >  How to open/close dialog in quasar using props correctly?
How to open/close dialog in quasar using props correctly?

Time:03-22

Hi I stucked with quasar dialog handling events. First of all I have one dialog component ( for cookies where user can click two buttons - one for accept cookie and one for show more). When user click on showMore it should open next dialog with some basic text and few buttons (that s not important for this issue).

So FirstDialog is parrent and second one is child. I tried to create ref in parrent (isOpen = true) and I send it to child. Problem is that props are read-only and when I clicked on screen outside from dialog I got warning about it. It is becouse there is native action to hide dialog (I need this action stay worked) So I can´t do it this way :/

I am really stuck long time on this so thx for any helps :/

Parrent

<template>
  <q-dialog :model-value='props.showCookies' position='bottom' persistent>
    <q-card style='max-width: 1920px; width:100%; background-color: rgba(77,80,83,0.9490196078431372 )'>
      <q-card-section>
        <div>
           ....
          <div class='flex' style='justify-content: right'>
            <!-- This is the button to open dialog-->
            <q-btn rounded color='grey-5' label='Zjistit více' class='cookie-button q-mr-sm' @click='showMore' />
            <q-btn rounded color='grey-5' label='Povolit vše' class='cookie-button' @click='acceptCookies' />
          </div>
        </div>
      </q-card-section>
    </q-card>
  </q-dialog>
  <CookiesDialogWhichWeUse :isOpen='isOpenCookieSettingsWhichWeUse' />
</template>

<script lang='ts'>
import { SetupContext, ref } from 'vue';
import CookiesDialogWhichWeUse from '@/components/Cookies/CookiesDialogWhichWeUse.vue';

export default {
  name: 'CookiesModal',
  emits: ['accept-cookies'],
  components: {
    CookiesDialogWhichWeUse
  },
  props: {
    showCookies: { type: Boolean, required: true }
  },
  setup(props: { showCookies: boolean }, { emit }: SetupContext) {
    const isOpenCookieSettingModal = ref(false);
    const isOpenCookieSettingsWhichWeUse = ref(false);

    const acceptCookies = () => {
      emit('accept-cookies');
    };

    const openCookiesSettings = (value: boolean) => {
      isOpenCookieSettingModal.value = value;
    };

    const showMore = () => {
      console.log('test');
      isOpenCookieSettingsWhichWeUse.value = true;
    };

    return {
      props,
      acceptCookies,
      showMore,
      openCookiesSettings,
      isOpenCookieSettingModal,
      isOpenCookieSettingsWhichWeUse
    };
  }
};
</script>

<style scoped>

</style>

ChildDialog

<template>
  <q-dialog v-model='open'>
    <q-card
      style='max-width: 761px; width:100%; max-height: 820px; height:100%; background-color: rgba(77,80,83,0.9490196078431372 )'>

      <q-card-section class='row items-center no-wrap'>
          ...
      </q-card-section>
    </q-card>
  </q-dialog>
</template>

<script>
import { toRef } from 'vue';
export default {
  name: 'CookiesDialogWhichWeUse',
  props: {
    isOpen: { type: Boolean, required: true }
  },
  setup(props) {
    const open = toRef(props, 'isOpen');

    return { open };
  }
};
</script>

<style scoped>
.cookie-text p {
  font-size: 22px;
}
</style>

CodePudding user response:

As by default Quasar dialogs can be closed by some user actions (click outside, pressing Esc key) props are usually not enough - you need events too. Imho the preferred way is to use v-model

<!-- ChildDialog -->
<template>
  <q-dialog v-model='model'>
    <q-card
      style='max-width: 761px; width:100%; max-height: 820px; height:100%; background-color: rgba(77,80,83,0.9490196078431372 )'>

      <q-card-section class='row items-center no-wrap'>
          ...
      </q-card-section>
    </q-card>
  </q-dialog>
</template>

<script>
import { computed } from 'vue';
export default {
  name: 'CookiesDialogWhichWeUse',
  props: {
    modelValue: { type: Boolean, required: true }
  },
  emits: ['update:modelValue'],
  setup(props, { emit }) {

    const model = computed({
      get() { return props.modelValue },
      set(newValue) { emit('update:modelValue', newValue) }
    })

    return { model };
  }
};
</script>

And usage:

<CookiesDialogWhichWeUse v-model='isOpenCookieSettingsWhichWeUse' />
  • Related