@react-native-hero/picker 0.0.5 → 0.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/HeroPicker.podspec +10 -9
- package/README.md +3 -3
- package/android/src/main/java/com/github/reactnativehero/picker/PickerView.kt +124 -0
- package/android/src/main/java/com/github/reactnativehero/picker/RNTPickerManager.kt +13 -83
- package/index.js +1 -2
- package/ios/RNTPicker/Picker/PickerView.swift +36 -22
- package/package.json +1 -1
package/HeroPicker.podspec
CHANGED
|
@@ -3,17 +3,18 @@ require 'json'
|
|
|
3
3
|
package = JSON.parse(File.read(File.join(__dir__, 'package.json')))
|
|
4
4
|
|
|
5
5
|
Pod::Spec.new do |s|
|
|
6
|
-
s.name
|
|
7
|
-
s.version
|
|
8
|
-
s.summary
|
|
9
|
-
s.license
|
|
6
|
+
s.name = "HeroPicker"
|
|
7
|
+
s.version = package['version']
|
|
8
|
+
s.summary = package['description']
|
|
9
|
+
s.license = package['license']
|
|
10
10
|
|
|
11
|
-
s.authors
|
|
12
|
-
s.homepage
|
|
13
|
-
s.platform
|
|
11
|
+
s.authors = package['author']
|
|
12
|
+
s.homepage = package['homepage']
|
|
13
|
+
s.platform = :ios, "9.0"
|
|
14
14
|
|
|
15
|
-
s.source
|
|
16
|
-
s.source_files
|
|
15
|
+
s.source = { :git => "https://github.com/react-native-hero/picker.git", :tag => "v#{s.version}" }
|
|
16
|
+
s.source_files = "ios/**/*.{h,m,swift}"
|
|
17
|
+
s.swift_version = '5.0'
|
|
17
18
|
|
|
18
19
|
s.dependency 'React'
|
|
19
20
|
end
|
package/README.md
CHANGED
|
@@ -45,9 +45,9 @@ import {
|
|
|
45
45
|
|
|
46
46
|
<Picker
|
|
47
47
|
options={[
|
|
48
|
-
{ text: '
|
|
49
|
-
{ text: '2', value: '
|
|
50
|
-
{ text: '3', value:
|
|
48
|
+
{ text: 'displayed text', value: 'support string or number' },
|
|
49
|
+
{ text: '2', value: 'string' },
|
|
50
|
+
{ text: '3', value: 3 },
|
|
51
51
|
]}
|
|
52
52
|
onChange={data => {
|
|
53
53
|
data.index
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
package com.github.reactnativehero.picker
|
|
2
|
+
|
|
3
|
+
import android.graphics.Color
|
|
4
|
+
import com.contrarywind.adapter.WheelAdapter
|
|
5
|
+
import com.contrarywind.view.WheelView
|
|
6
|
+
import com.facebook.react.bridge.Arguments
|
|
7
|
+
import com.facebook.react.bridge.ReadableArray
|
|
8
|
+
import com.facebook.react.bridge.ReadableMap
|
|
9
|
+
import com.facebook.react.bridge.WritableMap
|
|
10
|
+
import com.facebook.react.uimanager.ThemedReactContext
|
|
11
|
+
import com.facebook.react.uimanager.events.RCTEventEmitter
|
|
12
|
+
|
|
13
|
+
class PickerView(private val reactContext: ThemedReactContext) : WheelView(reactContext) {
|
|
14
|
+
|
|
15
|
+
companion object {
|
|
16
|
+
private val dividerColor = Color.parseColor("#33000000")
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
var options: ReadableArray = Arguments.createArray()
|
|
20
|
+
|
|
21
|
+
set(value) {
|
|
22
|
+
field = value
|
|
23
|
+
|
|
24
|
+
val count = value.size()
|
|
25
|
+
val stringList = arrayListOf<String>()
|
|
26
|
+
|
|
27
|
+
for (i in 0 until value.size()) {
|
|
28
|
+
val map = value.getMap(i)!!
|
|
29
|
+
var text = "undefined"
|
|
30
|
+
if (map.hasKey("text")) {
|
|
31
|
+
text = map.getString("text") as String
|
|
32
|
+
}
|
|
33
|
+
stringList.add(text)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
adapter = object: WheelAdapter<String> {
|
|
37
|
+
override fun indexOf(o: String?): Int {
|
|
38
|
+
return stringList.indexOf(o)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
override fun getItemsCount(): Int {
|
|
42
|
+
return count
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
override fun getItem(index: Int): String {
|
|
46
|
+
return stringList[index]
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (tempIndex >= 0) {
|
|
51
|
+
selectedIndex = tempIndex
|
|
52
|
+
tempIndex = -1
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
var tempIndex = -1
|
|
58
|
+
|
|
59
|
+
var selectedIndex = -1
|
|
60
|
+
|
|
61
|
+
set(value) {
|
|
62
|
+
field = value
|
|
63
|
+
|
|
64
|
+
val optionsSize = options.size()
|
|
65
|
+
|
|
66
|
+
if (value < 0 || value >= optionsSize) {
|
|
67
|
+
if (optionsSize == 0) {
|
|
68
|
+
tempIndex = value
|
|
69
|
+
}
|
|
70
|
+
return
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
currentItem = value
|
|
74
|
+
|
|
75
|
+
val map = Arguments.createMap()
|
|
76
|
+
map.putInt("index", value)
|
|
77
|
+
|
|
78
|
+
// putMap 必须传入 WritableMap
|
|
79
|
+
// 如果直接传 ReadableMap 会报错
|
|
80
|
+
val option = Arguments.createMap()
|
|
81
|
+
option.merge(options.getMap(value))
|
|
82
|
+
|
|
83
|
+
map.putMap("option", option)
|
|
84
|
+
sendEvent("onChange", map)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
var color = Color.BLACK
|
|
88
|
+
|
|
89
|
+
set(value) {
|
|
90
|
+
field = value
|
|
91
|
+
|
|
92
|
+
setTextColorOut(value)
|
|
93
|
+
setTextColorCenter(value)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
var fontSize = 16f
|
|
97
|
+
|
|
98
|
+
set(value) {
|
|
99
|
+
field = value
|
|
100
|
+
|
|
101
|
+
setTextSize(value)
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
var rowHeight = 44
|
|
105
|
+
|
|
106
|
+
set(value) {
|
|
107
|
+
field = value
|
|
108
|
+
|
|
109
|
+
setLineSpacingMultiplier(rowHeight / fontSize)
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
init {
|
|
113
|
+
setCyclic(false)
|
|
114
|
+
setDividerColor(dividerColor)
|
|
115
|
+
setOnItemSelectedListener {
|
|
116
|
+
selectedIndex = it
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
private fun sendEvent(eventName: String, params: WritableMap) {
|
|
121
|
+
reactContext.getJSModule(RCTEventEmitter::class.java).receiveEvent(id, eventName, params)
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
}
|
|
@@ -1,104 +1,45 @@
|
|
|
1
1
|
package com.github.reactnativehero.picker
|
|
2
2
|
|
|
3
|
-
import android.graphics.Color
|
|
4
|
-
import com.contrarywind.adapter.WheelAdapter
|
|
5
|
-
import com.contrarywind.view.WheelView
|
|
6
3
|
import com.facebook.react.bridge.*
|
|
7
4
|
import com.facebook.react.common.MapBuilder
|
|
8
5
|
import com.facebook.react.uimanager.SimpleViewManager
|
|
9
6
|
import com.facebook.react.uimanager.ThemedReactContext
|
|
10
7
|
import com.facebook.react.uimanager.ViewProps
|
|
11
8
|
import com.facebook.react.uimanager.annotations.ReactProp
|
|
12
|
-
import com.facebook.react.uimanager.events.RCTEventEmitter
|
|
13
|
-
import kotlin.collections.HashMap
|
|
14
9
|
|
|
15
|
-
class RNTPickerManager(private val reactAppContext: ReactApplicationContext) : SimpleViewManager<
|
|
16
|
-
|
|
17
|
-
companion object {
|
|
18
|
-
private val dividerColor = Color.parseColor("#CCCCCC")
|
|
19
|
-
private var fontSizes = HashMap<Int, Float>()
|
|
20
|
-
}
|
|
10
|
+
class RNTPickerManager(private val reactAppContext: ReactApplicationContext) : SimpleViewManager<PickerView>() {
|
|
21
11
|
|
|
22
12
|
override fun getName(): String {
|
|
23
13
|
return "RNTPicker"
|
|
24
14
|
}
|
|
25
15
|
|
|
26
|
-
override fun createViewInstance(reactContext: ThemedReactContext):
|
|
27
|
-
|
|
28
|
-
val view = WheelView(reactContext)
|
|
29
|
-
|
|
30
|
-
view.setCyclic(false)
|
|
31
|
-
view.setDividerColor(dividerColor)
|
|
32
|
-
|
|
33
|
-
return view
|
|
16
|
+
override fun createViewInstance(reactContext: ThemedReactContext): PickerView {
|
|
17
|
+
return PickerView(reactContext)
|
|
34
18
|
}
|
|
35
19
|
|
|
36
20
|
@ReactProp(name = "options")
|
|
37
|
-
fun setOptions(view:
|
|
38
|
-
|
|
39
|
-
val count = options.size()
|
|
40
|
-
val mapList = arrayListOf<HashMap<String, String>>()
|
|
41
|
-
val stringList = arrayListOf<String>()
|
|
42
|
-
|
|
43
|
-
for (item in options.toArrayList()) {
|
|
44
|
-
val map = item as HashMap<String, String>
|
|
45
|
-
var text = "undefined"
|
|
46
|
-
if (map.contains("text")) {
|
|
47
|
-
text = map["text"].toString()
|
|
48
|
-
}
|
|
49
|
-
mapList.add(map)
|
|
50
|
-
stringList.add(text)
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
view.adapter = object: WheelAdapter<String> {
|
|
54
|
-
override fun indexOf(o: String?): Int {
|
|
55
|
-
return stringList.indexOf(o)
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
override fun getItemsCount(): Int {
|
|
59
|
-
return count
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
override fun getItem(index: Int): String {
|
|
63
|
-
return stringList[index]
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
view.setOnItemSelectedListener {
|
|
68
|
-
val map = Arguments.createMap()
|
|
69
|
-
val option = Arguments.createMap()
|
|
70
|
-
for ((key, value) in mapList[it]) {
|
|
71
|
-
option.putString(key, value)
|
|
72
|
-
}
|
|
73
|
-
map.putInt("index", it)
|
|
74
|
-
map.putMap("option", option)
|
|
75
|
-
sendEvent(view.id, "onChange", map)
|
|
76
|
-
}
|
|
77
|
-
|
|
21
|
+
fun setOptions(view: PickerView, options: ReadableArray) {
|
|
22
|
+
view.options = options
|
|
78
23
|
}
|
|
79
24
|
|
|
80
25
|
@ReactProp(name = "selectedIndex")
|
|
81
|
-
fun setSelectedIndex(view:
|
|
82
|
-
view.
|
|
26
|
+
fun setSelectedIndex(view: PickerView, selectedIndex: Int) {
|
|
27
|
+
view.selectedIndex = selectedIndex
|
|
83
28
|
}
|
|
84
29
|
|
|
85
30
|
@ReactProp(name = ViewProps.COLOR, customType = "Color")
|
|
86
|
-
fun setColor(view:
|
|
87
|
-
view.
|
|
88
|
-
view.setTextColorCenter(color)
|
|
31
|
+
fun setColor(view: PickerView, color: Int) {
|
|
32
|
+
view.color = color
|
|
89
33
|
}
|
|
90
34
|
|
|
91
35
|
@ReactProp(name = "fontSize")
|
|
92
|
-
fun setFontSize(view:
|
|
93
|
-
|
|
94
|
-
view.setTextSize(fontSize)
|
|
36
|
+
fun setFontSize(view: PickerView, fontSize: Float) {
|
|
37
|
+
view.fontSize = fontSize
|
|
95
38
|
}
|
|
96
39
|
|
|
97
40
|
@ReactProp(name = "rowHeight")
|
|
98
|
-
fun setRowHeight(view:
|
|
99
|
-
|
|
100
|
-
view.setLineSpacingMultiplier(rowHeight / it)
|
|
101
|
-
}
|
|
41
|
+
fun setRowHeight(view: PickerView, rowHeight: Int) {
|
|
42
|
+
view.rowHeight = rowHeight
|
|
102
43
|
}
|
|
103
44
|
|
|
104
45
|
override fun getExportedCustomBubblingEventTypeConstants(): MutableMap<String, Any> {
|
|
@@ -107,15 +48,4 @@ class RNTPickerManager(private val reactAppContext: ReactApplicationContext) : S
|
|
|
107
48
|
.build()
|
|
108
49
|
}
|
|
109
50
|
|
|
110
|
-
override fun onDropViewInstance(view: WheelView) {
|
|
111
|
-
super.onDropViewInstance(view)
|
|
112
|
-
if (fontSizes.contains(view.id)) {
|
|
113
|
-
fontSizes.remove(view.id)
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
private fun sendEvent(id: Int, eventName: String, params: WritableMap) {
|
|
118
|
-
reactAppContext.getJSModule(RCTEventEmitter::class.java).receiveEvent(id, eventName, params)
|
|
119
|
-
}
|
|
120
|
-
|
|
121
51
|
}
|
package/index.js
CHANGED
|
@@ -14,8 +14,7 @@ class PickerComponent extends PureComponent {
|
|
|
14
14
|
static propTypes = {
|
|
15
15
|
options: PropTypes.arrayOf(
|
|
16
16
|
PropTypes.shape({
|
|
17
|
-
text: PropTypes.string,
|
|
18
|
-
value: PropTypes.string,
|
|
17
|
+
text: PropTypes.string.isRequired,
|
|
19
18
|
})
|
|
20
19
|
).isRequired,
|
|
21
20
|
selectedIndex: PropTypes.number,
|
|
@@ -7,26 +7,31 @@ public class PickerView : UIView {
|
|
|
7
7
|
|
|
8
8
|
@objc public var options = [NSDictionary]() {
|
|
9
9
|
didSet {
|
|
10
|
-
|
|
11
|
-
self.pickerView.reloadAllComponents()
|
|
12
|
-
}
|
|
10
|
+
refresh()
|
|
13
11
|
}
|
|
14
12
|
}
|
|
15
13
|
|
|
16
14
|
@objc public var selectedIndex = -1 {
|
|
17
15
|
didSet {
|
|
16
|
+
|
|
17
|
+
guard selectedIndex >= 0, selectedIndex < options.count else {
|
|
18
|
+
return
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if let onChange = onChange {
|
|
22
|
+
let dict = NSMutableDictionary()
|
|
23
|
+
dict["index"] = selectedIndex
|
|
24
|
+
dict["option"] = options[selectedIndex]
|
|
25
|
+
onChange(dict)
|
|
26
|
+
}
|
|
18
27
|
|
|
19
28
|
DispatchQueue.main.async {
|
|
20
29
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
guard selectedIndex != self.pickerView.selectedRow(inComponent: 0) else {
|
|
30
|
+
guard self.selectedIndex != self.pickerView.selectedRow(inComponent: 0) else {
|
|
24
31
|
return
|
|
25
32
|
}
|
|
26
33
|
|
|
27
|
-
|
|
28
|
-
self.pickerView.selectRow(selectedIndex, inComponent: 0, animated: true)
|
|
29
|
-
}
|
|
34
|
+
self.pickerView.selectRow(self.selectedIndex, inComponent: 0, animated: true)
|
|
30
35
|
|
|
31
36
|
}
|
|
32
37
|
|
|
@@ -35,7 +40,9 @@ public class PickerView : UIView {
|
|
|
35
40
|
|
|
36
41
|
@objc public var color = UIColor.black {
|
|
37
42
|
didSet {
|
|
38
|
-
|
|
43
|
+
if options.count > 0 {
|
|
44
|
+
refresh()
|
|
45
|
+
}
|
|
39
46
|
}
|
|
40
47
|
}
|
|
41
48
|
|
|
@@ -47,13 +54,17 @@ public class PickerView : UIView {
|
|
|
47
54
|
|
|
48
55
|
@objc public var rowHeight = 44 {
|
|
49
56
|
didSet {
|
|
50
|
-
|
|
57
|
+
if options.count > 0 {
|
|
58
|
+
refresh()
|
|
59
|
+
}
|
|
51
60
|
}
|
|
52
61
|
}
|
|
53
62
|
|
|
54
63
|
private var font = UIFont.systemFont(ofSize: 16) {
|
|
55
64
|
didSet {
|
|
56
|
-
|
|
65
|
+
if options.count > 0 {
|
|
66
|
+
refresh()
|
|
67
|
+
}
|
|
57
68
|
}
|
|
58
69
|
}
|
|
59
70
|
|
|
@@ -82,12 +93,21 @@ public class PickerView : UIView {
|
|
|
82
93
|
|
|
83
94
|
}()
|
|
84
95
|
|
|
85
|
-
private func
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
96
|
+
private func refresh() {
|
|
97
|
+
|
|
98
|
+
DispatchQueue.main.async {
|
|
99
|
+
|
|
100
|
+
self.pickerView.reloadComponent(0)
|
|
101
|
+
|
|
102
|
+
if self.selectedIndex >= 0 && self.selectedIndex < self.options.count {
|
|
103
|
+
self.pickerView.selectRow(self.selectedIndex, inComponent: 0, animated: true)
|
|
89
104
|
}
|
|
105
|
+
else {
|
|
106
|
+
self.selectedIndex = 0
|
|
107
|
+
}
|
|
108
|
+
|
|
90
109
|
}
|
|
110
|
+
|
|
91
111
|
}
|
|
92
112
|
|
|
93
113
|
}
|
|
@@ -133,12 +153,6 @@ extension PickerView : UIPickerViewDelegate {
|
|
|
133
153
|
|
|
134
154
|
public func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
|
|
135
155
|
selectedIndex = row
|
|
136
|
-
if let onChange = onChange, options.count > row {
|
|
137
|
-
let dict = NSMutableDictionary()
|
|
138
|
-
dict["index"] = row
|
|
139
|
-
dict["option"] = options[row]
|
|
140
|
-
onChange(dict)
|
|
141
|
-
}
|
|
142
156
|
}
|
|
143
157
|
|
|
144
158
|
}
|