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

Adding Features to Our Button Component

In this lecture, we elevated the usability of our React button component by integrating advanced ES6 features. Through techniques like string templates, reusable class methods, and dynamic properties, we aimed to reduce repetitive typing while maintaining the component’s adaptability and compliance with Bootstrap specifications. These enhancements not only streamline development but also make the component more robust and intuitive for future use.


1. Leveraging Dynamic Class Management

  • Objective: Automatically append the button class to relevant elements without explicitly declaring it every time.
  • Methodology:
    • Introduced the classify method within the component class to dynamically add or adjust class values.
    • Applied logic to prepend button when a class name begins with a dash (-) or includes a space-dash sequence.

Example Code:

classify(key, value) {
    if (value[0] === '-') {
        value = `${key}${value}`;
    }
    return value.split(' -').join(` ${key}-`);
}
  • Outcome: Simplified the creation of buttons with specific classes, minimizing redundant declarations.

2. Understanding ES6 Template Strings

  • Problem: Concatenating strings with spaces and special characters often led to errors and cluttered code.
  • Solution: ES6 template strings allowed us to embed variables directly into strings using backticks and ${} syntax.
  • Implementation: Replaced concatenation logic with template literals to improve readability and maintainability.

Before:

return key + ' ' + value;

After:

return `${key} ${value}`;
  • Benefits:
    • Fewer errors due to missing spaces or incorrect concatenations.
    • Cleaner and more intuitive code structure.

3. Enhancing Button Customization

  • Use Case: Allow developers to define buttons with variations like size (small, large), style (primary, secondary), and even block-level buttons.
  • Features Added:
    • Outline styles for buttons.
    • Support for block-level and size-specific buttons.

Example Usage:

<Button className="primary-outline block large">Click Me</Button>
  • Outcome: Developers can now create sophisticated button designs with minimal effort, following Bootstrap conventions.

4. Addressing Disabled State Logic

  • Challenge: Properly implementing the disabled attribute, which behaves differently for <a> and <button> tags.
  • Solution: Planned adjustments to handle the disabled state in the next lecture, ensuring compliance with Bootstrap and React best practices.

Conclusion

This lecture focused on reducing developer effort while enhancing functionality and customization options for React components. By incorporating ES6 features and dynamic properties, we made the button component more versatile and future-proof.

Next Steps:
In the following lecture, we’ll refine the disabled state behavior and address additional edge cases to finalize the button component.

Video Transcript

00:01Beyond making sure that everything is working according
00:03to the spec of Bootstrap,
00:04as right now it's in an alpha and it could change.
00:07One thing that we would really love
00:08to do is make our component even better than just bootstrap
00:11on its own by cutting down the amount of typing our users
00:15of our components will need.
00:16And to do that, we're gonna take a look at a lot of things
00:18and among them we'll learn about ES six string templates,
00:23ES six class methods and reusable components.
00:26Really in, in a essence, we're gonna try
00:28to make our component the most easily palatable
00:31for our consumer of it.
00:32But then in this lecture, you're gonna have an even deeper
00:34understanding of how to create really fabulous components.
00:39Alright, so the next thing that I want to do is really,
00:42I wanna leverage the fact that we have a component.
00:45Now to do that, what I'm gonna do is I'm gonna go into my
00:47app and what I wanna do is I wanna get rid of any place
00:50where I actually use the word button in the beginning
00:53because it is a button.
00:54So why don't we dynamically add
00:56that button into it whenever it's needed.
00:59That way we could type less,
01:00which typing less is always fun.
01:02Okay? So now that we don't have really the right properties
01:06for our buttons and they're not gonna be rendered
01:08appropriately as such, let's go ahead
01:10and enter our button, add the logic.
01:13Now to do that, I'm gonna go ahead
01:14and add another method to our button component class.
01:19And I'm just gonna go ahead and call it classify
01:21or classify, yeah, classify.
01:24Now what, how are we gonna classify?
01:26Well, I, I wanna build it out in a way where we could more
01:29or less grab this and reuse this once
01:32we start building other components.
01:34So to do that, I'm just gonna go ahead
01:36and set a key that will be the key that we're replacing
01:39and the value, which would be the body of that class name.
01:42So once we have that, I'm just gonna go ahead
01:45and return that value.
01:47But obviously we're gonna want
01:48to change stuff in that value.
01:49But I'm gonna go ahead and
01:51before we append it to our button, I'm just gonna go ahead
01:55and also call it that class to be Well that's interesting.
02:00Well, one thing that I could do is just go ahead
02:04and replace that content there, just this dot class
02:09if I, and literally just send in all of that item
02:13and send in as a peram that button.
02:16Okay, so now that we've done that, the first is our key.
02:19The second is these properties.
02:21So first of all, the first given is I need to add
02:24that button in,
02:26but we're gonna make sure to do that in a moment.
02:28But let's start off with the most basic beginning point,
02:33which is if our value, and let's just take a look at it.
02:36The way we're gonna approach this is we're gonna check if
02:38the first item is a dash.
02:40If it is, then we have to add a button
02:42and then we'll search for anything that has a space
02:45and a dash because in that case we also want
02:47to put a button in.
02:49So the, for the first instance instance, it's very easy.
02:52I'm just gonna ask if our value
02:55in position zero has exactly
02:59The value of dash.
03:00And in that case, let's change that value
03:03to equal the key plus the value.
03:06So we're just gonna append inside
03:07or put in the beginning of our string that key.
03:12Then we wanna return the value, but
03:13before we return it, we're gonna go ahead
03:15and split that value up
03:18and we're gonna split it in any place
03:20where we have a space and that dash.
03:23And then we want to join it
03:24and we want to join it with a space plus the key
03:29plus that dash that we just removed.
03:32And if I go ahead and I save this
03:34and check back into my app, we should now see
03:37that our buttons are rendering adding in.
03:41And if we take a look to just double check
03:43that our actual code itself is coming out nicely.
03:46And if we take a look at that last paragraph,
03:49and first of all we can see that button,
03:51we can see our button primary exactly as it should be.
03:53All these items are beautiful and same is applied for these.
03:57Now my only problem is why my buttons don't look all that is
04:00because I didn't add the actual button,
04:02which I need to also add the button.
04:04So before I return that item, I want
04:06to add the key plus a space plus the value
04:09that is being returned.
04:11So let's go ahead and do that.
04:12I need a key plus a space plus the value
04:17that is being returned now.
04:19Now we should have a button that actually looks like buttons
04:22and it should come back exactly as we had it previously.
04:25But before we wrap this up,
04:26I wanna talk just about one more thing
04:28and that one more thing is actually called string literals.
04:32And string literals are really useful in these type
04:35of situations where you're going from spaces and pluses
04:38and whatnot and in is in these type of situations.
04:43I wanna show you how it works.
04:44So I'm gonna go ahead, I'm gonna send you that our value,
04:46and I'm gonna go ahead
04:47and just copy all of this construct that we have right here
04:52and just assign it inside of here.
04:54Now instead of actually doing these pluses and dashes
04:58and so forth, I'm gonna go ahead
04:59and type here a comma, not the same as the regular
05:03that we use for strings,
05:04but the one that is located near the, on the left
05:07of the digit one on a regular standard keyboard.
05:10And in this one what I'm gonna do is I'm just gonna put your
05:13space, I'm gonna put your that dash and I'm gonna go ahead
05:16and declare that I wanna put a variable inside
05:19by literally putting a dollar
05:21curly bracket opening and closing.
05:22And inside of here, I'll name the name of the variable
05:25that wanna place inside of here,
05:27which in our case is gonna be the key.
05:29Now what actually is going to happen is
05:33automatically E ES six will transcribe it down
05:36and actually create the string structure
05:38that we need it to be.
05:39So if I go ahead and I do another search here, we'll see
05:42that our buttons are coming out.
05:45Although we're having a small issue in this bottom button,
05:47let's see why it's not rendering out as we want it.
05:50We could see that right now our SM is not actually getting
05:53captured and I could already see that I basically put here a
05:56Space that is not needed.
05:58So if I take a peek at my template, you'll see
06:01that I have your spaces just not warranted.
06:04So I'm gonna go ahead and delete that space.
06:06And if we go ahead and take another peek,
06:08our buttons should render properly.
06:10Let's go ahead and do this one more time.
06:11So instead of using this key plus value,
06:14I'm just gonna go ahead and use that template string.
06:18And instead of that, I'm gonna go ahead
06:19and declare that I want
06:20to insert here a variable, which is our key.
06:23And then I'll put a space, 'cause we need one space
06:25and declare that we want to place another variable
06:28and put here our value.
06:30And this is a really nice way,
06:32especially when you have a lot of plus values.
06:34It looks kind of weird, but once you get used to it,
06:37it's gonna save you a lot of adding pluses and spaces
06:40and making mistakes like we've just done pre right now.
06:43But it does save a lot of time once you get used to it.
06:46It's an incredibly cool new feature that has been added
06:49to E ES six and that, uh, that is template strings.
06:53And with them we really stripped down our logic
06:56to make our app a lot smaller and a lot easier to build.
06:59So dynamically, whenever we're working with a button,
07:02we don't actually have
07:03to declare specifically for a button class.
07:05And if we put your something that is not off the button,
07:07then we don't have to worry about it
07:09'cause we'll be more explicit.
07:10And if we went ahead
07:11and set the value with the button, then it won't add it in.
07:14They won't add it in twice
07:15because our logic is gonna be strong enough to be able
07:19to deal with that as well.
07:22In this lecture, I believe you're already aware
07:24that you're in the process of making a fabulous component
07:27and we're really close to finishing this section.
07:30But before we do that, we really wanna loop back
07:32to fixing a problem with the disabled state.
07:35If you check it out,
07:36it's not gonna work a hundred percent the way
07:39Bootstrap wants it to work.
07:41And for that, we'll fix it in the next last lecture
07:43of this section.

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!