Skip to content

Commit 7cd6111

Browse files
itrombitasmrchief
itrombitas
authored andcommitted
feat: disable input (#188) ✨
* chore: Update package.json dependencies * feat: Add disabled prop to Input and DropdownTreeSelect * chore: Rebuild bundle.js * style: Show arrow as disabled, update disabled logic * chore: Update docs * chore: Make demo script easier to use
1 parent d5798e4 commit 7cd6111

12 files changed

+84
-10
lines changed

__snapshots__/src/index.test.js.md

+28-1
Original file line numberDiff line numberDiff line change
@@ -261,4 +261,31 @@ Generated by [AVA](https://ava.li).
261261
/>
262262
</div>
263263
</div>
264-
</div>
264+
</div
265+
266+
## doesn't toggle dropdown if it's disabled
267+
268+
> Snapshot 1
269+
270+
<div
271+
className="react-dropdown-tree-select"
272+
>
273+
<div
274+
className="dropdown"
275+
>
276+
<a
277+
className="dropdown-trigger arrow disabled bottom"
278+
onClick={false}
279+
>
280+
<Input
281+
disabled={true}
282+
inputRef={Function inputRef {}}
283+
onBlur={Function {}}
284+
onFocus={Function {}}
285+
onInputChange={Function {}}
286+
onTagRemove={Function {}}
287+
tags={[]}
288+
/>
289+
</a>
290+
</div>
291+
</div>

__snapshots__/src/index.test.js.snap

177 Bytes
Binary file not shown.

__snapshots__/src/input/index.test.js.md

+21-1
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,24 @@ Generated by [AVA](https://ava.li).
107107
type="text"
108108
/>
109109
</li>
110-
</ul>
110+
</ul
111+
112+
## should render disabled input
113+
114+
> Snapshot 1
115+
116+
<ul
117+
className="tag-list"
118+
>
119+
<li
120+
className="tag-item"
121+
>
122+
<input
123+
className="search"
124+
disabled={true}
125+
onChange={Function {}}
126+
placeholder="Choose..."
127+
type="text"
128+
/>
129+
</li>
130+
</ul>
67 Bytes
Binary file not shown.

docs/bundle.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/src/stories/Options/index.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ class WithOptions extends PureComponent {
1414
clearSearchOnChange: false,
1515
keepTreeOnSearch: false,
1616
simpleSelect: false,
17-
showPartiallySelected: false
17+
showPartiallySelected: false,
18+
disabled: false
1819
}
1920
}
2021

@@ -33,7 +34,7 @@ class WithOptions extends PureComponent {
3334
}
3435

3536
render() {
36-
const { clearSearchOnChange, keepTreeOnSearch, simpleSelect, showPartiallySelected } = this.state
37+
const { clearSearchOnChange, keepTreeOnSearch, simpleSelect, showPartiallySelected, disabled } = this.state
3738

3839
return (
3940
<div>
@@ -51,6 +52,7 @@ class WithOptions extends PureComponent {
5152
<Checkbox label="Keep tree on search" value="keepTreeOnSearch" checked={keepTreeOnSearch} onChange={this.onOptionsChange} />
5253
<Checkbox label="Simple Select" value="simpleSelect" checked={simpleSelect} onChange={this.onOptionsChange} />
5354
<Checkbox label="Show Partially Selected" value="showPartiallySelected" checked={showPartiallySelected} onChange={this.onOptionsChange} />
55+
<Checkbox label="Disable Input" value="disabled" checked={disabled} onChange={this.onOptionsChange} />
5456
</div>
5557
<div>
5658
<DropdownTreeSelect
@@ -62,6 +64,7 @@ class WithOptions extends PureComponent {
6264
keepTreeOnSearch={keepTreeOnSearch}
6365
simpleSelect={simpleSelect}
6466
showPartiallySelected={showPartiallySelected}
67+
disabled={disabled}
6568
/>
6669
</div>
6770
</div>

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"commit": "git-cz",
2929
"commitmsg": "commitlint -e $GIT_PARAMS",
3030
"coveralls": "nyc npm test && nyc report --reporter=text-lcov | coveralls",
31-
"demo": "webpack-serve --content ./docs --port 3000 --open --config docs/webpack.config.js",
31+
"demo": "rimraf ./docs/bundle.js && webpack-serve --content ./docs --port 3000 --open --config docs/webpack.config.js",
3232
"prepublishOnly": "npm run build",
3333
"lint": "eslint src docs webpack.config.js && stylelint \"src/**/*.css\" --fix",
3434
"lint:nofix": "eslint src webpack.config.js && stylelint \"src/**/*.css\"",

src/index.css

+8
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@
3232
color: #3c3c3c;
3333
margin-right: 2px;
3434
}
35+
36+
&.disabled {
37+
cursor: not-allowed;
38+
39+
&.bottom::after {
40+
color: rgb(185, 185, 185);
41+
}
42+
}
3543
}
3644
}
3745

src/index.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ class DropdownTreeSelect extends Component {
3434
onBlur: PropTypes.func,
3535
simpleSelect: PropTypes.bool,
3636
noMatchesText: PropTypes.string,
37-
showPartiallySelected: PropTypes.bool
37+
showPartiallySelected: PropTypes.bool,
38+
disabled: PropTypes.bool
3839
}
3940

4041
static defaultProps = {
@@ -176,6 +177,7 @@ class DropdownTreeSelect extends Component {
176177
const dropdownTriggerClassname = cx({
177178
'dropdown-trigger': true,
178179
arrow: true,
180+
disabled: this.props.disabled,
179181
top: this.state.showDropdown,
180182
bottom: !this.state.showDropdown
181183
})
@@ -188,7 +190,7 @@ class DropdownTreeSelect extends Component {
188190
}}
189191
>
190192
<div className="dropdown">
191-
<a className={dropdownTriggerClassname} onClick={this.handleClick}>
193+
<a className={dropdownTriggerClassname} onClick={!this.props.disabled && this.handleClick}>
192194
<Input
193195
inputRef={el => {
194196
this.searchInput = el
@@ -199,6 +201,7 @@ class DropdownTreeSelect extends Component {
199201
onFocus={this.onInputFocus}
200202
onBlur={this.onInputBlur}
201203
onTagRemove={this.onTagRemove}
204+
disabled={this.props.disabled}
202205
/>
203206
</a>
204207
{this.state.showDropdown && (

src/index.test.js

+6
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,12 @@ test('toggles dropdown', t => {
129129
t.false(wrapper.state().showDropdown)
130130
})
131131

132+
test('doesn\'t toggle dropdown if it\'s disabled', t => {
133+
const { tree } = t.context
134+
const wrapper = shallow(<DropdownTreeSelect data={tree} disabled />)
135+
t.snapshot(toJson(wrapper))
136+
})
137+
132138
test('keeps dropdown active on focus', t => {
133139
const { tree } = t.context
134140
const wrapper = shallow(<DropdownTreeSelect data={tree} />)

src/input/index.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ class Input extends PureComponent {
2525
onFocus: PropTypes.func,
2626
onBlur: PropTypes.func,
2727
onTagRemove: PropTypes.func,
28-
inputRef: PropTypes.func
28+
inputRef: PropTypes.func,
29+
disabled: PropTypes.bool
2930
}
3031

3132
constructor(props) {
@@ -39,14 +40,15 @@ class Input extends PureComponent {
3940
}
4041

4142
render() {
42-
const { tags, onTagRemove, inputRef, placeholderText = 'Choose...', onFocus, onBlur } = this.props
43+
const { tags, onTagRemove, inputRef, placeholderText = 'Choose...', onFocus, onBlur, disabled } = this.props
4344

4445
return (
4546
<ul className={cx('tag-list')}>
4647
{getTags(tags, onTagRemove)}
4748
<li className={cx('tag-item')}>
4849
<input
4950
type="text"
51+
disabled={disabled}
5052
ref={inputRef}
5153
className={cx('search')}
5254
placeholder={placeholderText}

src/input/index.test.js

+5
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,8 @@ test('should render data attributes', t => {
4747

4848
t.snapshot(wrapper)
4949
})
50+
51+
test('should render disabled input', t => {
52+
const wrapper = toJson(shallow(<Input disabled />))
53+
t.snapshot(wrapper)
54+
})

0 commit comments

Comments
 (0)