Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(select): add new component #279

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,31 @@ npm install --save @naturacosmeticos/natds-rn

yarn add @naturacosmeticos/natds-rn
```
## Extra configuration (version > 7.29.0)

For native components within the application, we will need some extra configuration. If your dependency is greater than 7.29.0, you will need to do this step.

## Android

Import the androidLink module, inside your react-native.config.js file which is located in the project root. This will ensure that the native android dependencies are correct and that the autolink works correctly.

```javascript
require('@naturacosmeticos/natds-rn/tools/androidLink');
```

After configuring, run the command:

```sh
npx react native link
```

## IOS

To include the native dependency, just go to the `ios` folder, located at the root of the application and run the command:

```sh
pod install
```

**Dependencies**

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import java.lang.reflect.InvocationTargetException;
import java.util.List;

import com.naturareactnativecomponents.RNCPackage;

public class MainApplication extends Application implements ReactApplication {

private final ReactNativeHost mReactNativeHost =
Expand All @@ -26,6 +28,7 @@ protected List<ReactPackage> getPackages() {
List<ReactPackage> packages = new PackageList(this).getPackages();
// Packages that cannot be autolinked yet can be added manually here, for example:
// packages.add(new MyReactNativePackage());
packages.add(new RNCPackage());
return packages;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.naturareactnativecomponents">

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.naturareactnativecomponents;

import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class RNCPackage implements ReactPackage {
@Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
return Collections.emptyList();
}

@Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
return Arrays.<ViewManager>asList(new SelectViewManager());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package com.naturareactnativecomponents;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;

import androidx.appcompat.widget.AppCompatSpinner;

public class ReactSelect extends AppCompatSpinner {

private int mMode = Spinner.MODE_DIALOG;
private OnSelectListener mOnSelectListener;
private OnFocusListener mOnFocusListener;
private int mOldElementSize = Integer.MIN_VALUE;
private boolean mIsOpen = false;

private final OnItemSelectedListener mItemSelectedListener = new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (mOnSelectListener != null) {
mOnSelectListener.onItemSelected(position);
}
}

@Override
public void onNothingSelected(AdapterView<?> parent) {
if (mOnSelectListener != null) {
mOnSelectListener.onItemSelected(-1);
}
}
};

/**
* Listener interface for ReactSelect events.
*/
public interface OnSelectListener {
void onItemSelected(int position);
}

public interface OnFocusListener {
void onPickerBlur();
void onPickerFocus();
}

public ReactSelect(Context context) {
super(context);
}

public ReactSelect(Context context, int mode) {
super(context, mode);
mMode = mode;
}

public ReactSelect(Context context, AttributeSet attrs) {
super(context, attrs);
}

public ReactSelect(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}

public ReactSelect(Context context, AttributeSet attrs, int defStyle, int mode) {
super(context, attrs, defStyle, mode);
mMode = mode;
}

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);

// onItemSelected gets fired immediately after layout because checkSelectionChanged() in
// AdapterView updates the selection position from the default INVALID_POSITION.
// To match iOS behavior, which no onItemSelected during initial layout.
// We setup the listener after layout.
if (getOnItemSelectedListener() == null)
setOnItemSelectedListener(mItemSelectedListener);
}

public void setOnSelectListener(OnSelectListener onSelectListener) {
mOnSelectListener = onSelectListener;
}

public void setOnFocusListener(OnFocusListener onFocusListener) {
mOnFocusListener = onFocusListener;
}

public OnSelectListener getOnSelectListener() {
return mOnSelectListener;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.naturareactnativecomponents;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.facebook.infer.annotation.Assertions;
import com.facebook.react.bridge.ReadableMap;

public class SelectViewAdapter extends ArrayAdapter<ReadableMap> {

private final LayoutInflater mInflater;

public SelectViewAdapter(Context context, ReadableMap[] data) {
super(context, 0, data);

mInflater = (LayoutInflater) Assertions.assertNotNull(
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE));
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getView(position, convertView, parent, false);
}

@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return getView(position, convertView, parent, true);
}

private View getView(int position, View convertView, ViewGroup parent, boolean isDropdown) {
ReadableMap item = getItem(position);

if (convertView == null) {
int layoutResId = isDropdown
? android.R.layout.simple_spinner_dropdown_item
: android.R.layout.simple_spinner_item;
convertView = mInflater.inflate(layoutResId, parent, false);
}

TextView textView = (TextView) convertView;
textView.setText(item.getString("label"));

return convertView;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.naturareactnativecomponents;

import android.graphics.Color;
import android.view.View;

import androidx.annotation.NonNull;

import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.uimanager.SimpleViewManager;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.annotations.ReactProp;

public class SelectViewManager extends SimpleViewManager<ReactSelect> {
public static final String REACT_CLASS = "Select";

@Override
@NonNull
public String getName() {
return REACT_CLASS;
}

@Override
@NonNull
public ReactSelect createViewInstance(ThemedReactContext reactContext) {
return new ReactSelect(reactContext);
}

@ReactProp(name = "color")
public void setColor(View view, String color) {
view.setBackgroundColor(Color.parseColor(color));
}

@ReactProp(name = "items")
public void setItems(ReactSelect view, ReadableArray items) {

if (items != null) {
ReadableMap[] data = new ReadableMap[items.size()];
for (int i = 0; i < items.size(); i++) {
data[i] = items.getMap(i);
}
SelectViewAdapter adapter = new SelectViewAdapter(view.getContext(), data);
view.setAdapter(adapter);
} else {
view.setAdapter(null);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
buildscript {
if (project == rootProject) {
repositories {
google()
mavenCentral()
jcenter()
}

dependencies {
classpath 'com.android.tools.build:gradle:3.5.3'
}
}
}

apply plugin: 'com.android.library'

def safeExtGet(prop, fallback) {
rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
}

android {
compileSdkVersion safeExtGet('Naturareactnativecomponents_compileSdkVersion', 29)
defaultConfig {
minSdkVersion safeExtGet('Naturareactnativecomponents_minSdkVersion', 16)
targetSdkVersion safeExtGet('Naturareactnativecomponents_targetSdkVersion', 29)
versionCode 1
versionName "1.0"

}

buildTypes {
release {
minifyEnabled false
}
}
lintOptions {
disable 'GradleCompatible'
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

repositories {
mavenLocal()
maven {
// All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
url("$rootDir/../node_modules/react-native/android")
}
google()
mavenCentral()
jcenter()
}

dependencies {
//noinspection GradleDynamicVersion
implementation "com.facebook.react:react-native:+" // From node_modules
}
Loading