From 06c5e931b61a6a778811ca4c7d6046b4e86b709b Mon Sep 17 00:00:00 2001 From: Eric Olkowski Date: Tue, 8 Mar 2022 14:09:38 -0500 Subject: [PATCH] chore(Checkbox): convert examples to TypeScript/functional components --- .../components/Checkbox/examples/Checkbox.md | 126 ++---------------- .../Checkbox/examples/CheckboxControlled.tsx | 78 +++++++++++ .../Checkbox/examples/CheckboxDisabled.tsx | 9 ++ .../examples/CheckboxStandaloneInput.tsx | 6 + .../examples/CheckboxUncontrolled.tsx | 9 ++ .../Checkbox/examples/CheckboxWithBody.tsx | 6 + .../examples/CheckboxWithDescription.tsx | 10 ++ .../examples/CheckboxWithDescriptionBody.tsx | 11 ++ 8 files changed, 140 insertions(+), 115 deletions(-) create mode 100644 packages/react-core/src/components/Checkbox/examples/CheckboxControlled.tsx create mode 100644 packages/react-core/src/components/Checkbox/examples/CheckboxDisabled.tsx create mode 100644 packages/react-core/src/components/Checkbox/examples/CheckboxStandaloneInput.tsx create mode 100644 packages/react-core/src/components/Checkbox/examples/CheckboxUncontrolled.tsx create mode 100644 packages/react-core/src/components/Checkbox/examples/CheckboxWithBody.tsx create mode 100644 packages/react-core/src/components/Checkbox/examples/CheckboxWithDescription.tsx create mode 100644 packages/react-core/src/components/Checkbox/examples/CheckboxWithDescriptionBody.tsx diff --git a/packages/react-core/src/components/Checkbox/examples/Checkbox.md b/packages/react-core/src/components/Checkbox/examples/Checkbox.md index 7d06d77e1c3..d46293e9d8b 100644 --- a/packages/react-core/src/components/Checkbox/examples/Checkbox.md +++ b/packages/react-core/src/components/Checkbox/examples/Checkbox.md @@ -8,142 +8,38 @@ propComponents: ['Checkbox'] import './checkbox.css'; ## Examples + ### Controlled -```js -import React from 'react'; -import { Checkbox } from '@patternfly/react-core'; - -class ControlledCheckbox extends React.Component { - constructor(props) { - super(props); - this.state = { - check1: false, - check2: false, - check3: false, - check4: false - }; - this.handleChange = (checked, event) => { - const target = event.target; - const value = target.type === 'checkbox' ? target.checked : target.value; - const name = target.name; - this.setState({ [name]: value }); - }; - } - - componentDidUpdate(_prevProps, prevState) { - if (prevState.check1 !== this.state.check1 && this.state.check1 !== null) { - this.setState({ - check2: this.state.check1, - check3: this.state.check1, - }) - } - - if (prevState.check2 !== this.state.check2 || prevState.check3 !== this.state.check3) { - this.setState({ - check1: (this.state.check2 && this.state.check3) || (this.state.check2 || this.state.check3 ? null : false) - }) - } - } - - render() { - return ( - - - - - - - ); - } -} + +```ts file='./CheckboxControlled.tsx' ``` ### Uncontrolled -```js -import React from 'react'; -import { Checkbox } from '@patternfly/react-core'; - - - - - + +```ts file='./CheckboxUncontrolled.tsx' ``` ### Disabled -```js -import React from 'react'; -import { Checkbox } from '@patternfly/react-core'; - - - {' '} - - + +```ts file='./CheckboxDisabled.tsx' ``` ### Checkbox with description -```js -import React from 'react'; -import { Checkbox } from '@patternfly/react-core'; - +```ts file='./CheckboxWithDescription.tsx' ``` ### Checkbox with body -```js -import React from 'react'; -import { Checkbox } from '@patternfly/react-core'; - +```ts file='./CheckboxWithBody.tsx' ``` ### Checkbox with description and body -```js -import React from 'react'; -import { Checkbox } from '@patternfly/react-core'; - +```ts file='./CheckboxWithDescriptionBody.tsx' ``` ### Standalone input -```js -import React from 'react'; -import { Checkbox } from '@patternfly/react-core'; - +```ts file='./CheckboxStandaloneInput.tsx' ``` diff --git a/packages/react-core/src/components/Checkbox/examples/CheckboxControlled.tsx b/packages/react-core/src/components/Checkbox/examples/CheckboxControlled.tsx new file mode 100644 index 00000000000..4fa82632175 --- /dev/null +++ b/packages/react-core/src/components/Checkbox/examples/CheckboxControlled.tsx @@ -0,0 +1,78 @@ +import React from 'react'; +import { Checkbox } from '@patternfly/react-core'; + +export const CheckboxControlled: React.FunctionComponent = () => { + const [isChecked1, setIsChecked1] = React.useState(false); + const [isChecked2, setIsChecked2] = React.useState(false); + const [isChecked3, setIsChecked3] = React.useState(false); + const [isChecked4, setIsChecked4] = React.useState(false); + + const handleChange = (checked: boolean, event: React.FormEvent) => { + const target = event.currentTarget; + const name = target.name; + + switch (name) { + case 'check1': + setIsChecked1(checked); + break; + case 'check2': + setIsChecked2(checked); + break; + case 'check3': + setIsChecked3(checked); + break; + case 'check4': + setIsChecked4(checked); + break; + default: + // eslint-disable-next-line no-console + console.log(name); + } + }; + + React.useEffect(() => { + if (isChecked1 !== null) { + setIsChecked2(isChecked1); + setIsChecked3(isChecked1); + } + }, [isChecked1]); + + React.useEffect(() => { + setIsChecked1((isChecked2 && isChecked3) || (isChecked2 || isChecked3 ? null : false)); + }, [isChecked2, isChecked3]); + + return ( + + + + + + + ); +}; diff --git a/packages/react-core/src/components/Checkbox/examples/CheckboxDisabled.tsx b/packages/react-core/src/components/Checkbox/examples/CheckboxDisabled.tsx new file mode 100644 index 00000000000..79e388cfc27 --- /dev/null +++ b/packages/react-core/src/components/Checkbox/examples/CheckboxDisabled.tsx @@ -0,0 +1,9 @@ +import React from 'react'; +import { Checkbox } from '@patternfly/react-core'; + +export const CheckboxDisabled: React.FunctionComponent = () => ( + + + + +); diff --git a/packages/react-core/src/components/Checkbox/examples/CheckboxStandaloneInput.tsx b/packages/react-core/src/components/Checkbox/examples/CheckboxStandaloneInput.tsx new file mode 100644 index 00000000000..c594162b8a4 --- /dev/null +++ b/packages/react-core/src/components/Checkbox/examples/CheckboxStandaloneInput.tsx @@ -0,0 +1,6 @@ +import React from 'react'; +import { Checkbox } from '@patternfly/react-core'; + +export const CheckboxStandaloneInput: React.FunctionComponent = () => ( + +); diff --git a/packages/react-core/src/components/Checkbox/examples/CheckboxUncontrolled.tsx b/packages/react-core/src/components/Checkbox/examples/CheckboxUncontrolled.tsx new file mode 100644 index 00000000000..fbc92cd5623 --- /dev/null +++ b/packages/react-core/src/components/Checkbox/examples/CheckboxUncontrolled.tsx @@ -0,0 +1,9 @@ +import React from 'react'; +import { Checkbox } from '@patternfly/react-core'; + +export const CheckboxUncontrolled: React.FunctionComponent = () => ( + + + + +); diff --git a/packages/react-core/src/components/Checkbox/examples/CheckboxWithBody.tsx b/packages/react-core/src/components/Checkbox/examples/CheckboxWithBody.tsx new file mode 100644 index 00000000000..c660431cae7 --- /dev/null +++ b/packages/react-core/src/components/Checkbox/examples/CheckboxWithBody.tsx @@ -0,0 +1,6 @@ +import React from 'react'; +import { Checkbox } from '@patternfly/react-core'; + +export const CheckboxWithBody: React.FunctionComponent = () => ( + +); diff --git a/packages/react-core/src/components/Checkbox/examples/CheckboxWithDescription.tsx b/packages/react-core/src/components/Checkbox/examples/CheckboxWithDescription.tsx new file mode 100644 index 00000000000..2b7b7517357 --- /dev/null +++ b/packages/react-core/src/components/Checkbox/examples/CheckboxWithDescription.tsx @@ -0,0 +1,10 @@ +import React from 'react'; +import { Checkbox } from '@patternfly/react-core'; + +export const CheckboxWithDescription: React.FunctionComponent = () => ( + +); diff --git a/packages/react-core/src/components/Checkbox/examples/CheckboxWithDescriptionBody.tsx b/packages/react-core/src/components/Checkbox/examples/CheckboxWithDescriptionBody.tsx new file mode 100644 index 00000000000..faa13944140 --- /dev/null +++ b/packages/react-core/src/components/Checkbox/examples/CheckboxWithDescriptionBody.tsx @@ -0,0 +1,11 @@ +import React from 'react'; +import { Checkbox } from '@patternfly/react-core'; + +export const CheckboxWithDescriptionBody: React.FunctionComponent = () => ( + +);