Skip to content

Commit

Permalink
Merge branch 'master' into reverse_h_type_definition_order
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinhagemeister committed Nov 4, 2018
2 parents 1c336ae + b243a5e commit 0aece17
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 14 deletions.
15 changes: 14 additions & 1 deletion README.md
@@ -1,6 +1,8 @@
<p align="center">
<a href="https://preactjs.com" target="_blank">
<img alt="Preact" title="Preact" src="https://cdn.rawgit.com/developit/b4416d5c92b743dbaec1e68bc4c27cda/raw/3235dc508f7eb834ebf48418aea212a05df13db1/preact-logo-trans.svg" width="550">

![Preact](/logo.svg "Preact")

</a>
</p>
<p align="center">Fast <b>3kB</b> alternative to React with the same modern API.</p>
Expand Down Expand Up @@ -66,6 +68,7 @@ Preact supports modern browsers and IE9+:
- [**Preact Hacker News**](https://hn.kristoferbaxter.com) _([GitHub Project](https://github.com/kristoferbaxter/preact-hn))_
- [**Play.cash**](https://play.cash) :notes: _([GitHub Project](https://github.com/feross/play.cash))_
- [**BitMidi**](https://bitmidi.com/) 🎹 Wayback machine for free MIDI files _([GitHub Project](https://github.com/feross/bitmidi.com))_
- [**Ultimate Guitar**](https://www.ultimate-guitar.com) 🎸speed boosted by Preact.
- [**ESBench**](http://esbench.com) is built using Preact.
- [**BigWebQuiz**](https://bigwebquiz.com) _([GitHub Project](https://github.com/jakearchibald/big-web-quiz))_
- [**Nectarine.rocks**](http://nectarine.rocks) _([GitHub Project](https://github.com/developit/nectarine))_ :peach:
Expand Down Expand Up @@ -206,6 +209,16 @@ import preact from 'preact';
> ]
> }
> ```
>
> **For Babel 7:**
>
> ```json
> {
> "plugins": [
> ["@babel/plugin-transform-react-jsx", { "pragma":"h" }]
> ]
> }
> ```
> **For using Preact along with TypeScript add to `tsconfig.json`:**
>
> ```json
Expand Down
9 changes: 9 additions & 0 deletions logo.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 5 additions & 4 deletions src/preact.d.ts
Expand Up @@ -4,8 +4,8 @@ export as namespace preact;
declare namespace preact {
type Key = string | number;
type Ref<T> = (instance: T) => void;
type ComponentChild = VNode<any> | string | number | null;
type ComponentChildren = ComponentChild[] | ComponentChild | object | string | number | null;
type ComponentChild = VNode<any> | object | string | number | boolean | null;
type ComponentChildren = ComponentChild[] | ComponentChild;

/**
* @deprecated
Expand All @@ -22,7 +22,7 @@ declare namespace preact {
type PreactHTMLAttributes = ClassAttributes<any>;

interface Attributes {
key?: string | number | any;
key?: Key;
jsx?: boolean;
}

Expand Down Expand Up @@ -69,7 +69,7 @@ declare namespace preact {
}

// Type alias for a component considered generally, whether stateless or stateful.
type AnyComponent<P = {}, S = {}> = FunctionalComponent<P> | Component<P, S>;
type AnyComponent<P = {}, S = {}> = FunctionalComponent<P> | ComponentConstructor<P, S>;

interface Component<P = {}, S = {}> {
componentWillMount?(): void;
Expand Down Expand Up @@ -421,6 +421,7 @@ declare global {
interface DOMAttributes extends preact.PreactDOMAttributes {
// Image Events
onLoad?: GenericEventHandler;
onError?: GenericEventHandler;
onLoadCapture?: GenericEventHandler;

// Clipboard Events
Expand Down
2 changes: 1 addition & 1 deletion src/preact.js.flow
Expand Up @@ -2,7 +2,7 @@

import { createElement, cloneElement, Component, type Node } from 'react';

declare var h: createElement;
declare var h: typeof createElement;

declare function render(vnode: Node, parent: Element, toReplace?: Element): Element;

Expand Down
4 changes: 2 additions & 2 deletions src/vdom/component.js
Expand Up @@ -189,7 +189,7 @@ export function renderComponent(component, renderMode, mountAll, isChild) {
}

if (!isUpdate || mountAll) {
mounts.unshift(component);
mounts.push(component);
}
else if (!skip) {
// Ensure that pending componentDidMount() hooks of child components
Expand Down Expand Up @@ -282,7 +282,7 @@ export function unmountComponent(component) {
unmountComponent(inner);
}
else if (base) {
if (base[ATTR_KEY] && base[ATTR_KEY].ref) base[ATTR_KEY].ref(null);
if (base[ATTR_KEY]!=null) applyRef(base[ATTR_KEY].ref, null);

component.nextBase = base;

Expand Down
6 changes: 4 additions & 2 deletions src/vdom/diff.js
Expand Up @@ -24,11 +24,13 @@ let hydrating = false;

/** Invoke queued componentDidMount lifecycle methods */
export function flushMounts() {
let c;
while ((c=mounts.pop())) {
let c, i;
for (i=0; i<mounts.length; ++i) {
c = mounts[i];
if (options.afterMount) options.afterMount(c);
if (c.componentDidMount) c.componentDidMount();
}
mounts.length = 0;
}


Expand Down
41 changes: 37 additions & 4 deletions test/ts/VNode-test.tsx
Expand Up @@ -5,7 +5,7 @@ import {
Component,
FunctionalComponent,
ComponentConstructor,
VNode
AnyComponent
} from "../../src/preact";

class SimpleComponent extends Component<{}, {}> {
Expand All @@ -18,6 +18,9 @@ class SimpleComponent extends Component<{}, {}> {

const SimpleFunctionalComponent = () => <div />;

const a: AnyComponent = SimpleComponent;
const b: AnyComponent = SimpleFunctionalComponent;

describe("VNode", () => {
it("is returned by h", () => {
const actual = <div className="wow"/>;
Expand Down Expand Up @@ -58,8 +61,38 @@ describe("VNode", () => {
});
});

class TypedChildren extends Component<{children: (num: number) => string}> {
render() { return null }
class ComponentWithFunctionChild extends Component<{ children: (num: number) => string; }> {
render() { return null; }
}

<ComponentWithFunctionChild>{num => num.toFixed(2)}</ComponentWithFunctionChild>;

class ComponentWithStringChild extends Component<{ children: string; }> {
render() { return null; }
}

<ComponentWithStringChild>child</ComponentWithStringChild>;

class ComponentWithNumberChild extends Component<{ children: number; }> {
render() { return null; }
}

<ComponentWithNumberChild>{1}</ComponentWithNumberChild>;

class ComponentWithBooleanChild extends Component<{ children: boolean; }> {
render() { return null; }
}

<ComponentWithBooleanChild>{false}</ComponentWithBooleanChild>;

class ComponentWithNullChild extends Component<{ children: null; }> {
render() { return null; }
}

<ComponentWithNullChild>{null}</ComponentWithNullChild>;

class ComponentWithNumberChildren extends Component<{ children: number[]; }> {
render() { return null; }
}

const typedChild = <TypedChildren>{num => num.toFixed(2)}</TypedChildren>
<ComponentWithNumberChildren>{1}{2}</ComponentWithNumberChildren>;

0 comments on commit 0aece17

Please sign in to comment.