* Create a new radio button group. * * Radio groups must have at least one option. Each option gets its own widget * when you call `page.drawField()` with the `option` parameter. * * @param name - Field name (must be unique) * @param options - Radio group options (options array is
(name: string, options: RadioGroupOptions)
| 712 | * ``` |
| 713 | */ |
| 714 | createRadioGroup(name: string, options: RadioGroupOptions): RadioField { |
| 715 | this.validateUniqueName(name); |
| 716 | |
| 717 | if (!options.options || options.options.length === 0) { |
| 718 | throw new Error("Radio group must have at least one option"); |
| 719 | } |
| 720 | |
| 721 | // Button field type with Radio flag |
| 722 | const flags = FieldFlags.RADIO; |
| 723 | |
| 724 | const selectedValue = options.defaultValue ?? null; |
| 725 | |
| 726 | // Create field dictionary with /Kids array (separate widget model) |
| 727 | const fieldDict = PdfDict.of({ |
| 728 | FT: PdfName.of("Btn"), |
| 729 | T: PdfString.fromString(name), |
| 730 | Ff: PdfNumber.of(flags), |
| 731 | Kids: new PdfArray([]), |
| 732 | }); |
| 733 | |
| 734 | if (selectedValue && options.options.includes(selectedValue)) { |
| 735 | fieldDict.set("V", PdfName.of(selectedValue)); |
| 736 | fieldDict.set("DV", PdfName.of(selectedValue)); |
| 737 | } else { |
| 738 | fieldDict.set("V", PdfName.of("Off")); |
| 739 | } |
| 740 | |
| 741 | // Store options for validation in drawField |
| 742 | fieldDict.set("Opt", PdfArray.of(...options.options.map(o => PdfString.fromString(o)))); |
| 743 | |
| 744 | // Store symbol preference |
| 745 | if (options.symbol) { |
| 746 | fieldDict.set("_Symbol", PdfName.of(options.symbol)); |
| 747 | } |
| 748 | |
| 749 | // Store styling metadata |
| 750 | this.storeFieldStyling(fieldDict, options); |
| 751 | |
| 752 | // Register and add to form |
| 753 | const fieldRef = this._ctx.registry.register(fieldDict); |
| 754 | this._acroForm.addField(fieldRef); |
| 755 | |
| 756 | // Create the RadioField instance |
| 757 | // oxlint-disable-next-line typescript/no-unsafe-type-assertion |
| 758 | const field = createFormField( |
| 759 | fieldDict, |
| 760 | fieldRef, |
| 761 | this._ctx.registry, |
| 762 | this._acroForm, |
| 763 | name, |
| 764 | ) as RadioField; |
| 765 | |
| 766 | // Add to cache |
| 767 | this.allFields.push(field); |
| 768 | this.fieldsByName.set(name, field); |
| 769 | |
| 770 | return field; |
| 771 | } |
no test coverage detected