Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
73a3bc9
feat: Added support for single select in tree dropdown
Mar 12, 2019
d3e68e9
fix: Removed trailing white space
Mar 12, 2019
8921d56
fix: Refactor identical blocks
Mar 13, 2019
ae83e09
fix: Avoid prop is never used
Mar 13, 2019
ec90256
fix: Add some tests for singleselect
Mar 13, 2019
eea1595
Merge branch 'develop' into fix/#119
ellinge Mar 14, 2019
73140c5
fix: No need for spread
Mar 14, 2019
3de97b0
Merge branch 'fix/#119' of https://github.com/ellinge/react-dropdown-…
Mar 14, 2019
fc1cd47
Merge branch 'develop' into fix/#119
mrchief Mar 14, 2019
4c10100
Merge branch 'develop'
ellinge Mar 15, 2019
c8b8ce6
fix: Convert to separate radio select instead
ellinge Mar 16, 2019
863f177
fix: Try to fix code climate issues
ellinge Mar 16, 2019
e14c737
fix: Code complexity
ellinge Mar 16, 2019
9a25354
fix: Code complexity
ellinge Mar 16, 2019
f01f428
fix: Not sure what's similiar
ellinge Mar 16, 2019
18a7fd0
fix: Random eof issue with css
ellinge Mar 16, 2019
0d23a4c
fix: Code climate issues
ellinge Mar 16, 2019
4aa38df
fix: Code climate issues
ellinge Mar 16, 2019
a8f16af
fix: Clarify single select
ellinge Mar 16, 2019
3b3777b
fix: Update docs
ellinge Mar 16, 2019
368aa26
fix: Lint issue and fixed check on mark visit sub node
ellinge Mar 16, 2019
3820cbf
fix: Add comma on single select node in readme
ellinge Mar 16, 2019
a6ca244
fix: Modified readme note for single select
ellinge Mar 16, 2019
ce015e2
fix: Code review adjustments
ellinge Mar 18, 2019
7c80caa
fix: Rewrote single select ensurance
ellinge Mar 18, 2019
fadb8d9
fix: Code climate issues
ellinge Mar 18, 2019
763c7d6
fix: Code climate issues
ellinge Mar 18, 2019
98f0d9f
fix: Code climate issue
ellinge Mar 18, 2019
75f865a
fix: Code climate issues
ellinge Mar 18, 2019
fc3aa89
fix: IE11 does not close single selects
ellinge Mar 19, 2019
6fa5941
fix: Allow unselect/delete node on single select
ellinge Mar 19, 2019
6b392c9
Merge branch 'develop'
Mar 21, 2019
519dc02
fix: Use clientId for name
ellinge Mar 24, 2019
e60b8d4
fix: Add brackets to name on checkboxes
ellinge Mar 24, 2019
ccc5d2c
fix: Restored old styling
ellinge Mar 24, 2019
bd47c70
fix: Change so isDefaultValue has precedence
Mar 26, 2019
aab7a40
fix: Try to optimize traversing
ellinge Mar 27, 2019
852bb4a
fix: Restore 'old name' for checkboxes
ellinge Mar 27, 2019
ba037f4
Merge branch 'develop' into 'fix/#119'
Mar 28, 2019
782e8ca
fix: Codeclimate
Mar 28, 2019
ac488a6
fix: Code climate, last workaround
Mar 28, 2019
9e3a0d1
Merge branch 'develop' into fix/#119
mrchief Mar 31, 2019
e7e40b1
fix: Added radioSelect to typings
ellinge Mar 31, 2019
813c282
feat: Added Keep tree open on select (for radio/simple)
Apr 1, 2019
a1991da
Merge branch 'develop' into fix/#119
ellinge Apr 2, 2019
937db59
Merge branch 'develop' into fix/#119
ellinge Apr 3, 2019
26a302d
style: Add prettier changes from develop
mrchief Apr 14, 2019
f2fd2d2
Merge remote-tracking branch 'upstream/develop' into fix/#119
mrchief Apr 14, 2019
21403cb
fix: Bundle radio and simple into mode instead
ellinge Apr 14, 2019
ff5d4fa
fix: Bundle radio and simple into mode instead
ellinge Apr 14, 2019
f8a3958
fix: Added migration guide
ellinge Apr 14, 2019
dedb6f2
fix: Story
ellinge Apr 14, 2019
d891408
fix: Bundle text props
ellinge Apr 15, 2019
31c2e34
Revert "fix: Bundle text props"
ellinge Apr 17, 2019
501cd3e
Revert "fix: Story"
ellinge Apr 17, 2019
84330e6
Revert "fix: Added migration guide"
ellinge Apr 17, 2019
1a8b112
Revert "fix: Bundle radio and simple into mode instead"
ellinge Apr 18, 2019
b1d6ef6
Revert "fix: Bundle radio and simple into mode instead"
ellinge Apr 18, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ A lightweight and fast control to render a select component that can display hie
- [keepTreeOnSearch](#keeptreeonsearch)
- [keepChildrenOnSearch](#keepchildrenonsearch)
- [simpleSelect](#simpleselect)
- [singleSelect](#singleselect)
- [showPartiallySelected](#showpartiallyselected)
- [showDropdown](#showDropdown)
- [form states (disabled|readOnly)](#formstates)
Expand Down Expand Up @@ -290,6 +291,12 @@ Type: `bool` (default: `false`)

Turns the dropdown into a simple, single select dropdown. If you pass tree data, only immediate children are picked, grandchildren nodes are ignored. Defaults to `false`.

### singleSelect

Type: `bool` (default: `false`)

Turns the dropdown into single select dropdown. Similar to simpleSelect but keeps tree/children. Defaults to `false`.

### showPartiallySelected

Type: `bool` (default: `false`)
Expand Down
2 changes: 1 addition & 1 deletion docs/bundle.js

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion docs/src/stories/Options/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class WithOptions extends PureComponent {
clearSearchOnChange: false,
keepTreeOnSearch: false,
simpleSelect: false,
singleSelect: false,
showPartiallySelected: false,
disabled: false,
readOnly: false,
Expand All @@ -36,7 +37,7 @@ class WithOptions extends PureComponent {
}

render() {
const { clearSearchOnChange, keepTreeOnSearch, simpleSelect, showPartiallySelected, disabled, readOnly, hierarchical } = this.state
const { clearSearchOnChange, keepTreeOnSearch, simpleSelect, singleSelect, showPartiallySelected, disabled, readOnly, hierarchical } = this.state

return (
<div>
Expand All @@ -53,6 +54,7 @@ class WithOptions extends PureComponent {
<Checkbox label="Clear search on selection" value="clearSearchOnChange" checked={clearSearchOnChange} onChange={this.onOptionsChange} />
<Checkbox label="Keep tree on search" value="keepTreeOnSearch" checked={keepTreeOnSearch} onChange={this.onOptionsChange} />
<Checkbox label="Simple Select" value="simpleSelect" checked={simpleSelect} onChange={this.onOptionsChange} />
<Checkbox label="Single Select" value="singleSelect" checked={singleSelect} onChange={this.onOptionsChange} />
<Checkbox label="Show Partially Selected" value="showPartiallySelected" checked={showPartiallySelected} onChange={this.onOptionsChange} />
<Checkbox label="Disabled" value="disabled" checked={disabled} onChange={this.onOptionsChange} />
<Checkbox label="Read Only" value="readOnly" checked={readOnly} onChange={this.onOptionsChange} />
Expand All @@ -67,6 +69,7 @@ class WithOptions extends PureComponent {
clearSearchOnChange={clearSearchOnChange}
keepTreeOnSearch={keepTreeOnSearch}
simpleSelect={simpleSelect}
singleSelect={singleSelect}
showPartiallySelected={showPartiallySelected}
disabled={disabled}
readOnly={readOnly}
Expand Down
4 changes: 4 additions & 0 deletions src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@
border-top: rgba(0, 0, 0, 0.05) 1px solid;
box-shadow: 0 5px 8px rgba(0, 0, 0, 0.15);

&.simple-select .checked .node-label {
font-weight: 600;
}

ul {
margin: 0;
padding: 0;
Expand Down
27 changes: 16 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class DropdownTreeSelect extends Component {
onFocus: PropTypes.func,
onBlur: PropTypes.func,
simpleSelect: PropTypes.bool,
singleSelect: PropTypes.bool,
noMatchesText: PropTypes.string,
showPartiallySelected: PropTypes.bool,
disabled: PropTypes.bool,
Expand All @@ -55,8 +56,8 @@ class DropdownTreeSelect extends Component {
}
}

createList = ({ data, simpleSelect, showPartiallySelected, hierarchical }) => {
this.treeManager = new TreeManager({ data, simpleSelect, showPartiallySelected, hierarchical })
createList = ({ data, simpleSelect, singleSelect, showPartiallySelected, hierarchical }) => {
this.treeManager = new TreeManager({ data, simpleSelect, singleSelect, showPartiallySelected, hierarchical })
return this.treeManager.tree
}

Expand All @@ -71,8 +72,8 @@ class DropdownTreeSelect extends Component {
}

componentWillMount() {
const { data, simpleSelect, showPartiallySelected, hierarchical } = this.props
const tree = this.createList({ data, simpleSelect, showPartiallySelected, hierarchical })
const { data, simpleSelect, singleSelect, showPartiallySelected, hierarchical } = this.props
const tree = this.createList({ data, simpleSelect, singleSelect, showPartiallySelected, hierarchical })
const tags = this.treeManager.getTags()
this.setState({ tree, tags })
}
Expand All @@ -82,8 +83,8 @@ class DropdownTreeSelect extends Component {
}

componentWillReceiveProps(nextProps) {
const { data, simpleSelect, showPartiallySelected, hierarchical } = nextProps
const tree = this.createList({ data, simpleSelect, showPartiallySelected, hierarchical })
const { data, simpleSelect, singleSelect, showPartiallySelected, hierarchical } = nextProps
const tree = this.createList({ data, simpleSelect, singleSelect, showPartiallySelected, hierarchical })
const tags = this.treeManager.getTags()
this.setState({ tree, tags })
}
Expand Down Expand Up @@ -142,7 +143,7 @@ class DropdownTreeSelect extends Component {
onCheckboxChange = (id, checked) => {
this.treeManager.setNodeCheckedState(id, checked)
let tags = this.treeManager.getTags()
const showDropdown = this.props.simpleSelect ? false : this.state.showDropdown
const showDropdown = (this.props.simpleSelect || this.props.singleSelect) ? false : this.state.showDropdown

if (!tags.length) {
this.treeManager.restoreDefaultValues()
Expand All @@ -156,11 +157,13 @@ class DropdownTreeSelect extends Component {
showDropdown
}

if (this.props.simpleSelect || this.props.clearSearchOnChange) {
const isSimpleSelect = this.props.simpleSelect || this.props.singleSelect

if (isSimpleSelect || this.props.clearSearchOnChange) {
Object.assign(nextState, this.resetSearchState())
}

if (this.props.simpleSelect) {
if (isSimpleSelect) {
document.removeEventListener('click', this.handleOutsideClick, false)
}

Expand Down Expand Up @@ -189,6 +192,8 @@ class DropdownTreeSelect extends Component {
top: this.state.showDropdown,
bottom: !this.state.showDropdown
})

const isSimpleSelect = this.props.simpleSelect || this.props.singleSelect

return (
<div
Expand All @@ -214,7 +219,7 @@ class DropdownTreeSelect extends Component {
/>
</a>
{this.state.showDropdown && (
<div className={cx('dropdown-content')}>
<div className={cx('dropdown-content', { 'simple-select': isSimpleSelect })}>
{this.state.allNodesHidden ? (
<span className="no-matches">{this.props.noMatchesText || 'No matches found'}</span>
) : (
Expand All @@ -226,7 +231,7 @@ class DropdownTreeSelect extends Component {
onAction={this.onAction}
onCheckboxChange={this.onCheckboxChange}
onNodeToggle={this.onNodeToggle}
simpleSelect={this.props.simpleSelect}
simpleSelect={isSimpleSelect}
showPartiallySelected={this.props.showPartiallySelected}
readOnly={this.props.readOnly}
/>
Expand Down
2 changes: 1 addition & 1 deletion src/tree-manager/flatten-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ const tree = [
}
```
* @param {[type]} tree The incoming tree object
* @param {[bool]} simple Whether its in Single slect mode (simple dropdown)
* @param {[bool]} simple Whether its in Single select mode (simple dropdown)
* @param {[bool]} showPartialState Whether to show partially checked state
* @return {object} The flattened list
*/
Expand Down
12 changes: 11 additions & 1 deletion src/tree-manager/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import { isEmpty } from '../utils'
import flattenTree from './flatten-tree'

class TreeManager {
constructor({ data, simpleSelect, showPartiallySelected, hierarchical }) {
constructor({ data, simpleSelect, singleSelect, showPartiallySelected, hierarchical }) {
this._src = data
const { list, defaultValues } = flattenTree(JSON.parse(JSON.stringify(data)), simpleSelect, showPartiallySelected, hierarchical)
this.tree = list
this.defaultValues = defaultValues
this.simpleSelect = simpleSelect
this.singleSelect = singleSelect
this.showPartialState = !hierarchical && showPartiallySelected
this.searchMaps = new Map()
this.hierarchical = hierarchical
Expand Down Expand Up @@ -145,6 +146,15 @@ class TreeManager {

if (this.simpleSelect) {
this.togglePreviousChecked(id)
} else if (this.singleSelect) {
this.togglePreviousChecked(id)
if (!checked) {
this.toggleChildren(id, checked)
this.unCheckParents(node)
}
if (this.showPartialState) {
this.partialCheckParents(node)
}
} else {
if (!this.hierarchical) this.toggleChildren(id, checked)

Expand Down
5 changes: 3 additions & 2 deletions src/tree-node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const isLeaf = children => isEmpty(children)
const getNodeCx = props => {
const {
keepTreeOnSearch, keepChildrenOnSearch, _children, matchInChildren, matchInParent,
disabled, partial, hide, className, showPartiallySelected, readOnly
disabled, partial, hide, className, showPartiallySelected, readOnly, checked
} = props

return cx(
Expand All @@ -29,7 +29,8 @@ const getNodeCx = props => {
'match-in-children': keepTreeOnSearch && matchInChildren,
'match-in-parent': keepTreeOnSearch && keepChildrenOnSearch && matchInParent,
partial: showPartiallySelected && partial,
readOnly
readOnly,
checked
},
className
)
Expand Down
4 changes: 2 additions & 2 deletions src/tree-node/node-label.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ class NodeLabel extends PureComponent {
const nodeLabelProps = { className: 'node-label' }

// in case of simple select mode, there is no checkbox, so we need to handle the click via the node label
// but not if the control is in readOnly state
const shouldRegisterClickHandler = simpleSelect && !readOnly
// but not if the control is in readOnly or disabled state
const shouldRegisterClickHandler = simpleSelect && !readOnly && !disabled

if (shouldRegisterClickHandler) {
nodeLabelProps.onClick = this.handleCheckboxChange
Expand Down