From 9bd48005fda1d94a526e36bec256b56add65b28d Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Fri, 28 Oct 2022 00:23:40 +0300 Subject: use `react-hook-form` and fix tests --- vnext/src/ui/Input.css | 8 ------- vnext/src/ui/Input.js | 23 ------------------- vnext/src/ui/Login.js | 26 ++++++++-------------- vnext/src/ui/SearchBox.js | 17 +++++++------- .../ui/__tests__/__snapshots__/Avatar.test.js.snap | 2 +- .../__tests__/__snapshots__/UserLink.test.js.snap | 3 +-- 6 files changed, 20 insertions(+), 59 deletions(-) delete mode 100644 vnext/src/ui/Input.css delete mode 100644 vnext/src/ui/Input.js (limited to 'vnext/src') diff --git a/vnext/src/ui/Input.css b/vnext/src/ui/Input.css deleted file mode 100644 index 6a1e659f..00000000 --- a/vnext/src/ui/Input.css +++ /dev/null @@ -1,8 +0,0 @@ -.input { - background: #FFF; - border: 1px solid #ccc; - outline: none !important; - padding: 4px; - appearance: none; - border-radius: 0; -} diff --git a/vnext/src/ui/Input.js b/vnext/src/ui/Input.js deleted file mode 100644 index 2f01f0c6..00000000 --- a/vnext/src/ui/Input.js +++ /dev/null @@ -1,23 +0,0 @@ -import { memo } from 'react'; - -import './Input.css'; - -/** - * @typedef {Object} InputProps - * @property {string} name - * @property {string} value - * @property {string=} placeholder - * @property {React.CSSProperties=} rest - */ - -/** - * Input component - * @param {InputProps} props - */ -function Input({ name, value, ...rest }) { - return ( - - ); -} - -export default memo(Input); diff --git a/vnext/src/ui/Login.js b/vnext/src/ui/Login.js index 774664c0..fc3922c8 100644 --- a/vnext/src/ui/Login.js +++ b/vnext/src/ui/Login.js @@ -3,8 +3,7 @@ import { useLocation, useHistory } from 'react-router-dom'; import Icon from './Icon'; import Button from './Button'; -import Input from './Input'; -import { useFormState } from 'react-use-form-state'; +import { useForm } from 'react-hook-form'; import { me, facebookLink, vkLink, appleLink } from '../api'; @@ -33,14 +32,11 @@ function Login({ visitor, onAuth }) { } }, [history, location.state, visitor]); - const [formState, { text, password }] = useFormState(); + const { register, handleSubmit, formState: { errors }, } = useForm(); - /** - * @param {React.SyntheticEvent} event - */ - let onSubmit = (event) => { - event.preventDefault(); - me(formState.values.username, formState.values.password) + /** @type { import('react-hook-form').SubmitHandler } */ + let onSubmit = (values) => { + me(values.username, values.password) .then(response => { onAuth(response); } @@ -63,14 +59,10 @@ function Login({ visitor, onAuth }) {

Already registered?

-
-
-
- + +
+
+
diff --git a/vnext/src/ui/SearchBox.js b/vnext/src/ui/SearchBox.js index c79c9e8f..e6085fdc 100644 --- a/vnext/src/ui/SearchBox.js +++ b/vnext/src/ui/SearchBox.js @@ -1,4 +1,4 @@ -import { useFormState } from 'react-use-form-state'; +import { useForm } from 'react-hook-form'; /** * @typedef {Object} SearchBoxPropsFields @@ -13,15 +13,16 @@ import { useFormState } from 'react-use-form-state'; * @param {SearchBoxProps} props */ function SearchBox({ onSearch }) { - let onSubmit = (/** @type React.FormEvent */ event) => { - event.preventDefault(); - onSearch(formState.values.search); + const { register, handleSubmit, formState: { errors }, } = useForm(); + /** @type { import('react-hook-form').SubmitHandler } */ + let onSubmit = ( values ) => { + onSearch(values.search); }; - const [formState, { text }] = useFormState(); return ( -
- + + +
); } diff --git a/vnext/src/ui/__tests__/__snapshots__/Avatar.test.js.snap b/vnext/src/ui/__tests__/__snapshots__/Avatar.test.js.snap index 47614f5a..a28b4036 100644 --- a/vnext/src/ui/__tests__/__snapshots__/Avatar.test.js.snap +++ b/vnext/src/ui/__tests__/__snapshots__/Avatar.test.js.snap @@ -19,7 +19,7 @@ exports[`Avatar renders correctly 1`] = `
- , ] `; -- cgit v1.2.3