-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Export utility for passing props for higher order components
- Loading branch information
1 parent
a8bfe2e
commit 5a520dc
Showing
3 changed files
with
80 additions
and
1 deletion.
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,24 @@ | ||
'use strict'; | ||
|
||
import { object } from 'metal'; | ||
|
||
/** | ||
* Returns props that are not used or declared in the component. | ||
* @param {Component} component Instance of metal-jsx component | ||
* @return {Object} Object containing props | ||
*/ | ||
export default function otherProps(component) { | ||
const removeKeys = [...component.getDataManager().getPropsInstance(component).getStateKeys(), 'key', 'ref']; | ||
|
||
const retObj = object.mixin({}, component.props); | ||
|
||
for (let i = 0; i < removeKeys.length; i++) { | ||
const key = removeKeys[i]; | ||
|
||
if (retObj.hasOwnProperty(key)) { | ||
delete retObj[key]; | ||
} | ||
} | ||
|
||
return retObj; | ||
} |
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,54 @@ | ||
'use strict'; | ||
|
||
import JSXComponent from '../src/JSXComponent'; | ||
import otherProps from '../src/otherProps'; | ||
|
||
describe('otherProps', function() { | ||
var component; | ||
|
||
afterEach(function() { | ||
if (component) { | ||
component.dispose(); | ||
} | ||
}); | ||
|
||
it('should return object', function() { | ||
class TestComponent extends JSXComponent { | ||
render() { | ||
return <div />; | ||
} | ||
} | ||
|
||
assert.deepEqual(otherProps(new TestComponent()), {}); | ||
}); | ||
|
||
it('should pass through unspecified props', function() { | ||
class TestComponent extends JSXComponent { | ||
render() { | ||
return <div />; | ||
} | ||
} | ||
|
||
TestComponent.PROPS = { | ||
foo: {}, | ||
}; | ||
|
||
assert.deepEqual( | ||
otherProps(new TestComponent({baz: 'qux', foo: 'bar'})), | ||
{baz: 'qux'} | ||
); | ||
}); | ||
|
||
it('should ignore key and ref', function() { | ||
class TestComponent extends JSXComponent { | ||
render() { | ||
return <div />; | ||
} | ||
} | ||
|
||
assert.deepEqual( | ||
otherProps(new TestComponent({key: 'bar', ref: 'qux'})), | ||
{} | ||
); | ||
}); | ||
}); |