Skip to content

Commit

Permalink
fix: state in child component
Browse files Browse the repository at this point in the history
  • Loading branch information
sandrvvu committed Dec 3, 2024
1 parent deadd9c commit 50b9f1d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
19 changes: 17 additions & 2 deletions src/design-system/components/chip/Chip.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react'
import React, { useState } from 'react'
import {
ChipProps,
FilterChip,
Expand All @@ -8,9 +8,24 @@ import {
} from './ChipTypes'

const Chip: React.FC<ChipProps> = (props) => {
const [isOpen, setIsOpen] = useState(false)
const [selectedOption, setSelectedOption] = useState<string | null>(null)

const handleSelectChange = (option: string) => {
setSelectedOption(option)
}

switch (props.type) {
case 'filter':
return <FilterChip {...props} />
return (
<FilterChip
{...props}
isOpen={isOpen}
onSelectChange={handleSelectChange}
selectedOption={selectedOption}
setIsOpen={setIsOpen}
/>
)
case 'input':
return <InputChip {...props} />
case 'category':
Expand Down
24 changes: 16 additions & 8 deletions src/design-system/components/chip/ChipTypes.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { CSSProperties, useState } from 'react'
import React, { CSSProperties } from 'react'
import { cn } from '~/utils/cn'
import CircleIcon from '@mui/icons-material/Circle'
import CloseRoundedIcon from '@mui/icons-material/CloseRounded'
Expand Down Expand Up @@ -99,22 +99,30 @@ const BaseChip: React.FC<
)
}

export const FilterChip: React.FC<FilterChipProps> = ({
export const FilterChip: React.FC<
FilterChipProps & {
isOpen: boolean
selectedOption: string | null
setIsOpen: (isOpen: boolean) => void
onSelectChange: (option: string) => void
}
> = ({
type,
label,
options = [],
variant = 'filled',
startIcon = <CircleIcon style={{ fontSize: 'inherit' }} />,
endIcon = <ExpandMoreIcon style={{ fontSize: 'inherit' }} />,
disabled = false,
size = 'md'
size = 'md',
isOpen,
setIsOpen,
selectedOption,
onSelectChange
}) => {
const [isOpen, setIsOpen] = useState(false)
const [selectedOption, setSelectedOption] = useState<string | null>(null)

const handleSelect = (option: string) => {
setSelectedOption(option)
setIsOpen((prev) => !prev)
onSelectChange(option)
setIsOpen(false)
}

const isSelected = Boolean(selectedOption)
Expand Down

0 comments on commit 50b9f1d

Please sign in to comment.