diff --git a/packages/react-core/src/components/ChipGroup/examples/ChipGroup.md b/packages/react-core/src/components/ChipGroup/examples/ChipGroup.md index 42e689284f1..9eacd216aca 100644 --- a/packages/react-core/src/components/ChipGroup/examples/ChipGroup.md +++ b/packages/react-core/src/components/ChipGroup/examples/ChipGroup.md @@ -8,145 +8,17 @@ ouia: true ## Examples -### Simple inline chip group +### Simple inline -```js -import React from 'react'; -import { Chip, ChipGroup } from '@patternfly/react-core'; - -class SimpleInlineChipGroup extends React.Component { - constructor(props) { - super(props); - this.state = { - chips: ['Chip one', 'Really long chip that goes on and on', 'Chip three', 'Chip four', 'Chip five'] - }; - - this.deleteItem = id => { - const copyOfChips = this.state.chips; - const index = copyOfChips.indexOf(id); - if (index !== -1) { - copyOfChips.splice(index, 1); - this.setState({ chips: copyOfChips }); - } - }; - } - - render() { - const { chips } = this.state; - - return ( - - {chips.map(currentChip => ( - this.deleteItem(currentChip)}> - {currentChip} - - ))} - - ); - } -} +```ts file='./ChipGroupInline.tsx' ``` -### Chip groups with categories - -```js -import React from 'react'; -import { Chip, ChipGroup } from '@patternfly/react-core'; - -class SimpleCategoryChipGroup extends React.Component { - constructor(props) { - super(props); - this.state = { - chips: ['Chip one', 'Chip two', 'Chip three', 'Chip four', 'Chip five'] - }; - this.deleteItem = id => { - const copyOfChips = this.state.chips; - const index = copyOfChips.indexOf(id); - if (index !== -1) { - copyOfChips.splice(index, 1); - this.setState({ chips: copyOfChips }); - } - }; - } - - render() { - const { chips } = this.state; +### With categories - return ( - - {chips.map(currentChip => ( - this.deleteItem(currentChip)}> - {currentChip} - - ))} - - ); - } -} +```ts file='./ChipGroupWithCategories.tsx' ``` -### Chip groups with categories removable - -```js -import React from 'react'; -import { Chip, ChipGroup } from '@patternfly/react-core'; - -class CategoryChipGroupRemovable extends React.Component { - constructor(props) { - super(props); - this.state = { - chips: ['Chip one', 'Chip two', 'Chip three'], - chips2: ['Chip one', 'Chip two', 'Chip three', 'Chip 4'] - }; - this.deleteItem = id => { - const copyOfChips = this.state.chips; - const index = copyOfChips.indexOf(id); - if (index !== -1) { - copyOfChips.splice(index, 1); - this.setState({ chips: copyOfChips }); - } - }; - - this.deleteItem2 = id => { - const copyOfChips = this.state.chips2; - const index = copyOfChips.indexOf(id); - if (index !== -1) { - copyOfChips.splice(index, 1); - this.setState({ chips2: copyOfChips }); - } - }; - - this.deleteCategory = () => { - this.setState({ chips: [] }); - }; - - this.deleteCategory2 = () => { - this.setState({ chips2: [] }); - }; - } - - render() { - const { chips, chips2 } = this.state; +### With removable categories - return ( - - - {chips.map(currentChip => ( - this.deleteItem(currentChip)}> - {currentChip} - - ))} - -

- - {chips2.map(currentChip => ( - this.deleteItem2(currentChip)}> - {currentChip} - - ))} - -
- ); - } -} +```ts file='./ChipGroupRemovableCategories.tsx' ``` diff --git a/packages/react-core/src/components/ChipGroup/examples/ChipGroupInline.tsx b/packages/react-core/src/components/ChipGroup/examples/ChipGroupInline.tsx new file mode 100644 index 00000000000..891dbe81534 --- /dev/null +++ b/packages/react-core/src/components/ChipGroup/examples/ChipGroupInline.tsx @@ -0,0 +1,28 @@ +import React from 'react'; +import { Chip, ChipGroup } from '@patternfly/react-core'; + +export const ChipGroupInline: React.FunctionComponent = () => { + const [chips, setChips] = React.useState([ + 'Chip one', + 'Really long chip that goes on and on', + 'Chip three', + 'Chip four', + 'Chip five' + ]); + + const deleteItem = (id: string) => { + const copyOfChips = [...chips]; + const filteredCopy = copyOfChips.filter(chip => chip !== id); + setChips(filteredCopy); + }; + + return ( + + {chips.map(currentChip => ( + deleteItem(currentChip)}> + {currentChip} + + ))} + + ); +}; diff --git a/packages/react-core/src/components/ChipGroup/examples/ChipGroupRemovableCategories.tsx b/packages/react-core/src/components/ChipGroup/examples/ChipGroupRemovableCategories.tsx new file mode 100644 index 00000000000..6cf9df8b644 --- /dev/null +++ b/packages/react-core/src/components/ChipGroup/examples/ChipGroupRemovableCategories.tsx @@ -0,0 +1,46 @@ +import React from 'react'; +import { Chip, ChipGroup } from '@patternfly/react-core'; + +export const ChipGroupRemovableCategories: React.FunctionComponent = () => { + const [chipGroup1, setChipGroup1] = React.useState(['Chip one', 'Chip two', 'Chip three']); + const [chipGroup2, setChipGroup2] = React.useState(['Chip one', 'Chip two', 'Chip three', 'Chip four']); + + const deleteItem = (id: string, group: string[]) => { + const copyOfChips = [...group]; + const filteredCopy = copyOfChips.filter(chip => chip !== id); + + if (group === chipGroup1) { + setChipGroup1(filteredCopy); + } else { + setChipGroup2(filteredCopy); + } + }; + + const deleteCategory = (group: string[]) => { + if (group === chipGroup1) { + setChipGroup1([]); + } else { + setChipGroup2([]); + } + }; + + return ( + + deleteCategory(chipGroup1)}> + {chipGroup1.map(currentChip => ( + deleteItem(currentChip, chipGroup1)}> + {currentChip} + + ))} + +

+ deleteCategory(chipGroup2)}> + {chipGroup2.map(currentChip => ( + deleteItem(currentChip, chipGroup2)}> + {currentChip} + + ))} + +
+ ); +}; diff --git a/packages/react-core/src/components/ChipGroup/examples/ChipGroupWithCategories.tsx b/packages/react-core/src/components/ChipGroup/examples/ChipGroupWithCategories.tsx new file mode 100644 index 00000000000..09a5d508201 --- /dev/null +++ b/packages/react-core/src/components/ChipGroup/examples/ChipGroupWithCategories.tsx @@ -0,0 +1,28 @@ +import React from 'react'; +import { Chip, ChipGroup } from '@patternfly/react-core'; + +export const ChipGroupWithCategories: React.FunctionComponent = () => { + const [chips, setChips] = React.useState([ + 'Chip one', + 'Really long chip that goes on and on', + 'Chip three', + 'Chip four', + 'Chip five' + ]); + + const deleteItem = (id: string) => { + const copyOfChips = [...chips]; + const filteredCopy = copyOfChips.filter(chip => chip !== id); + setChips(filteredCopy); + }; + + return ( + + {chips.map(currentChip => ( + deleteItem(currentChip)}> + {currentChip} + + ))} + + ); +};