RCTSwitchManager.m 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /**
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. #import "RCTSwitchManager.h"
  8. #import "RCTBridge.h"
  9. #import "RCTEventDispatcher.h"
  10. #import "RCTSwitch.h"
  11. #import "UIView+React.h"
  12. @implementation RCTSwitchManager
  13. RCT_EXPORT_MODULE()
  14. - (UIView *)view
  15. {
  16. RCTSwitch *switcher = [RCTSwitch new];
  17. [switcher addTarget:self
  18. action:@selector(onChange:)
  19. forControlEvents:UIControlEventValueChanged];
  20. return switcher;
  21. }
  22. - (void)onChange:(RCTSwitch *)sender
  23. {
  24. if (sender.wasOn != sender.on) {
  25. if (sender.onChange) {
  26. sender.onChange(@{ @"value": @(sender.on) });
  27. }
  28. sender.wasOn = sender.on;
  29. }
  30. }
  31. RCT_EXPORT_VIEW_PROPERTY(onTintColor, UIColor);
  32. RCT_EXPORT_VIEW_PROPERTY(tintColor, UIColor);
  33. RCT_EXPORT_VIEW_PROPERTY(thumbTintColor, UIColor);
  34. RCT_REMAP_VIEW_PROPERTY(value, on, BOOL);
  35. RCT_EXPORT_VIEW_PROPERTY(onChange, RCTBubblingEventBlock);
  36. RCT_CUSTOM_VIEW_PROPERTY(disabled, BOOL, RCTSwitch)
  37. {
  38. if (json) {
  39. view.enabled = !([RCTConvert BOOL:json]);
  40. } else {
  41. view.enabled = defaultView.enabled;
  42. }
  43. }
  44. RCT_REMAP_VIEW_PROPERTY(thumbColor, thumbTintColor, UIColor);
  45. RCT_REMAP_VIEW_PROPERTY(trackColorForFalse, tintColor, UIColor);
  46. RCT_REMAP_VIEW_PROPERTY(trackColorForTrue, onTintColor, UIColor);
  47. @end