Skip to content

Commit ed05ce6

Browse files
committed
Allow more types to be nullable
1 parent 068f357 commit ed05ce6

File tree

1 file changed

+26
-14
lines changed

1 file changed

+26
-14
lines changed

js/scripts/prop-types.js

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -153,56 +153,66 @@ _.extend(ThreeTypeDict.prototype, BaseType.prototype, {
153153
},
154154
});
155155

156-
function Bool(defaultValue) {
156+
function Bool(defaultValue, nullable) {
157+
this.nullable = nullable === true;
157158
this.defaultValue = defaultValue;
158159
}
159160
_.extend(Bool.prototype, BaseType.prototype, {
160161
getTraitlet: function() {
161162
var pyBoolValue = this.defaultValue ? 'True' : 'False';
162-
return 'Bool(' + pyBoolValue + ').tag(sync=True)';
163+
var nullableStr = this.nullable ? 'True' : 'False';
164+
return 'Bool(' + pyBoolValue + ', allow_none=' + nullableStr + ').tag(sync=True)';
163165
},
164166
});
165167

166-
function Int(defaultValue) {
167-
this.defaultValue = defaultValue == null ? 0 : defaultValue;
168+
function Int(defaultValue, nullable) {
169+
this.nullable = nullable === true;
170+
this.defaultValue = defaultValue == null && !this.nullable ? 0 : defaultValue;
168171
}
169172
_.extend(Int.prototype, BaseType.prototype, {
170173
getTraitlet: function() {
171-
return 'CInt(' + this.defaultValue + ').tag(sync=True)';
174+
var nullableStr = this.nullable ? 'True' : 'False';
175+
return 'CInt(' + this.defaultValue + ', allow_none=' + nullableStr + ').tag(sync=True)';
172176
},
173177

174178
});
175179

176-
function Float(defaultValue) {
177-
this.defaultValue = defaultValue == null ? 0.0 : defaultValue;
180+
function Float(defaultValue, nullable) {
181+
this.nullable = nullable === true;
182+
this.defaultValue = defaultValue == null && !this.nullable ? 0.0 : defaultValue;
178183
}
179184
_.extend(Float.prototype, BaseType.prototype, {
180185
getTraitlet: function() {
181-
return 'CFloat(' + this.getPythonDefaultValue() + ').tag(sync=True)';
186+
var nullableStr = this.nullable ? 'True' : 'False';
187+
return 'CFloat(' + this.getPythonDefaultValue() + ', allow_none=' + nullableStr + ').tag(sync=True)';
182188
},
183189
getPropertyConverterFn: function() {
184190
return 'convertFloat';
185191
},
186192

187193
});
188194

189-
function StringType(defaultValue) {
195+
function StringType(defaultValue, nullable) {
196+
this.nullable = nullable === true;
190197
this.defaultValue = defaultValue;
191198
}
192199
_.extend(StringType.prototype, BaseType.prototype, {
193200
getTraitlet: function() {
194-
return 'Unicode("' + this.defaultValue + '").tag(sync=True)';
201+
var nullableStr = this.nullable ? 'True' : 'False';
202+
return 'Unicode(' + JSON.stringify(this.defaultValue) + ', allow_none=' + nullableStr + ').tag(sync=True)';
195203
},
196204

197205
});
198206

199-
function Enum(enumTypeName, defaultValue) {
207+
function Enum(enumTypeName, defaultValue, nullable) {
200208
this.enumTypeName = enumTypeName;
201209
this.defaultValue = defaultValue;
210+
this.nullable = nullable === true;
202211
}
203212
_.extend(Enum.prototype, BaseType.prototype, {
204213
getTraitlet: function() {
205-
return 'Enum(' + this.enumTypeName + ', "' + this.defaultValue + '").tag(sync=True)';
214+
var nullableStr = this.nullable ? 'True' : 'False';
215+
return 'Enum(' + this.enumTypeName + ', "' + this.defaultValue + ', allow_none=' + nullableStr + '").tag(sync=True)';
206216
},
207217
getPropertyConverterFn: function() {
208218
return 'convertEnum';
@@ -262,12 +272,14 @@ _.extend(ArrayBufferType.prototype, BaseType.prototype, {
262272
},
263273
});
264274

265-
function DictType(defaultValue={}) {
275+
function DictType(defaultValue={}, nullable) {
266276
this.defaultValue = defaultValue;
277+
this.nullable = nullable === true;
267278
}
268279
_.extend(DictType.prototype, BaseType.prototype, {
269280
getTraitlet: function() {
270-
return 'Dict().tag(sync=True)';
281+
var nullableStr = this.nullable ? 'True' : 'False';
282+
return 'Dict(default_value=' + this.defaultValue + ', allow_none=' + nullableStr + ').tag(sync=True)';
271283
},
272284
});
273285

0 commit comments

Comments
 (0)