Skip to content

Commit 5d890a8

Browse files
harshithmullapudiad1992
authored andcommitted
fix: Update class (#456)
1 parent aa5797c commit 5d890a8

File tree

4 files changed

+46
-24
lines changed

4 files changed

+46
-24
lines changed

__tests__/reactTags.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,23 @@ describe('Test ReactTags', () => {
5858
expect($el.props().children.props).to.deep.equal(expectedProps);
5959
});
6060

61+
test('should update the class when the prop classNames changes', () => {
62+
let $el = mount(
63+
mockItem({
64+
classNames: {
65+
tag: 'tag',
66+
},
67+
})
68+
);
69+
expect($el.find('.tag').length).to.equal(1);
70+
$el.setProps({
71+
classNames: {
72+
tag: 'changed',
73+
},
74+
});
75+
expect($el.find('.changed').length).to.equal(1);
76+
});
77+
6178
test('focus on input by default', () => {
6279
const $el = mount(mockItem());
6380
expect(document.activeElement.tagName).to.equal('INPUT');

package-lock.json

Lines changed: 12 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"classnames": "^2.2.6",
3838
"core-js": "2.6.0",
3939
"lodash": "4.17.11",
40+
"memoize-one": "^5.0.0",
4041
"prop-types": "15.6.1",
4142
"react": "16.4.2",
4243
"react-dnd": "5.0.0",

src/components/ReactTags.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import uniq from 'lodash/uniq';
66
import Suggestions from './Suggestions';
77
import PropTypes from 'prop-types';
88
import ClassNames from 'classnames';
9-
9+
import memoizeOne from 'memoize-one';
1010
import Tag from './Tag';
1111

1212
import { buildRegExpFromDelimiters } from './utils';
@@ -21,6 +21,13 @@ import {
2121

2222
import '../styles/react-tags.scss';
2323

24+
const updateClassNames = memoizeOne((classNames) =>
25+
{
26+
return {
27+
classNames : {...DEFAULT_CLASSNAMES,...classNames},
28+
};
29+
});
30+
2431
class ReactTags extends Component {
2532
static propTypes = {
2633
placeholder: PropTypes.string,
@@ -95,7 +102,7 @@ class ReactTags extends Component {
95102
selectionMode: false,
96103
classNames: { ...DEFAULT_CLASSNAMES, ...classNames },
97104
};
98-
105+
// TODO : remove classNames from state and change updateClassNames to instance function
99106
this.handleFocus = this.handleFocus.bind(this);
100107
this.handleBlur = this.handleBlur.bind(this);
101108
this.handleKeyDown = this.handleKeyDown.bind(this);
@@ -105,6 +112,13 @@ class ReactTags extends Component {
105112
this.resetAndFocusInput = this.resetAndFocusInput.bind(this);
106113
this.handleSuggestionHover = this.handleSuggestionHover.bind(this);
107114
this.handleSuggestionClick = this.handleSuggestionClick.bind(this);
115+
116+
}
117+
118+
static getDerivedStateFromProps(props)
119+
{
120+
const { classNames } = props;
121+
return updateClassNames(classNames);
108122
}
109123

110124
componentDidMount() {

0 commit comments

Comments
 (0)