I’m trying to create my own Form Components (Text-Input, Select-Boxes, etc.)
I copied the “FullNameComponent” from the docs - when using it as it is it renders the String “undefined” as the content of the <input>
-Element.
Here’s the full code:
import type { ViewConfig } from "@vaadin/hilla-file-router/types.js";
import {useForm, useFormPart} from "@vaadin/hilla-react-form";
import PersonModel from "Frontend/generated/org/vaadin/example/model/PersonModel";
import {StringModel} from "@vaadin/hilla-lit-form";
interface FullNameProps {
fullNameModel: StringModel;
}
export default function MainView() {
const {model, field, value} = useForm(PersonModel)
return (
<>
{/* Question: why does it write "undefined" as the value into the <input /> */}
<FullNameComponent fullNameModel={model.name} />
<pre>{JSON.stringify(value, null, 2)}</pre>
</>
);
}
function FullNameComponent({fullNameModel}: FullNameProps) {
const {model, field, required, errors, invalid, value, setValue} = useFormPart(fullNameModel);
return (
<>
<label htmlFor="fullName">
Full name
{required ? '*' : ''}
</label>
<input id="fullName" {...field(model)}></input>
<br/>
<span className="label" style={{visibility: invalid ? 'visible' : 'hidden'}}>
<strong>
{errors[0]?.message}
</strong>
</span>
</>
);
}
Here’s the Person.java
package org.vaadin.example.model;
public class Person {
private String name;
public Person(String name) {
this.name = name;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}