Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

refactor: Migrate Button component from class to function style (#1020)

Anass-Daoudi1c4178f9fdf1
diff --git a/public/components/common/Button.tsx b/public/components/common/Button.tsx
index fed514cf4..3bc6423b0 100644
--- a/public/components/common/Button.tsx
+++ b/public/components/common/Button.tsx
@@ -1,6 +1,6 @@
import "./Button.scss"
-import React from "react"
+import React, { useEffect, useRef, useState } from "react"
import { classSet } from "@fider/services"
interface ButtonProps {
@@ -15,10 +15,6 @@ interface ButtonProps {
onClick?: (event: ButtonClickEvent) => Promise<any> | void
}
-interface ButtonState {
- clicked: boolean
-}
-
export class ButtonClickEvent {
private shouldEnable = true
public preventEnable(): void {
@@ -29,75 +25,74 @@ export class ButtonClickEvent {
}
}
-export class Button extends React.Component<ButtonProps, ButtonState> {
- private unmounted = false
+export const Button: React.FC<ButtonProps> = (props) => {
+ const [clicked, setClicked] = useState(false)
+ const unmountedContainer = useRef(false)
- public static defaultProps: Partial<ButtonProps> = {
- size: "default",
- variant: "secondary",
- type: "button",
- }
-
- public constructor(props: ButtonProps) {
- super(props)
- this.state = {
- clicked: false,
+ useEffect(() => {
+ return () => {
+ unmountedContainer.current = true
}
- }
+ }, [])
- public componentWillUnmount() {
- this.unmounted = true
- }
+ const className = classSet({
+ "c-button": true,
+ [`c-button--${props.size}`]: props.size,
+ [`c-button--${props.variant}`]: props.variant,
+ "c-button--loading": clicked,
+ "c-button--disabled": clicked || props.disabled,
+ [props.className || ""]: props.className,
+ "shadow-sm": props.variant !== "tertiary",
+ })
- public click = async (e?: React.SyntheticEvent<HTMLElement>) => {
- if (e) {
- e.preventDefault()
- e.stopPropagation()
- }
+ let buttonContent: JSX.Element
+ const onClickProp = props.onClick
- if (this.state.clicked) {
- return
- }
+ if (props.href) {
+ buttonContent = (
+ <a href={props.href} rel={props.rel} target={props.target} className={className}>
+ {props.children}
+ </a>
+ )
+ } else if (onClickProp) {
+ const onClick = async (e?: React.SyntheticEvent<HTMLElement>) => {
+ if (e) {
+ e.preventDefault()
+ e.stopPropagation()
+ }
- const event = new ButtonClickEvent()
- this.setState({ clicked: true })
- if (this.props.onClick) {
- await this.props.onClick(event)
- if (!this.unmounted && event.canEnable()) {
- this.setState({ clicked: false })
+ if (clicked) {
+ return
}
- }
- }
- public render() {
- const className = classSet({
- "c-button": true,
- [`c-button--${this.props.size}`]: this.props.size,
- [`c-button--${this.props.variant}`]: this.props.variant,
- "c-button--loading": this.state.clicked,
- "c-button--disabled": this.state.clicked || this.props.disabled,
- [this.props.className || ""]: this.props.className,
- "shadow-sm": this.props.variant !== "tertiary",
- })
+ const event = new ButtonClickEvent()
+ setClicked(true)
- if (this.props.href) {
- return (
- <a href={this.props.href} rel={this.props.rel} target={this.props.target} className={className}>
- {this.props.children}
- </a>
- )
- } else if (this.props.onClick) {
- return (
- <button type={this.props.type} className={className} onClick={this.click}>
- {this.props.children}
- </button>
- )
- } else {
- return (
- <button type={this.props.type} className={className}>
- {this.props.children}
- </button>
- )
+ await onClickProp(event)
+
+ if (!unmountedContainer.current && event.canEnable()) {
+ setClicked(false)
+ }
}
+
+ buttonContent = (
+ <button type={props.type} className={className} onClick={onClick}>
+ {props.children}
+ </button>
+ )
+ } else {
+ buttonContent = (
+ <button type={props.type} className={className}>
+ {props.children}
+ </button>
+ )
}
+
+ return buttonContent
+}
+
+Button.defaultProps = {
+ size: "default",
+ variant: "secondary",
+ type: "button",
}