import React, { useState } from 'react';
import { JoyDoc, getDefaultDocument } from '@joyfill/components';
function MyForm() {
const [document, setDocument] = useState(() => {
// Create a default document
const doc = getDefaultDocument();
// Add a text field
doc.fields.push({
_id: 'name-field',
identifier: 'name',
type: 'text',
title: 'Your Name',
value: '',
file: doc.files[0]._id
});
// Add field position for layout
doc.files[0].pages[0].fieldPositions.push({
_id: 'name-position',
field: 'name-field',
x: 0,
y: 0,
width: 1,
height: 1,
displayType: 'original'
});
return doc;
});
const handleChange = (changelogs, updatedDoc) => {
console.log('Form changed:', changelogs);
setDocument(updatedDoc);
};
const handleSubmit = () => {
// Extract form data
const formData = {};
document.fields.forEach(field => {
formData[field.identifier] = field.value;
});
console.log('Form data:', formData);
alert(`Hello, ${formData.name || 'Anonymous'}!`);
};
return (
<div>
<h1>My First Joyfill Form</h1>
<JoyDoc
doc={document}
onChange={handleChange}
mode="fill"
width={816}
height={600}
/>
<button onClick={handleSubmit} style={{ marginTop: '20px' }}>
Submit Form
</button>
</div>
);
}
export default MyForm;