简体   繁体   中英

Quasar, Input Clearable, Show "clear" button only when in focus

I'm using q-input from Quasar framework. I made the "clear" button from this website -> https://quasar.dev/vue-components/input#clearable

How to make the "clear" button is hidden when there is no focus in the "input" field. And make the "clear" button is displayed when you focus in the "input" field. (using Quasar framework)

The "clear" button: is a small icon of a cross that shows in the input field.

HTML

<div id="q-app" style="min-height: 100vh;">
  <div class="q-pa-md">
    <div class="q-gutter-y-md column" style="max-width: 300px">
      <!-- equivalent -->
      <q-input color="orange" filled v-model="text" label="Label">
        <template v-if="text" v-slot:append>
          <q-icon name="cancel" @click.stop.prevent="text = null" class="cursor-pointer"></q-icon>
        </template>
      </q-input>
    </div>
    <div class="q-gutter-y-md column" style="max-width: 300px">
      <!-- equivalent -->
      <q-input color="orange" filled v-model="text" label="Label">
        <template v-if="text" v-slot:append>
          <q-icon name="cancel" @click.stop.prevent="text = null" class="cursor-pointer"></q-icon>
        </template>
      </q-input>
    </div>
    <div class="q-gutter-y-md column" style="max-width: 300px">
      <!-- equivalent -->
      <q-input color="orange" filled v-model="text" label="Label">
        <template v-if="text" v-slot:append>
          <q-icon name="cancel" @click.stop.prevent="text = null" class="cursor-pointer"></q-icon>
        </template>
      </q-input>
    </div>
  </div>
</div>

JS + Quasar

const { ref } = Vue

const app = Vue.createApp({
  setup () {
    return {
      text: ref('Some text')
    }
  }
})

app.use(Quasar, { config: {} })
app.mount('#q-app')

Now the input fields look like this (The "clear" button is displayed regardless of focus):

在此处输入图像描述

I want it to look like this (The "clear" button only in the field in which there is focus) 在此处输入图像描述

How can this be done?

Please refer following code.

    <q-input color="orange" filled v-model="text" label="Label" @focus="focus=true" @blur="focus=false">
      <template v-slot:append>
        <q-icon name="cancel" v-if="text && focus" @click.stop.prevent="text = null"
                class="cursor-pointer"></q-icon>
      </template>
    </q-input>

https://codepen.io/Pratik__007/pen/RwBoLQb

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM