I’m trying to create a custom field type on Forge and want to use onChange to validate user input
But it doesn’t work at all and gives no error.
I tried a few different things but nothing really worked. Here’s my edit.jsx:
import ForgeUI, {CustomFieldEdit, TextField, useProductContext, useState} from "@forge/ui";
export const Edit = () => {
const {extensionContext: {fieldValue}} = useProductContext();
const [error, setError] = useState('');
const [inputValue, setInputValue] = useState("initial")
const handleInputChange = (event) => {
console.log(`triggered`);
const value = event.target.value;
setInputValue(value);
if (event.target.value.length < 5) {
console.log(`Less than 5`);
setError('ERROR');
} else {
console.log(`MORE`);
setError('');
}
}
const onSubmit = values => {
const copy = JSON.parse(JSON.stringify(values));
let a = {"str": Number(copy.num)};
return a;
};
return (
<CustomFieldEdit onSubmit={onSubmit}>
<TextField name="num" label="Num: " defaultValue={fieldValue?.num} onChange={(value) => console.log(value)}/>
</CustomFieldEdit>
);
};
Any help is appreciated