|
| 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 | +}); |
0 commit comments