Skip to content

Commit 233c1d8

Browse files
Custom toolbar radio button (#178)
Co-authored-by: Miguel Calderón <miguelcalderon@users.noreply.github.com>
1 parent 51c6e2f commit 233c1d8

4 files changed

Lines changed: 83 additions & 3 deletions

File tree

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import type { Instance, ToolbarItem } from "@nutrient-sdk/viewer";
2+
import { baseOptions } from "../../shared/base-options";
3+
4+
let instance: Instance | null = null;
5+
6+
const createGroupedRadioButtons = async (instance: Instance | null) => {
7+
const radioWidget1 = new NutrientViewer.Annotations.WidgetAnnotation({
8+
id: NutrientViewer.generateInstantId(),
9+
pageIndex: 0,
10+
formFieldName: "MyFormField",
11+
boundingBox: new NutrientViewer.Geometry.Rect({
12+
left: 100,
13+
top: 100,
14+
width: 20,
15+
height: 20,
16+
}),
17+
});
18+
19+
const radioWidget2 = new NutrientViewer.Annotations.WidgetAnnotation({
20+
id: NutrientViewer.generateInstantId(),
21+
pageIndex: 0,
22+
formFieldName: "MyFormField",
23+
boundingBox: new NutrientViewer.Geometry.Rect({
24+
left: 130,
25+
top: 100,
26+
width: 20,
27+
height: 20,
28+
}),
29+
});
30+
31+
const formField = new NutrientViewer.FormFields.RadioButtonFormField({
32+
name: "MyFormField",
33+
annotationIds: NutrientViewer.Immutable.List([
34+
radioWidget1.id,
35+
radioWidget2.id,
36+
]),
37+
options: NutrientViewer.Immutable.List([
38+
new NutrientViewer.FormOption({
39+
label: "Option 1",
40+
value: "1",
41+
}),
42+
new NutrientViewer.FormOption({
43+
label: "Option 2",
44+
value: "2",
45+
}),
46+
]),
47+
defaultValue: "1",
48+
});
49+
50+
await instance?.create([radioWidget1, radioWidget2, formField]);
51+
};
52+
53+
const item: ToolbarItem = {
54+
type: "custom",
55+
id: "add-radio-group",
56+
title: "Add Radio Group",
57+
onPress: () => createGroupedRadioButtons(instance),
58+
};
59+
60+
NutrientViewer.load({
61+
...baseOptions,
62+
theme: NutrientViewer.Theme.DARK,
63+
toolbarItems: [
64+
...NutrientViewer.defaultToolbarItems,
65+
{ type: "form-creator" },
66+
],
67+
}).then((_instance: Instance) => {
68+
instance = _instance;
69+
instance.setToolbarItems((items) => [...items, item]);
70+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
category: forms
3+
title: Add Grouped Radio Buttons with Custom Toolbar Button
4+
description: Create a custom toolbar item that programmatically places grouped radio buttons with a single click, avoiding manual renaming.
5+
keywords: [forms, radio-button, custom-toolbar, toolbar-item, widget, form-field, radio-group, grouped radio-buttons]
6+
---
7+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[InternetShortcut]
2+
URL=https://www.nutrient.io/demo/sandbox?p=eyJqcyI6ImxldCBpbnN0YW5jZSA9IG51bGw7XG5cbmNvbnN0IGl0ZW0gPSB7XG4gIHR5cGU6IFwiY3VzdG9tXCIsXG4gIGlkOiBcImFkZC1yYWRpby1ncm91cFwiLFxuICB0aXRsZTogXCJBZGQgUmFkaW8gR3JvdXBcIixcbiAgb25QcmVzczogYXN5bmMgKCkgPT4ge1xuICAgIGNvbnN0IHJhZGlvV2lkZ2V0MSA9IG5ldyBOdXRyaWVudFZpZXdlci5Bbm5vdGF0aW9ucy5XaWRnZXRBbm5vdGF0aW9uKHtcbiAgICAgIGlkOiBOdXRyaWVudFZpZXdlci5nZW5lcmF0ZUluc3RhbnRJZCgpLFxuICAgICAgcGFnZUluZGV4OiAwLFxuICAgICAgZm9ybUZpZWxkTmFtZTogXCJNeUZvcm1GaWVsZFwiLFxuICAgICAgYm91bmRpbmdCb3g6IG5ldyBOdXRyaWVudFZpZXdlci5HZW9tZXRyeS5SZWN0KHtcbiAgICAgICAgbGVmdDogMTAwLFxuICAgICAgICB0b3A6IDEwMCxcbiAgICAgICAgd2lkdGg6IDIwLFxuICAgICAgICBoZWlnaHQ6IDIwLFxuICAgICAgfSksXG4gIC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bnN0YW5jZT8uY3JlYXRlKFtyYWRpb1dpZGdldDEsIHJhZGlvV2lkZ2V0MiwgZm9ybUZpZWxkXSk7XG4gIH0sXG59O1xuXG5OdXRyaWVudFZpZXdlci5sb2FkKHtcbiAgLi4uYmFzZU9wdGlvbnMsXG4gIHRoZW1lOiBOdXRyaWVudFZpZXdlci5UaGVtZS5EQVJLLFxuICB0b29sYmFySXRlbXM6IFtcbiAgICAuLi5OdXRyaWVudFZpZXdlci5kZWZhdWx0VG9vbGJhckl0ZW1zLFxuICAgIHsgdHlwZTogXCJmb3JtLWNyZWF0b3JcIiB9LFxuICBdLFxufSkudGhlbigoX2luc3RhbmNlKSA9PiB7XG4gIGluc3RhbmNlID0gX2luc3RhbmNlO1xuICBpbnN0YW5jZS5zZXRUb29sYmFySXRlbXMoKGl0ZW1zKSA9PiBbLi4uaXRlbXMsIGl0ZW1dKTtcbn0pO1xuIiwiY3NzIjoiLyogQWRkIHlvdXIgQ1NTIGhlcmUgKi9cbiIsInNldHRpbmdzIjp7ImZpbGVOYW1lIjoiYmFzaWMucGRmIiwidXNlRHluYW1pY0ZvbnRzIjp0cnVlLCJjdXN0b21Gb250RmlsZU5hbWVzIjpbXX0sInYiOjF9

playground/types/global.d.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import type NutrientViewer from "@nutrient-sdk/viewer";
1+
import type NutrientViewerType from "@nutrient-sdk/viewer";
22

33
declare global {
4+
var NutrientViewer: typeof NutrientViewerType;
5+
46
interface Window {
5-
// Nutrient Web SDK loaded via CDN
6-
NutrientViewer: typeof NutrientViewer;
7+
NutrientViewer: typeof NutrientViewerType;
78
}
89
}

0 commit comments

Comments
 (0)