bindMapValue is a version of [Bind] that works for values in a map.
(mapv reflect.Value, key reflect.Value, vw T)
| 269 | |
| 270 | // bindMapValue is a version of [Bind] that works for values in a map. |
| 271 | func bindMapValue[T Value](mapv reflect.Value, key reflect.Value, vw T) T { |
| 272 | wb := vw.AsWidget() |
| 273 | alreadyBound := wb.ValueUpdate != nil |
| 274 | wb.ValueUpdate = func() { |
| 275 | value := mapv.MapIndex(key).Interface() |
| 276 | if vws, ok := any(vw).(ValueSetter); ok { |
| 277 | ErrorSnackbar(vw, vws.SetWidgetValue(value)) |
| 278 | } else { |
| 279 | ErrorSnackbar(vw, reflectx.SetRobust(vw.WidgetValue(), value)) |
| 280 | } |
| 281 | } |
| 282 | wb.ValueOnChange = func() { |
| 283 | value := reflect.New(mapv.Type().Elem()) |
| 284 | ErrorSnackbar(vw, reflectx.SetRobust(value.Interface(), vw.WidgetValue())) |
| 285 | mapv.SetMapIndex(key, value.Elem()) |
| 286 | } |
| 287 | if alreadyBound { |
| 288 | ResetWidgetValue(vw) |
| 289 | } |
| 290 | if ob, ok := any(vw).(OnBinder); ok { |
| 291 | value := mapv.MapIndex(key).Interface() |
| 292 | ob.OnBind(value, "") |
| 293 | } |
| 294 | wb.ValueUpdate() // we update it with the initial value immediately |
| 295 | return vw |
| 296 | } |
no test coverage detected