<?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: Sampath Kumar Krishnan</title>
    <description>The latest articles on DEV Community by Sampath Kumar Krishnan (@sampathblam).</description>
    <link>https://dev.to/sampathblam</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%2F445543%2Fa4e413e0-eb78-49e9-aaaf-1b3f165de3ba.jpeg</url>
      <title>DEV Community: Sampath Kumar Krishnan</title>
      <link>https://dev.to/sampathblam</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sampathblam"/>
    <language>en</language>
    <item>
      <title>Customize Select All Check box in DevExtreme Reactive grid</title>
      <dc:creator>Sampath Kumar Krishnan</dc:creator>
      <pubDate>Sat, 24 Jul 2021 11:56:44 +0000</pubDate>
      <link>https://dev.to/sampathblam/customize-select-all-check-box-in-devextreme-reactive-grid-lk5</link>
      <guid>https://dev.to/sampathblam/customize-select-all-check-box-in-devextreme-reactive-grid-lk5</guid>
      <description>&lt;p&gt;I happen to be using the &lt;a href="https://devexpress.github.io/devextreme-reactive/react/grid/"&gt;React Grid component&lt;/a&gt; from DevExtreme Reactive for sometime now. &lt;/p&gt;

&lt;h4&gt;
  
  
  DevExtreme React Grid Overview
&lt;/h4&gt;

&lt;p&gt;DevExtreme React Grid is a component that can be used to display table content from local and remote data sources. It includes a diversity of functionality on top of the table such as filtering, paging, grouping, sorting, selection and editing. On top of this all the elements and functionalities can be customized to our needs. &lt;/p&gt;

&lt;h4&gt;
  
  
  Problem statement
&lt;/h4&gt;

&lt;p&gt;DevExtreme React Grid supports selection of items in the grid. In particular it supports selection of all items in the grid. &lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/bz8xu?runonclick=1"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;The Select All checkbox includes three different states - Selected, Unselected, Partially selected. When one or more but not all check boxes are selected, the Select All check box displays a partially selected state.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--gOPKADYE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qslkuq5vb6rbpbrr45fg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--gOPKADYE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qslkuq5vb6rbpbrr45fg.png" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But there seems to be a difference in the color of the check box between the Selected and Partially Selected states. In the partially selected state, the checkbox seems to be in grey color. Our motive is to customize the color of this Partially Selected state of the Select all checkbox.&lt;/p&gt;

&lt;h4&gt;
  
  
  Customizing Select All checkbox
&lt;/h4&gt;

&lt;p&gt;Each element in the table created with the help of a basic &lt;a href="https://material-ui.com/api/table-cell/"&gt;TableCell&lt;/a&gt; component. Hence customizing the Select All checkbox would mean customizing the TableCell that holds the checkbox.&lt;/p&gt;

&lt;p&gt;We can do the same by utilizing the &lt;code&gt;headerCellComponent&lt;/code&gt; property of the &lt;code&gt;TableSelection&lt;/code&gt; component in DevExtreme React Grid.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;TableSelection
      showSelectAll={true}
      headerCellComponent={CustomHeaderCellWithCheckbox}
 /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that we know where to include the custom cell component, let's see how to create the component to customize the select all check box.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;headerCellComponent&lt;/code&gt; requires a Component that takes in the properties defined in type &lt;a href="https://devexpress.github.io/devextreme-reactive/react/grid/docs/reference/table-selection/#tableselectionheadercellprops"&gt;HeaderCellProps&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The HeaderCellProps has four fields:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--aWnIasqw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gqy9tudt5ag5gyfc731u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--aWnIasqw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gqy9tudt5ag5gyfc731u.png" alt="alt text"&gt;&lt;/a&gt;&lt;br&gt;
( img source: &lt;a href="https://devexpress.github.io/devextreme-reactive/react/grid/docs/reference/table-selection/#tableselectionheadercellprops"&gt;https://devexpress.github.io/devextreme-reactive/react/grid/docs/reference/table-selection/#tableselectionheadercellprops&lt;/a&gt; )&lt;/p&gt;

&lt;p&gt;The custom component that we are going to write should handle all these four properties and manage the status of the checkbox based on these properties. Let's start writing the component &lt;code&gt;CustomHeaderCellWithCheckbox&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const CustomHeaderCellWithCheckbox = ({
  disabled,
  allSelected,
  someSelected,
  onToggle,
}) =&amp;gt; {
  return (
    // TODO: Map the input props to Checkbox's states
  );
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create a Checkbox element and wrap it inside a TableCell Component. Use necessary padding to have the checkbox in the center of the cell. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Map the inbound &lt;code&gt;disabled&lt;/code&gt; property directly to the &lt;code&gt;disabled&lt;/code&gt; property of the checkbox.&lt;/li&gt;
&lt;li&gt;Map the inbound &lt;code&gt;someSelected&lt;/code&gt; property to the &lt;code&gt;intermediate&lt;/code&gt; property of the checkbox. The &lt;code&gt;indeterminate&lt;/code&gt; boolean property is responsible for displaying the partially selected state of the checkbox. Mapping the state of the &lt;code&gt;someSelected&lt;/code&gt; property to it would let the check box know whether to display the indeterminate check box or not.&lt;/li&gt;
&lt;li&gt;As this is the Select All check box, it would have to be checked if all items are selected. The &lt;code&gt;allSelected&lt;/code&gt; inbound property reflects that state and can be mapped to &lt;code&gt;checked&lt;/code&gt; property of the Checkbox.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;Checkbox
        disabled={disabled}
        indeterminate={someSelected}
        checked={allSelected}
/&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Now the left over inbound property is onToggle, which is a function that takes in a boolean value. The onToggle function is used to change the state of the checkbox based on the current state. Our responsibility is to call the onToggle method with &lt;code&gt;true&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt; based on the state of someSelected and allSelected, whenever the checkbox is clicked. Hence we need to wire the &lt;code&gt;onChange&lt;/code&gt;handler of the checkbox with the onToggle method. 
We need to call onToggle based on the following logic. 

&lt;ol&gt;
&lt;li&gt;If the check box was clicked when some elements were selected, we need to remove the selection.&lt;/li&gt;
&lt;li&gt;If the check box was clicked when all elements were selected, we need to remove the selection.&lt;/li&gt;
&lt;li&gt;If the check box was clicked when no elements were selected, we need to select the checkbox.
The logic would then become,
&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;onChange={(event) =&amp;gt; {
          /**
           * Simplify the following logic
           * if (someSelected) {
           *   onToggle(!someSelected);
           * } else {
           *   onToggle(!allSelected);
           * }
           *
           * Explanation:
           *
           * If someSelected = true, allSelected = false
           * onToggle(false &amp;amp;&amp;amp; true) =&amp;gt; onToggle(false)
           *
           * If someSelected = false, allSelected = true
           * onToggle(true &amp;amp;&amp;amp; false) =&amp;gt; onToggle(false)
           *
           * If someSelected = false, allSelected = false
           * onToggle(true &amp;amp;&amp;amp; true) =&amp;gt; onToggle(true)
           */
          onToggle(!someSelected &amp;amp;&amp;amp; !allSelected);
        }}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Changing the color of the partially selected checkbox
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Set the &lt;code&gt;color&lt;/code&gt; of the checkbox to "secondary". Now this would change the color of the icon in checked status alone. &lt;/li&gt;
&lt;li&gt;Our ultimate aim is to change the color of the partially selected checkbox. For this, we have to customize the indeterminate state icon. This can be done by setting the customized icon to &lt;code&gt;indeterminateIcon&lt;/code&gt; property of the checkbox. 
&lt;code&gt;indeterminateIcon={&amp;lt;IndeterminateCheckBox color="secondary" /&amp;gt;}&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;IndeterminateCheckBox is available in Material UI icons package.&lt;/li&gt;
&lt;li&gt;Now we have set the color of the indeterminate check box icon to secondary color. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That's it. We have customized the Select All checkbox and changed the color of the checkbox in partially selected state. &lt;/p&gt;

&lt;p&gt;Find the modified code sandbox here:&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/lively-glade-3ejlz?runonclick=1"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

</description>
      <category>react</category>
      <category>devextreme</category>
      <category>reactive</category>
      <category>grid</category>
    </item>
  </channel>
</rss>
