@telus-uds/components-community.data-grid 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,110 @@
1
+ import React from 'react'
2
+ import PropTypes from 'prop-types'
3
+ import { styledComponents } from '@telus-uds/components-web'
4
+ import { ROW_TYPE } from './utility'
5
+
6
+ const { styled, css } = styledComponents
7
+
8
+ const StyledExpandedRow = styled.tr`
9
+ ${(props) => {
10
+ return css`
11
+ background-color: ${props.rowBackgroundColorOnHover};
12
+ `
13
+ }}
14
+ transition: 0.3s ease;
15
+ `
16
+
17
+ const StyledRow = styled.tr`
18
+ ${(props) => {
19
+ if (props.isExpandedRowOpen) {
20
+ return css`
21
+ background-color: ${props.rowBackgroundColorOnHover};
22
+ cursor: pointer;
23
+ `
24
+ }
25
+ return css`
26
+ cursor: ${props.hasExpandedContent ? `pointer` : `arrow`};
27
+ background-color: ${props.rowBackgroundColor};
28
+ padding: ${props.rowPadding}px;
29
+ &:hover {
30
+ background-color: ${props.rowBackgroundColorOnHover};
31
+ }
32
+ `
33
+ }}
34
+ transition: 0.3s ease;
35
+ `
36
+
37
+ const DataGridRow = React.forwardRef(
38
+ (
39
+ {
40
+ children,
41
+ tokens,
42
+ type,
43
+ rowId,
44
+ isExpandedRowOpen = false,
45
+ hasExpandedContent = false,
46
+ onClick
47
+ },
48
+ ref
49
+ ) => {
50
+ const onClickHandler = () => {
51
+ onClick(rowId)
52
+ }
53
+ if (type === ROW_TYPE.HEADING) {
54
+ return <tr ref={ref}>{children}</tr>
55
+ }
56
+ if (type === ROW_TYPE.EXPANDEDROW) {
57
+ return (
58
+ <StyledExpandedRow {...tokens} ref={ref}>
59
+ {children}
60
+ </StyledExpandedRow>
61
+ )
62
+ }
63
+ return (
64
+ <StyledRow
65
+ {...tokens}
66
+ isExpandedRowOpen={isExpandedRowOpen}
67
+ hasExpandedContent={hasExpandedContent}
68
+ onClick={hasExpandedContent ? onClickHandler : null}
69
+ ref={ref}
70
+ >
71
+ {children}
72
+ </StyledRow>
73
+ )
74
+ }
75
+ )
76
+
77
+ DataGridRow.displayName = 'DataGridRow'
78
+
79
+ DataGridRow.propTypes = {
80
+ /**
81
+ Row type
82
+ */
83
+ type: PropTypes.oneOf(['heading', 'subHeading', 'expandedRow']),
84
+ /**
85
+ * Accepts any React or HTML node
86
+ */
87
+ children: PropTypes.node.isRequired,
88
+ /**
89
+ * data-id passed from the data to the row passed to the component
90
+ */
91
+ rowId: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
92
+ /**
93
+ * Tokens passed to the component
94
+ */
95
+ tokens: PropTypes.object,
96
+ /**
97
+ * isExpandedRowOpen determines component's behavior to additonal available data
98
+ */
99
+ isExpandedRowOpen: PropTypes.bool,
100
+ /**
101
+ * hasExpandedContent determines component's behavior to additional available data
102
+ */
103
+ hasExpandedContent: PropTypes.bool,
104
+ /**
105
+ * onClick is a function to open/close a row with expanded content
106
+ */
107
+ onClick: PropTypes.func
108
+ }
109
+
110
+ export default DataGridRow
@@ -0,0 +1,68 @@
1
+ import React from 'react'
2
+ import PropTypes from 'prop-types'
3
+ import { styledComponents } from '@telus-uds/components-web'
4
+ import { Scrollbar } from 'smooth-scrollbar-react'
5
+
6
+ const { styled, css } = styledComponents
7
+
8
+ const StyledWrapper = styled.div`
9
+ display: flex;
10
+ width: 100%;
11
+ margin: 0px;
12
+ padding: 8px;
13
+ ${(props) =>
14
+ props.tokens.maxHeight &&
15
+ css`
16
+ max-height: ${props.tokens.maxHeight}px;
17
+ `}
18
+ box-sizing: border-box;
19
+ border: 0px solid #000000;
20
+ overflow-y: auto;
21
+ `
22
+
23
+ const StyledTable = styled.table`
24
+ width: 100%;
25
+ border-spacing: 0;
26
+ `
27
+
28
+ const WrappedScrollbar = styled(Scrollbar)`
29
+ width: 100%;
30
+ `
31
+
32
+ const DataGridTable = React.forwardRef(
33
+ ({ children, tokens, makeScrollBarAlwaysVisible, testID }, ref) => {
34
+ return (
35
+ <StyledWrapper tokens={tokens} ref={ref} testID={testID}>
36
+ <WrappedScrollbar alwaysShowTracks={makeScrollBarAlwaysVisible}>
37
+ <div>
38
+ <StyledTable>{children}</StyledTable>
39
+ </div>
40
+ </WrappedScrollbar>
41
+ </StyledWrapper>
42
+ )
43
+ }
44
+ )
45
+
46
+ DataGridTable.displayName = 'DataGridTable'
47
+
48
+ DataGridTable.propTypes = {
49
+ /**
50
+ * Accepts any React or HTML node
51
+ */
52
+ children: PropTypes.node.isRequired,
53
+ /**
54
+ * Tokens passed to the component
55
+ */
56
+ tokens: PropTypes.object,
57
+ /**
58
+ * Set this to true to make scrollbar always visible when content is larger than the grid
59
+ */
60
+ makeScrollBarAlwaysVisible: PropTypes.bool,
61
+ /**
62
+ /**
63
+ * Use in tests if the datagrid test cases need to find the element with the id.
64
+ */
65
+ testID: PropTypes.string
66
+ }
67
+
68
+ export default DataGridTable
@@ -0,0 +1,18 @@
1
+ export default {
2
+ en: {
3
+ all: 'All',
4
+ hideRow: 'Hide Row',
5
+ expandRow: 'Expand Row',
6
+ sortAsc: 'Sort Ascending',
7
+ sortDesc: 'Sort Descending',
8
+ button: 'Button'
9
+ },
10
+ fr: {
11
+ all: 'Tout',
12
+ hideRow: 'Masquer la ligne',
13
+ expandRow: 'Développer la ligne',
14
+ sortAsc: 'Tri croissant',
15
+ sortDesc: 'Tri décroissant',
16
+ button: 'Bouton'
17
+ }
18
+ }
package/src/index.js ADDED
@@ -0,0 +1,14 @@
1
+ import DataGrid from './DataGrid'
2
+ import DataGridBody from './DataGridBody'
3
+ import DataGridCell from './DataGridCell'
4
+ import DataGridHead from './DataGridHead'
5
+ import DataGridRow from './DataGridRow'
6
+ import DataGridTable from './DataGridTable'
7
+
8
+ DataGrid.Body = DataGridBody
9
+ DataGrid.Cell = DataGridCell
10
+ DataGrid.Head = DataGridHead
11
+ DataGrid.Row = DataGridRow
12
+ DataGrid.Table = DataGridTable
13
+
14
+ export default DataGrid
@@ -0,0 +1,76 @@
1
+ export const ROW_TYPE = {
2
+ HEADING: 'heading',
3
+ SUBHEADING: 'subHeading',
4
+ EXPANDEDROW: 'expandedRow'
5
+ }
6
+
7
+ export const CELL_TYPE = {
8
+ HEADING: 'heading',
9
+ SUBHEADING: 'subHeading',
10
+ EXPANDEDROWCELL: 'expandedRowCell'
11
+ }
12
+
13
+ export const SORT_DIRECTION = {
14
+ ASC: 'asc',
15
+ DESC: 'desc'
16
+ }
17
+
18
+ export const DATA_TYPE = {
19
+ STRING: 'string',
20
+ NUMBER: 'number'
21
+ }
22
+
23
+ const mappedData = (array) => {
24
+ return array.map((element) => {
25
+ const eachRow = { ...element, isSelected: false }
26
+ if (eachRow.expandedRowChildComponent) {
27
+ eachRow.hasExpandedRow = true
28
+ eachRow.isExpandedRowOpen = element.expandedRowChildComponent.displayOnLoad
29
+ }
30
+ return eachRow
31
+ })
32
+ }
33
+
34
+ export const isAtLeastOneRowUnselected = (array) => {
35
+ return array.some((each) => each.isSelected === false)
36
+ }
37
+
38
+ export const resetColumnsData = (data) => {
39
+ return data.map((element) => ({
40
+ ...element,
41
+ currentSort: false,
42
+ sortDirection: SORT_DIRECTION.DESC
43
+ }))
44
+ }
45
+
46
+ export const resetRowData = (data, isGrouped = false) => {
47
+ if (isGrouped) {
48
+ const result = {}
49
+ data.forEach((el) => {
50
+ result[el.key] = { ...el, data: mappedData(el.data) }
51
+ })
52
+ return result
53
+ }
54
+
55
+ return mappedData(data)
56
+ }
57
+
58
+ export const toggleAllCheckBoxes = (array, headerCheckBoxState) => {
59
+ return array.map((element) => {
60
+ return { ...element, isSelected: !headerCheckBoxState }
61
+ })
62
+ }
63
+
64
+ export const toggleRowCheckbox = (array, rowId) => {
65
+ return array.map((el) => ({
66
+ ...el,
67
+ isSelected: el.id === rowId ? !el.isSelected : el.isSelected
68
+ }))
69
+ }
70
+
71
+ export const toggleExpandedRow = (array, rowId) => {
72
+ return array.map((el) => ({
73
+ ...el,
74
+ isExpandedRowOpen: el.id === rowId ? !el.isExpandedRowOpen : el.isExpandedRowOpen
75
+ }))
76
+ }