Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vuetify v-text-field Default Slot Not Working

I am trying to use a custom filter with the Vuetify v-text-field control. I am having trouble getting a value to show using the default slot of the v-text-field control. It is apparently derived from v-input, which seems to work fine.

This does not work:

<v-text-field>
   {{ purchasePrice | currency }}
</v-text-field>

This works:

<v-input>
   {{ purchasePrice | currency }}
</v-input>

Am I missing a template slot or something? I've been able to successfully use the "append" and "prepend" slots on this control, but not the "default" slot. Any suggestions?

Thanks.

like image 933
connr Avatar asked Feb 20 '19 22:02

connr


2 Answers

I just ran into this as well, and did some source diving. Documenting my findings below:

As of Vuetify 2.5.8 (most recent version), and any other 2+ version, default slot is ignored on v-text-element.

The relevant part in the source code of VTextField.ts:

genDefaultSlot () {
  return [
    this.genFieldset(),
    this.genTextFieldSlot(),
    this.genClearIcon(),
    this.genIconSlot(),
    this.genProgress(),
  ]
},

it overrides genDefaultSlot method of VInput.ts, which is included as a mixin in VTextField.ts:

genDefaultSlot () {
  return [
    this.genLabel(),
    this.$slots.default,
  ]
},
like image 76
Amade Avatar answered Nov 13 '22 20:11

Amade


I could only make it work with named slots: (Also, I'm reusing this component, so it accepts another slot on the inside)

<template>
  <v-layout>
    <v-text-field
      :type="type"
      v-bind="
        // https://vuejs.org/v2/guide/components-props.html#Disabling-Attribute-Inheritance
        $attrs
      "
      @input="$emit('update', $event)"
      v-on="
        // https://vuejs.org/v2/guide/components-custom-events.html#Binding-Native-Events-to-Components
        $listeners
      "
    >
    <!-- ⬇️ HERE  ⬇️ -->
      <template v-slot:label>
        <slot></slot>
      </template>
    </v-text-field>
  </v-layout>
</template>


<script>
import { defaultMaterialTextFiledsProps } from '~/config/inputsStyle'

// See https://github.com/chrisvfritz/vue-enterprise-boilerplate/blob/master/src/components/_base-input-text.vue
export default {
  // Disable automatic attribute inheritance, so that $attrs are
  // passed to the <input>, even if it's not the root element.
  // https://vuejs.org/v2/guide/components-props.html#Disabling-Attribute-Inheritance
  inheritAttrs: false,
  props: {
    type: {
      type: String,
      default: 'text',
      // Only allow types that essentially just render text boxes.
      validator(value) {
        return [
          'email',
          'number',
          'password',
          'search',
          'tel',
          'text',
          'url'
        ].includes(value)
      }
    }
  }
}
</script>
like image 33
Paul Melero Avatar answered Nov 13 '22 22:11

Paul Melero