44from typing import Any , Callable , Iterable , Mapping , Sequence
55
66from .uiCallbacks import registerCallback
7+ from .uiValue import UiValue
78
89
910DESCRIPTOR_TYPES = {
@@ -249,16 +250,14 @@ def number(
249250 max : int | float | None = None ,
250251 step : int | float | None = None ,
251252 onChange : Callable [..., Any ] | None = None ,
252- ) -> dict [str , Any ]:
253- return _uiDescriptor (
254- "number" ,
255- value = value ,
256- label = label ,
257- min = min ,
258- max = max ,
259- step = step ,
260- events = _bindEvents (change = onChange ),
261- )
253+ ) -> UiValue :
254+ return UiValue ("number" , value , {
255+ "label" : label ,
256+ "min" : min ,
257+ "max" : max ,
258+ "step" : step ,
259+ "events" : _bindEvents (change = onChange ),
260+ })
262261
263262 def slider (
264263 self ,
@@ -269,30 +268,26 @@ def slider(
269268 step : int | float = 1 ,
270269 label : str = "" ,
271270 onChange : Callable [..., Any ] | None = None ,
272- ) -> dict [str , Any ]:
273- return _uiDescriptor (
274- "slider" ,
275- value = start if value is None else value ,
276- label = label ,
277- min = start ,
278- max = stop ,
279- step = step ,
280- events = _bindEvents (change = onChange ),
281- )
271+ ) -> UiValue :
272+ return UiValue ("slider" , start if value is None else value , {
273+ "label" : label ,
274+ "min" : start ,
275+ "max" : stop ,
276+ "step" : step ,
277+ "events" : _bindEvents (change = onChange ),
278+ })
282279
283280 def checkbox (
284281 self ,
285282 value : bool = False ,
286283 * ,
287284 label : str = "" ,
288285 onChange : Callable [..., Any ] | None = None ,
289- ) -> dict [str , Any ]:
290- return _uiDescriptor (
291- "checkbox" ,
292- value = bool (value ),
293- label = label ,
294- events = _bindEvents (change = onChange ),
295- )
286+ ) -> UiValue :
287+ return UiValue ("checkbox" , bool (value ), {
288+ "label" : label ,
289+ "events" : _bindEvents (change = onChange ),
290+ })
296291
297292 def dropdown (
298293 self ,
@@ -301,16 +296,14 @@ def dropdown(
301296 value : object | None = None ,
302297 label : str = "" ,
303298 onChange : Callable [..., Any ] | None = None ,
304- ) -> dict [ str , Any ] :
299+ ) -> UiValue :
305300 normalizedOptions = [str (option ) for option in options ]
306301 selected = str (value ) if value is not None else (normalizedOptions [0 ] if normalizedOptions else "" )
307- return _uiDescriptor (
308- "dropdown" ,
309- value = selected ,
310- label = label ,
311- options = normalizedOptions ,
312- events = _bindEvents (change = onChange ),
313- )
302+ return UiValue ("dropdown" , selected , {
303+ "label" : label ,
304+ "options" : normalizedOptions ,
305+ "events" : _bindEvents (change = onChange ),
306+ })
314307
315308 def button (
316309 self ,
@@ -439,6 +432,8 @@ def isDescriptorPayload(value: object) -> bool:
439432def toDescriptor (value : object ) -> object :
440433 if value is None :
441434 return {"type" : "plain" , "content" : "" }
435+ if isinstance (value , UiValue ):
436+ return _sanitizeValue (value .codaroDescriptor ())
442437 if isDescriptorPayload (value ):
443438 return _sanitizeValue (value )
444439 if isinstance (value , str ):
@@ -487,6 +482,8 @@ def _uiDescriptor(component: str, **props: object) -> dict[str, Any]:
487482
488483
489484def _sanitizeValue (value : object ) -> Any :
485+ if isinstance (value , UiValue ):
486+ return _sanitizeValue (value .codaroDescriptor ())
490487 if isDescriptorPayload (value ):
491488 payload = value if isinstance (value , dict ) else {}
492489 return {key : _sanitizeValue (item ) for key , item in payload .items ()}
0 commit comments