DiscoverField Component with Button Functionality and Checkbox Dilemma

I'm working with Vue 3, Vuetify, and TypeScript for my searchField component. Inside, I have two buttons: FreeItemMaster and PatternMaster. Clicking on these buttons should change their background color and display respective content below them. However, the code isn't behaving as expected, and the checkbox feature isn't functioning either. What could be causing this issue? Below is how I envision the screen after clicking each button: https://i.stack.imgur.com/Gsg7N.png(https://i.stack.imgur.com/Ih3bS.png)(https://i.stack.imgur.com/Gsg7N.png)

<template>
    <div class="search-fields">
      <div class="search-condition mt-4">
        <div class="d-flex ml-6">
            <button :class="{'dark-grey-button': isFreeItemMasterBtnClicked, 'light-grey-button': !isFreeItemMasterBtnClicked}" @click="isFreeItemMasterBtnClicked = true; isPatternMasterBtnClicked = false">FreeItemMaster</button>
            <button :class="{'dark-grey-button': isPatternMasterBtnClicked, 'light-grey-button': !isPatternMasterBtnClicked}" @click="isPatternMasterBtnClicked = true; isFreeItemMasterBtnClicked = false">PatternMaster</button>
        </div>
        <div class="d-flex" v-if="isFreeItemMasterBtnClicked">
          <!-- FreeItemMasterBtn content -->
          <div class="ml-3 mt-5">
            <v-checkbox
              dense
              hide-details
              label="FreeItemMaster content"
            />
          </div>
          <div class="ml-5 mt-7">
            <v-btn rounded small style="background-color: #1ea0dc; color: #ffffff"
              >Search</v-btn
            >
          </div>
        </div>
        <div class="d-flex" v-if="isPatternMasterBtnClicked">
          <!-- PatternMasterBtn content -->
          <div class="ml-3 mt-5">
            <v-checkbox
              dense
              hide-details
              label="PatternMaster content"
            />
          </div>
          <div class="ml-5 mt-7">
            <v-btn rounded small style="background-color: #1ea0dc; color: #ffffff"
              >Search</v-btn
            >
          </div>
        </div>
      </div>
    </div>
  </template>
  
  <script setup lang="ts">
  import { ref } from 'vue';
  const isFreeItemMasterBtnClicked = ref(false);
  const isPatternMasterBtnClicked = ref(true);
  </script>
  
  <style lang="scss" scoped>
  .search-fields {
    display: flex;
    justify-content: space-between;
    .search-condition {
      p {
        font-size: 14px;
        font-weight: bold;
      }
    }
  }
  
  .dark-grey-button {
    background-color: #51565b;
    color: white;
    padding: 10px 10px;
    border: none;
    text-align: center;
    display: inline-block;
    font-size: 14px;
    margin-right: 2px;
  }
  
  .light-grey-button {
    background-color: #f0f3f4;
    color: black;
    padding: 10px 10px;
    border: none;
    text-align: center;
    display: inline-block;
    font-size: 14px;
    margin-right: 10px;
  }
  </style>

Answer №1

Consider implementing functions to handle the button clicks:

<div class="d-flex ml-6">
            <button :class="{'dark-grey-button': isHomeBtnClicked, 'light-grey-button': !isHomeBtnClicked}" @click="onHomeClick()">Home</button>
            <button :class="{'dark-grey-button': isProfileBtnClicked, 'light-grey-button': !isProfileBtnClicked}" @click="onProfileClick()">Profile</button>
        </div>
// inside setup script
  const onHomeClick = () => {
      isHomeBtnClicked.value = true;
      isProfileBtnClicked.value = false;
  };

  const onProfileClick = () => {
      isHomeBtnClicked.value = false;
      isProfileBtnClicked.value = true;
  };

If you only need to handle two buttons, a single boolean variable should suffice. It will simplify the code.

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

Changing the data type of a column in an Excel file from XLSX to

I am currently working with the XLSX npm package and attempting to download a sample Excel file, add some data to it, and then upload it back. The fields in the file include MOBILE NUMBER, DATE, TIME, and NAME. When I upload the file, the values for the DA ...

`Browser Extension Compatibility``

I am currently working on developing a TypeScript extension that is compatible with all major browsers. I have come across the package https://www.npmjs.com/package/web-ext-types which I have integrated into my package.json file. While coding in TypeScrip ...

Retrieve Firestore Information in Vue Component Before Loading Using Vuefire

My goal is to populate a table with names and profile pictures of people. The names are retrieved from a Firestore database, while the profile pictures are fetched from a Firebase Storage bucket using the corresponding name. After hours of watching videos ...

The data type '{ [key: string]: number; }' cannot be assigned to type 'T'

I’m working with a complex TypeScript type and trying to manage it within a function. Here’s what I have: type T = { a: number } | { b: number } function func(k: 'a' | 'b', v: number) { // error message below const t: T = { ...

Avoid refreshing the page when clicking on an anchor tag in Vue.js

I have the code below in my Vue file. The problem I am encountering is that the page reloads whenever I click on the link. I have tried using event.preventDefault() but it did not solve the issue. Can someone please help me identify what I am doing wrong ...

Encountered difficulty locating the module path 'stream/promises'

When importing the following in a typescript nodejs app import { pipeline } from "stream/promises"; Visual Studio Code (vscode) / eslint is showing an error message Unable to resolve path to module 'stream/promises' This issue appeare ...

Having trouble implementing the latest Angular Library release

Just starting out with publishing Angular libraries, I've made my first attempt to publish a lib on NPM called wps-ng https://www.npmjs.com/package/wps-ng. You can check out my Public API file here https://github.com/singkara/wps-js-ng/blob/library_t ...

Is there a way to retrieve the modal's viewport height in Angular?

Is it possible to determine the viewport height of my ng bootstrap modal within my Angular application? Here is what I currently have: I have a modal with CSS styling as shown below: .modal-xxl { width: 95% !important; max-height: 90% !important; ...

Troubleshooting: Getting 405 or 415 errors when making POST requests from Vue.js to .NET Web API 2

I'm currently struggling with POSTing data to my WEBAPI. Here is the ASP.NET WEBAPI code that I am using: [RoutePrefix("api/Test")] public class TestController : ApiController { // GET: api/Test [Route] public IEnumerable& ...

Creating bidirectional binding between a Vue.js instance and a custom native web component: A step-by-step guide

Here is an example showcasing a custom web component called my-input. The goal is to establish a binding between the value attribute of this custom input component and the email attribute of a Vue instance. (Please note that this example may require Chrome ...

Exploring Typescript's Generic Unions

I'm facing an issue where I have an Array of generic objects and want to iterate over them, but TypeScript is not allowing me to do so. Below is a snippet of the code I am working with. Any ideas on how to solve this problem? type someGeneric<T> ...

Transmit a message from the background.js script to the popup window

Currently, I am looking to integrate FCM (Firebase Cloud Messaging) into my chrome extension. After conducting thorough research, I have discovered that the most efficient way to implement FCM is by utilizing the old API chrome.gcm. So far, this method has ...

Observing for form input in Nuxt.js

There is a form on my webpage, which includes the following elements: <div v-if="this.userdata.address == ''">Please enter your address</div> Your address <input type="text" v-model="userdata.address" ...

How can interfaces be effectively integrated with node and mongoose?

I am working on a model interface where I need to fetch specific data from the record // file: code.interface.ts import { Document } from 'mongoose'; export interface CodeI extends Document { readonly _id: string; readonly logs: any; } Howe ...

Preventing the "Block-scoped variable used before its declaration" error in an Angular/TypeScript tree structure with child/parent references

Imagine a scenario where we have a set of simple nodes/objects forming a tree structure, each with parent and children references. The challenge lies in the need to reference both the parent and children nodes independently from the tree itself. This means ...

Why aren't the child elements in my React / Framer Motion animation staggered as expected?

In my finance application, I am creating a balance overview feature. To display the content, I pass props into a single <BalanceEntry> component and then map all entries onto the page. With Framer Motion, my goal is to animate each rendered <Bala ...

The VueJS component reference component is completely inaccessible

I am having trouble understanding how to properly use refs in Vue components. I have two files that are not working together correctly. show.vue <template> <div> <b-container fluid class="bg-white" v-if="$refs.chart"> <b- ...

Error encountered within eot file using file-loader and webpack

I am facing an issue while trying to integrate React Rainbow Components with Next.js (TypeScript). I encountered a problem with importing fonts, which led me to use webpack along with the url-loader. However, despite my efforts, I keep encountering the er ...

What are some creative ways to emphasize certain dates?

Is there a way to customize mui-x-date-pickers to highlight specific days from a Date array with green filled circles around them? I am using new Date and wondering how to achieve this effect. Below is the code snippet I am currently working with: <Dat ...

Pass data from Symfony using an array of objects to a Vue.js component within a Twig template

I am currently working with symfony5 and I want to incorporate vue.js into one of my twig template views. My goal is to pass 3 different Objects to vue.js, each containing multiple Arrays which typically store several Objects. After installing vue.js, here ...