02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School

Making Disabled Tags Work

In this lecture, we addressed the final hurdle in completing our reusable Bootstrap React button component: properly implementing the disabled state. While button elements naturally handle the disabled attribute, a tags require additional classes to simulate this state. This lesson demonstrates how to achieve parity between these two elements while adhering to Bootstrap’s documentation.


1. The Disabled Property in HTML

  • Key Issue:

    • The disabled attribute works seamlessly with button elements, rendering them unclickable and visually distinct.
    • For a tags, simply adding the disabled attribute does not disable the link or alter its appearance.
  • Solution Overview:

    • Supplement the disabled property with the disabled class for a tags.
    • Dynamically apply this logic based on the type of tag being rendered.

2. Implementing the Disabled State Logic

Step 1: Modify the Button Component

  • Check if the disabled property exists.
  • Append the disabled class dynamically to the component’s class list.

Code Example:

render() {
    let className = `${this.props.className || ''}`;
    if (this.props.disabled) {
        className += ' disabled';
    }

    const Tag = this.props.href ? 'a' : 'button';

    return (
        <Tag 
            className={className.trim()}
            disabled={this.props.href ? undefined : this.props.disabled}
            {...this.props}
        >
            {this.props.children}
        </Tag>
    );
}

Explanation:

  1. Class Name Logic:
    • If disabled is true, append disabled to the class name.
  2. Dynamic Tag Rendering:
    • Use an <a> tag if the href attribute exists; otherwise, use a <button> tag.
  3. Disabled Attribute Assignment:
    • Assign the disabled attribute only to <button> elements.

3. Testing and Verifying Disabled Behavior

  • For <button> Elements:
    • Verify that the disabled attribute prevents user interaction and displays the proper styling.
  • For <a> Tags:
    • Ensure that both the disabled class and attribute are applied to prevent clicks and match visual cues.

Steps:

  1. Inspect the rendered elements in the browser to confirm the presence of the disabled class and attribute where applicable.
  2. Attempt to click disabled elements to validate that they behave as expected.

4. Final Thoughts and Future Extensions

  • Best Practices:
    • Always check the latest Bootstrap documentation for updates or changes to how the disabled state is handled.
  • Extensions:
    • Consider adding role-based accessibility features, such as aria-disabled, to improve compatibility with assistive technologies.

Conclusion

With the disabled state fully implemented, we have successfully completed our reusable Bootstrap React button component. This powerful yet flexible component adheres to Bootstrap's standards while providing developers with dynamic features and ease of use.

Next Steps:
In the next chapter, we’ll shift our focus to building another component, learning new techniques, and expanding our understanding of React and Bootstrap integration.

Video Transcript

00:00We are practically almost done creating our React
00:04bootstrap button.
00:06But before that we have to fix our issue
00:08with the disabled buttons
00:09and we're gonna take a look at how to fix
00:12and make buttons disable,
00:14make them work basically the way they need to work according
00:17to the bootstrap documentation.
00:18And we're gonna do that.
00:19We're gonna follow the current version.
00:21Really if something changes, it doesn't really matter
00:24'cause it's really about the flow
00:25and understanding how to build things out.
00:27So make sure to also double check
00:28with the documentation if something
00:30doesn't work or something changes.
00:32Alright, so I wanna wrap things up with a small note
00:35regarding disabled.
00:36I went ahead and in our HTML
00:40or more accurately inside of our app,
00:42I've added in the disabled property.
00:45Now I can go ahead and also add this to every single button
00:49and I'm gonna add it to all of them, including our atag.
00:52Now, adding the disabled property,
00:55we'll surely transcribe into our component
00:58and if we inspect those elements and let's just go ahead
01:01and do that and inspect them,
01:04we'll see something very interesting.
01:06Although each and every one
01:08of our elements has got a disabled status, you,
01:12as you can see, even our attag has a disabled status.
01:15Whenever I click into that item,
01:17it actually will continue working on contrary to buttons
01:20that actually will get disabled
01:22and even get the iconography of the disabled state contr.
01:27On contrary with the A tag, we really have
01:29to add the class in as a class of disabled as well.
01:34So to do that, I'm just gonna go ahead
01:36and add it for all the items.
01:38And in the end, after we set our class names,
01:42I'm just gonna go ahead and add to it
01:44and just append to it, the disabled property if it exists.
01:49So to do that, I'm just gonna open and close your
01:51and ask if this stop props disabled.
01:54In the case that it does exist, I'm gonna go ahead
01:57and add the disabled option.
01:59While in any other situation, I wouldn't.
02:02Now, if I go ahead and save this and we go
02:04and take a peek again in our application, we'll see now
02:08that in our buttons, if we can find them inside of our data,
02:12inside of our container, we'll see
02:14that our button has now a disabled state added into it
02:17and so does our aag.
02:19And now if I try to click into the aag,
02:21the AAG will be disabled as well and won't be clickable.
02:25So that there goes, we've created our first component
02:28that more or less captures everything there is about the
02:32Bootstrap button component,
02:34and you're more than encouraged
02:35to look into the documentation
02:37and see if there's anything else
02:38that makes sense to add into it.
02:40But we're done with this chapter
02:42because in the next section we'll move into a different
02:45component and walk out of just working
02:48with the button itself.
02:51In this lecture, we really completed to create our
02:55reusable bootstrap React component.
02:57Congrats. In the next lecture, we'll move on
02:59to the next component, and in the process learn a lot more
03:02things as we merge things together between Bootstrap
03:05and React.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!