Skip to content

Commit

Permalink
feat: add wallet address search form
Browse files Browse the repository at this point in the history
Adds a Search Wallet form component - with validation, without any search logic.
  • Loading branch information
martinkyselak committed Dec 29, 2023
1 parent f6ac757 commit f985410
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 1 deletion.
17 changes: 17 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"@types/react-dom": "^18.2.18",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-hook-form": "^7.49.2",
"react-scripts": "5.0.1",
"typescript": "^4.9.5",
"web-vitals": "^2.1.4"
Expand Down
8 changes: 7 additions & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { Header } from './Header';
import { WalletPage } from './WalletPage';

function App() {
return <Header />;
return (
<>
<Header />
<WalletPage />
</>
);
}

export default App;
53 changes: 53 additions & 0 deletions src/SearchWalletForm.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { FieldError, useForm } from 'react-hook-form';
import { SearchCriteria } from './api/types';

type Props = {
onSearch: (search: SearchCriteria) => void;
};

export function SearchWalletForm({ onSearch }: Props) {
const {
register,
handleSubmit,
formState: { errors, isSubmitting },
} = useForm<SearchCriteria>();

const fieldStyle = 'flex flex-col mb-2';
function getEditorStyle(fieldError: FieldError | undefined) {
return fieldError ? 'border-red-500' : 'border-slate-300';
}
return (
<form noValidate className="border-b py-4" onSubmit={handleSubmit(onSearch)}>
<div className={fieldStyle}>
<label htmlFor="address">Wallet Address</label>
<input
type="text"
id="address"
{...register('address', { required: 'You must enter a wallet address' })}
className={getEditorStyle(errors.address)}
/>
<ValidationError fieldError={errors.address} />
</div>
<div className={fieldStyle}>
<button
type="submit"
disabled={isSubmitting}
className="mt-2 h-10 px-6 font-semibold bg-slate-700 text-white self-start"
>
Search
</button>
</div>
</form>
);
}

function ValidationError({ fieldError }: { fieldError: FieldError | undefined }) {
if (!fieldError) {
return null;
}
return (
<div role="alert" className="text-red-500 text-xs mt-1">
{fieldError.message}
</div>
);
}
18 changes: 18 additions & 0 deletions src/WalletPage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { useState } from 'react';
import { SearchCriteria } from './api/types';
import { SearchWalletForm } from './SearchWalletForm';

export function WalletPage() {
const [searchCriteria, setSearchCriteria] = useState<SearchCriteria | undefined>();

function handleSearch(search: SearchCriteria) {
setSearchCriteria(search);
}

return (
<main className="max-w-xs ml-auto mr-auto">
<SearchWalletForm onSearch={handleSearch} />
<div>Wallet balance and last ten transactions will be here.</div>
</main>
);
}
3 changes: 3 additions & 0 deletions src/api/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export type SearchCriteria = {
address: string;
};

0 comments on commit f985410

Please sign in to comment.