<?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: barzin144</title>
    <description>The latest articles on DEV Community by barzin144 (@barzin144).</description>
    <link>https://dev.to/barzin144</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%2F855867%2F7a8b9882-0151-4c43-b5ac-e2139b11c355.png</url>
      <title>DEV Community: barzin144</title>
      <link>https://dev.to/barzin144</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/barzin144"/>
    <language>en</language>
    <item>
      <title>A light react table package</title>
      <dc:creator>barzin144</dc:creator>
      <pubDate>Mon, 15 Aug 2022 01:57:31 +0000</pubDate>
      <link>https://dev.to/barzin144/a-light-react-table-package-1888</link>
      <guid>https://dev.to/barzin144/a-light-react-table-package-1888</guid>
      <description>&lt;p&gt;Hi everybody :)&lt;/p&gt;

&lt;p&gt;Creating a table in reactJS with selectable rows has been the biggest challenge for me, So I decided to create a light package that help me.&lt;/p&gt;

&lt;p&gt;you can visit its storybook online &lt;a href="https://barzin144.github.io/react-custable/?path=/story/components--table"&gt;Here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Install via NPM
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install --save react-custable
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You only need two variables for configuration, column and data&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Table } from 'react-custable';

//the fieldName should be as same as data's key
const column = [
  { fieldName: 'name', title: 'Name', width: '180px', sortable: true },
  { fieldName: 'email', title: 'Email', width: '180px', sortable: true },
];

const data = [
  { id: '1', name: 'name one', email: 'mailone@mail.com' },
  { id: '2', name: 'name two', email: 'mailtwo@mail.com' },
];
&amp;lt;div className="App"&amp;gt;
  &amp;lt;Table columns={columns} data={data} /&amp;gt;
&amp;lt;/div&amp;gt;;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  what's column props?
&lt;/h3&gt;

&lt;p&gt;the 'fieldName' is the key name of your data and 'title' is the table header for that data and these are mandatory.&lt;/p&gt;

&lt;h4&gt;
  
  
  Optional column configuration
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;width: you can assign certain width to each column&lt;/li&gt;
&lt;li&gt;sortable: this table can sort columns data if these are string&lt;/li&gt;
&lt;li&gt;sortFunc: if the column's data isn't string you can pass a function that knows how to sort your data.&lt;/li&gt;
&lt;li&gt;fixed: if you want to fixed the column for horizontal scroll (only work for first column or last column)&lt;/li&gt;
&lt;li&gt;render: if you want to render custom component, you should pass a function that get row (data of current row) and index (index of current row) and your function should return a Cell object
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  value: React.ReactNode,
  props: { [key: string]: string }, //props will be applied to td elemenet like colspan
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Option&lt;/th&gt;
&lt;th&gt;Type&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;fieldName*&lt;/td&gt;
&lt;td&gt;string&lt;/td&gt;
&lt;td&gt;data key&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;title*&lt;/td&gt;
&lt;td&gt;string&lt;/td&gt;
&lt;td&gt;column header title&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;width&lt;/td&gt;
&lt;td&gt;string(px)&lt;/td&gt;
&lt;td&gt;column width (Default is auto)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;fixed&lt;/td&gt;
&lt;td&gt;string ('left' or 'right')&lt;/td&gt;
&lt;td&gt;to fix column&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;sortable&lt;/td&gt;
&lt;td&gt;boolean&lt;/td&gt;
&lt;td&gt;Default is false&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;sortFunc&lt;/td&gt;
&lt;td&gt;( a , b ) =&amp;gt; number&lt;/td&gt;
&lt;td&gt;sort function should return -1 when a &amp;lt; b , 1 when a &amp;gt; b , 0 when a = b&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;render&lt;/td&gt;
&lt;td&gt;(row, index) =&amp;gt; Cell&lt;/td&gt;
&lt;td&gt;for rendering custom component in cell&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;As you've seen before 'data' and 'column' are mandatory for the table, let's see what are optional for the table&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Option&lt;/th&gt;
&lt;th&gt;Type&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;column*&lt;/td&gt;
&lt;td&gt;Column[]&lt;/td&gt;
&lt;td&gt;array of columns&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;data*&lt;/td&gt;
&lt;td&gt;{ id:string, ... }[]&lt;/td&gt;
&lt;td&gt;array of data&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;isSelectable&lt;/td&gt;
&lt;td&gt;boolean&lt;/td&gt;
&lt;td&gt;to enable checkboxes for rows&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;selectRowHandler&lt;/td&gt;
&lt;td&gt;(selectedRowIds) =&amp;gt; void&lt;/td&gt;
&lt;td&gt;the callback function will receiver selected row IDs&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;selectedRowKeys&lt;/td&gt;
&lt;td&gt;string[]&lt;/td&gt;
&lt;td&gt;default value for selected rows&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;pagination&lt;/td&gt;
&lt;td&gt;{ currentPage: number; totalCount: number; pageLimit: number; }&lt;/td&gt;
&lt;td&gt;values for table pagination&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;pageChangeHandler&lt;/td&gt;
&lt;td&gt;(pageNumner: number) =&amp;gt; void&lt;/td&gt;
&lt;td&gt;the callback for handle page changes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;rowClickHandler&lt;/td&gt;
&lt;td&gt;(row: Row) =&amp;gt; void&lt;/td&gt;
&lt;td&gt;the callback for handle row click&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;showLoading&lt;/td&gt;
&lt;td&gt;boolean&lt;/td&gt;
&lt;td&gt;show spinner over table&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Contribute
&lt;/h3&gt;

&lt;p&gt;this is the first version of my package, so feel free to contribute&lt;br&gt;
&lt;a href="https://github.com/barzin144/react-custable"&gt;https://github.com/barzin144/react-custable&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>react</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
