Skip to content

Commit

Permalink
feat: Reduce bundle size by removing classnames (#322)
Browse files Browse the repository at this point in the history
Gets rid of classnames dependency. This package was not being used for its main features anyway (just for emitting classnames conditionally).
  • Loading branch information
mrchief committed Jan 11, 2020
1 parent 3b80d50 commit f90e9f8
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 69 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,6 @@ dist
# Webstorm cache
.idea
/webpack-stats.json

# Mac crap
.DS_Store
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
],
"dependencies": {
"array.partial": "^1.0.5",
"classnames": "^2.2.6",
"react-infinite-scroll-component": "^4.0.2"
},
"devDependencies": {
Expand Down
17 changes: 7 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
* license MIT
* see https://github.com/dowjones/react-dropdown-tree-select
*/
import cn from 'classnames/bind'
import PropTypes from 'prop-types'
import React, { Component } from 'react'

Expand All @@ -17,11 +16,9 @@ import Tree from './tree'
import TreeManager from './tree-manager'
import keyboardNavigation from './tree-manager/keyboardNavigation'

import styles from './index.css'
import './index.css'
import { getAriaLabel } from './a11y'

const cx = cn.bind(styles)

class DropdownTreeSelect extends Component {
static propTypes = {
data: PropTypes.oneOfType([PropTypes.object, PropTypes.array]).isRequired,
Expand Down Expand Up @@ -286,17 +283,17 @@ class DropdownTreeSelect extends Component {
return (
<div
id={this.clientId}
className={cx(this.props.className, 'react-dropdown-tree-select')}
className={[this.props.className && this.props.className, 'react-dropdown-tree-select']
.filter(Boolean)
.join(' ')}
ref={node => {
this.node = node
}}
>
<div
className={cx(
'dropdown',
{ 'simple-select': mode === 'simpleSelect' },
{ 'radio-select': mode === 'radioSelect' }
)}
className={['dropdown', mode === 'simpleSelect' && 'simple-select', mode === 'radioSelect' && 'radio-select']
.filter(Boolean)
.join(' ')}
>
<Trigger onTrigger={this.onTrigger} showDropdown={showDropdown} {...commonProps} tags={tags}>
<Input
Expand Down
17 changes: 9 additions & 8 deletions src/input/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import React, { PureComponent } from 'react'
import PropTypes from 'prop-types'
import cn from 'classnames/bind'
import Tag from '../tag'
import styles from './index.css'
import './index.css'
import { getDataset, debounce } from '../utils'
import { getAriaLabel } from '../a11y'

const cx = cn.bind(styles)

const getTags = (tags = [], onDelete, readOnly, disabled, labelRemove) =>
tags.map(tag => {
const { _id, label, tagClassName, dataset } = tag
return (
<li className={cx('tag-item', tagClassName)} key={`tag-item-${_id}`} {...getDataset(dataset)}>
<li
className={['tag-item', tagClassName && tagClassName.toString()].filter(Boolean).join(' ')}
key={`tag-item-${_id}`}
{...getDataset(dataset)}
>
<Tag
label={label}
id={_id}
Expand Down Expand Up @@ -65,14 +66,14 @@ class Input extends PureComponent {
} = this.props

return (
<ul className={cx('tag-list')}>
<ul className="tag-list">
{getTags(tags, onTagRemove, readOnly, disabled, texts.labelRemove)}
<li className={cx('tag-item')}>
<li className="tag-item">
<input
type="text"
disabled={disabled}
ref={inputRef}
className={cx('search')}
className="search"
placeholder={texts.placeholder || 'Choose...'}
onKeyDown={onKeyDown}
onChange={this.handleInputChange}
Expand Down
9 changes: 3 additions & 6 deletions src/tag/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import cn from 'classnames/bind'
import PropTypes from 'prop-types'
import React, { PureComponent } from 'react'

import styles from './index.css'

const cx = cn.bind(styles)
import './index.css'

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

Expand Down Expand Up @@ -44,11 +41,11 @@ class Tag extends PureComponent {

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

return (
<span className={cx('tag')} id={tagId} aria-label={label}>
<span className="tag" id={tagId} aria-label={label}>
{label}
<button
id={buttonId}
Expand Down
35 changes: 16 additions & 19 deletions src/tree-node/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import cn from 'classnames/bind'
import PropTypes from 'prop-types'
import React, { PureComponent } from 'react'

Expand All @@ -7,9 +6,7 @@ import Actions from './actions'
import NodeLabel from './node-label'
import Toggle from './toggle'

import styles from './index.css'

const cx = cn.bind(styles)
import './index.css'

const isLeaf = children => isEmpty(children)

Expand All @@ -30,22 +27,22 @@ const getNodeCx = props => {
_focused: focused,
} = props

return cx(
return [
'node',
{
leaf: isLeaf(_children),
tree: !isLeaf(_children),
disabled,
hide,
'match-in-children': keepTreeOnSearch && matchInChildren,
'match-in-parent': keepTreeOnSearch && keepChildrenOnSearch && matchInParent,
partial: showPartiallySelected && partial,
readOnly,
checked,
focused,
},
className
)
isLeaf(_children) && 'leaf',
!isLeaf(_children) && 'tree',
disabled && 'disabled',
hide && 'hide',
keepTreeOnSearch && matchInChildren && 'match-in-children',
keepTreeOnSearch && keepChildrenOnSearch && matchInParent && 'match-in-parent',
showPartiallySelected && partial && 'partial',
readOnly && 'readOnly',
checked && 'checked',
focused && 'focused',
className,
]
.filter(Boolean)
.join(' ')
}

class TreeNode extends PureComponent {
Expand Down
8 changes: 2 additions & 6 deletions src/tree-node/node-label.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import cn from 'classnames/bind'
import PropTypes from 'prop-types'
import React, { PureComponent } from 'react'
import Checkbox from '../checkbox'
import RadioButton from '../radio'

import styles from './index.css'

const cx = cn.bind(styles)

class NodeLabel extends PureComponent {
static propTypes = {
id: PropTypes.string.isRequired,
Expand Down Expand Up @@ -55,6 +50,7 @@ class NodeLabel extends PureComponent {
}

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

return (
<label title={title || label} htmlFor={id}>
Expand All @@ -63,7 +59,7 @@ class NodeLabel extends PureComponent {
) : (
<Checkbox
name={id}
className={cx('checkbox-item', { 'simple-select': mode === 'simpleSelect' })}
className={className}
indeterminate={showPartiallySelected && partial}
onChange={this.handleCheckboxChange}
{...sharedProps}
Expand Down
6 changes: 1 addition & 5 deletions src/tree-node/toggle.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import cn from 'classnames/bind'
import PropTypes from 'prop-types'
import React, { PureComponent } from 'react'
import styles from './index.css'

const cx = cn.bind(styles)

class Toggle extends PureComponent {
static propTypes = {
Expand All @@ -30,7 +26,7 @@ class Toggle extends PureComponent {
const { expanded, isLeaf } = this.props
if (isLeaf) return null

const toggleCx = cx('toggle', { expanded, collapsed: !expanded })
const toggleCx = ['toggle', expanded && 'expanded', !expanded && 'collapsed'].filter(Boolean).join(' ')
return (
<i
role="button"
Expand Down
23 changes: 10 additions & 13 deletions src/trigger/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import React, { PureComponent } from 'react'
import PropTypes from 'prop-types'
import cn from 'classnames/bind'

import { getAriaLabel } from '../a11y'
import { getTagId } from '../tag'

import styles from '../index.css'

const cx = cn.bind(styles)

class Trigger extends PureComponent {
static propTypes = {
onTrigger: PropTypes.func,
Expand Down Expand Up @@ -69,14 +64,16 @@ class Trigger extends PureComponent {
render() {
const { disabled, readOnly, showDropdown } = this.props

const dropdownTriggerClassname = cx({
'dropdown-trigger': true,
arrow: true,
disabled,
readOnly,
top: showDropdown,
bottom: !showDropdown,
})
const dropdownTriggerClassname = [
'dropdown-trigger',
'arrow',
disabled && 'disabled',
readOnly && 'readOnly',
showDropdown && 'top',
!showDropdown && 'bottom',
]
.filter(Boolean)
.join(' ')

return (
<a
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2831,7 +2831,7 @@ class-utils@^0.3.5:
isobject "^3.0.0"
static-extend "^0.1.1"

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

Expand Down

0 comments on commit f90e9f8

Please sign in to comment.