Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the types of Component's render function #1085

Merged
merged 1 commit into from May 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/preact.d.ts
Expand Up @@ -98,7 +98,7 @@ declare namespace preact {

forceUpdate(callback?: () => void): void;

abstract render(props?: RenderableProps<P>, state?: Readonly<S>, context?: any): JSX.Element | null;
abstract render(props?: RenderableProps<P>, state?: Readonly<S>, context?: any): ComponentChild;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I just ran into this myself in my own project

}

function h<P>(
Expand Down
32 changes: 32 additions & 0 deletions test/browser/components.js
Expand Up @@ -118,6 +118,38 @@ describe('Components', () => {
expect(clone.prototype).to.equal(instance.prototype);
});

it('should render string', () => {
class StringComponent extends Component {
render() {
return "Hi there";
}
}

render(<StringComponent />, scratch);
expect(scratch.innerHTML).to.equal('Hi there');
});

it('should render number as string', () => {
class NumberComponent extends Component {
render() {
return 42;
}
}

render(<NumberComponent />, scratch);
expect(scratch.innerHTML).to.equal('42');
});

it('should render null as empty string', () => {
class NullComponent extends Component {
render() {
return null;
}
}

render(<NullComponent />, scratch);
expect(scratch.innerHTML).to.equal('');
});

// Test for Issue #73
it('should remove orphaned elements replaced by Components', () => {
Expand Down
41 changes: 34 additions & 7 deletions test/ts/Component-test.tsx
Expand Up @@ -5,10 +5,11 @@ import {
Component,
FunctionalComponent,
ComponentConstructor,
RenderableProps
RenderableProps,
render
} from "../../src/preact";

export class ContextComponent extends Component<{ foo: string}> {
export class ContextComponent extends Component<{ foo: string }> {
getChildContext() {
return { something: 2 };
}
Expand All @@ -23,10 +24,13 @@ export interface SimpleComponentProps {
}

export interface SimpleState {
name: string | null
name: string | null;
}

export class SimpleComponent extends Component<SimpleComponentProps, SimpleState> {
export class SimpleComponent extends Component<
SimpleComponentProps,
SimpleState
> {
constructor(props: SimpleComponentProps) {
super(props);
this.state = {
Expand All @@ -50,9 +54,10 @@ export class SimpleComponent extends Component<SimpleComponentProps, SimpleState
}
}

class DestructuringRenderPropsComponent
extends Component<SimpleComponentProps, SimpleState> {

class DestructuringRenderPropsComponent extends Component<
SimpleComponentProps,
SimpleState
> {
constructor(props: SimpleComponentProps) {
super(props);
this.state = {
Expand All @@ -72,6 +77,28 @@ class DestructuringRenderPropsComponent
}
}

interface RandomChildrenComponenProps {
num?: number;
val?: string;
span?: boolean;
}

class RandomChildrenComponen extends Component<RandomChildrenComponenProps> {
render() {
const { num, val, span } = this.props;
if (num) {
return num;
}
if (val) {
return val;
}
if (span) {
return <span>hi</span>
}
return null;
}
}

describe("Component", () => {
const component = new SimpleComponent({ initialName: "da name" });

Expand Down