Reputation: 119
I have an array consisting of names and dates of birth. I have to display their name by selecting from the list automatically the date of birth will appear corresponding in the other field. I'm unable to do that. Can anyone help me with this? I'm using the Mantine library for styling purposes. The Select and DatePicker is from the Mantine library.
const user= [{"name": "Gavin",
"id":1,
"DOB": "2007-01-03",
},
{
"name": "Harvey",
"id":2,
"DOB": "1980-11-23",
}]
const [user, setUser] = useState();
const[birthDate, setBirthDate] = useState();
function HeaderSelect() {
return (
<div style={{ marginTop: 70, marginBottom: 70 }}>
<Grid>
<Grid.Col span={3}>
<Select
label="Name"
data={user.map((op) => ({
label: op.name,
value: op.name,
}))}
value={user}
onChange={(selected) => setUser(selected)}
/>
</Grid.Col>
<Grid.Col span={3}>
<DatePicker
placeholder={new Date().toLocaleString(undefined, {
year: "numeric",
month: "long",
day: "numeric",
})}
label="DOB"
clearable={false}
value={birthDate}
onChange={setBirthDate}
/>
</Grid.Col>
</Grid>
</div>
);
}
Upvotes: 0
Views: 83
Reputation: 118
Just change this in onchange function. I think it will help!
onChange={(selected) => {
setUser(selected);
setBirthDate(user.find(u => u.name == selected.value).DOB);
}}
Upvotes: 1
Reputation: 374
try using this
function HeaderSelect() {
return (
<div style={{ marginTop: 70, marginBottom: 70 }}>
<Grid>
<Grid.Col span={3}>
<Select
label="Name"
data={user.map((op) => ({
label: op.name,
value: op.name,
}))}
value={user}
onChange={(selected) => {
setUser(selected);
setBirthDate(user.find(u => u.name == selected.value).DOB);
}}
/>
</Grid.Col>
<Grid.Col span={3}>
<DatePicker
placeholder={new Date().toLocaleString(undefined, {
year: "numeric",
month: "long",
day: "numeric",
})}
label="DOB"
clearable={false}
value={birthDate}
onChange={setBirthDate}
/>
</Grid.Col>
Upvotes: 2