I am interested in utilizing Vue Fallthrough attributes, but I specifically want them to be applied only to the input elements within the component and not on the container itself

I am facing an issue with my checkbox component. I want to utilize Fallthrough attributes to pass non-declared attributes to the input inside the checkbox component. However, when I add HTML attributes to the Vue component, those attributes are applied not only to the input within the container but also to the container element of the checkbox component.

Here is the HTML code for my component:

    <template>
      <div class="form-checkbox">
        <v-label
          v-if="legend"
          :suffix="legendSuffix"
          :for="$attrs.id || uid"
          :helper="helperText"
        >
          {{ legend }}
        </v-label>
    
        <div class="form-input__wrapper">
          <input
            :id="$attrs.id || uid"
            ref="checkboxRef"
            type="checkbox"
            v-bind="$attrs"
            v-model="inputVal"
            @input="onInput"
          />
    
          <label :for="$attrs.id || uid">{{ label }}</label>
        </div>
    
      </div>
    </template>
    
    <script lang="ts" src="./Checkbox.ts" />

And here is the TypeScript code for my component:

export default defineComponent({
  props: {
    legend: {
      type: String,
      default: ''
    },
    legendSuffix: {
      type: String,
      default: ''
    },
    helperText: { type: String, default: '' },
    label: { type: String, default: '' },
    modelValue: {
      type: [String, Number],
      default: ''
    }
  },

  emits: ['error', 'input-event', 'mounted'],

  setup(props, { emit }) {
    const checkboxRef = ref();
    const inputVal = ref();
    const uid = ref(uniqueId());

    watch(
      () => props.modelValue,
      () => {
        inputVal.value = String(props.modelValue);
      },
      { immediate: true }
    );

    const onInput = (event: CustomEvent): void => {
      emit('input-event', event);
    };

    onMounted(() => emit('mounted', unref(checkboxRef)));

    return {
      checkboxRef,
      inputVal,
      uid,
      onInput
    };
  }
});

When I include the component like this, the ID property gets added to both the container and the checkbox input:

    <v-checkbox
      id="test"
      :label="test"
    />

I am looking for a solution to prevent these attributes from being duplicated on the container and the input, as having the ID used twice is causing issues.

Answer №1

To prevent attribute inheritance, consult the documentation for guidance

https://vuejs.org/guide/components/attrs.html#disabling-attribute-inheritance

Disabling Attribute Inheritance {#disabling-attribute-inheritance}

If you do not wish for a component to automatically inherit attributes, you can specify inheritAttrs: false in the component's options.

Starting from version 3.3, you also have the option to utilize defineOptions directly inside <script setup>:

<script setup>
defineOptions({
  inheritAttrs: false
})
// ...setup logic
</script>

A common use case for disabling attribute inheritance is when attributes need to be applied to elements other than the root node. By setting the inheritAttrs option to false, you gain full control over where the inherited attributes should be assigned.

You can access these inherited attributes directly in template expressions using $attrs:

<span>Inherited Attributes: {{ $attrs }}</span>

The $attrs object contains all attributes that are not defined in the component's props or emits options (e.g., class, style, v-on listeners, etc.).

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Error encountered: `npm ERR! code E503`

While attempting to execute npm install on my project, which was cloned from my GitHub repository, I encountered the following error: npm ERR! code E503 npm ERR! 503 Maximum threads for service reached: fs-extra@https://registry.npmjs.org/fs-extra/-/fs-ex ...

Submitting the form leads to an empty dynamically added row

I am currently working on a gender overview that allows you to edit, add, or delete genders using a simple table. The functionality of adding and deleting rows is already implemented. However, I am facing issues with displaying the correct web API data as ...

What is the best way to display a Nested JSON structure without an object key?

Need help with extracting data from two different JSON structures. The first one is straightforward, but the second is nested in multiple arrays. How can I access the content? See below for the code snippets: // First JSON { "allSuSa": [ { ...

How can you optimize the storage of keys in JS objects?

Just pondering over this scenario: Consider a line definition like the one below, where start and end are both points. let ln = { s: {x:0, y:0}, e: {x:0, y:0}, o: 'vertical' } Now imagine having a vast array of lines, how can we sav ...

Error: Unable to locate module: Could not find 'react-server-dom-webpack/client.edge'

I've been trying to incorporate server components into my nextJS project, but I keep encountering an issue when using "use server" in my component. Error message: `./node_modules/next/dist/build/webpack/loaders/next-flight-loader/action-client-wrappe ...

Enhance your React Highchart by incorporating gradient shading to the data points

One interesting feature in classic highcharts is the ability to apply gradient coloring to points: Highcharts.setOptions({ colors: Highcharts.getOptions().colors.map(function (color) { return { radialGradient: { cx: ...

Issue with Laravel Pusher integration and Vue.js not functioning correctly

Recently, I started using Laravel Pusher with Vue.js and followed this tutorial to create a chat application. Unfortunately, the chat functionality is not working correctly as users are unable to receive messages from each other. image description can be ...

Is there a way to update Checkbox changes within a Datagrid without selecting the entire row?

My Table Cell Checkbox Behavior Issue: Within a table cell, I have a checkbox that changes upon clicking it. However, the change only occurs the first time. Subsequent clicks on the same checkbox do not trigger any change until I click outside the cell. T ...

Is it possible to deactivate the error message related to "Unable to ascertain the module for component..."?

I recently incorporated a new component into my TypeScript2+Angular2+Ionic2 project. Right now, I have chosen not to reference it anywhere in the project until it is fully developed. However, there seems to be an error thrown by Angular/ngc stating "Cannot ...

Is there a way for me to retrieve the bodyHeight attribute of ag-grid using public variables or data?

Working on a project using ag-grid community react. The main feature is a scrollable section filled with data, which can range from one piece to millions of pieces. I'm also incorporating a footer component for the grid that needs to adjust its height ...

How can I combine multiple styles using Material-UI themes in TypeScript?

There are two different styles implementations in my code. The first one is located in global.ts: const globalStyles = (theme: Theme) => { return { g: { marginRight: theme.spacing(40), }, } } export const mergedStyle = (params: any) ...

Issues with encapsulating a nested* object of properties in Vue 2.6 (Revised)

As I work on iterating through an array of objects using a simple v-for, I encounter an issue with setting the data- attributes dynamically for an anchor tag within the loop. I try to bind an object of attributes but upon reloading npm, I receive an error ...

Why does "excess property checking" seem pleased when I utilize a key from set A or set B, even though "keyof (A|B)" is consistently "never"?

I am diving deeper into Typescript types and encountering some puzzling behavior: interface Person { name: string; } interface Lifespan { birth: number; death?: number; } let k: keyof (Person | Lifespan); //k is never let test1: Person | Life ...

Having trouble declaring a module in an npm package with Typescript?

I'm currently working on a project using Vue.js and TypeScript. Within project "A," I am utilizing a private npm package called "B," which serves as a component library. This package "B" also incorporates another library, 'tiptap,' which unf ...

The absence of typings.json in Typescript is creating an issue

As of now, I am encountering the following error during compilation: typings.json is missing In my existing packages.json, I have included the following dependency: "devDependencies": { "typescript": "^2.6.1", ... } Do you have any suggestion ...

Is it possible to utilize Vue 3's watch feature with primitive data types?

When I retrieve data, I want to toggle a boolean value to display the <Loading /> component. I prefer not to base my condition on array length. Therefore, I have opted for this approach. However, the <Loading /> component does not respond to ch ...

How do I fix the build error that says "Operator '+' cannot be used with types 'number[]'?

The function below is designed to generate unique uuidv4 strings. function uuidv4() { return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c => ( c ^ (crypto.getRandomValues(new Uint8Array(1))[0] & (15 >> (c / 4)) ...

Issue with Angular ngFor binding. What could be causing this error to occur?

I have a main component called DOCUMENT. This document receives a URL segment and retrieves an array of associated objects from my database. Then, using @Output() documents = new EventEmitter() and an @Input() in a DOCUMENT VIEW component, I loop through t ...

Learn how to dynamically pass a value from a prop to a router-link in Vue.js

I created a custom button component and decided to switch from using <a> tags to <router-link>. However, I encountered an error because the router-link was rendering before the prop received its value. To address this, I added an if statement b ...

Adding two comparable Objects in Javascript / vuejs

I have two objects and I want to add the values of each key separately with another object. Here's an example: CharacterStats: { a: 0, b: 2, c: 0, d: 0 } ItemStats: { a: 0, b: -1, c: 4, d: 0 } The expected result is: CharacterStats: { a: 0, b: 1, c: ...