@enact/core
Version:
Enact is an open source JavaScript framework containing everything you need to create a fast, scalable mobile or web application.
44 lines (39 loc) • 1.37 kB
TypeScript
// Type definitions for core/hoc
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
type Merge<M, N> = Omit<M, Extract<keyof M, keyof N>> & N;
/**
* Constructs a higher-order component (HOC) using an optional set of default configuration parameters and
a factory method that accepts instance configuration parameters and a component to wrap. The
returned function can accept:
* * an instance config and a component constructor to wrap and return a renderable component, or
* * an instance config only and return a decorator function expecting a component constructor
(like the next bullet), or
* * a component constructor and return a renderable component
*
* Example:
* ```
const Countable = hoc({prop: 'data-count'}, (config, Wrapped) => {
return class extends React.Component {
constructor (props) {
super(props);
this.state = {
count: 0
};
},
inc = () => this.setState({count: this.state.count + 1}),
render () {
const props = Object.assign({}, this.props, {
[config.prop]: this.state.count,
onClick: this.inc
});
return <Wrapped {...props} />
}
}
});
const CountableAsDataNumber({prop: 'data-number'});
const CountableDiv('div');
const CountableDivAsDataNumber = CountableAsDataNumber('div');
```
*/
export function hoc(defaultConfig: object, hawk: Function): Function;
export default hoc;