Skip to content

Commit

Permalink
Add injectNames argument to react2angular
Browse files Browse the repository at this point in the history
  • Loading branch information
fand committed Oct 30, 2017
1 parent 6be1f43 commit 1a3ae9f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
11 changes: 7 additions & 4 deletions index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,24 @@ import { render, unmountComponentAtNode } from 'react-dom'
*/
export function react2angular<Props>(
Class: React.ComponentClass<Props> | React.SFC<Props>,
bindingNames?: (keyof Props)[]
bindingNames: (keyof Props)[] | null = null,
injectNames: string[] = []
): IComponentOptions {
const names = bindingNames
|| (Class.propTypes && Object.keys(Class.propTypes))
|| []

return {
bindings: fromPairs(names.map(_ => [_, '<'])),
controller: ['$element', class extends NgComponent<Props> {
constructor(private $element: IAugmentedJQuery) {
controller: ['$element', ...(injectNames as any), class extends NgComponent<Props> {
injectedProps: any[];
constructor(private $element: IAugmentedJQuery, ...injectedProps: any[]) {
super()
this.injectedProps = injectedProps;
}
render() {
// TODO: rm any when https://github.com/Microsoft/TypeScript/pull/13288 is merged
render(<Class {...(this.props as any)} />, this.$element[0])
render(<Class {...(this.props as any)} {...this.injectedProps} />, this.$element[0])
}
componentWillUnmount() {
unmountComponentAtNode(this.$element[0])
Expand Down
12 changes: 12 additions & 0 deletions test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,18 @@ describe('react2angular', () => {
it('should have empty bindings when parameter is not passed', () => {
expect(react2angular(TestThree).bindings).toEqual({})
})

it('should use the injectNames for DI', () => {
const defaultDi = (react2angular(TestThree).controller as any).slice(0, -1)
const injectedDi = (react2angular(TestThree, null, ['foo', 'bar']).controller as any).slice(0, -1)
expect(injectedDi).toEqual(defaultDi.concat(['foo', 'bar']))
})

it('should have default DI specifications if injectNames is empty', () => {
const defaultDi = (react2angular(TestThree).controller as any).slice(0, -1)
const injectedDi = (react2angular(TestThree, null, []).controller as any).slice(0, -1)
expect(injectedDi).toEqual(defaultDi)
})
})

describe('react classes', () => {
Expand Down

0 comments on commit 1a3ae9f

Please sign in to comment.