Skip to content

Commit 0375ae5

Browse files
authored
ChipsInput - Removing UNSAFE method (#1018)
* ChipsInput - Removing UNSAFE method * export for tests * fix tests * Fix add tag function and keep component as controlled component * Fix example screen
1 parent 9f516cd commit 0375ae5

File tree

3 files changed

+57
-41
lines changed

3 files changed

+57
-41
lines changed

demo/src/screens/componentScreens/ChipsInputScreen.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ export default class ChipsInputScreen extends Component {
1010
this.state = {
1111
tags: [{label: 'Amit'}, {label: 'Ethan', invalid: true}],
1212
tags2: ['Chips', 'Input'],
13-
tags3: ['Non', 'Removable', 'Tags']
13+
tags3: ['Non', 'Removable', 'Tags'],
14+
tags4: ['Change', 'Typography']
1415
};
1516
}
1617

@@ -34,10 +35,18 @@ export default class ChipsInputScreen extends Component {
3435
ChipsInput
3536
</Text>
3637

37-
<ChipsInput containerStyle={{marginBottom: 25}} placeholder="Enter Tags" tags={this.state.tags2}/>
38+
<ChipsInput
39+
containerStyle={{marginBottom: 25}}
40+
placeholder="Enter Tags"
41+
tags={this.state.tags2}
42+
/>
3843

39-
<ChipsInput containerStyle={{marginBottom: 25}} placeholder="Enter Tags" tags={this.state.tags}
40-
validationErrorMessage="error validation message" />
44+
<ChipsInput
45+
containerStyle={{marginBottom: 25}}
46+
placeholder="Enter Tags"
47+
tags={this.state.tags}
48+
validationErrorMessage="error validation message"
49+
/>
4150

4251
<ChipsInput
4352
containerStyle={{marginBottom: 25}}
@@ -57,6 +66,13 @@ export default class ChipsInputScreen extends Component {
5766
onTagPress={this.onTagPress}
5867
inputStyle={{...Typography.text60, color: Colors.blue30}}
5968
/>
69+
70+
<ChipsInput
71+
text60
72+
containerStyle={{marginBottom: 25}}
73+
placeholder="Enter Tags"
74+
tags={this.state.tags4}
75+
/>
6076
</View>
6177
</ScrollView>
6278
);

src/components/chipsInput/__tests__/index.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import _ from 'lodash';
22
import {Constants} from '../../../helpers';
3-
import ChipsInput from '../index';
3+
import {ChipsInput} from '../index';
44

55
describe('ChipsInput', () => {
66
let uut;

src/components/chipsInput/index.js

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import _ from 'lodash';
22
import PropTypes from 'prop-types';
3-
import React from 'react';
3+
import React, {Component} from 'react';
44
import ReactNative, {NativeModules, StyleSheet, ViewPropTypes, Image, DeviceEventEmitter} from 'react-native';
55
import {Constants} from '../../helpers';
66
import {Colors, BorderRadiuses, ThemeManager, Typography} from '../../style';
77
import Assets from '../../assets';
8-
import {BaseComponent} from '../../commons';
8+
import {asBaseComponent} from '../../commons/new';
99
import View from '../view';
1010
import TouchableOpacity from '../touchableOpacity';
1111
import {TextField} from '../inputs';
@@ -15,6 +15,7 @@ import Text from '../text';
1515
// TODO: support char array as tag creators (like comma)
1616
// TODO: add notes to Docs about the Android fix for onKeyPress
1717

18+
1819
const GUTTER_SPACING = 8;
1920

2021
/**
@@ -25,7 +26,7 @@ const GUTTER_SPACING = 8;
2526
* @extends: TextField
2627
* @extendsLink: https://github.com/wix/react-native-ui-lib/blob/master/src/components/inputs/TextField.js
2728
*/
28-
export default class ChipsInput extends BaseComponent {
29+
class ChipsInput extends Component {
2930
static displayName = 'ChipsInput';
3031

3132
static propTypes = {
@@ -91,18 +92,11 @@ export default class ChipsInput extends BaseComponent {
9192
constructor(props) {
9293
super(props);
9394

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-
10295
this.state = {
10396
value: props.value,
10497
tags: _.cloneDeep(props.tags) || [],
105-
tagIndexToRemove: undefined
98+
tagIndexToRemove: undefined,
99+
initialTags: props.tags
106100
};
107101
}
108102

@@ -122,16 +116,18 @@ export default class ChipsInput extends BaseComponent {
122116
}
123117
}
124118

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,
128123
tags: nextProps.tags
129-
});
124+
};
130125
}
126+
return null;
131127
}
132128

133-
addTag() {
134-
const {onCreateTag, disableTagAdding} = this.getThemeProps();
129+
addTag = () => {
130+
const {onCreateTag, disableTagAdding} = this.props;
135131
const {value, tags} = this.state;
136132

137133
if (disableTagAdding) {
@@ -143,10 +139,12 @@ export default class ChipsInput extends BaseComponent {
143139

144140
const newTag = _.isFunction(onCreateTag) ? onCreateTag(value) : value;
145141
const newTags = [...tags, newTag];
142+
146143
this.setState({
147144
value: '',
148145
tags: newTags
149146
});
147+
150148
_.invoke(this.props, 'onChangeTags', newTags, ChipsInput.onChangeTagsActions.ADDED, newTag);
151149
this.clear();
152150
}
@@ -162,15 +160,16 @@ export default class ChipsInput extends BaseComponent {
162160
tags,
163161
tagIndexToRemove: undefined
164162
});
163+
165164
_.invoke(this.props, 'onChangeTags', tags, ChipsInput.onChangeTagsActions.REMOVED, removedTag);
166165
}
167166
}
168167

169-
markTagIndex(tagIndex) {
168+
markTagIndex = (tagIndex) => {
170169
this.setState({tagIndexToRemove: tagIndex});
171170
}
172171

173-
onChangeText(value) {
172+
onChangeText = (value) => {
174173
this.setState({value, tagIndexToRemove: undefined});
175174
_.invoke(this.props, 'onChangeText', value);
176175
}
@@ -201,10 +200,10 @@ export default class ChipsInput extends BaseComponent {
201200
return isLastTagMarked;
202201
}
203202

204-
onKeyPress(event) {
203+
onKeyPress = (event) => {
205204
_.invoke(this.props, 'onKeyPress', event);
206205

207-
const {disableTagRemoval} = this.getThemeProps();
206+
const {disableTagRemoval} = this.props;
208207
if (disableTagRemoval) {
209208
return;
210209
}
@@ -227,7 +226,7 @@ export default class ChipsInput extends BaseComponent {
227226
}
228227
}
229228

230-
getLabel(item) {
229+
getLabel = (item) => {
231230
const {getLabel} = this.props;
232231

233232
if (getLabel) {
@@ -240,7 +239,7 @@ export default class ChipsInput extends BaseComponent {
240239
}
241240

242241
renderLabel(tag, shouldMarkTag) {
243-
const typography = this.extractTypographyValue();
242+
const {typography} = this.props.modifiers;
244243
const label = this.getLabel(tag);
245244

246245
return (
@@ -261,8 +260,8 @@ export default class ChipsInput extends BaseComponent {
261260
);
262261
}
263262

264-
renderTag(tag, index) {
265-
const {tagStyle, renderTag} = this.getThemeProps();
263+
renderTag = (tag, index) => {
264+
const {tagStyle, renderTag} = this.props;
266265
const {tagIndexToRemove} = this.state;
267266
const shouldMarkTag = tagIndexToRemove === index;
268267

@@ -288,7 +287,7 @@ export default class ChipsInput extends BaseComponent {
288287
);
289288
}
290289

291-
renderTagWrapper(tag, index) {
290+
renderTagWrapper = (tag, index) => {
292291
return (
293292
<TouchableOpacity
294293
key={index}
@@ -302,7 +301,7 @@ export default class ChipsInput extends BaseComponent {
302301
}
303302

304303
renderTextInput() {
305-
const {inputStyle, selectionColor, ...others} = this.getThemeProps();
304+
const {inputStyle, selectionColor, ...others} = this.props;
306305
const {value} = this.state;
307306
const isLastTagMarked = this.isLastTagMarked();
308307

@@ -332,7 +331,7 @@ export default class ChipsInput extends BaseComponent {
332331
}
333332

334333
render() {
335-
const {disableTagRemoval, containerStyle, hideUnderline, validationErrorMessage} = this.getThemeProps();
334+
const {disableTagRemoval, containerStyle, hideUnderline, validationErrorMessage} = this.props;
336335
const tagRenderFn = disableTagRemoval ? this.renderTag : this.renderTagWrapper;
337336
const {tags, tagIndexToRemove} = this.state;
338337

@@ -366,6 +365,11 @@ export default class ChipsInput extends BaseComponent {
366365
this.input.clear();
367366
}
368367
}
368+
369+
export {ChipsInput}; // For tests
370+
export default asBaseComponent(ChipsInput);
371+
372+
369373
const basicTagStyle = {
370374
borderRadius: BorderRadiuses.br100,
371375
paddingVertical: 4.5,
@@ -374,12 +378,6 @@ const basicTagStyle = {
374378
marginVertical: GUTTER_SPACING / 2
375379
};
376380

377-
const basicIconStyle = {
378-
width: 10,
379-
height: 10,
380-
marginRight: 6
381-
};
382-
383381
const styles = StyleSheet.create({
384382
withUnderline: {
385383
borderBottomWidth: StyleSheet.hairlineWidth,
@@ -411,7 +409,9 @@ const styles = StyleSheet.create({
411409
},
412410
removeIcon: {
413411
tintColor: Colors.white,
414-
...basicIconStyle
412+
width: 10,
413+
height: 10,
414+
marginRight: 6
415415
},
416416
inValidTagRemoveIcon: {
417417
tintColor: Colors.red10

0 commit comments

Comments
 (0)