Skip to content

Commit d2fdddf

Browse files
committed
Refactor ModuleGrid Component
1 parent 4285e66 commit d2fdddf

File tree

1 file changed

+112
-20
lines changed

1 file changed

+112
-20
lines changed

src/ModuleGrid/index.tsx

Lines changed: 112 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,30 @@
55
import * as React from 'react';
66

77
// -----------------------------------------------------------------------------
8-
// Types
8+
// ModuleGrid Component
99
// -----------------------------------------------------------------------------
1010

1111
export type ModuleGridCol = 'auto' | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12;
1212

13-
export interface ModuleGridProps {
13+
export interface ModuleGridProps
14+
extends React.HTMLAttributes<
15+
| HTMLDivElement
16+
| HTMLSpanElement
17+
| HTMLUListElement
18+
| HTMLOListElement
19+
| HTMLLIElement
20+
> {
21+
component?:
22+
| 'div'
23+
| 'span'
24+
| 'ul'
25+
| 'ol'
26+
| 'li'
27+
| 'header'
28+
| 'footer'
29+
| 'aside'
30+
| 'section'
31+
| 'main';
1432
cols?: ModuleGridCol;
1533
xxsCols?: ModuleGridCol;
1634
xsCols?: ModuleGridCol;
@@ -23,10 +41,6 @@ export interface ModuleGridProps {
2341
hdCols?: ModuleGridCol;
2442
}
2543

26-
// -----------------------------------------------------------------------------
27-
// ModuleGrid Component
28-
// -----------------------------------------------------------------------------
29-
3044
const colClassName = (col?: ModuleGridCol, mq: string = ''): string | boolean => {
3145
return typeof col === 'number' || col === 'auto'
3246
? `_${mq}module-grid--${col}`
@@ -37,16 +51,41 @@ export const ModuleGrid: React.FC<ModuleGridProps> = ({
3751
cols,
3852
xxsCols,
3953
xsCols,
40-
children
54+
smCols,
55+
mdCols,
56+
dfCols,
57+
lgCols,
58+
xlCols,
59+
xxlCols,
60+
hdCols,
61+
className,
62+
component = 'div',
63+
children,
64+
...htmlProps
4165
}) => {
42-
const classes = [
43-
'_module-grid',
44-
colClassName(cols),
45-
colClassName(xxsCols, 'xxs:'),
46-
colClassName(xsCols, 'xs:')
47-
];
48-
49-
return <div className={classes.filter(Boolean).join(' ')}>{children}</div>;
66+
return React.createElement(
67+
component,
68+
{
69+
...htmlProps,
70+
classNames: [
71+
'_module-grid',
72+
colClassName(cols),
73+
colClassName(xxsCols, 'xxs:'),
74+
colClassName(xsCols, 'xs:'),
75+
colClassName(smCols, 'sm:'),
76+
colClassName(mdCols, 'md:'),
77+
colClassName(dfCols, 'df:'),
78+
colClassName(lgCols, 'lg:'),
79+
colClassName(xlCols, 'xl:'),
80+
colClassName(xxlCols, 'xxl:'),
81+
colClassName(hdCols, 'hd:'),
82+
className
83+
]
84+
.filter(Boolean)
85+
.join(' ')
86+
},
87+
children
88+
);
5089
};
5190

5291
// -----------------------------------------------------------------------------
@@ -55,7 +94,24 @@ export const ModuleGrid: React.FC<ModuleGridProps> = ({
5594

5695
export type ModuleCellSpan = 'auto' | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12;
5796

58-
export type ModuleCellProps = {
97+
export interface ModuleCellProps
98+
extends React.HTMLAttributes<
99+
| HTMLDivElement
100+
| HTMLSpanElement
101+
| HTMLUListElement
102+
| HTMLOListElement
103+
| HTMLLIElement
104+
> {
105+
component?:
106+
| 'div'
107+
| 'span'
108+
| 'ul'
109+
| 'ol'
110+
| 'li'
111+
| 'header'
112+
| 'footer'
113+
| 'aside'
114+
| 'section';
59115
span?: ModuleCellSpan;
60116
xxsSpan?: ModuleCellSpan;
61117
xsSpan?: ModuleCellSpan;
@@ -66,15 +122,51 @@ export type ModuleCellProps = {
66122
xlSpan?: ModuleCellSpan;
67123
xxlSpan?: ModuleCellSpan;
68124
hdSpan?: ModuleCellSpan;
69-
};
125+
}
70126

71127
const spanClassName = (span?: ModuleCellSpan, mq: string = ''): string | boolean => {
72128
return typeof span === 'number' || span === 'auto'
73129
? `_${mq}module-cell--${span}`
74130
: false;
75131
};
76132

77-
export const ModuleCell: React.FC<ModuleCellProps> = ({ span, children }) => {
78-
const classes = ['_module-cell', spanClassName(span)];
79-
return <div className={classes.join(' ')}>{children}</div>;
133+
export const ModuleCell: React.FC<ModuleCellProps> = ({
134+
span,
135+
xxsSpan,
136+
xsSpan,
137+
smSpan,
138+
mdSpan,
139+
dfSpan,
140+
lgSpan,
141+
xlSpan,
142+
xxlSpan,
143+
hdSpan,
144+
className,
145+
component = 'div',
146+
children,
147+
...htmlProps
148+
}) => {
149+
return React.createElement(
150+
component,
151+
{
152+
...htmlProps,
153+
classNames: [
154+
'_module-cell',
155+
spanClassName(span),
156+
spanClassName(xxsSpan, 'xxs:'),
157+
spanClassName(xsSpan, 'xs:'),
158+
spanClassName(smSpan, 'sm:'),
159+
spanClassName(mdSpan, 'md:'),
160+
spanClassName(dfSpan, 'df:'),
161+
spanClassName(lgSpan, 'lg:'),
162+
spanClassName(xlSpan, 'xl:'),
163+
spanClassName(xxlSpan, 'xxl:'),
164+
spanClassName(hdSpan, 'hd:'),
165+
className
166+
]
167+
.filter(Boolean)
168+
.join(' ')
169+
},
170+
children
171+
);
80172
};

0 commit comments

Comments
 (0)