-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added Form Annotation support (#2845)
- Loading branch information
1 parent
e5968c6
commit 8cd872f
Showing
14 changed files
with
693 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
'use client'; | ||
|
||
import dynamic from 'next/dynamic'; | ||
import { | ||
Document, | ||
Page, | ||
View, | ||
Text, | ||
Checkbox, | ||
FormField, | ||
TextInput, | ||
Select, | ||
List, | ||
} from '@react-pdf/renderer'; | ||
|
||
const PDFViewer = dynamic( | ||
() => import('@react-pdf/renderer').then((mod) => mod.PDFViewer), | ||
{ | ||
ssr: false, | ||
loading: () => <p>Loading...</p>, | ||
}, | ||
); | ||
|
||
export default function Form() { | ||
const doc = ( | ||
<Document> | ||
<Page> | ||
<View | ||
style={{ | ||
backgroundColor: 'rgba(182,28,28,0.62)', | ||
width: '30%', | ||
height: '100%', | ||
}} | ||
> | ||
<FormField name="user-info" style={{ flexDirection: 'column' }}> | ||
<Text>TextInput</Text> | ||
<TextInput | ||
name="username" | ||
value="foo" | ||
align="center" | ||
style={{ height: '50px' }} | ||
/> | ||
|
||
{/* Nested works as well */} | ||
<View> | ||
<Text>TextInput</Text> | ||
<TextInput | ||
name="password" | ||
value="bar" | ||
align="center" | ||
style={{ height: '50px' }} | ||
password | ||
/> | ||
</View> | ||
|
||
<Text>Checkbox (not checked)</Text> | ||
<Checkbox name="checkbox-default" style={{ height: '20px' }} /> | ||
|
||
<Text>Checkbox (checked)</Text> | ||
<Checkbox | ||
name="checkbox-checked" | ||
checked | ||
style={{ height: '20px' }} | ||
/> | ||
|
||
<Text>Select</Text> | ||
<Select | ||
name="combo" | ||
select={['', 'option 1', 'option 2']} | ||
value="" | ||
defaultValue="" | ||
style={{ height: '20px' }} | ||
/> | ||
|
||
<Text>List</Text> | ||
<List | ||
name="list" | ||
select={['', 'option 1', 'option 2']} | ||
value="" | ||
defaultValue="" | ||
style={{ height: '50px' }} | ||
/> | ||
</FormField> | ||
</View> | ||
</Page> | ||
|
||
<Page> | ||
<View | ||
style={{ | ||
backgroundColor: 'rgba(182,28,28,0.62)', | ||
width: '30%', | ||
height: '100%', | ||
}} | ||
> | ||
<FormField name="user-details" style={{ flexDirection: 'column' }}> | ||
<Text>TextInput (multiline)</Text> | ||
<TextInput | ||
name="details" | ||
value="hello" | ||
align="center" | ||
multiline | ||
style={{ fontSize: 8, height: '100px' }} | ||
/> | ||
</FormField> | ||
</View> | ||
</Page> | ||
|
||
<Page> | ||
<View | ||
style={{ | ||
backgroundColor: 'rgba(182,28,28,0.62)', | ||
width: '30%', | ||
height: '100%', | ||
}} | ||
> | ||
<Text>TextInput (no FormField)</Text> | ||
<TextInput | ||
name="textinput-no-formfield" | ||
value="no formfield" | ||
align="center" | ||
style={{ height: '50px' }} | ||
/> | ||
|
||
<Text>Checkbox (checked, no FormField)</Text> | ||
<Checkbox | ||
name="checkbox-no-formfield" | ||
checked | ||
style={{ height: '20px' }} | ||
/> | ||
</View> | ||
</Page> | ||
</Document> | ||
); | ||
|
||
return <PDFViewer className="w-full h-svh">{doc}</PDFViewer>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
'use client'; | ||
|
||
import dynamic from 'next/dynamic'; | ||
import { | ||
Document, | ||
Page, | ||
View, | ||
Text, | ||
Checkbox, | ||
FormField, | ||
TextInput, | ||
Select, | ||
List, | ||
} from '@react-pdf/renderer'; | ||
|
||
const PDFViewer = dynamic( | ||
() => import('@react-pdf/renderer').then((mod) => mod.PDFViewer), | ||
{ | ||
ssr: false, | ||
loading: () => <p>Loading...</p>, | ||
}, | ||
); | ||
|
||
export default function Form() { | ||
const doc = ( | ||
<Document> | ||
<Page> | ||
<View | ||
style={{ | ||
backgroundColor: 'rgba(182,28,28,0.62)', | ||
width: '30%', | ||
height: '100%', | ||
}} | ||
> | ||
<FormField name="user-info" style={{ flexDirection: 'column' }}> | ||
<Text>TextInput</Text> | ||
<TextInput | ||
name="username" | ||
value="foo" | ||
align="center" | ||
style={{ height: '50px' }} | ||
/> | ||
|
||
{/* Nested works as well */} | ||
<View> | ||
<Text>TextInput</Text> | ||
<TextInput | ||
name="password" | ||
value="bar" | ||
align="center" | ||
style={{ height: '50px' }} | ||
password | ||
/> | ||
</View> | ||
|
||
<Text>Checkbox (not checked)</Text> | ||
<Checkbox name="checkbox-default" style={{ height: '20px' }} /> | ||
|
||
<Text>Checkbox (checked)</Text> | ||
<Checkbox | ||
name="checkbox-checked" | ||
checked | ||
style={{ height: '20px' }} | ||
/> | ||
|
||
<Text>Select</Text> | ||
<Select | ||
name="combo" | ||
select={['', 'option 1', 'option 2']} | ||
value="" | ||
defaultValue="" | ||
style={{ height: '20px' }} | ||
/> | ||
|
||
<Text>List</Text> | ||
<List | ||
name="list" | ||
select={['', 'option 1', 'option 2']} | ||
value="" | ||
defaultValue="" | ||
style={{ height: '50px' }} | ||
/> | ||
</FormField> | ||
</View> | ||
</Page> | ||
|
||
<Page> | ||
<View | ||
style={{ | ||
backgroundColor: 'rgba(182,28,28,0.62)', | ||
width: '30%', | ||
height: '100%', | ||
}} | ||
> | ||
<FormField name="user-details" style={{ flexDirection: 'column' }}> | ||
<Text>TextInput (multiline)</Text> | ||
<TextInput | ||
name="details" | ||
value="hello" | ||
align="center" | ||
multiline | ||
style={{ fontSize: 8, height: '100px' }} | ||
/> | ||
</FormField> | ||
</View> | ||
</Page> | ||
|
||
<Page> | ||
<View | ||
style={{ | ||
backgroundColor: 'rgba(182,28,28,0.62)', | ||
width: '30%', | ||
height: '100%', | ||
}} | ||
> | ||
<Text>TextInput (no FormField)</Text> | ||
<TextInput | ||
name="textinput-no-formfield" | ||
value="no formfield" | ||
align="center" | ||
style={{ height: '50px' }} | ||
/> | ||
|
||
<Text>Checkbox (checked, no FormField)</Text> | ||
<Checkbox | ||
name="checkbox-no-formfield" | ||
checked | ||
style={{ height: '20px' }} | ||
/> | ||
</View> | ||
</Page> | ||
</Document> | ||
); | ||
|
||
return <PDFViewer className="w-full h-svh">{doc}</PDFViewer>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { parseCheckboxOptions } from '../../utils/parseFormOptions'; | ||
|
||
const renderCheckbox = (ctx, node, options = {}) => { | ||
const { top, left, width, height } = node.box || {}; | ||
|
||
// Element's name | ||
const name = node.props?.name || ''; | ||
const formFieldOptions = options.formFields?.at(0); | ||
|
||
if (!ctx._root.data.AcroForm) { | ||
ctx.initForm(); | ||
} | ||
|
||
ctx.formCheckbox( | ||
name, | ||
left, | ||
top, | ||
width, | ||
height, | ||
parseCheckboxOptions(ctx, node, formFieldOptions), | ||
); | ||
}; | ||
|
||
export default renderCheckbox; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
const renderFormField = (ctx, node, options = {}) => { | ||
const name = node.props?.name || ''; | ||
|
||
if (!ctx._root.data.AcroForm) { | ||
ctx.initForm(); | ||
} | ||
|
||
const formField = ctx.formField(name); | ||
const option = options; | ||
if (!option.formFields) option.formFields = [formField]; | ||
else option.formFields.push(formField); | ||
}; | ||
|
||
export const cleanUpFormField = (_ctx, _node, options) => { | ||
options.formFields.pop(); | ||
}; | ||
|
||
export default renderFormField; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { parseSelectAndListFieldOptions } from '../../utils/parseFormOptions'; | ||
|
||
const renderList = (ctx, node) => { | ||
const { top, left, width, height } = node.box || {}; | ||
|
||
// Element's name | ||
const name = node.props?.name || ''; | ||
|
||
if (!ctx._root.data.AcroForm) { | ||
ctx.initForm(); | ||
} | ||
|
||
ctx.formList( | ||
name, | ||
left, | ||
top, | ||
width, | ||
height, | ||
parseSelectAndListFieldOptions(node), | ||
); | ||
}; | ||
|
||
export default renderList; |
Oops, something went wrong.