* Inserts a field (or label from string), and all prefix and suffix fields, * at the location of the input's field row. * * @param index The index at which to insert field. * @param field Something to add as a field. * @param opt_name Language-neutral identifier which may used to find
(
index: number,
field: string | Field<T>,
opt_name?: string,
)
| 91 | * @returns The index following the last inserted field. |
| 92 | */ |
| 93 | insertFieldAt<T>( |
| 94 | index: number, |
| 95 | field: string | Field<T>, |
| 96 | opt_name?: string, |
| 97 | ): number { |
| 98 | if (index < 0 || index > this.fieldRow.length) { |
| 99 | throw Error('index ' + index + ' out of bounds.'); |
| 100 | } |
| 101 | // Falsy field values don't generate a field, unless the field is an empty |
| 102 | // string and named. |
| 103 | if (!field && !(field === '' && opt_name)) { |
| 104 | return index; |
| 105 | } |
| 106 | |
| 107 | // Generate a FieldLabel when given a plain text field. |
| 108 | if (typeof field === 'string') { |
| 109 | field = fieldRegistry.fromJson({ |
| 110 | type: 'field_label', |
| 111 | text: field, |
| 112 | })!; |
| 113 | } |
| 114 | |
| 115 | field.setSourceBlock(this.sourceBlock); |
| 116 | if (this.sourceBlock.initialized) this.initField(field); |
| 117 | field.name = opt_name; |
| 118 | field.setVisible(this.isVisible()); |
| 119 | |
| 120 | if (field.prefixField) { |
| 121 | // Add any prefix. |
| 122 | index = this.insertFieldAt(index, field.prefixField); |
| 123 | } |
| 124 | // Add the field to the field row. |
| 125 | this.fieldRow.splice(index, 0, field as Field); |
| 126 | index++; |
| 127 | if (field.suffixField) { |
| 128 | // Add any suffix. |
| 129 | index = this.insertFieldAt(index, field.suffixField); |
| 130 | } |
| 131 | |
| 132 | if (this.sourceBlock.rendered) { |
| 133 | (this.sourceBlock as BlockSvg).queueRender(); |
| 134 | } |
| 135 | return index; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Remove a field from this input. |
no test coverage detected