Is it possible to attach a (click) event listener directly to a mat-step component?

Can a click event handler be directly added to the mat-step element?

I attempted to add one to step 2 in this example, but I am not seeing the click being logged:

The code snippet used:

HTML:

<mat-step label="Step 2" (click)="click()" optional>
 // Other content
</mat-step>

TS:

click() {
  console.log("CLICKED STEP 2")
}

Check out the Stackblitz Demo to replicate the issue.

Associated Angular Material Concern

Click here for more information on the related Angular Material Issue

The event only triggers when completion status of step 1 is set to true

Answer №1

To handle the selection change event for the parent tag, which is mat-horizontal-stepper, you can utilize the (selectionChange) event.

HTML:

<mat-horizontal-stepper linear (selectionChange)="selectionChange($event)">
    <mat-step label="Step 1" [completed]="step1Complete">
        <h4>Step 1</h4>
        <button (click)="toggleStep1Complete()" mat-flat-button color="primary">{{ step1Complete === true ? "Toggle:Complete" : "Toggle:Incomplete"}}
</button>
        <div>
            <button mat-button matStepperNext>Next</button>
        </div>
    </mat-step>
    <mat-step label="Step 2" optional>
        <p>Step 2</p>
        <div>
            <button mat-button matStepperPrevious>Back</button>
            <button mat-button matStepperNext>Next</button>
        </div>
    </mat-step>
    <mat-step label="Step 3">
        <p>Step 3</p>
    </mat-step>
</mat-horizontal-stepper>

TS Code:

import { StepperSelectionEvent } from '@angular/cdk/stepper'; -- It's recommended to use this import for better intellisense

selectionChange(event: StepperSelectionEvent) {
  console.log(event.selectedStep.label);
  let stepLabel = event.selectedStep.label;
  if (stepLabel == "Step 2") {
    console.log("CLICKED STEP 2");
  }
}

Check out a working demo here

EDIT:

If you want every step to be clickable or available to the user, consider removing the linear attribute from the mat-horizontal-stepper element or setting it to false.

View Documentation for More Details

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

Tips for reversing a sketch: Creating a timer where the text continuously refreshes causing it to intersect

Currently, I am working on developing a stopwatch that is functional. However, I am facing an issue where the text overlaps itself when it changes due to repetitive drawing. Removing the strokeText and fillText from the interval prevents it from changing a ...

Customizing Webpack 4's Entry Point

Below is the layout of my project: -node_modules -src -client -js -styles -views -index.js -webpack.config.js -server -.babelrc -package -package-lock -README.md -webpack ...

Continue the consecutive updates of rows in the history moving both backward and forward

There are two main pages in this scenario: Home.vue and Statistics.vue. On the home page, there is an interval that counts some numbers upon page load. When you switch to the "/statistics" page, the interval stops running. Upon returning to the "/" page, t ...

Tips for handling binary data retrieved from a SQL query (such as LONGBLOB type) in node.js

I am trying to send binary data to the client using node.js, but I have encountered a limitation where write can only send string or Buffer. How can I successfully send binary data to the client? dbconnect.selectBinary(conn,function(result) { //resul ...

Executing JavaScript functions when a browser tab is closed

When a user closes the browser tab, I want to call a specific JavaScript function. However, I only want this to occur when the user is actually closing the browser, not during page refreshes, link navigation, form submissions, or pressing the back button. ...

Determine the field's type without using generic type arguments

In my code, there is a type called Component with a generic parameter named Props, which must adhere to the Record<string, any> structure. I am looking to create a type that can accept a component in one property and also include a function that retu ...

Navigating with optional parameters appended to the query string

I am faced with a situation where a search can only be performed based on one value at a time (id, age, or sex). If I utilize the following route where only the id is passed, this.router.navigate(['details'], { queryParams: { ...

I am sorry, but there seems to be an issue with the JSON input as it is ending

Whenever I try to submit the form in edit mode, I encounter two errors. An unexpected end of JSON occurred Input provided caused an unexpected end of JSON The update process works perfectly fine and successfully saves values in the database. However, I ...

Extract CSS from Chrome developer tools and convert it into a JavaScript object

Recently, we have started incorporating styles into our React components using the makeStyles hook from Material-UI. Instead of traditional CSS, we are now using JavaScript objects to define styles. An example of this is shown below: const useStyles = ma ...

Refresh the current page in Next.js when a tab is clicked

I am currently working on a Next.js page located at /product While on the /product page, I want to be able to refresh the same page when I click on the product link in the top banner (navbar) that takes me back to /product. Is there a way to achieve this ...

Using the JQuery post method along with the name attribute

I have a basic form that I would like to submit using jQuery. In my Django code, I have some custom logic that relies on the name attribute of the form (such as if request.POST.get('BoekForm2')). Below is the structure of my page: <script sr ...

The filter element is no longer supported by react-native-svg, causing SVGR to remove certain elements

I'm having trouble using my svg file in react native. I attempted to convert the svg to react native but received a warning: "/* SVGR has dropped some elements not supported by react-native-svg: filter */". The result is not exactly what I w ...

React-select: issue with maintaining selected option on input

Hello everyone, I am new to React and seeking some assistance in reviewing my code. I have a component called Bucket that contains multiple challenges as separate components. Here is a simplified version of the code: class CLL_Bucket extends Component { ...

Problem with implementing swipeable tabs using Material-UI in React

I'm experiencing an issue in my application with the react swipeable tabs from material-ui. I followed all the installation steps recommended in the documentation. Currently, I am encountering the error shown in the screenshot below. Could there be a ...

Is JavaScript the Key to Navigating Through a Website?

I am faced with the challenge of creating a script to extract a database from a website. The website's main page features a table where each row contains a link to another page that holds the desired database. Currently, my script can successfully e ...

Using an Ember color picker to dynamically change SCSS variables

Is there a way to allow an admin to change the skin of one of my websites by selecting a color from a palette that will update a universal SASS variable? I am aware of methods to dynamically change CSS using jQuery, but I specifically want to modify the S ...

When it comes to iterating through arrays in nodejs

Can someone assist me with using a for loop on an array to search for titles? I have a basic ADD TODO form input that submits and adds todos or titles to the array: todos.push({ title: req.body.add_todo_input, complete: false }); I am pushing user input ...

A dysfunctional JS object

I am grappling with a rather intricate object and have simplified it by removing unnecessary functions. However, I am facing an issue when I try to initialize it and I can't figure out the reason behind it. Safari is throwing this error at me: [Error] ...

Encountered a type error during project compilation: "Type '(e: ChangeEvent<{ value: string[] | unknown; }>) => void' error occurred."

I recently started working with react and I'm facing an issue with a CustomMultiSelect component in my project. When I try to call events in another component, I encounter the following error during the project build: ERROR: [BUILD] Failed to compile ...

Setting the OrbitControls Position in ThreeJS

Currently, I am saving the position of the OrbitControls by using .getAzimuthalAngle() and .getPolarAngle(). In the future, I would like to update these angles with slightly different values. I chose to utilize getAzimuthalAngle and getPolarAngle because ...