Browse Source

ref(js): Allow form getValue to take a default (#50024)

Evan Purkhiser 1 year ago
parent
commit
13a492ec67
1 changed files with 9 additions and 2 deletions
  1. 9 2
      static/app/components/forms/model.tsx

+ 9 - 2
static/app/components/forms/model.tsx

@@ -292,8 +292,15 @@ class FormModel {
     return fieldState[key];
     return fieldState[key];
   }
   }
 
 
-  getValue(id: string) {
-    return this.fields.has(id) ? this.fields.get(id) : '';
+  getValue<T = FieldValue>(id: string, defaltValue?: T): T {
+    if (this.fields.has(id)) {
+      return this.fields.get(id) as T;
+    }
+
+    // XXX(epurkhiser): When you don't specify a default value it WILL become a
+    // empty string, which is not correctly accounted for in the types. We're
+    // doing this for legacy reasons
+    return defaltValue ?? ('' as T);
   }
   }
 
 
   getTransformedValue(id: string) {
   getTransformedValue(id: string) {