1
1
import _ from 'lodash' ;
2
2
import PropTypes from 'prop-types' ;
3
- import React from 'react' ;
3
+ import React , { Component } from 'react' ;
4
4
import ReactNative , { NativeModules , StyleSheet , ViewPropTypes , Image , DeviceEventEmitter } from 'react-native' ;
5
5
import { Constants } from '../../helpers' ;
6
6
import { Colors , BorderRadiuses , ThemeManager , Typography } from '../../style' ;
7
7
import Assets from '../../assets' ;
8
- import { BaseComponent } from '../../commons' ;
8
+ import { asBaseComponent } from '../../commons/new ' ;
9
9
import View from '../view' ;
10
10
import TouchableOpacity from '../touchableOpacity' ;
11
11
import { TextField } from '../inputs' ;
@@ -15,6 +15,7 @@ import Text from '../text';
15
15
// TODO: support char array as tag creators (like comma)
16
16
// TODO: add notes to Docs about the Android fix for onKeyPress
17
17
18
+
18
19
const GUTTER_SPACING = 8 ;
19
20
20
21
/**
@@ -25,7 +26,7 @@ const GUTTER_SPACING = 8;
25
26
* @extends : TextField
26
27
* @extendsLink : https://github.com/wix/react-native-ui-lib/blob/master/src/components/inputs/TextField.js
27
28
*/
28
- export default class ChipsInput extends BaseComponent {
29
+ class ChipsInput extends Component {
29
30
static displayName = 'ChipsInput' ;
30
31
31
32
static propTypes = {
@@ -91,18 +92,11 @@ export default class ChipsInput extends BaseComponent {
91
92
constructor ( props ) {
92
93
super ( props ) ;
93
94
94
- this . addTag = this . addTag . bind ( this ) ;
95
- this . onChangeText = this . onChangeText . bind ( this ) ;
96
- this . renderTagWrapper = this . renderTagWrapper . bind ( this ) ;
97
- this . renderTag = this . renderTag . bind ( this ) ;
98
- this . getLabel = this . getLabel . bind ( this ) ;
99
- this . onKeyPress = this . onKeyPress . bind ( this ) ;
100
- this . markTagIndex = this . markTagIndex . bind ( this ) ;
101
-
102
95
this . state = {
103
96
value : props . value ,
104
97
tags : _ . cloneDeep ( props . tags ) || [ ] ,
105
- tagIndexToRemove : undefined
98
+ tagIndexToRemove : undefined ,
99
+ initialTags : props . tags
106
100
} ;
107
101
}
108
102
@@ -122,16 +116,18 @@ export default class ChipsInput extends BaseComponent {
122
116
}
123
117
}
124
118
125
- UNSAFE_componentWillReceiveProps ( nextProps ) {
126
- if ( nextProps . tags !== this . state . tags ) {
127
- this . setState ( {
119
+ static getDerivedStateFromProps ( nextProps , prevState ) {
120
+ if ( nextProps . tags !== prevState . initialTags ) {
121
+ return {
122
+ initialTags : nextProps . tags ,
128
123
tags : nextProps . tags
129
- } ) ;
124
+ } ;
130
125
}
126
+ return null ;
131
127
}
132
128
133
- addTag ( ) {
134
- const { onCreateTag, disableTagAdding} = this . getThemeProps ( ) ;
129
+ addTag = ( ) => {
130
+ const { onCreateTag, disableTagAdding} = this . props ;
135
131
const { value, tags} = this . state ;
136
132
137
133
if ( disableTagAdding ) {
@@ -143,10 +139,12 @@ export default class ChipsInput extends BaseComponent {
143
139
144
140
const newTag = _ . isFunction ( onCreateTag ) ? onCreateTag ( value ) : value ;
145
141
const newTags = [ ...tags , newTag ] ;
142
+
146
143
this . setState ( {
147
144
value : '' ,
148
145
tags : newTags
149
146
} ) ;
147
+
150
148
_ . invoke ( this . props , 'onChangeTags' , newTags , ChipsInput . onChangeTagsActions . ADDED , newTag ) ;
151
149
this . clear ( ) ;
152
150
}
@@ -162,15 +160,16 @@ export default class ChipsInput extends BaseComponent {
162
160
tags,
163
161
tagIndexToRemove : undefined
164
162
} ) ;
163
+
165
164
_ . invoke ( this . props , 'onChangeTags' , tags , ChipsInput . onChangeTagsActions . REMOVED , removedTag ) ;
166
165
}
167
166
}
168
167
169
- markTagIndex ( tagIndex ) {
168
+ markTagIndex = ( tagIndex ) => {
170
169
this . setState ( { tagIndexToRemove : tagIndex } ) ;
171
170
}
172
171
173
- onChangeText ( value ) {
172
+ onChangeText = ( value ) => {
174
173
this . setState ( { value, tagIndexToRemove : undefined } ) ;
175
174
_ . invoke ( this . props , 'onChangeText' , value ) ;
176
175
}
@@ -201,10 +200,10 @@ export default class ChipsInput extends BaseComponent {
201
200
return isLastTagMarked ;
202
201
}
203
202
204
- onKeyPress ( event ) {
203
+ onKeyPress = ( event ) => {
205
204
_ . invoke ( this . props , 'onKeyPress' , event ) ;
206
205
207
- const { disableTagRemoval} = this . getThemeProps ( ) ;
206
+ const { disableTagRemoval} = this . props ;
208
207
if ( disableTagRemoval ) {
209
208
return ;
210
209
}
@@ -227,7 +226,7 @@ export default class ChipsInput extends BaseComponent {
227
226
}
228
227
}
229
228
230
- getLabel ( item ) {
229
+ getLabel = ( item ) => {
231
230
const { getLabel} = this . props ;
232
231
233
232
if ( getLabel ) {
@@ -240,7 +239,7 @@ export default class ChipsInput extends BaseComponent {
240
239
}
241
240
242
241
renderLabel ( tag , shouldMarkTag ) {
243
- const typography = this . extractTypographyValue ( ) ;
242
+ const { typography} = this . props . modifiers ;
244
243
const label = this . getLabel ( tag ) ;
245
244
246
245
return (
@@ -261,8 +260,8 @@ export default class ChipsInput extends BaseComponent {
261
260
) ;
262
261
}
263
262
264
- renderTag ( tag , index ) {
265
- const { tagStyle, renderTag} = this . getThemeProps ( ) ;
263
+ renderTag = ( tag , index ) => {
264
+ const { tagStyle, renderTag} = this . props ;
266
265
const { tagIndexToRemove} = this . state ;
267
266
const shouldMarkTag = tagIndexToRemove === index ;
268
267
@@ -288,7 +287,7 @@ export default class ChipsInput extends BaseComponent {
288
287
) ;
289
288
}
290
289
291
- renderTagWrapper ( tag , index ) {
290
+ renderTagWrapper = ( tag , index ) => {
292
291
return (
293
292
< TouchableOpacity
294
293
key = { index }
@@ -302,7 +301,7 @@ export default class ChipsInput extends BaseComponent {
302
301
}
303
302
304
303
renderTextInput ( ) {
305
- const { inputStyle, selectionColor, ...others } = this . getThemeProps ( ) ;
304
+ const { inputStyle, selectionColor, ...others } = this . props ;
306
305
const { value} = this . state ;
307
306
const isLastTagMarked = this . isLastTagMarked ( ) ;
308
307
@@ -332,7 +331,7 @@ export default class ChipsInput extends BaseComponent {
332
331
}
333
332
334
333
render ( ) {
335
- const { disableTagRemoval, containerStyle, hideUnderline, validationErrorMessage} = this . getThemeProps ( ) ;
334
+ const { disableTagRemoval, containerStyle, hideUnderline, validationErrorMessage} = this . props ;
336
335
const tagRenderFn = disableTagRemoval ? this . renderTag : this . renderTagWrapper ;
337
336
const { tags, tagIndexToRemove} = this . state ;
338
337
@@ -366,6 +365,11 @@ export default class ChipsInput extends BaseComponent {
366
365
this . input . clear ( ) ;
367
366
}
368
367
}
368
+
369
+ export { ChipsInput } ; // For tests
370
+ export default asBaseComponent ( ChipsInput ) ;
371
+
372
+
369
373
const basicTagStyle = {
370
374
borderRadius : BorderRadiuses . br100 ,
371
375
paddingVertical : 4.5 ,
@@ -374,12 +378,6 @@ const basicTagStyle = {
374
378
marginVertical : GUTTER_SPACING / 2
375
379
} ;
376
380
377
- const basicIconStyle = {
378
- width : 10 ,
379
- height : 10 ,
380
- marginRight : 6
381
- } ;
382
-
383
381
const styles = StyleSheet . create ( {
384
382
withUnderline : {
385
383
borderBottomWidth : StyleSheet . hairlineWidth ,
@@ -411,7 +409,9 @@ const styles = StyleSheet.create({
411
409
} ,
412
410
removeIcon : {
413
411
tintColor : Colors . white ,
414
- ...basicIconStyle
412
+ width : 10 ,
413
+ height : 10 ,
414
+ marginRight : 6
415
415
} ,
416
416
inValidTagRemoveIcon : {
417
417
tintColor : Colors . red10
0 commit comments