-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0b44643
commit 6e526b8
Showing
4 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* @file 空组件,用于给`Switch`使用 | ||
* @author daxuewen | ||
*/ | ||
|
||
import PropTypes from 'prop-types'; | ||
|
||
/* istanbul ignore next line */ | ||
const Case = () => null; | ||
|
||
Case.displayName = 'Case'; | ||
|
||
Case.propTypes = { | ||
children: PropTypes.oneOfType([PropTypes.node, PropTypes.func]).isRequired, | ||
value: PropTypes.any.isRequired | ||
}; | ||
|
||
export default Case; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** | ||
* @file 空组件,用于给`Switch`使用 | ||
* @author daxuewen | ||
*/ | ||
|
||
import PropTypes from 'prop-types'; | ||
|
||
/* istanbul ignore next line */ | ||
const Default = () => null; | ||
|
||
Default.displayName = 'Default'; | ||
|
||
Default.propTypes = { | ||
children: PropTypes.oneOfType([PropTypes.node, PropTypes.func]).isRequired | ||
}; | ||
|
||
export default Default; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/** | ||
* @file 模拟`switch case`语句 | ||
* @author daxuewen | ||
*/ | ||
|
||
import {Children} from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import Case from './Case'; | ||
import Default from './Default'; | ||
|
||
const renderElement = element => (typeof element === 'function' ? element() : element); | ||
|
||
const Switch = ({value, children, equal}) => { | ||
const childrenCount = Children.count(children); | ||
const lastChild = childrenCount === 1 ? children : children[children.length - 1]; | ||
const defaultChild = lastChild.type === Default ? lastChild : null; | ||
|
||
const cases = children.filter(child => child.type === Case); | ||
const matchedCase = cases.find(({props}) => { | ||
if (Array.isArray(props.value)) { | ||
return props.value.some(item => equal(item, value)); | ||
} | ||
|
||
return equal(props.value, value); | ||
}); | ||
|
||
if (matchedCase) { | ||
return renderElement(matchedCase.props.children); | ||
} | ||
|
||
if (defaultChild) { | ||
return renderElement(defaultChild.props.children); | ||
} | ||
|
||
return null; | ||
}; | ||
|
||
Switch.propTypes = { | ||
value: PropTypes.oneOfType([PropTypes.string, PropTypes.number, PropTypes.bool]).isRequired, | ||
children: PropTypes.oneOfType([PropTypes.node, PropTypes.func]).isRequired, | ||
equal: PropTypes.func | ||
}; | ||
|
||
Switch.defaultProps = { | ||
equal: (a, b) => a === b | ||
}; | ||
|
||
export default Switch; |