MCPcopy Create free account
hub / github.com/Monogatari/Monogatari / render

Method render

src/components/text-input/index.ts:176–266  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

174 }
175
176 override render(): string {
177 const { type, default: defaultValue, options, attributes } = this.props;
178 const text: TextInputType[] = ['text', 'password', 'email', 'url', 'number', 'color', 'file', 'date', 'datetime-local', 'month', 'time', 'week', 'tel', 'range'];
179 let input = '';
180 let attr = '';
181
182 if (typeof attributes === 'object' && attributes !== null) {
183 attr = Object.keys(attributes).map((key) => {
184 let value = attributes[key];
185
186 // If it's a string value, we'll do the variable interpolation
187 // for it.
188 if (typeof value === 'string') {
189 value = this.engine.replaceVariables(value);
190 }
191
192 return `${key}="${value}"`;
193 }).join(' ');
194 }
195
196 if (text.indexOf(type) > -1) {
197 input = `<input data-content="field" name="field" type="${type}" tabindex="0" ${attr}>`;
198 } else if (type === 'textarea') {
199 input = `<textarea data-content="field" name="field" tabindex="0" ${attr}></textarea>`;
200 } else if (type === 'select') {
201 const optionElements = options.map((o) => {
202 let selected = '';
203 let parsedDefault = defaultValue;
204
205 // If the default value provided is a string, we need to do the variable
206 // interpolation for it.
207 if (typeof defaultValue === 'string' && defaultValue !== null && defaultValue !== '') {
208 parsedDefault = this.engine.replaceVariables(defaultValue);
209 // We're doing a == comparison instead of === since the numeric
210 // values could be a string.
211 if (parsedDefault == this.engine.replaceVariables(String(o.value))) {
212 selected = 'selected';
213 }
214 } else if (typeof defaultValue === 'number') {
215 // We're doing a == comparison instead of === since the numeric
216 // values could be a string.
217 if (parsedDefault == o.value) {
218 selected = 'selected';
219 }
220 }
221 return `<option value="${typeof o.value === 'string' ? this.engine.replaceVariables(o.value) : o.value}" ${selected}>${this.engine.replaceVariables(o.label)}</option>`;
222 }).join('');
223
224 input = `<select data-content="field" name="field" tabindex="0" ${attr}>${optionElements}</select>`;
225
226 } else if (type === 'radio' || type === 'checkbox') {
227 input = options.map((o, index) => {
228 let checked = '';
229 let parsedDefault = defaultValue;
230
231 // If the default value provided is a string, we need to do the variable
232 // interpolation for it.
233 if (typeof defaultValue === 'string' && defaultValue !== null && defaultValue !== '') {

Callers

nothing calls this directly

Calls 3

replaceVariablesMethod · 0.80
stringMethod · 0.80
keysMethod · 0.65

Tested by

no test coverage detected