-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use radio group in Data source manage (#680)
Co-authored-by: nina992 <[email protected]> Co-authored-by: KaWaite <[email protected]>
- Loading branch information
1 parent
884b763
commit 72a00bf
Showing
6 changed files
with
67 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,52 @@ | ||
import { memo, useCallback, useState } from "react"; | ||
import { memo, useCallback } from "react"; | ||
|
||
import RadioBox from "@reearth/beta/components/RadioGroup/RadioBox"; | ||
import { styled } from "@reearth/services/theme"; | ||
|
||
export type Option = { | ||
key: string; | ||
value: string; | ||
selected: boolean; | ||
label?: string; | ||
keyValue: string; | ||
}; | ||
|
||
export type RadioGroupProps = { | ||
options: Option[]; | ||
layout?: "vertical" | "horizontal"; | ||
selectedValue?: string; | ||
onChange?: (value: string) => void; | ||
}; | ||
|
||
const RadioGroup: React.FC<RadioGroupProps> = ({ options, layout, onChange }) => { | ||
const [currentOptions, updateOptions] = useState<Option[]>(options); | ||
const [key, setKey] = useState(0); | ||
|
||
const RadioGroup: React.FC<RadioGroupProps> = ({ | ||
options, | ||
layout = "horizontal", | ||
selectedValue, | ||
onChange, | ||
}) => { | ||
const handleRadioChange = useCallback( | ||
(value: string) => { | ||
updateOptions( | ||
currentOptions.map(option => ({ | ||
...option, | ||
selected: !option.selected && option.value === value, | ||
})), | ||
); | ||
setKey(prevKey => prevKey + 1); | ||
if (value === selectedValue) return; | ||
onChange?.(value); | ||
}, | ||
[currentOptions, onChange], | ||
[onChange, selectedValue], | ||
); | ||
|
||
return ( | ||
<RadioGroupContainer layout={layout}> | ||
{currentOptions.map(option => ( | ||
{options.map(option => ( | ||
<RadioBox | ||
key={`${option.key}-${key}`} | ||
value={option.value} | ||
selected={option.selected} | ||
onClick={() => handleRadioChange(option.value)} | ||
key={option.keyValue} | ||
selected={option.keyValue === selectedValue} | ||
label={option.label} | ||
onClick={() => handleRadioChange(option.keyValue)} | ||
/> | ||
))} | ||
</RadioGroupContainer> | ||
); | ||
}; | ||
|
||
export default memo(RadioGroup); | ||
|
||
const RadioGroupContainer = styled.div<{ layout?: "vertical" | "horizontal" }>` | ||
display: flex; | ||
flex-direction: ${({ layout }) => (layout === "vertical" ? "column" : "row")}; | ||
gap: 12px; | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters