-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.tsx
51 lines (46 loc) · 1.34 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import * as React from "react";
import { createPraise, Phrases, Config } from "praise";
export interface PraiseProps extends Config {
/**
* An object whose keys are strings of phrases to respond to,
* the values of which are functions to execute upon
* recognizing the phrase.
*/
phrases: Phrases;
/** Children ought to be a function that accept
* the result of the recognized callback as input.
*/
children: (output: any) => React.ReactChild;
}
export interface PraiseState {
output?: any;
}
/**
* A React wrapper for Praise that takes its
* Config as top-level props and passes the
* value returned from the callback response
* to a phrase into its children using the
* children as a function (or render props)
* pattern.
*
* @augments React.Component<PraiseProps, PraiseState>
*/
class Praise extends React.Component<PraiseProps, PraiseState> {
state: PraiseState = {};
componentDidMount() {
const { phrases, ...otherProps } = this.props;
const { keepListening, onlyWhenIStop, confidence } = otherProps;
createPraise(phrases, {
keepListening,
onlyWhenIStop,
confidence,
onSuccess: output => this.setState(() => ({ output }))
}).start();
}
render() {
const { children } = this.props;
const { output } = this.state;
return children(output);
}
}
export default Praise;