forked from MergeFi/frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddContact.tsx
More file actions
42 lines (38 loc) · 1.51 KB
/
Copy pathAddContact.tsx
File metadata and controls
42 lines (38 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import React, { useState } from 'react'
import { isValidStellarAddress } from '../../utils/stellar'
import type { Contact } from './ContactItem'
interface Props {
onAdd: (contact: Contact) => void
onCancel: () => void
}
export function AddContact({ onAdd, onCancel }: Props) {
const [name, setName] = useState('')
const [address, setAddress] = useState('')
const [errors, setErrors] = useState<{ name?: string; address?: string }>({})
const submit = () => {
const e: typeof errors = {}
if (!name.trim()) e.name = 'Name is required'
if (!isValidStellarAddress(address)) e.address = 'Invalid Stellar address'
setErrors(e)
if (Object.keys(e).length === 0) onAdd({ name: name.trim(), address })
}
return (
<div className="mt-4 p-4 bg-gray-50 rounded-lg space-y-3">
<h3 className="font-medium">Add contact</h3>
<input
value={name} onChange={e => setName(e.target.value)}
placeholder="Name" className="input w-full"
/>
{errors.name && <p className="text-red-500 text-xs">{errors.name}</p>}
<input
value={address} onChange={e => setAddress(e.target.value.trim())}
placeholder="G... address" className="input w-full font-mono"
/>
{errors.address && <p className="text-red-500 text-xs">{errors.address}</p>}
<div className="flex gap-2">
<button onClick={submit} className="btn-primary btn-sm">Add</button>
<button onClick={onCancel} className="btn-secondary btn-sm">Cancel</button>
</div>
</div>
)
}