-
Notifications
You must be signed in to change notification settings - Fork 0
Created React/Table component #66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Zayac11
wants to merge
13
commits into
master
Choose a base branch
from
react/table
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 12 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
0129091
fixed dropdown logic and style
Zayac11 641c6a3
start table
Zayac11 cf3ca03
add layout
Zayac11 0d241ad
added colspan and rowspan
Zayac11 40c7264
Added some styles + footer
Zayac11 0573b7c
Split for components
Zayac11 523d763
added columns sort, it sorts initial data array
Zayac11 016ab77
fixed problem with sort
Zayac11 81d65d0
added storybook examples
Zayac11 8bfc2e6
change style and storybook
Zayac11 11ead32
added render prop, replaced sort onClick on arrow icon, style fixes
Zayac11 8d24a12
added displaying functions in storybook
Zayac11 b96eb9e
removed console.log
Zayac11 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import React, {FC} from 'react' | ||
| import styles from '../table.module.scss' | ||
| import {ColumnsType} from '../TableProps' | ||
|
|
||
| const TableBody:FC<MyProps<any>> = React.memo(({data, columns}) => { | ||
| return ( | ||
| <tbody> | ||
| { | ||
| (data.map((item, index) => { | ||
| return ( | ||
| <tr key={item.key}> | ||
| {columns.map((column, tdIndex) => { | ||
| let attributes | ||
| if(column.onCell) { | ||
| attributes = column.onCell(item, index) | ||
| } | ||
| if((attributes?.colSpan === 0 || column.colSpan === 0) || (attributes?.rowSpan === 0 || column.rowSpan === 0)) return null | ||
| return ( | ||
| <td className={styles['table-cell']} colSpan={attributes?.colSpan || column.colSpan} rowSpan={attributes?.rowSpan || column.rowSpan} style={{width: column.width, ...attributes?.style}} key={tdIndex}> | ||
| { | ||
| column.render | ||
| ? column.render(item[column.dataIndex], index) // index - row number | ||
| : item[column.dataIndex] | ||
| } | ||
| </td> | ||
| ) | ||
| })} | ||
| </tr> | ||
| ) | ||
| })) | ||
| } | ||
| </tbody> | ||
| ) | ||
| }) | ||
|
|
||
| export default TableBody | ||
|
|
||
| type MyProps<RecordType> = { | ||
| data: RecordType[] | ||
| columns: ColumnsType<RecordType>[] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| import React, {FC} from 'react' | ||
| import styles from '../table.module.scss' | ||
| import {ColumnsType} from '../TableProps' | ||
|
|
||
| const TableFooter:FC<MyProps<any>> = React.memo(({footer, columns, dataLength}) => { | ||
|
|
||
| return ( | ||
| <> | ||
| { | ||
| footer && | ||
| (!React.isValidElement(footer) | ||
| ? | ||
| <tfoot className={styles['table-footer']}> | ||
| <tr> | ||
| {columns.map((column, index) => { | ||
| let attributes | ||
| if(column.onCell) { | ||
| attributes = column.onCell(footer, dataLength) // footer row number | ||
| } | ||
| console.log(attributes?.colSpan) | ||
| if((attributes?.colSpan === 0 || column.colSpan === 0) || (attributes?.rowSpan === 0 || column.rowSpan === 0)) return null | ||
| return ( | ||
| <td className={styles['table-cell']} colSpan={attributes?.colSpan || column.colSpan} rowSpan={attributes?.rowSpan || column.rowSpan} style={{width: column.width, ...attributes?.style}} key={index}> | ||
| {footer[column.dataIndex]} | ||
| </td> | ||
| ) | ||
| })} | ||
| </tr> | ||
| </tfoot> | ||
| : | ||
| <tfoot className={styles['table-footer']}> | ||
| <tr> | ||
| <td className={`${styles['table-cell']} ${styles['table-footer-element']}`} colSpan={columns.length}> | ||
| {footer} | ||
| </td> | ||
| </tr> | ||
| </tfoot>) | ||
| } | ||
| </> | ||
| ) | ||
| }) | ||
|
|
||
| export default TableFooter | ||
|
|
||
| type MyProps<RecordType> = { | ||
| footer?: RecordType | React.ReactNode | ||
| columns: ColumnsType<RecordType>[] | ||
| dataLength: number | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import React, {FC} from 'react' | ||
| import styles from '../table.module.scss' | ||
| import {ColumnsType, TableSortType} from '../TableProps' | ||
| import Icon from '../../icon/icon' | ||
| import {useTableHeader} from './useTableHeader' | ||
|
|
||
| const TableHeader:FC<TableHeaderProps<any>> = React.memo(({columns, sortValue, sortType, sortTable}) => { | ||
| const {getIconClasses} = useTableHeader({columns, sortValue, sortType, sortTable}) | ||
| return ( | ||
| <thead> | ||
| <tr> | ||
| {columns.map((item) => { | ||
| if(item.colSpan === 0 || item.rowSpan === 0) return null | ||
| return ( | ||
| <th className={styles['table-head']} colSpan={item.colSpan} rowSpan={item.rowSpan} style={{width: item.width}} key={item.key}> | ||
| <span className={styles['table-head-cell']}> | ||
| {item.title} | ||
| { | ||
| item.sorter && <Icon onClick={() => item.sorter && sortTable(item.dataIndex, sortType)} className={getIconClasses(item.dataIndex)} name={'ri-arrow-down-s-fill'} size={18} type={'fill'} /> | ||
| } | ||
| </span> | ||
| </th> | ||
| ) | ||
| })} | ||
| </tr> | ||
| </thead> | ||
| ) | ||
| }) | ||
|
|
||
| export default TableHeader | ||
|
|
||
| export type TableHeaderProps<RecordType> = { | ||
| columns: ColumnsType<RecordType>[] | ||
| sortValue: string | ||
| sortType: TableSortType | ||
| sortTable: (value: string, type: TableSortType) => void | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import {useCallback} from 'react' | ||
| import styles from '../table.module.scss' | ||
| import {TableHeaderProps} from './tableHeader' | ||
| import {getClasses} from '../../../utils/getClasses' | ||
|
|
||
| export const useTableHeader = (props: TableHeaderProps<any>) => { | ||
|
|
||
| const getIconClasses = useCallback((dataIndex: string) => { | ||
| const conditions:{[index: string]:boolean} = { | ||
| "table-head-sort": true, | ||
| "table-head-sort-active": props.sortValue === dataIndex && props.sortType !== '', | ||
| "table-head-sort-ascending": props.sortValue === dataIndex && props.sortType === 'ascending', | ||
| }; | ||
| return getClasses(conditions, styles) | ||
| }, [props]); | ||
|
|
||
| return {getIconClasses} | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import {DefaultParams} from '../../default-types/defaultParams' | ||
| import React from 'react' | ||
| import {ClickableObjectMini} from '../../default-types/ClickableObjectMini' | ||
|
|
||
| export interface TableProps<RecordType> extends DefaultParams, ClickableObjectMini{ | ||
|
|
||
| /** ColumnsType<{DataType}> { <br/> | ||
| * title: string <br/> | ||
| * key: React.Key <br/> | ||
| * dataIndex: string <br/> | ||
| * colSpan?: number <br/> | ||
| * rowSpan?: number <br/> | ||
| * width?: number <br/> | ||
| * render?: (value: DataType, index: number) => React.ReactNode <br/> | ||
| * sorter?: boolean <br/> | ||
| * onCell?: GetComponentProps<RecordType> <br/> | ||
| * } | ||
| * | ||
| * */ | ||
| columns: ColumnsType<RecordType>[] | ||
|
|
||
| /** Data of table body | ||
| * | ||
| * DataType[] - Array of column's dataIndexes | ||
| * | ||
| * */ | ||
| data: RecordType[] | ||
|
|
||
| /** Data of table footer | ||
| * | ||
| * DataType - Last object of column's dataIndexes or ReactNode | ||
| * | ||
| * */ | ||
| footer?: RecordType | React.ReactNode | ||
|
|
||
| /** Table layout prop */ | ||
| tableLayout?: TableLayout | ||
| } | ||
|
|
||
| export interface ColumnsType<RecordType> { | ||
| title: string | ||
| key: React.Key | ||
| dataIndex: string | ||
| colSpan?: number | ||
| rowSpan?: number | ||
| width?: number | ||
| sorter?: boolean | ||
| render?: ( | ||
| value: RecordType, | ||
| index: number, | ||
| ) => React.ReactNode; | ||
| onCell?: GetComponentProps<RecordType> | ||
| } | ||
|
|
||
| export type GetComponentProps<DataType> = ( | ||
| data: DataType, | ||
| index: number, | ||
| ) => React.TdHTMLAttributes<any>; | ||
|
|
||
| export type TableLayout = 'auto' | 'fixed'; | ||
|
|
||
| export type TableSortType = 'descending' | 'ascending' | '' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| @use '../../../../../../styles/components/table' as *; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import { render } from '@testing-library/react'; | ||
|
|
||
| import { Table } from './table'; | ||
|
|
||
| describe('Table', () => { | ||
| it('should render successfully', () => { | ||
| const { baseElement } = render(<Table columns={[]} data={[]} />); | ||
| expect(baseElement).toBeTruthy(); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Нужно убрать