From 205f7d2a7e2554dc654b3f9d4391a4009c0d6fbd Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Sun, 1 Jan 2023 18:35:02 +0000 Subject: [PATCH] chore: Move Input to functional component (#4629) * chore: Remove ReactHookWrappedInput workaround Move Input to functional component * I love Typescript --- app/components/Input.tsx | 150 ++++++++---------- app/components/InputSearch.tsx | 2 +- app/components/InputSearchPage.tsx | 2 +- app/scenes/Settings/GoogleAnalytics.tsx | 2 +- app/scenes/Settings/SelfHosted.tsx | 2 +- .../components/WebhookSubscriptionForm.tsx | 8 +- app/scenes/UserDelete.tsx | 2 +- 7 files changed, 78 insertions(+), 90 deletions(-) diff --git a/app/components/Input.tsx b/app/components/Input.tsx index 7cad0db43..5a0d67bf5 100644 --- a/app/components/Input.tsx +++ b/app/components/Input.tsx @@ -1,5 +1,3 @@ -import { observable } from "mobx"; -import { observer } from "mobx-react"; import * as React from "react"; import { VisuallyHidden } from "reakit/VisuallyHidden"; import styled from "styled-components"; @@ -123,93 +121,89 @@ export type Props = React.InputHTMLAttributes< margin?: string | number; error?: string; icon?: React.ReactNode; - innerRef?: React.Ref; onFocus?: (ev: React.SyntheticEvent) => unknown; onBlur?: (ev: React.SyntheticEvent) => unknown; }; -@observer -class Input extends React.Component { - input = this.props.innerRef; +function Input( + props: Props, + ref: React.RefObject +) { + const [focused, setFocused] = React.useState(false); - @observable - focused = false; + const handleBlur = (ev: React.SyntheticEvent) => { + setFocused(false); - handleBlur = (ev: React.SyntheticEvent) => { - this.focused = false; - - if (this.props.onBlur) { - this.props.onBlur(ev); + if (props.onBlur) { + props.onBlur(ev); } }; - handleFocus = (ev: React.SyntheticEvent) => { - this.focused = true; + const handleFocus = (ev: React.SyntheticEvent) => { + setFocused(true); - if (this.props.onFocus) { - this.props.onFocus(ev); + if (props.onFocus) { + props.onFocus(ev); } }; - render() { - const { - type = "text", - icon, - label, - margin, - error, - className, - short, - flex, - labelHidden, - onFocus, - onBlur, - ...rest - } = this.props; + const { + type = "text", + icon, + label, + margin, + error, + className, + short, + flex, + labelHidden, + onFocus, + onBlur, + ...rest + } = props; - const wrappedLabel = {label}; + const wrappedLabel = {label}; - return ( - - - {error && ( - - - {error} - - - )} - - ); - } + return ( + + + {error && ( + + + {error} + + + )} + + ); } export const TextWrapper = styled.span` @@ -222,10 +216,4 @@ export const StyledText = styled(Text)` margin-bottom: 0; `; -export const ReactHookWrappedInput = React.forwardRef( - (props: Omit, ref: React.Ref) => { - return ; - } -); - -export default Input; +export default React.forwardRef(Input); diff --git a/app/components/InputSearch.tsx b/app/components/InputSearch.tsx index a49c7f648..77e7de243 100644 --- a/app/components/InputSearch.tsx +++ b/app/components/InputSearch.tsx @@ -42,7 +42,7 @@ function InputSearch( onBlur={handleBlur} margin={0} labelHidden - innerRef={ref} + ref={ref} {...rest} /> ); diff --git a/app/components/InputSearchPage.tsx b/app/components/InputSearchPage.tsx index 289673688..35fe49540 100644 --- a/app/components/InputSearchPage.tsx +++ b/app/components/InputSearchPage.tsx @@ -68,7 +68,7 @@ function InputSearchPage({ return ( - - -