@@ -5,6 +5,9 @@ import { Checkbox } from 'pretty-checkbox-react';
5
5
import { Title } from '../components/Title' ;
6
6
import { CollapseContainer } from '../components/CollapseContainer' ;
7
7
8
+ import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter' ;
9
+ import { darcula } from 'react-syntax-highlighter/dist/styles/prism' ;
10
+
8
11
import {
9
12
LiveProvider ,
10
13
LiveEditor ,
@@ -41,7 +44,8 @@ const indeterminateExample = `class Indeterminate extends React.Component {
41
44
shape="curve"
42
45
onChange={this.handleCheckboxChange.bind(this)}
43
46
checked={this.state.checked}
44
- icon={<i className={"mdi " + (this.state.indeterminate ? "mdi-minus" : "mdi-check")} />}>Indeterminate</Checkbox>
47
+ icon={<i className={"mdi " + (this.state.indeterminate ? "mdi-minus" : "mdi-check")} />}>
48
+ Indeterminate</Checkbox>
45
49
<button className="btn btn-small ml-auto mr-2"
46
50
onClick={() => this.setState({
47
51
indeterminate: true,
@@ -54,6 +58,44 @@ const indeterminateExample = `class Indeterminate extends React.Component {
54
58
}
55
59
` ;
56
60
61
+ const withHooks = `// converting the class example to use hooks...
62
+ import * as React from 'react';
63
+ import { Checkbox } from 'pretty-checkbox-react';
64
+
65
+ function Indeterminate(props) {
66
+ const [indeterminate, setIndeterminate] = React.useState(props.indeterminate);
67
+ const [checked, setChecked ] = React.useState(props.checked);
68
+
69
+ const handleCheckboxChange = React.useCallback(() => {
70
+ setChecked(!checked);
71
+
72
+ if (indeterminate) {
73
+ setIndeterminate(false);
74
+ }
75
+ }, [checked, setChecked, indeterminate, setIndeterminate]);
76
+
77
+ return (
78
+ <>
79
+ <Checkbox indeterminate={indeterminate}
80
+ shape="curve"
81
+ onChange={handleCheckboxChange}
82
+ checked={checked}
83
+ icon={
84
+ <i className={"mdi "
85
+ + (indeterminate ? "mdi-minus" : "mdi-check")} />
86
+ }>
87
+ Indeterminate</Checkbox>
88
+ <button className="btn btn-small ml-auto mr-2"
89
+ onClick={() => {
90
+ setIndeterminate(true);
91
+ setChecked(true);
92
+ }}
93
+ disabled={indeterminate}>Apply</button>
94
+ </>
95
+ );
96
+ }
97
+ ` ;
98
+
57
99
function Image ( ) {
58
100
return (
59
101
< >
@@ -70,6 +112,11 @@ function Image() {
70
112
</ >
71
113
</ CollapseContainer >
72
114
</ LiveProvider >
115
+ < CollapseContainer title = "With Hooks" collapsed = { false } >
116
+ < p > React hooks are relatively new and are currently in alpha.
117
+ If you are on the bleeding edge then this implementation is for you! < a href = "https://reactjs.org/docs/hooks-intro.html" target = "_blank" rel = "nofollow noreferrer noopener" > Read more about hooks</ a > .</ p >
118
+ < SyntaxHighlighter language = "javascript" style = { darcula } > { withHooks } </ SyntaxHighlighter >
119
+ </ CollapseContainer >
73
120
</ div >
74
121
</ >
75
122
) ;
0 commit comments