mirror of
https://github.com/GoogleChromeLabs/squoosh.git
synced 2025-11-19 20:19:05 +00:00
Use strict TypeScript, enable TSLint, update all types to work in strict mode.
This commit is contained in:
@@ -1,19 +0,0 @@
|
||||
import { Component } from 'preact';
|
||||
|
||||
export function updater(obj, property, value) {
|
||||
return e => {
|
||||
let update = {};
|
||||
update[property] = typeof value === 'function' ? value(obj.state[property], e) : value;
|
||||
obj.setState(update);
|
||||
};
|
||||
}
|
||||
|
||||
export const toggle = value => !value;
|
||||
|
||||
export class When extends Component {
|
||||
state = { ready: !!this.props.value };
|
||||
render({ value, children: [child] }, { ready }) {
|
||||
if (value && !ready) this.setState({ ready: true });
|
||||
return ready ? (typeof child === 'function' ? child() : child) : null;
|
||||
}
|
||||
}
|
||||
39
src/lib/util.ts
Normal file
39
src/lib/util.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { Component, ComponentProps } from 'preact';
|
||||
|
||||
type WhenProps = ComponentProps<When> & {
|
||||
value: boolean,
|
||||
children?: (JSX.Element | (() => JSX.Element))[]
|
||||
};
|
||||
|
||||
type WhenState = {
|
||||
ready: boolean
|
||||
};
|
||||
|
||||
export class When extends Component<WhenProps, WhenState> {
|
||||
state: WhenState = {
|
||||
ready: !!this.props.value
|
||||
};
|
||||
|
||||
render({ value, children = [] }: WhenProps, { ready }: WhenState) {
|
||||
let child = children[0];
|
||||
if (value && !ready) this.setState({ ready: true });
|
||||
return ready ? (typeof child === 'function' ? child() : child) : null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A decorator that binds values to their class instance.
|
||||
* @example
|
||||
* class C {
|
||||
* @bind
|
||||
* foo () {
|
||||
* return this;
|
||||
* }
|
||||
* }
|
||||
* let f = new C().foo;
|
||||
* f() instanceof C; // true
|
||||
*/
|
||||
export function bind(target: any, propertyKey: string, descriptor: PropertyDescriptor) {
|
||||
descriptor.value = descriptor.value.bind(target);
|
||||
return descriptor;
|
||||
}
|
||||
Reference in New Issue
Block a user