<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: eczeps</title>
    <description>The latest articles on DEV Community by eczeps (@eczeps).</description>
    <link>https://dev.to/eczeps</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F496182%2Fd8f1502b-4298-477e-aebb-c2672e503493.png</url>
      <title>DEV Community: eczeps</title>
      <link>https://dev.to/eczeps</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/eczeps"/>
    <language>en</language>
    <item>
      <title>Creating A Custom Radio Button Group Component in Vue.js</title>
      <dc:creator>eczeps</dc:creator>
      <pubDate>Wed, 21 Oct 2020 22:27:04 +0000</pubDate>
      <link>https://dev.to/eczeps/creating-a-custom-radio-button-group-component-in-vue-js-2ec5</link>
      <guid>https://dev.to/eczeps/creating-a-custom-radio-button-group-component-in-vue-js-2ec5</guid>
      <description>&lt;p&gt;Creating custom components in Vue can be a great way to abstract out logic and complexity, and form input components are often a great place for that. I recently created a custom radio group component, and although I found documentation for many of the necessary pieces, it was particularly challenging to find fully-coded examples — so after finishing it up, I decided to share the full code here.&lt;/p&gt;

&lt;p&gt;There isn’t one way to solve this problem, and there were absolutely tradeoffs along the way. I’ll do my best to highlight those, and you should feel free to make different choices based on your specific needs.&lt;/p&gt;

&lt;p&gt;Finally, a huge shoutout to my coworkers at &lt;a href="https://www.energysage.com/"&gt;EnergySage&lt;/a&gt;, who spent hours reviewing my code, researching, and coming up with new ideas. I find that the best work is often done collaboratively, and this was no different.&lt;/p&gt;

&lt;h2&gt;
  
  
  What does this component do?
&lt;/h2&gt;

&lt;p&gt;This custom radio group component can be used at a variety of complexities. It supports simple radio button questions, like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--uWGadKwb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/jsrwr2jcko0yw09ufecx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--uWGadKwb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/jsrwr2jcko0yw09ufecx.png" alt="Screen Shot 2020-09-26 at 8.59.15 PM"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It also supports more complex radio button questions, like toggling the visibility of extra details between the options:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--v2qLB5gf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/n70o1j4kiax7unt6365u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--v2qLB5gf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/n70o1j4kiax7unt6365u.png" alt="Screen Shot 2020-09-26 at 9.39.01 PM"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And even nesting interactive elements:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nOw5iCyH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/q4o9xmobfodqzx8id1ce.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nOw5iCyH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/q4o9xmobfodqzx8id1ce.png" alt="Screen Shot 2020-09-26 at 8.59.52 PM"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This component also supports required and disabled questions.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Code
&lt;/h2&gt;

&lt;p&gt;Here’s the CodeSandbox for the component. Feel free to use this code, or read on for details on some of the implementation decisions and troubleshooting tips I ran across in creating it.&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/custom-radio-group-with-vuejs-znnh7"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h3&gt;
  
  
  Setting the :checked attribute
&lt;/h3&gt;

&lt;p&gt;One of the most challenging parts was figuring out how to set the &lt;code&gt;checked&lt;/code&gt; attribute properly. For a while, I was using the approach from a &lt;a href="https://www.smashingmagazine.com/2017/08/creating-custom-inputs-vue-js/"&gt;Smashing Magazine tutorial&lt;/a&gt; (definitely worth a read if you’re looking into different ways to go about this; it got me 90% of the way there). However, I ended up consolidating the logic into the checked attribute on the &lt;code&gt;&amp;lt;input&amp;gt;&lt;/code&gt;. &lt;code&gt;option.value === value&lt;/code&gt; compares whether the current value of the component is equal to the value of the given radio button option on this iteration of the v-for loop; if they are the same, then we must want to display that the given radio button option is selected. I personally found this to be more concise and readable, plus it helped out with the logic for nested elements.&lt;/p&gt;

&lt;h3&gt;
  
  
  Supporting nested elements
&lt;/h3&gt;

&lt;p&gt;Supporting nested elements can be done using the same logic that the checked attribute uses:&lt;br&gt;
&lt;code&gt;&amp;lt;slot v-if="option.value === value" :name="option.value" /&amp;gt;&lt;/code&gt;&lt;br&gt;
This slot will be filled with any content passed in from ParentComponent.vue whose &lt;code&gt;&amp;lt;template&amp;gt;&lt;/code&gt; matches the slot’s name attribute. The slot’s &lt;code&gt;v-if&lt;/code&gt; ensures that only the currently selected option’s slot renders, as opposed to all of the slots passed in from the parent rendering.&lt;/p&gt;

&lt;p&gt;I found this logic a bit confusing at first. I think a good way to understand it is to look at how the component renders without the &lt;code&gt;v-if&lt;/code&gt; attribute:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--5mSgTsm5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/bqisgfdao7qpdpir1d5o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5mSgTsm5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/bqisgfdao7qpdpir1d5o.png" alt="Screen Shot 2020-10-02 at 4.26.06 PM"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And without the &lt;code&gt;:name&lt;/code&gt; the slots can’t be matched up with the options properly (this is also what would happen if the &lt;code&gt;&amp;lt;template&amp;gt;&lt;/code&gt; didn’t match the values in the options array):&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--chXTgiiw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/aahdabdgg3dzjj6h7vk5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--chXTgiiw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/aahdabdgg3dzjj6h7vk5.png" alt="Screen Shot 2020-10-02 at 4.26.23 PM"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can add interactive elements inside one of these slots, and access its value just as you would any other element. In the example code above, we have &lt;code&gt;v-model="fields.otherText"&lt;/code&gt; on our &lt;code&gt;“More details” &amp;lt;input&amp;gt;&lt;/code&gt;. This will update the &lt;code&gt;fields.other_text&lt;/code&gt; attribute in our data just as we would expect it to with any other input.&lt;/p&gt;

&lt;h2&gt;
  
  
  More resources
&lt;/h2&gt;

&lt;p&gt;Again, huge thanks to my coworkers; many of these insights and ideas were theirs and the collaboration that went into this was incredibly helpful. Here are a few of the other resources that came in handy while creating this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.smashingmagazine.com/2017/08/creating-custom-inputs-vue-js/"&gt;https://www.smashingmagazine.com/2017/08/creating-custom-inputs-vue-js/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.vuemastery.com/courses/vue3-forms/base-input"&gt;https://www.vuemastery.com/courses/vue3-forms/base-input&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://bootstrap-vue.org/docs/components/form-radio"&gt;https://bootstrap-vue.org/docs/components/form-radio&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;This article was cross-posted on Medium.com &lt;a href="https://medium.com/@eczepiel/creating-a-custom-radio-button-group-component-in-vue-js-d96e177c84a4"&gt;here&lt;/a&gt;&lt;/em&gt;.&lt;/p&gt;

</description>
      <category>vue</category>
    </item>
  </channel>
</rss>
