@momo-kits/auto-complete 0.0.34-beta → 0.0.36-beta
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/AutoComplete.js +342 -1
- package/index.js +5 -1
- package/package.json +3 -10
- package/publish.sh +2 -2
- package/babel.config.js +0 -1
package/AutoComplete.js
CHANGED
|
@@ -1 +1,342 @@
|
|
|
1
|
-
var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.default=void 0;var _classCallCheck2=_interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));var _createClass2=_interopRequireDefault(require("@babel/runtime/helpers/createClass"));var _inherits2=_interopRequireDefault(require("@babel/runtime/helpers/inherits"));var _possibleConstructorReturn2=_interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));var _getPrototypeOf2=_interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));var _react=_interopRequireWildcard(require("react"));var _reactNative=require("react-native");var _lodash=require("lodash");var _propTypes=_interopRequireDefault(require("prop-types"));var _reactNativeGestureHandler=require("react-native-gesture-handler");var _core=require("@momo-kits/core");var _jsxFileName="/Users/trinh.ho2/momo-folk/kits/src/libs/autoComplete/dist/AutoComplete.js";function _getRequireWildcardCache(nodeInterop){if(typeof WeakMap!=="function")return null;var cacheBabelInterop=new WeakMap();var cacheNodeInterop=new WeakMap();return(_getRequireWildcardCache=function _getRequireWildcardCache(nodeInterop){return nodeInterop?cacheNodeInterop:cacheBabelInterop;})(nodeInterop);}function _interopRequireWildcard(obj,nodeInterop){if(!nodeInterop&&obj&&obj.__esModule){return obj;}if(obj===null||typeof obj!=="object"&&typeof obj!=="function"){return{default:obj};}var cache=_getRequireWildcardCache(nodeInterop);if(cache&&cache.has(obj)){return cache.get(obj);}var newObj={};var hasPropertyDescriptor=Object.defineProperty&&Object.getOwnPropertyDescriptor;for(var key in obj){if(key!=="default"&&Object.prototype.hasOwnProperty.call(obj,key)){var desc=hasPropertyDescriptor?Object.getOwnPropertyDescriptor(obj,key):null;if(desc&&(desc.get||desc.set)){Object.defineProperty(newObj,key,desc);}else{newObj[key]=obj[key];}}}newObj.default=obj;if(cache){cache.set(obj,newObj);}return newObj;}function _createSuper(Derived){var hasNativeReflectConstruct=_isNativeReflectConstruct();return function _createSuperInternal(){var Super=(0,_getPrototypeOf2.default)(Derived),result;if(hasNativeReflectConstruct){var NewTarget=(0,_getPrototypeOf2.default)(this).constructor;result=Reflect.construct(Super,arguments,NewTarget);}else{result=Super.apply(this,arguments);}return(0,_possibleConstructorReturn2.default)(this,result);};}function _isNativeReflectConstruct(){if(typeof Reflect==="undefined"||!Reflect.construct)return false;if(Reflect.construct.sham)return false;if(typeof Proxy==="function")return true;try{Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}));return true;}catch(e){return false;}}var AutoComplete=function(_Component){(0,_inherits2.default)(AutoComplete,_Component);var _super=_createSuper(AutoComplete);function AutoComplete(props){var _this;(0,_classCallCheck2.default)(this,AutoComplete);_this=_super.call(this,props);_this.getValueByKey=function(key,value){var splitKey=key.split('-');return splitKey.length>1?splitKey.reduce(function(result,item,index){return result=result+value[item]+(index===splitKey.length-1?'':!!value[item]?' ':'');},''):key==='phone'?_core.NumberUtil.formatPhoneNumberVN(value[key]):value[key];};_this.querySearch=function(child,text){var keyAutoComplete=(0,_lodash.get)(child.props,'keyAutoComplete','');var isShowAutoComplete=(0,_lodash.get)(child.props,'isShowAutoComplete',true);var data=_this.props.data;var dataOutput=data&&data.length>0&&_this.filter(data,keyAutoComplete,text);if(_this.hashmapRefs[keyAutoComplete]){_this.setState({suggest:{data:dataOutput,position:_this.hashmapPosition[keyAutoComplete],isShowAutoComplete:isShowAutoComplete}});}};_this.changeTextHandle=function(child,text,onChangeText){_this.querySearch(child,text);if(onChangeText&&typeof onChangeText==='function'){onChangeText(text);}};_this.focusHandle=function(e,child,onFocus){var text='';if(child&&typeof child.getText==='function'){text=child.getText();}_this.querySearch(child,text);if(onFocus&&typeof onFocus==='function'){onFocus(e);}};_this.endFocusHandle=function(e,onEndEditing){if(_this.isShowingSuggest()){_this.hideSuggest();}if(onEndEditing&&typeof onEndEditing==='function'){onEndEditing(e);}};_this.filter=function(data,key,query){if(!data||!key){return null;}if(query===''){return data;}return data.filter(function(item){var valueStr=item?_this.getValueByKey(key,item):'';var valueStrFormated=_core.ValueUtil.removeAlias(valueStr).toLowerCase().trim().replace(/\s/g,'');var queryFormated=_core.ValueUtil.removeAlias(query).toLowerCase().trim().replace(/\s/g,'');return valueStrFormated.indexOf(queryFormated)!==-1;});};_this.renderItem=function(_ref){var item=_ref.item,index=_ref.index;var renderSuggestItem=_this.props.renderSuggestItem;return _react.default.createElement(_reactNativeGestureHandler.TouchableOpacity,{onPress:function onPress(){return _this.onPressItemSuggest(item);},__source:{fileName:_jsxFileName,lineNumber:238,columnNumber:13}},renderSuggestItem&&typeof renderSuggestItem==='function'?renderSuggestItem({item:item,index:index}):_this.renderSuggestItemDefault({item:item,index:index}));};_this.renderSeparator=function(){return _react.default.createElement(_reactNative.View,{style:styles.separator,__source:{fileName:_jsxFileName,lineNumber:249,columnNumber:9}});};_this.renderSuggestItemDefault=function(_ref2){var item=_ref2.item;var title=item.title,value=item.value;return _react.default.createElement(_reactNative.View,{style:[styles.viewSuggest],__source:{fileName:_jsxFileName,lineNumber:257,columnNumber:13}},_react.default.createElement(_core.Text.Title,{__source:{fileName:_jsxFileName,lineNumber:258,columnNumber:17}},title),_react.default.createElement(_core.Text.Title,{__source:{fileName:_jsxFileName,lineNumber:259,columnNumber:17}},value));};_this.onPressItemSuggest=function(item){_this.selectedItem=item;Object.keys(_this.hashmapRefs).forEach(function(key){if(_this.hashmapRefs[key].setText&&typeof _this.hashmapRefs[key].setText==='function')_this.hashmapRefs[key].setText(_this.getValueByKey(key,_this.selectedItem));if(_this.hashmapRefs[key].setValue&&typeof _this.hashmapRefs[key].setValue==='function')_this.hashmapRefs[key].setValue(_this.getValueByKey(key,_this.selectedItem));});var onSelected=_this.props.onSelected;_this.setState({suggest:{}},function(){if(onSelected&&typeof onSelected==='function'){onSelected(item);}_this.selectedItem=null;});};_this.isShowingSuggest=function(){var suggest=_this.state.suggest;return suggest&&suggest.data&&suggest.data.length>0;};_this.hideSuggest=function(){_this.setState({suggest:{}},function(){_this.selectedItem=null;});};_this.hashmapRefs={};_this.hashmapPosition={};_this.hashmapInputValue={};_this.selectedItem=null;_this.childrenWithProps=null;return _this;}(0,_createClass2.default)(AutoComplete,[{key:"measure",value:function measure(){var _this2=this;try{if(this.hashmapRefs){Object.keys(this.hashmapRefs).forEach(function(key){if(_reactNative.Platform.OS==='android'){_reactNative.UIManager.measureLayoutRelativeToParent((0,_reactNative.findNodeHandle)(_this2.hashmapRefs[key]),function(e){console.error(e);},function(x,y,width,height){_this2.hashmapPosition[key]={x:x,y:y+height,width:width};});}else{_reactNative.UIManager.measure((0,_reactNative.findNodeHandle)(_this2.hashmapRefs[key]),function(x,y,width,height){_this2.hashmapPosition[key]={x:x,y:y+height,width:width};});}});}}catch(e){console.log("try catch :: "+e);}}},{key:"componentDidMount",value:function componentDidMount(){var _this3=this;setTimeout(function(){_this3.measure();},500);}},{key:"cloneChildren",value:function cloneChildren(components){var _this4=this;if(components){return _react.default.Children.map(components,function(child){var _child$props,_child$props2;if(!(child!=null&&child.props))return child;if(child!=null&&(_child$props=child.props)!=null&&_child$props.children&&_react.default.Children.count(child==null?void 0:(_child$props2=child.props)==null?void 0:_child$props2.children)>0&&child.type.name!==_core.Text){return _react.default.cloneElement(child,{children:_this4.cloneChildren(child.props.children)});}var _child$props3=child.props,_onChangeText=_child$props3.onChangeText,keyAutoComplete=_child$props3.keyAutoComplete,_onFocus=_child$props3.onFocus,_onEndEditing=_child$props3.onEndEditing;if(keyAutoComplete){if(_this4.selectedItem){_this4.hashmapInputValue[keyAutoComplete]=_this4.getValueByKey(keyAutoComplete,_this4.selectedItem);return _react.default.cloneElement(child,{ref:function ref(view){return _this4.hashmapRefs[keyAutoComplete]=view;},onChangeText:function onChangeText(text){return _this4.changeTextHandle(child,text,_onChangeText);},onFocus:function onFocus(e){return _this4.focusHandle(e,child,_onFocus);},onEndEditing:function onEndEditing(e){return _this4.endFocusHandle(e,_onEndEditing);}});}return _react.default.cloneElement(child,{ref:function ref(view){return _this4.hashmapRefs[keyAutoComplete]=view;},onChangeText:function onChangeText(text){return _this4.changeTextHandle(child,text,_onChangeText);},onFocus:function onFocus(e){return _this4.focusHandle(e,child,_onFocus);},onEndEditing:function onEndEditing(e){return _this4.endFocusHandle(e,_onEndEditing);}});}return child;});}return components;}},{key:"render",value:function render(){var _this$props$style=this.props.style,style=_this$props$style===void 0?{}:_this$props$style;var suggest=(0,_lodash.get)(this.state,'suggest',{});var childrenWithProps=this.cloneChildren((0,_lodash.get)(this.props,'children',null));return _react.default.createElement(_reactNative.View,{style:[{zIndex:1},style],__source:{fileName:_jsxFileName,lineNumber:127,columnNumber:13}},childrenWithProps,this.renderSuggest(suggest));}},{key:"renderSuggest",value:function renderSuggest(suggest){var _this5=this;var numSuggest=this.props.numSuggest;if(suggest&&suggest.data&&suggest.data.length>0&&suggest.isShowAutoComplete){var _ref3=suggest.position||{},_ref3$x=_ref3.x,x=_ref3$x===void 0?0:_ref3$x,_ref3$y=_ref3.y,y=_ref3$y===void 0?0:_ref3$y,_ref3$width=_ref3.width,width=_ref3$width===void 0?0:_ref3$width;var sliceData=suggest.data.slice(0,numSuggest);return _react.default.createElement(_reactNative.View,{style:[styles.containerSuggest,{left:x,top:y,width:width}],__source:{fileName:_jsxFileName,lineNumber:210,columnNumber:17}},sliceData.map(function(item,index){return _react.default.createElement(_reactNative.View,{key:index.toString(),__source:{fileName:_jsxFileName,lineNumber:220,columnNumber:29}},_this5.renderItem({item:item,index:index}),index!==sliceData.length-1&&_this5.renderSeparator());}));}return null;}}]);return AutoComplete;}(_react.Component);exports.default=AutoComplete;var styles=_reactNative.StyleSheet.create({viewSuggest:{flexDirection:'row',justifyContent:'space-between',alignItems:'center',paddingVertical:5},containerSuggest:{backgroundColor:'white',paddingVertical:10,paddingHorizontal:12,position:'absolute',maxHeight:240,borderColor:'#DADADA',borderRadius:4,borderWidth:1,shadowColor:'#000000',shadowOffset:{width:0,height:1},shadowRadius:1,elevation:2,shadowOpacity:0.5},separator:{height:1,width:'100%',backgroundColor:_core.Colors.placeholder,marginVertical:10}});AutoComplete.propTypes={style:_propTypes.default.oneOfType([_propTypes.default.array,_propTypes.default.object,_propTypes.default.number]),data:_propTypes.default.arrayOf(_propTypes.default.object).isRequired,renderSuggestItem:_propTypes.default.func,onSelected:_propTypes.default.func.isRequired,numSuggest:_propTypes.default.number};AutoComplete.defaultProps={numSuggest:2};
|
|
1
|
+
/* eslint-disable no-extra-boolean-cast */
|
|
2
|
+
/* eslint-disable no-param-reassign */
|
|
3
|
+
import React, { Component } from 'react';
|
|
4
|
+
import {
|
|
5
|
+
findNodeHandle,
|
|
6
|
+
StyleSheet,
|
|
7
|
+
UIManager,
|
|
8
|
+
View,
|
|
9
|
+
Platform
|
|
10
|
+
} from 'react-native';
|
|
11
|
+
import { get } from 'lodash';
|
|
12
|
+
import PropTypes from 'prop-types';
|
|
13
|
+
import { TouchableOpacity } from 'react-native-gesture-handler';
|
|
14
|
+
import {
|
|
15
|
+
ValueUtil, NumberUtil, Colors, Text
|
|
16
|
+
} from '@momo-kits/core';
|
|
17
|
+
|
|
18
|
+
export default class AutoComplete extends Component {
|
|
19
|
+
constructor(props) {
|
|
20
|
+
super(props);
|
|
21
|
+
this.hashmapRefs = {};
|
|
22
|
+
this.hashmapPosition = {};
|
|
23
|
+
this.hashmapInputValue = {};
|
|
24
|
+
this.selectedItem = null;
|
|
25
|
+
this.childrenWithProps = null;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* func measure all component have keyAutoComplete to get it's position
|
|
30
|
+
*/
|
|
31
|
+
measure() {
|
|
32
|
+
try {
|
|
33
|
+
if (this.hashmapRefs) {
|
|
34
|
+
Object.keys(this.hashmapRefs)
|
|
35
|
+
.forEach((key) => {
|
|
36
|
+
if (Platform.OS === 'android') {
|
|
37
|
+
UIManager.measureLayoutRelativeToParent(findNodeHandle(this.hashmapRefs[key]), (e) => { console.error(e); }, (x, y, width, height) => {
|
|
38
|
+
this.hashmapPosition[key] = {
|
|
39
|
+
x,
|
|
40
|
+
y: y + height,
|
|
41
|
+
width
|
|
42
|
+
};
|
|
43
|
+
});
|
|
44
|
+
} else {
|
|
45
|
+
UIManager.measure(findNodeHandle(this.hashmapRefs[key]), (x, y, width, height) => {
|
|
46
|
+
this.hashmapPosition[key] = {
|
|
47
|
+
x,
|
|
48
|
+
y: y + height,
|
|
49
|
+
width
|
|
50
|
+
};
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
} catch (e) {
|
|
56
|
+
console.log(`try catch :: ${e}`);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
componentDidMount() {
|
|
61
|
+
// setTimeout to fix async
|
|
62
|
+
setTimeout(() => {
|
|
63
|
+
this.measure();
|
|
64
|
+
}, 500);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
getValueByKey = (key, value) => {
|
|
68
|
+
const splitKey = key.split('-');
|
|
69
|
+
return splitKey.length > 1 ? splitKey.reduce((result, item, index) => result = result + value[item] + (index === splitKey.length - 1 ? '' : !!value[item] ? ' ' : ''), '')
|
|
70
|
+
: key === 'phone' ? NumberUtil.formatPhoneNumberVN(value[key]) : value[key];
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* loop all child components
|
|
75
|
+
* @param {"Children"} components ;
|
|
76
|
+
*/
|
|
77
|
+
cloneChildren(components) {
|
|
78
|
+
if (components) {
|
|
79
|
+
return React.Children.map(components, (child) => {
|
|
80
|
+
if (!child?.props) return child;
|
|
81
|
+
if (child?.props?.children && React.Children.count(child?.props?.children) > 0 && child.type.name !== Text) {
|
|
82
|
+
// component have children -> clone it and all it's children
|
|
83
|
+
return React.cloneElement(child, {
|
|
84
|
+
children: this.cloneChildren(child.props.children)
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const {
|
|
89
|
+
onChangeText,
|
|
90
|
+
keyAutoComplete,
|
|
91
|
+
onFocus,
|
|
92
|
+
onEndEditing
|
|
93
|
+
} = child.props;
|
|
94
|
+
if (keyAutoComplete) {
|
|
95
|
+
// Update props when component have keyAutoComplete
|
|
96
|
+
if (this.selectedItem) {
|
|
97
|
+
this.hashmapInputValue[keyAutoComplete] = this.getValueByKey(keyAutoComplete, this.selectedItem);// this.selectedItem[keyAutoComplete];
|
|
98
|
+
return React.cloneElement(child, {
|
|
99
|
+
ref: (view) => this.hashmapRefs[keyAutoComplete] = view,
|
|
100
|
+
onChangeText: (text) => this.changeTextHandle(child, text, onChangeText),
|
|
101
|
+
onFocus: (e) => this.focusHandle(e, child, onFocus),
|
|
102
|
+
// value: this.getValueByKey(keyAutoComplete, this.selectedItem),
|
|
103
|
+
onEndEditing: (e) => this.endFocusHandle(e, onEndEditing)
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
return React.cloneElement(child, {
|
|
107
|
+
ref: (view) => this.hashmapRefs[keyAutoComplete] = view,
|
|
108
|
+
onChangeText: (text) => this.changeTextHandle(child, text, onChangeText),
|
|
109
|
+
onFocus: (e) => this.focusHandle(e, child, onFocus),
|
|
110
|
+
onEndEditing: (e) => this.endFocusHandle(e, onEndEditing)
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
return child;
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return components;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
render() {
|
|
121
|
+
const {
|
|
122
|
+
style = {},
|
|
123
|
+
} = this.props;
|
|
124
|
+
const suggest = get(this.state, 'suggest', {});
|
|
125
|
+
const childrenWithProps = this.cloneChildren(get(this.props, 'children', null));
|
|
126
|
+
return (
|
|
127
|
+
<View style={[{ zIndex: 1 }, style]}>
|
|
128
|
+
{childrenWithProps}
|
|
129
|
+
{this.renderSuggest(suggest)}
|
|
130
|
+
</View>
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
querySearch = (child, text) => {
|
|
135
|
+
const keyAutoComplete = get(child.props, 'keyAutoComplete', '');
|
|
136
|
+
const isShowAutoComplete = get(child.props, 'isShowAutoComplete', true);
|
|
137
|
+
const { data } = this.props;
|
|
138
|
+
const dataOutput = data && data.length > 0 && this.filter(data, keyAutoComplete, text);
|
|
139
|
+
if (this.hashmapRefs[keyAutoComplete]) {
|
|
140
|
+
this.setState({
|
|
141
|
+
suggest: {
|
|
142
|
+
data: dataOutput,
|
|
143
|
+
position: this.hashmapPosition[keyAutoComplete],
|
|
144
|
+
isShowAutoComplete
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
changeTextHandle = (child, text, onChangeText) => {
|
|
151
|
+
this.querySearch(child, text);
|
|
152
|
+
if (onChangeText && typeof onChangeText === 'function') {
|
|
153
|
+
onChangeText(text);
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
focusHandle = (e, child, onFocus) => {
|
|
158
|
+
let text = '';
|
|
159
|
+
if (child && typeof child.getText === 'function') {
|
|
160
|
+
text = child.getText();
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
this.querySearch(child, text);
|
|
164
|
+
|
|
165
|
+
if (onFocus && typeof onFocus === 'function') {
|
|
166
|
+
onFocus(e);
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
endFocusHandle = (e, onEndEditing) => {
|
|
171
|
+
if (this.isShowingSuggest()) {
|
|
172
|
+
this.hideSuggest();
|
|
173
|
+
}
|
|
174
|
+
if (onEndEditing && typeof onEndEditing === 'function') {
|
|
175
|
+
onEndEditing(e);
|
|
176
|
+
}
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
filter = (data, key, query) => {
|
|
180
|
+
if (!data || !key) {
|
|
181
|
+
return null;
|
|
182
|
+
}
|
|
183
|
+
if (query === '') {
|
|
184
|
+
return data;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
return data.filter((item) => {
|
|
188
|
+
const valueStr = item ? this.getValueByKey(key, item) : '';
|
|
189
|
+
const valueStrFormated = ValueUtil.removeAlias(valueStr)
|
|
190
|
+
.toLowerCase()
|
|
191
|
+
.trim()
|
|
192
|
+
.replace(/\s/g, '');
|
|
193
|
+
const queryFormated = ValueUtil.removeAlias(query)
|
|
194
|
+
.toLowerCase()
|
|
195
|
+
.trim()
|
|
196
|
+
.replace(/\s/g, '');
|
|
197
|
+
return (
|
|
198
|
+
valueStrFormated.indexOf(queryFormated) !== -1
|
|
199
|
+
);
|
|
200
|
+
});
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
renderSuggest(suggest) {
|
|
204
|
+
const { numSuggest } = this.props;
|
|
205
|
+
if (suggest && suggest.data && suggest.data.length > 0 && suggest.isShowAutoComplete) {
|
|
206
|
+
const { x = 0, y = 0, width = 0 } = suggest.position || {};
|
|
207
|
+
const sliceData = suggest.data.slice(0, numSuggest);
|
|
208
|
+
|
|
209
|
+
return (
|
|
210
|
+
<View style={
|
|
211
|
+
[styles.containerSuggest, {
|
|
212
|
+
left: x,
|
|
213
|
+
top: y,
|
|
214
|
+
width
|
|
215
|
+
}]
|
|
216
|
+
}
|
|
217
|
+
>
|
|
218
|
+
{
|
|
219
|
+
sliceData.map((item, index) => (
|
|
220
|
+
<View key={index.toString()}>
|
|
221
|
+
{this.renderItem({ item, index })}
|
|
222
|
+
{index !== sliceData.length - 1 && this.renderSeparator()}
|
|
223
|
+
</View>
|
|
224
|
+
))
|
|
225
|
+
}
|
|
226
|
+
</View>
|
|
227
|
+
|
|
228
|
+
);
|
|
229
|
+
}
|
|
230
|
+
return null;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
renderItem = ({ item, index }) => {
|
|
234
|
+
const {
|
|
235
|
+
renderSuggestItem
|
|
236
|
+
} = this.props;
|
|
237
|
+
return (
|
|
238
|
+
<TouchableOpacity onPress={() => this.onPressItemSuggest(item)}>
|
|
239
|
+
{
|
|
240
|
+
(renderSuggestItem && typeof renderSuggestItem === 'function')
|
|
241
|
+
? renderSuggestItem({ item, index })
|
|
242
|
+
: this.renderSuggestItemDefault({ item, index })
|
|
243
|
+
}
|
|
244
|
+
</TouchableOpacity>
|
|
245
|
+
);
|
|
246
|
+
};
|
|
247
|
+
|
|
248
|
+
renderSeparator = () => (
|
|
249
|
+
<View
|
|
250
|
+
style={styles.separator}
|
|
251
|
+
/>
|
|
252
|
+
);
|
|
253
|
+
|
|
254
|
+
renderSuggestItemDefault = ({ item }) => {
|
|
255
|
+
const { title, value } = item;
|
|
256
|
+
return (
|
|
257
|
+
<View style={[styles.viewSuggest]}>
|
|
258
|
+
<Text.Title>{title}</Text.Title>
|
|
259
|
+
<Text.Title>{value}</Text.Title>
|
|
260
|
+
</View>
|
|
261
|
+
);
|
|
262
|
+
};
|
|
263
|
+
|
|
264
|
+
onPressItemSuggest = (item) => {
|
|
265
|
+
this.selectedItem = item;
|
|
266
|
+
// Loop hashRef to update value of child
|
|
267
|
+
Object.keys(this.hashmapRefs).forEach((key) => {
|
|
268
|
+
if (this.hashmapRefs[key].setText && typeof this.hashmapRefs[key].setText === 'function') this.hashmapRefs[key].setText(this.getValueByKey(key, this.selectedItem));
|
|
269
|
+
if (this.hashmapRefs[key].setValue && typeof this.hashmapRefs[key].setValue === 'function') this.hashmapRefs[key].setValue(this.getValueByKey(key, this.selectedItem));
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
const {
|
|
273
|
+
onSelected
|
|
274
|
+
} = this.props;
|
|
275
|
+
this.setState({
|
|
276
|
+
suggest: {}
|
|
277
|
+
}, () => {
|
|
278
|
+
if (onSelected && typeof onSelected === 'function') {
|
|
279
|
+
onSelected(item);
|
|
280
|
+
}
|
|
281
|
+
this.selectedItem = null;
|
|
282
|
+
});
|
|
283
|
+
};
|
|
284
|
+
|
|
285
|
+
isShowingSuggest = () => {
|
|
286
|
+
const { suggest } = this.state;
|
|
287
|
+
return suggest && suggest.data && suggest.data.length > 0;
|
|
288
|
+
};
|
|
289
|
+
|
|
290
|
+
hideSuggest = () => {
|
|
291
|
+
this.setState({
|
|
292
|
+
suggest: {}
|
|
293
|
+
}, () => {
|
|
294
|
+
this.selectedItem = null;
|
|
295
|
+
});
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
const styles = StyleSheet.create({
|
|
300
|
+
viewSuggest: {
|
|
301
|
+
flexDirection: 'row',
|
|
302
|
+
justifyContent: 'space-between',
|
|
303
|
+
alignItems: 'center',
|
|
304
|
+
paddingVertical: 5,
|
|
305
|
+
},
|
|
306
|
+
containerSuggest: {
|
|
307
|
+
backgroundColor: 'white',
|
|
308
|
+
paddingVertical: 10,
|
|
309
|
+
paddingHorizontal: 12,
|
|
310
|
+
position: 'absolute',
|
|
311
|
+
maxHeight: 240,
|
|
312
|
+
borderColor: '#DADADA',
|
|
313
|
+
borderRadius: 4,
|
|
314
|
+
borderWidth: 1,
|
|
315
|
+
shadowColor: '#000000',
|
|
316
|
+
shadowOffset: {
|
|
317
|
+
width: 0,
|
|
318
|
+
height: 1
|
|
319
|
+
},
|
|
320
|
+
shadowRadius: 1,
|
|
321
|
+
elevation: 2,
|
|
322
|
+
shadowOpacity: 0.5,
|
|
323
|
+
},
|
|
324
|
+
separator: {
|
|
325
|
+
height: 1,
|
|
326
|
+
width: '100%',
|
|
327
|
+
backgroundColor: Colors.placeholder,
|
|
328
|
+
marginVertical: 10
|
|
329
|
+
}
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
AutoComplete.propTypes = {
|
|
333
|
+
style: PropTypes.oneOfType([PropTypes.array, PropTypes.object, PropTypes.number]),
|
|
334
|
+
data: PropTypes.arrayOf(PropTypes.object).isRequired,
|
|
335
|
+
renderSuggestItem: PropTypes.func,
|
|
336
|
+
onSelected: PropTypes.func.isRequired,
|
|
337
|
+
numSuggest: PropTypes.number,
|
|
338
|
+
};
|
|
339
|
+
|
|
340
|
+
AutoComplete.defaultProps = {
|
|
341
|
+
numSuggest: 2,
|
|
342
|
+
};
|
package/index.js
CHANGED
|
@@ -1 +1,5 @@
|
|
|
1
|
-
|
|
1
|
+
import AutoComplete from './AutoComplete';
|
|
2
|
+
|
|
3
|
+
export {
|
|
4
|
+
AutoComplete
|
|
5
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@momo-kits/auto-complete",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.36-beta",
|
|
4
4
|
"private": false,
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"dependencies": {},
|
|
@@ -12,13 +12,6 @@
|
|
|
12
12
|
"react-native-gesture-handler": "^1.5.5",
|
|
13
13
|
"@momo-kits/core": ">=0.0.4-beta"
|
|
14
14
|
},
|
|
15
|
-
"devDependencies": {
|
|
16
|
-
|
|
17
|
-
"@babel/core": "^7.12.9",
|
|
18
|
-
"@babel/runtime": "^7.12.5"
|
|
19
|
-
},
|
|
20
|
-
"license": "MoMo",
|
|
21
|
-
"jest": {
|
|
22
|
-
"preset": "react-native"
|
|
23
|
-
}
|
|
15
|
+
"devDependencies": {},
|
|
16
|
+
"license": "MoMo"
|
|
24
17
|
}
|
package/publish.sh
CHANGED
|
@@ -12,7 +12,7 @@ echo VERSION: $VERSION
|
|
|
12
12
|
rsync -r --verbose --exclude '*.mdx' --exclude '*Demo.js' --exclude 'props-type.js' --exclude 'prop-types.js' ./* dist
|
|
13
13
|
|
|
14
14
|
# #babel component to dist
|
|
15
|
-
babel ./dist -d dist --copy-files
|
|
15
|
+
#babel ./dist -d dist --copy-files
|
|
16
16
|
|
|
17
17
|
#copy option
|
|
18
18
|
#cp -r ./src/ dist
|
|
@@ -26,4 +26,4 @@ cd ..
|
|
|
26
26
|
rm -rf dist
|
|
27
27
|
|
|
28
28
|
|
|
29
|
-
|
|
29
|
+
curl -X POST -H 'Content-Type: application/json' 'https://chat.googleapis.com/v1/spaces/AAAAbP8987c/messages?key=AIzaSyDdI0hCZtE6vySjMm-WEfRq3CPzqKqqsHI&token=UGSFRvk_oYb9uGsAgs31bVvMm6jDkmD8zihGm3eyaQA%3D&threadKey=JoaXTEYaNNkl' -d '{"text": "@momo-kits/auto-complete new version release: '*"$VERSION"*' https://www.npmjs.com/package/@momo-kits/auto-complete"}'
|
package/babel.config.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
module.exports={presets:['module:metro-react-native-babel-preset']};
|