No More Posting New Topics!

If you have a question or an issue, please start a thread in our Github Discussions Forum.
This forum is closed for new threads/ topics.

Navigation

    Quasar Framework

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search

    Label Outside of Input?

    Help
    2
    2
    741
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • J
      JoshKisb last edited by

      Is there an easy way to have the label outside the input. like most common forms do

      1 Reply Last reply Reply Quote 0
      • R
        rodrigosaling last edited by rodrigosaling

        Hey @JoshKisb ! Maybe this is a very late response, but this is a code that you can start working with:

        <template>
          <div>
            <label class="label" :class="{ 'has-error': hasError }" :for="id">
              {{ label }}
            </label>
            <q-input
              outlined
              dense
              :for="id"
              :id="id"
              :rules="rules"
              lazy-rules
              v-bind="$attrs"
              v-on="$listeners"
              @input="onInput"
              @blur="onBlur"
            />
          </div>
        </template>
        
        <script>
        const HInput = {
          name: 'HInput',
        
          props: ['label', 'id', 'rules'],
        
          data() {
            return {
              hasError: false,
            };
          },
        
          methods: {
            onInput(value) {
              this.checkInputAgainstRules(value, this.rules);
            },
        
            onBlur(event) {
              this.checkInputAgainstRules(event.target.value, this.rules);
            },
        
            checkInputAgainstRules(value, rules) {
              this.hasError = rules.some((rule) => rule(value) !== true);
            },
          },
        };
        
        export default HInput;
        </script>
        
        <style scoped lang="scss">
        .label {
          display: block;
          margin-bottom: 3px;
        
          &.has-error {
            color: $negative;
          }
        }
        </style>
        

        Then you use it like the default q-input:

            <h-input
              v-model.trim="username"
              label="Username"
              id="username"
              :rules="[
                (val) => (val && val.length > 0) || 'Required field',
                (val) => val.length <= 10 || 'Maximum of 10 characters',
              ]"
            />
        
        1 Reply Last reply Reply Quote 0
        • First post
          Last post