react-native-ui-lib
Version:
UI Components Library for React Native ###### Lateset version support RN44
212 lines (140 loc) • 7.91 kB
JavaScript
Object.defineProperty(exports,"__esModule",{value:true});var _extends=Object.assign||function(target){for(var i=1;i<arguments.length;i++){var source=arguments[i];for(var key in source){if(Object.prototype.hasOwnProperty.call(source,key)){target[key]=source[key];}}}return target;};var _createClass=function(){function defineProperties(target,props){for(var i=0;i<props.length;i++){var descriptor=props[i];descriptor.enumerable=descriptor.enumerable||false;descriptor.configurable=true;if("value"in descriptor)descriptor.writable=true;Object.defineProperty(target,descriptor.key,descriptor);}}return function(Constructor,protoProps,staticProps){if(protoProps)defineProperties(Constructor.prototype,protoProps);if(staticProps)defineProperties(Constructor,staticProps);return Constructor;};}();var _react=require('react');var _react2=_interopRequireDefault(_react);
var _reactNative=require('react-native');
var _propTypes=require('prop-types');var _propTypes2=_interopRequireDefault(_propTypes);
var _lodash=require('lodash');var _lodash2=_interopRequireDefault(_lodash);
var _commons=require('../../commons');
var _helpers=require('../../helpers');
var _inputs=require('../inputs');
var _view=require('../view');var _view2=_interopRequireDefault(_view);
var _text=require('../text');var _text2=_interopRequireDefault(_text);
var _style=require('../../style');function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj};}function _objectWithoutProperties(obj,keys){var target={};for(var i in obj){if(keys.indexOf(i)>=0)continue;if(!Object.prototype.hasOwnProperty.call(obj,i))continue;target[i]=obj[i];}return target;}function _toConsumableArray(arr){if(Array.isArray(arr)){for(var i=0,arr2=Array(arr.length);i<arr.length;i++){arr2[i]=arr[i];}return arr2;}else{return Array.from(arr);}}function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError("Cannot call a class as a function");}}function _possibleConstructorReturn(self,call){if(!self){throw new ReferenceError("this hasn't been initialised - super() hasn't been called");}return call&&(typeof call==="object"||typeof call==="function")?call:self;}function _inherits(subClass,superClass){if(typeof superClass!=="function"&&superClass!==null){throw new TypeError("Super expression must either be null or a function, not "+typeof superClass);}subClass.prototype=Object.create(superClass&&superClass.prototype,{constructor:{value:subClass,enumerable:false,writable:true,configurable:true}});if(superClass)Object.setPrototypeOf?Object.setPrototypeOf(subClass,superClass):subClass.__proto__=superClass;}var
TagsInput=function(_BaseComponent){_inherits(TagsInput,_BaseComponent);
function TagsInput(props){_classCallCheck(this,TagsInput);var _this=_possibleConstructorReturn(this,(TagsInput.__proto__||Object.getPrototypeOf(TagsInput)).call(this,
props));
_this.addTag=_this.addTag.bind(_this);
_this.onChangeText=_this.onChangeText.bind(_this);
_this.renderTag=_this.renderTag.bind(_this);
_this.getLabel=_this.getLabel.bind(_this);
_this.onKeyPress=_this.onKeyPress.bind(_this);
_this.state={
value:props.value,
tags:props.tags,
prepareToRemoveLastTag:false};return _this;
}_createClass(TagsInput,[{key:'componentDidMount',value:function componentDidMount()
{
_reactNative.DeviceEventEmitter.addListener('onBackspacePress',this.onKeyPress);
}},{key:'componentWillUnmount',value:function componentWillUnmount()
{
_reactNative.DeviceEventEmitter.removeListener('onBackspacePress',this.onKeyPress);
}},{key:'componentWillReceiveProps',value:function componentWillReceiveProps(
nextProps){
if(nextProps.tags!==this.state.tags){
this.setState({
tags:nextProps.tags});
}
}},{key:'addTag',value:function addTag()
{var _state=
this.state,value=_state.value,tags=_state.tags;
if(_lodash2.default.isEmpty(value.trim()))return;
var newTags=[].concat(_toConsumableArray(tags),[value]);
this.setState({
value:'',
tags:newTags});
_lodash2.default.invoke(this.props,'onChangeTags',newTags);
this.input.clear();
}},{key:'removeLastTag',value:function removeLastTag()
{var
tags=this.state.tags;
tags.splice(-1);
this.setState({
tags:tags});
_lodash2.default.invoke(this.props,'onChangeTags',tags);
}},{key:'onChangeText',value:function onChangeText(
value){
console.log('ethan - onChangeText',value);
this.setState({value:value,prepareToRemoveLastTag:false});
_lodash2.default.invoke(this.props,'onChangeText',value);
}},{key:'onKeyPress',value:function onKeyPress(
event){var _state2=
this.state,prepareToRemoveLastTag=_state2.prepareToRemoveLastTag,value=_state2.value;
var keyCode=_lodash2.default.get(event,'nativeEvent.key');
if(_lodash2.default.isEmpty(value)&&(keyCode==='Backspace'||_helpers.Constants.isAndroid)){
if(prepareToRemoveLastTag){
this.removeLastTag();
}
this.setState({
prepareToRemoveLastTag:!prepareToRemoveLastTag});
}
_lodash2.default.invoke(this.props,'onKeyPress',event);
}},{key:'getLabel',value:function getLabel(
item){var
getLabel=this.props.getLabel;
if(getLabel){
return getLabel(item);
}
if(_lodash2.default.isString(item)){
return item;
}
return _lodash2.default.get(item,'label');
}},{key:'renderTag',value:function renderTag(
tag,index){var _state3=
this.state,prepareToRemoveLastTag=_state3.prepareToRemoveLastTag,tags=_state3.tags;
var tagsCount=_lodash2.default.size(tags);
var isLastTag=index===tagsCount-1;
var shouldFocusLastTag=isLastTag&&prepareToRemoveLastTag;
var typography=this.extractTypographyValue();
return(
_react2.default.createElement(_view2.default,{key:index,style:[styles.tag,shouldFocusLastTag&&styles.tagFocused]},
_react2.default.createElement(_text2.default,{style:[styles.tagLabel,typography]},
this.getLabel(tag))));
}},{key:'renderTextInput',value:function renderTextInput()
{var _this2=this;var _props=
this.props,containerStyle=_props.containerStyle,others=_objectWithoutProperties(_props,['containerStyle']);var _state4=
this.state,value=_state4.value,prepareToRemoveLastTag=_state4.prepareToRemoveLastTag;
return(
_react2.default.createElement(_view2.default,{style:styles.inputWrapper},
_react2.default.createElement(_inputs.TextInput,_extends({
ref:function ref(r){return _this2.input=r;}},
others,{
value:value,
onSubmitEditing:this.addTag,
onChangeText:this.onChangeText,
onKeyPress:this.onKeyPress,
blurOnSubmit:false,
enableErrors:false,
hideUnderline:true,
selectionColor:prepareToRemoveLastTag?'transparent':undefined}))));
}},{key:'render',value:function render()
{var _props2=
this.props,containerStyle=_props2.containerStyle,hideUnderline=_props2.hideUnderline;var
tags=this.state.tags;
return(
_react2.default.createElement(_view2.default,{style:[!hideUnderline&&styles.withUnderline,containerStyle]},
_react2.default.createElement(_view2.default,{style:styles.tagsList},
_lodash2.default.map(tags,this.renderTag),
this.renderTextInput())));
}}]);return TagsInput;}(_commons.BaseComponent);TagsInput.displayName='TagsInput';TagsInput.propTypes={tags:_propTypes2.default.arrayOf(_propTypes2.default.oneOfType([_propTypes2.default.object,_propTypes2.default.string])),getLabel:_propTypes2.default.func,onChangeTags:_propTypes2.default.func,containerStyle:_reactNative.ViewPropTypes.style,hideUnderline:_propTypes2.default.bool};exports.default=TagsInput;
var GUTTER_SPACING=8;
var styles=_reactNative.StyleSheet.create({
withUnderline:{
borderBottomWidth:_reactNative.StyleSheet.hairlineWidth,
borderColor:_style.ThemeManager.dividerColor},
tagsList:{
flexDirection:'row',
flexWrap:'wrap',
alignItems:'center'},
inputWrapper:{
flexGrow:1,
minWidth:120,
marginBottom:GUTTER_SPACING},
tag:{
backgroundColor:_style.Colors.blue30,
borderRadius:_style.BorderRadiuses.br100,
paddingVertical:4,
paddingHorizontal:12,
marginRight:GUTTER_SPACING,
marginBottom:GUTTER_SPACING},
tagFocused:{
backgroundColor:_style.Colors.blue50},
tagLabel:{
color:_style.Colors.white}});