Skip to content

Commit 9144cff

Browse files
committed
Adding example using hooks
1 parent 342efe2 commit 9144cff

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

docs/src/sections/Indeterminate.js

+48-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import { Checkbox } from 'pretty-checkbox-react';
55
import { Title } from '../components/Title';
66
import { CollapseContainer } from '../components/CollapseContainer';
77

8+
import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter';
9+
import { darcula } from 'react-syntax-highlighter/dist/styles/prism';
10+
811
import {
912
LiveProvider,
1013
LiveEditor,
@@ -41,7 +44,8 @@ const indeterminateExample = `class Indeterminate extends React.Component {
4144
shape="curve"
4245
onChange={this.handleCheckboxChange.bind(this)}
4346
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>
4549
<button className="btn btn-small ml-auto mr-2"
4650
onClick={() => this.setState({
4751
indeterminate: true,
@@ -54,6 +58,44 @@ const indeterminateExample = `class Indeterminate extends React.Component {
5458
}
5559
`;
5660

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+
5799
function Image() {
58100
return (
59101
<>
@@ -70,6 +112,11 @@ function Image() {
70112
</>
71113
</CollapseContainer>
72114
</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>
73120
</div>
74121
</>
75122
);

0 commit comments

Comments
 (0)