mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-05-11 19:01:43 +08:00
Add Android React Native Checkbox
Reviewed By: achen1 Differential Revision: D5281736 fbshipit-source-id: 9a3c93eeace2d80be4ddbd4ffc3258c1d3637480
This commit is contained in:
committed by
Facebook Github Bot
parent
dacb1fbc11
commit
84b11dd518
@@ -47,6 +47,7 @@ android_library(
|
||||
react_native_target("java/com/facebook/react/modules/websocket:websocket"),
|
||||
react_native_target("java/com/facebook/react/uimanager:uimanager"),
|
||||
react_native_target("java/com/facebook/react/views/art:art"),
|
||||
react_native_target("java/com/facebook/react/views/checkbox:checkbox"),
|
||||
react_native_target("java/com/facebook/react/views/drawer:drawer"),
|
||||
react_native_target("java/com/facebook/react/views/image:image"),
|
||||
react_native_target("java/com/facebook/react/views/modal:modal"),
|
||||
|
||||
@@ -54,6 +54,7 @@ import com.facebook.react.modules.websocket.WebSocketModule;
|
||||
import com.facebook.react.uimanager.ViewManager;
|
||||
import com.facebook.react.views.art.ARTRenderableViewManager;
|
||||
import com.facebook.react.views.art.ARTSurfaceViewManager;
|
||||
import com.facebook.react.views.checkbox.ReactCheckBoxManager;
|
||||
import com.facebook.react.views.drawer.ReactDrawerLayoutManager;
|
||||
import com.facebook.react.views.image.ReactImageManager;
|
||||
import com.facebook.react.views.modal.ReactModalHostManager;
|
||||
@@ -309,6 +310,7 @@ public class MainReactPackage extends LazyReactPackage {
|
||||
viewManagers.add(ARTRenderableViewManager.createARTGroupViewManager());
|
||||
viewManagers.add(ARTRenderableViewManager.createARTShapeViewManager());
|
||||
viewManagers.add(ARTRenderableViewManager.createARTTextViewManager());
|
||||
viewManagers.add(new ReactCheckBoxManager());
|
||||
viewManagers.add(new ReactDialogPickerManager());
|
||||
viewManagers.add(new ReactDrawerLayoutManager());
|
||||
viewManagers.add(new ReactDropdownPickerManager());
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
include_defs("//ReactAndroid/DEFS")
|
||||
|
||||
android_library(
|
||||
name = "checkbox",
|
||||
srcs = glob(["*.java"]),
|
||||
visibility = [
|
||||
"PUBLIC",
|
||||
],
|
||||
deps = [
|
||||
react_native_dep("third-party/android/support/v4:lib-support-v4"),
|
||||
react_native_dep("third-party/java/jsr-305:jsr-305"),
|
||||
react_native_target("java/com/facebook/react/bridge:bridge"),
|
||||
react_native_target("java/com/facebook/react/common:common"),
|
||||
react_native_target("java/com/facebook/react/uimanager:uimanager"),
|
||||
react_native_target("java/com/facebook/react/uimanager/annotations:annotations"),
|
||||
],
|
||||
)
|
||||
@@ -0,0 +1,38 @@
|
||||
/**
|
||||
* Copyright (c) 2017-present, Facebook, Inc. All rights reserved.
|
||||
*
|
||||
* <p>This source code is licensed under the BSD-style license found in the LICENSE file in the root
|
||||
* directory of this source tree. An additional grant of patent rights can be found in the PATENTS
|
||||
* file in the same directory.
|
||||
*/
|
||||
package com.facebook.react.views.checkbox;
|
||||
|
||||
import android.content.Context;
|
||||
import android.widget.CheckBox;
|
||||
|
||||
/** CheckBox that has its value controlled by JS. */
|
||||
/*package*/ class ReactCheckBox extends CheckBox {
|
||||
|
||||
private boolean mAllowChange;
|
||||
|
||||
public ReactCheckBox(Context context) {
|
||||
super(context);
|
||||
mAllowChange = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setChecked(boolean checked) {
|
||||
if (mAllowChange) {
|
||||
mAllowChange = false;
|
||||
super.setChecked(checked);
|
||||
}
|
||||
}
|
||||
|
||||
/*package*/ void setOn(boolean on) {
|
||||
// If the checkbox has a different value than the value sent by JS, we must change it.
|
||||
if (isChecked() != on) {
|
||||
super.setChecked(on);
|
||||
}
|
||||
mAllowChange = true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
/**
|
||||
* Copyright (c) 2017-present, Facebook, Inc. All rights reserved.
|
||||
*
|
||||
* <p>This source code is licensed under the BSD-style license found in the LICENSE file in the root
|
||||
* directory of this source tree. An additional grant of patent rights can be found in the PATENTS
|
||||
* file in the same directory.
|
||||
*/
|
||||
package com.facebook.react.views.checkbox;
|
||||
|
||||
import com.facebook.react.bridge.Arguments;
|
||||
import com.facebook.react.bridge.WritableMap;
|
||||
import com.facebook.react.uimanager.events.Event;
|
||||
import com.facebook.react.uimanager.events.RCTEventEmitter;
|
||||
|
||||
/** Event emitted by a ReactCheckBoxManager once a checkbox is manipulated. */
|
||||
/*package*/ class ReactCheckBoxEvent extends Event<ReactCheckBoxEvent> {
|
||||
|
||||
public static final String EVENT_NAME = "topChange";
|
||||
|
||||
private final boolean mIsChecked;
|
||||
|
||||
public ReactCheckBoxEvent(int viewId, boolean isChecked) {
|
||||
super(viewId);
|
||||
mIsChecked = isChecked;
|
||||
}
|
||||
|
||||
public boolean getIsChecked() {
|
||||
return mIsChecked;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getEventName() {
|
||||
return EVENT_NAME;
|
||||
}
|
||||
|
||||
@Override
|
||||
public short getCoalescingKey() {
|
||||
// All checkbox events for a given view can be coalesced.
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dispatch(RCTEventEmitter rctEventEmitter) {
|
||||
rctEventEmitter.receiveEvent(getViewTag(), getEventName(), serializeEventData());
|
||||
}
|
||||
|
||||
private WritableMap serializeEventData() {
|
||||
WritableMap eventData = Arguments.createMap();
|
||||
eventData.putInt("target", getViewTag());
|
||||
eventData.putBoolean("value", getIsChecked());
|
||||
return eventData;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
/**
|
||||
* Copyright (c) 2017-present, Facebook, Inc. All rights reserved.
|
||||
*
|
||||
* <p>This source code is licensed under the BSD-style license found in the LICENSE file in the root
|
||||
* directory of this source tree. An additional grant of patent rights can be found in the PATENTS
|
||||
* file in the same directory.
|
||||
*/
|
||||
package com.facebook.react.views.checkbox;
|
||||
|
||||
import android.widget.CompoundButton;
|
||||
import com.facebook.react.bridge.ReactContext;
|
||||
import com.facebook.react.uimanager.SimpleViewManager;
|
||||
import com.facebook.react.uimanager.ThemedReactContext;
|
||||
import com.facebook.react.uimanager.UIManagerModule;
|
||||
import com.facebook.react.uimanager.ViewProps;
|
||||
import com.facebook.react.uimanager.annotations.ReactProp;
|
||||
|
||||
/** View manager for {@link ReactCheckBox} components. */
|
||||
public class ReactCheckBoxManager extends SimpleViewManager<ReactCheckBox> {
|
||||
|
||||
private static final String REACT_CLASS = "AndroidCheckBox";
|
||||
|
||||
private static final CompoundButton.OnCheckedChangeListener ON_CHECKED_CHANGE_LISTENER =
|
||||
new CompoundButton.OnCheckedChangeListener() {
|
||||
@Override
|
||||
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
|
||||
ReactContext reactContext = (ReactContext) buttonView.getContext();
|
||||
reactContext
|
||||
.getNativeModule(UIManagerModule.class)
|
||||
.getEventDispatcher()
|
||||
.dispatchEvent(new ReactCheckBoxEvent(buttonView.getId(), isChecked));
|
||||
}
|
||||
};
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return REACT_CLASS;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void addEventEmitters(final ThemedReactContext reactContext, final ReactCheckBox view) {
|
||||
view.setOnCheckedChangeListener(ON_CHECKED_CHANGE_LISTENER);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ReactCheckBox createViewInstance(ThemedReactContext context) {
|
||||
ReactCheckBox view = new ReactCheckBox(context);
|
||||
return view;
|
||||
}
|
||||
|
||||
@ReactProp(name = ViewProps.ENABLED, defaultBoolean = true)
|
||||
public void setEnabled(ReactCheckBox view, boolean enabled) {
|
||||
view.setEnabled(enabled);
|
||||
}
|
||||
|
||||
@ReactProp(name = ViewProps.ON)
|
||||
public void setOn(ReactCheckBox view, boolean on) {
|
||||
// we set the checked change listener to null and then restore it so that we don't fire an
|
||||
// onChange event to JS when JS itself is updating the value of the checkbox
|
||||
view.setOnCheckedChangeListener(null);
|
||||
view.setOn(on);
|
||||
view.setOnCheckedChangeListener(ON_CHECKED_CHANGE_LISTENER);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user