Last active
April 29, 2020 18:50
-
-
Save nathansmith/2c1002faa0f430375aefe1c427e23e77 to your computer and use it in GitHub Desktop.
Spreading specific "other" props
This file contains 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
import React from 'react'; | |
import PropTypes from 'prop-types'; | |
import cx from 'classnames'; | |
// UI. | |
import SkipLink from '../components/SkipLink'; | |
// Layout. | |
import AppLayoutFooter from './AppLayoutFooter'; | |
import AppLayoutHeader from './AppLayoutHeader'; | |
import AppLayoutMain from './AppLayoutMain'; | |
// ========== | |
// Constants. | |
// ========== | |
const REGEX_FOOTER = /^footer[A-Z]/; | |
const REGEX_HEADER = /^header[A-Z]/; | |
const REGEX_MAIN = /^main[A-Z]/; | |
// ========== | |
// Component. | |
// ========== | |
const AppLayout = ({ | |
// Layout props. | |
children = null, | |
className = null, | |
style = null, | |
// Other props. | |
...otherProps | |
}) => { | |
// ================= | |
// Build class list. | |
// ================= | |
const classList = cx({ | |
'app-layout': true, | |
[String(className)]: className, | |
}); | |
// =================== | |
// Get specific props. | |
// =================== | |
const footerProps = {}; | |
const headerProps = {}; | |
const mainProps = {}; | |
for (const key in otherProps) { | |
if (key.match(REGEX_FOOTER)) { | |
footerProps[key] = otherProps[key]; | |
} | |
if (key.match(REGEX_HEADER)) { | |
headerProps[key] = otherProps[key]; | |
} | |
if (key.match(REGEX_MAIN)) { | |
mainProps[key] = otherProps[key]; | |
} | |
} | |
// ========== | |
// Expose UI. | |
// ========== | |
return ( | |
<> | |
<SkipLink /> | |
<div className={classList} style={style}> | |
<AppLayoutHeader {...headerProps} /> | |
<AppLayoutMain {...mainProps}>{children}</AppLayoutMain> | |
<AppLayoutFooter {...footerProps} /> | |
</div> | |
</> | |
); | |
}; | |
AppLayout.propTypes = { | |
children: PropTypes.node, | |
className: PropTypes.string, | |
style: PropTypes.object, | |
}; | |
export default AppLayout; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment