Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

refactor: Migrate RadioButton from class to function style (#1021)

Anass-Daoudi2f27a3717ae9
diff --git a/public/components/common/form/RadioButton.tsx b/public/components/common/form/RadioButton.tsx
index 9c4a0a8ba..066bb2e3c 100644
--- a/public/components/common/form/RadioButton.tsx
+++ b/public/components/common/form/RadioButton.tsx
@@ -1,5 +1,5 @@
import { HStack, VStack } from "@fider/components/layout"
-import React from "react"
+import React, { useState } from "react"
import "./RadioButton.scss"
@@ -16,47 +16,25 @@ interface RadioButtonProps {
onSelect?: (value: RadioButtonOption) => void
}
-interface RadioButtonState {
- selected: RadioButtonOption
-}
-
-export class RadioButton extends React.Component<RadioButtonProps, RadioButtonState> {
- constructor(props: RadioButtonProps) {
- super(props)
- this.state = {
- selected: props.defaultOption || props.options[0],
- }
- }
+export const RadioButton = (props: RadioButtonProps) => {
+ const [selected, setSelected] = useState(props.defaultOption)
- private onChange = (selected: RadioButtonOption) => {
- this.setState({ selected }, () => {
- if (this.props.onSelect) {
- this.props.onSelect(this.state.selected)
- }
- })
+ const onChange = (option: RadioButtonOption) => () => {
+ setSelected(option)
+ props.onSelect?.(option)
}
- public render() {
- const inputs = this.props.options.map((option) => {
- return (
- <HStack key={option.value} className="text-sm">
- <input
- id={`visibility-${option.value}`}
- type="radio"
- name={`input-${this.props.field}`}
- checked={this.state.selected === option}
- onChange={this.onChange.bind(this, option)}
- />
- <label htmlFor={`visibility-${option.value}`}>{option.label}</label>
- </HStack>
- )
- })
-
- return (
- <div className="c-form-field">
- <label htmlFor={`input-${this.props.field}`}>{this.props.label}</label>
- <VStack className="c-radiobutton">{inputs}</VStack>
- </div>
- )
- }
+ const inputs = props.options.map((option) => (
+ <HStack key={option.value} className="text-sm">
+ <input id={`visibility-${option.value}`} type="radio" name={`input-${props.field}`} checked={selected === option} onChange={onChange(option)} />
+ <label htmlFor={`visibility-${option.value}`}>{option.label}</label>
+ </HStack>
+ ))
+
+ return (
+ <div className="c-form-field">
+ <label htmlFor={`input-${props.field}`}>{props.label}</label>
+ <VStack className="c-radiobutton">{inputs}</VStack>
+ </div>
+ )
}