Skip to content

Commit f90e9f8

Browse files
committed
feat: Reduce bundle size by removing classnames (dowjones#322)
Gets rid of classnames dependency. This package was not being used for its main features anyway (just for emitting classnames conditionally).
1 parent 3b80d50 commit f90e9f8

File tree

10 files changed

+52
-69
lines changed

10 files changed

+52
-69
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,6 @@ dist
4545
# Webstorm cache
4646
.idea
4747
/webpack-stats.json
48+
49+
# Mac crap
50+
.DS_Store

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
],
4545
"dependencies": {
4646
"array.partial": "^1.0.5",
47-
"classnames": "^2.2.6",
4847
"react-infinite-scroll-component": "^4.0.2"
4948
},
5049
"devDependencies": {

src/index.js

+7-10
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
* license MIT
77
* see https://github.com/dowjones/react-dropdown-tree-select
88
*/
9-
import cn from 'classnames/bind'
109
import PropTypes from 'prop-types'
1110
import React, { Component } from 'react'
1211

@@ -17,11 +16,9 @@ import Tree from './tree'
1716
import TreeManager from './tree-manager'
1817
import keyboardNavigation from './tree-manager/keyboardNavigation'
1918

20-
import styles from './index.css'
19+
import './index.css'
2120
import { getAriaLabel } from './a11y'
2221

23-
const cx = cn.bind(styles)
24-
2522
class DropdownTreeSelect extends Component {
2623
static propTypes = {
2724
data: PropTypes.oneOfType([PropTypes.object, PropTypes.array]).isRequired,
@@ -286,17 +283,17 @@ class DropdownTreeSelect extends Component {
286283
return (
287284
<div
288285
id={this.clientId}
289-
className={cx(this.props.className, 'react-dropdown-tree-select')}
286+
className={[this.props.className && this.props.className, 'react-dropdown-tree-select']
287+
.filter(Boolean)
288+
.join(' ')}
290289
ref={node => {
291290
this.node = node
292291
}}
293292
>
294293
<div
295-
className={cx(
296-
'dropdown',
297-
{ 'simple-select': mode === 'simpleSelect' },
298-
{ 'radio-select': mode === 'radioSelect' }
299-
)}
294+
className={['dropdown', mode === 'simpleSelect' && 'simple-select', mode === 'radioSelect' && 'radio-select']
295+
.filter(Boolean)
296+
.join(' ')}
300297
>
301298
<Trigger onTrigger={this.onTrigger} showDropdown={showDropdown} {...commonProps} tags={tags}>
302299
<Input

src/input/index.js

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
import React, { PureComponent } from 'react'
22
import PropTypes from 'prop-types'
3-
import cn from 'classnames/bind'
43
import Tag from '../tag'
5-
import styles from './index.css'
4+
import './index.css'
65
import { getDataset, debounce } from '../utils'
76
import { getAriaLabel } from '../a11y'
87

9-
const cx = cn.bind(styles)
10-
118
const getTags = (tags = [], onDelete, readOnly, disabled, labelRemove) =>
129
tags.map(tag => {
1310
const { _id, label, tagClassName, dataset } = tag
1411
return (
15-
<li className={cx('tag-item', tagClassName)} key={`tag-item-${_id}`} {...getDataset(dataset)}>
12+
<li
13+
className={['tag-item', tagClassName && tagClassName.toString()].filter(Boolean).join(' ')}
14+
key={`tag-item-${_id}`}
15+
{...getDataset(dataset)}
16+
>
1617
<Tag
1718
label={label}
1819
id={_id}
@@ -65,14 +66,14 @@ class Input extends PureComponent {
6566
} = this.props
6667

6768
return (
68-
<ul className={cx('tag-list')}>
69+
<ul className="tag-list">
6970
{getTags(tags, onTagRemove, readOnly, disabled, texts.labelRemove)}
70-
<li className={cx('tag-item')}>
71+
<li className="tag-item">
7172
<input
7273
type="text"
7374
disabled={disabled}
7475
ref={inputRef}
75-
className={cx('search')}
76+
className="search"
7677
placeholder={texts.placeholder || 'Choose...'}
7778
onKeyDown={onKeyDown}
7879
onChange={this.handleInputChange}

src/tag/index.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
import cn from 'classnames/bind'
21
import PropTypes from 'prop-types'
32
import React, { PureComponent } from 'react'
43

5-
import styles from './index.css'
6-
7-
const cx = cn.bind(styles)
4+
import './index.css'
85

96
export const getTagId = id => `${id}_tag`
107

@@ -44,11 +41,11 @@ class Tag extends PureComponent {
4441

4542
const tagId = getTagId(id)
4643
const buttonId = `${id}_button`
47-
const className = cx('tag-remove', { readOnly }, { disabled })
44+
const className = ['tag-remove', readOnly && 'readOnly', disabled && 'disabled'].filter(Boolean).join(' ')
4845
const isDisabled = readOnly || disabled
4946

5047
return (
51-
<span className={cx('tag')} id={tagId} aria-label={label}>
48+
<span className="tag" id={tagId} aria-label={label}>
5249
{label}
5350
<button
5451
id={buttonId}

src/tree-node/index.js

+16-19
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import cn from 'classnames/bind'
21
import PropTypes from 'prop-types'
32
import React, { PureComponent } from 'react'
43

@@ -7,9 +6,7 @@ import Actions from './actions'
76
import NodeLabel from './node-label'
87
import Toggle from './toggle'
98

10-
import styles from './index.css'
11-
12-
const cx = cn.bind(styles)
9+
import './index.css'
1310

1411
const isLeaf = children => isEmpty(children)
1512

@@ -30,22 +27,22 @@ const getNodeCx = props => {
3027
_focused: focused,
3128
} = props
3229

33-
return cx(
30+
return [
3431
'node',
35-
{
36-
leaf: isLeaf(_children),
37-
tree: !isLeaf(_children),
38-
disabled,
39-
hide,
40-
'match-in-children': keepTreeOnSearch && matchInChildren,
41-
'match-in-parent': keepTreeOnSearch && keepChildrenOnSearch && matchInParent,
42-
partial: showPartiallySelected && partial,
43-
readOnly,
44-
checked,
45-
focused,
46-
},
47-
className
48-
)
32+
isLeaf(_children) && 'leaf',
33+
!isLeaf(_children) && 'tree',
34+
disabled && 'disabled',
35+
hide && 'hide',
36+
keepTreeOnSearch && matchInChildren && 'match-in-children',
37+
keepTreeOnSearch && keepChildrenOnSearch && matchInParent && 'match-in-parent',
38+
showPartiallySelected && partial && 'partial',
39+
readOnly && 'readOnly',
40+
checked && 'checked',
41+
focused && 'focused',
42+
className,
43+
]
44+
.filter(Boolean)
45+
.join(' ')
4946
}
5047

5148
class TreeNode extends PureComponent {

src/tree-node/node-label.js

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
1-
import cn from 'classnames/bind'
21
import PropTypes from 'prop-types'
32
import React, { PureComponent } from 'react'
43
import Checkbox from '../checkbox'
54
import RadioButton from '../radio'
65

7-
import styles from './index.css'
8-
9-
const cx = cn.bind(styles)
10-
116
class NodeLabel extends PureComponent {
127
static propTypes = {
138
id: PropTypes.string.isRequired,
@@ -55,6 +50,7 @@ class NodeLabel extends PureComponent {
5550
}
5651

5752
const sharedProps = { id, value, checked, disabled, readOnly, tabIndex: -1 }
53+
const className = ['checkbox-item', mode === 'simpleSelect' && 'simple-select'].filter(Boolean).join(' ')
5854

5955
return (
6056
<label title={title || label} htmlFor={id}>
@@ -63,7 +59,7 @@ class NodeLabel extends PureComponent {
6359
) : (
6460
<Checkbox
6561
name={id}
66-
className={cx('checkbox-item', { 'simple-select': mode === 'simpleSelect' })}
62+
className={className}
6763
indeterminate={showPartiallySelected && partial}
6864
onChange={this.handleCheckboxChange}
6965
{...sharedProps}

src/tree-node/toggle.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
import cn from 'classnames/bind'
21
import PropTypes from 'prop-types'
32
import React, { PureComponent } from 'react'
4-
import styles from './index.css'
5-
6-
const cx = cn.bind(styles)
73

84
class Toggle extends PureComponent {
95
static propTypes = {
@@ -30,7 +26,7 @@ class Toggle extends PureComponent {
3026
const { expanded, isLeaf } = this.props
3127
if (isLeaf) return null
3228

33-
const toggleCx = cx('toggle', { expanded, collapsed: !expanded })
29+
const toggleCx = ['toggle', expanded && 'expanded', !expanded && 'collapsed'].filter(Boolean).join(' ')
3430
return (
3531
<i
3632
role="button"

src/trigger/index.js

+10-13
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
import React, { PureComponent } from 'react'
22
import PropTypes from 'prop-types'
3-
import cn from 'classnames/bind'
43

54
import { getAriaLabel } from '../a11y'
65
import { getTagId } from '../tag'
76

8-
import styles from '../index.css'
9-
10-
const cx = cn.bind(styles)
11-
127
class Trigger extends PureComponent {
138
static propTypes = {
149
onTrigger: PropTypes.func,
@@ -69,14 +64,16 @@ class Trigger extends PureComponent {
6964
render() {
7065
const { disabled, readOnly, showDropdown } = this.props
7166

72-
const dropdownTriggerClassname = cx({
73-
'dropdown-trigger': true,
74-
arrow: true,
75-
disabled,
76-
readOnly,
77-
top: showDropdown,
78-
bottom: !showDropdown,
79-
})
67+
const dropdownTriggerClassname = [
68+
'dropdown-trigger',
69+
'arrow',
70+
disabled && 'disabled',
71+
readOnly && 'readOnly',
72+
showDropdown && 'top',
73+
!showDropdown && 'bottom',
74+
]
75+
.filter(Boolean)
76+
.join(' ')
8077

8178
return (
8279
<a

yarn.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -2831,7 +2831,7 @@ class-utils@^0.3.5:
28312831
isobject "^3.0.0"
28322832
static-extend "^0.1.1"
28332833

2834-
classnames@^2.2.5, classnames@^2.2.6:
2834+
classnames@^2.2.5:
28352835
version "2.2.6"
28362836
resolved "https://registry.yarnpkg.com/classnames/-/classnames-2.2.6.tgz#43935bffdd291f326dad0a205309b38d00f650ce"
28372837

0 commit comments

Comments
 (0)