Created
November 13, 2020 15:34
-
-
Save smitroshin/62cfd72cd142ca53bb0cfec9ef49917e to your computer and use it in GitHub Desktop.
Easy way to put conditions into class names
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
/** | |
* Filter a list of expressions to final string. | |
* | |
* @param args | |
* @returns {string} | |
*/ | |
const classNames = (...args) => | |
args | |
.reduce((accumulator, itm) => { | |
if (itm) { | |
accumulator.push(`${itm}`.trim()); | |
return accumulator; | |
} | |
return accumulator; | |
}, []) | |
.join(' '); | |
export default classNames; | |
const isSelected = true; | |
const isDisabled = false; | |
classNames('option', isSelected && 'selected', isDisabled && 'disabled') // will return 'option selected' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment