Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Label adjustments #660

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
docs/**/bundle.js
docs/**/bundle.js
/**/*.d.ts
139 changes: 109 additions & 30 deletions docs/src/stories/DefaultValues/data.json

Large diffs are not rendered by default.

54 changes: 41 additions & 13 deletions docs/src/stories/DefaultValues/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import React from 'react'
import React, { useState } from 'react'

import DropdownTreeSelect from '../../../../src'
import '../../../../dist/styles.css'

import './index.css'
import data from './data.json'

const onChange = (curNode, selectedNodes) => {
console.log('onChange::', curNode, selectedNodes)
const onChange = (curNode, selectedNodes, all) => {
console.log('onChange::', curNode, selectedNodes, all)
}
const onAction = (node, action) => {
console.log('onAction::', action, node)
Expand All @@ -16,15 +16,43 @@ const onNodeToggle = curNode => {
console.log('onNodeToggle::', curNode)
}

const Simple = () => (
<div>
<h1>Component with Default Values</h1>
<p>
Default Values get applied when there is no other user based selection. User can select more values and unselect
default values as long as there is at least one user-selected value still present.
</p>
<DropdownTreeSelect data={data} onChange={onChange} onAction={onAction} onNodeToggle={onNodeToggle} />
</div>
)
const ref = React.createRef()

const runChange = () => {
const arr = Array.from(ref.current.state.tree.values())
// ref.current.treeManager.setNodeCheckedState(arr[0]._id, true)

// let tags = ref.current.treeManager.tags

ref.current.onTagRemove(arr[0]._id)
}

const Simple = () => {
// const [selected, setSelected] = useState(['iway'])

// setTimeout(() => {
// setSelected(['marmara-university'])
// console.log('SHOULD UPDATE', selected)
// }, 5000)

// console.log(ref.treeManager.setNodeCheckedState(, checked))

// if (ref.current) {
// const arr = Array.from(ref.current.state.tree.values())
// ref.current.treeManager.setNodeCheckedState(arr[0]._id, true)
// }
setTimeout(runChange, 5000)

return (
<div>
<h1>Component with Default Values</h1>
<p>
Default Values get applied when there is no other user based selection. User can select more values and unselect
default values as long as there is at least one user-selected value still present.
</p>
<DropdownTreeSelect ref={ref} data={data} onChange={onChange} onAction={onAction} onNodeToggle={onNodeToggle} />
</div>
)
}

export default Simple
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-dropdown-tree-select",
"version": "0.0.0-semantic-release",
"name": "@sm2dev/react-dropdown-tree-select",
"version": "0.1.22",
"description": "Lightweight, customizable and fast Dropdown Tree Select component for React",
"keywords": [
"react",
Expand Down
22 changes: 20 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ class DropdownTreeSelect extends Component {
this.setState(nextState, () => {
callback && callback(tags)
})
this.props.onChange(node, tags)
this.props.onChange(node, tags, Array.from(this.state.tree.values()))
}

onAction = (nodeId, action) => {
Expand Down Expand Up @@ -305,6 +305,24 @@ class DropdownTreeSelect extends Component {
const activeDescendant = currentFocus ? `${currentFocus}_li` : undefined

const commonProps = { disabled, readOnly, activeDescendant, texts, mode, clientId: this.clientId }
const mergedProps = {
...commonProps,
...{
texts: {
placeholder: tags && tags.length > 0 ? `(${tags.length}) selected` : commonProps.texts.placeholder || '',
},
},
}

// const arr = Array.from(this.state.tree.values())
// for (let index = 0; index < arr.length; ++index) {
// if (!arr[index].checked && this.state.selected.includes(arr[index].value)) {
// this.treeManager.setNodeCheckedState(
// arr[index]._id,
// this.state.selected.includes(arr[index].value) ? true : false
// )
// }
// }

const searchInput = (
<Input
Expand All @@ -315,7 +333,7 @@ class DropdownTreeSelect extends Component {
onFocus={this.onInputFocus}
onBlur={this.onInputBlur}
onKeyDown={this.onKeyboardKeyDown}
{...commonProps}
{...mergedProps}
inlineSearchInput={inlineSearchInput}
/>
)
Expand Down
3 changes: 3 additions & 0 deletions src/tree-node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class TreeNode extends PureComponent {
className: PropTypes.string,
title: PropTypes.string,
label: PropTypes.string.isRequired,
labelCount: PropTypes.string,
value: PropTypes.string.isRequired,
checked: PropTypes.bool,
expanded: PropTypes.bool,
Expand Down Expand Up @@ -98,6 +99,7 @@ class TreeNode extends PureComponent {
expanded,
title,
label,
labelCount,
partial,
checked,
value,
Expand All @@ -122,6 +124,7 @@ class TreeNode extends PureComponent {
<NodeLabel
title={title}
label={label}
labelCount={labelCount}
id={_id}
partial={partial}
checked={checked}
Expand Down
8 changes: 6 additions & 2 deletions src/tree-node/node-label.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class NodeLabel extends PureComponent {
actions: PropTypes.array,
title: PropTypes.string,
label: PropTypes.string.isRequired,
labelCount: PropTypes.string,
value: PropTypes.string.isRequired,
checked: PropTypes.bool,
partial: PropTypes.bool,
Expand Down Expand Up @@ -37,7 +38,7 @@ class NodeLabel extends PureComponent {
}

render() {
const { mode, title, label, id, partial, checked } = this.props
const { mode, title, label, labelCount, id, partial, checked } = this.props
const { value, disabled, showPartiallySelected, readOnly, clientId } = this.props
const nodeLabelProps = { className: 'node-label' }

Expand Down Expand Up @@ -65,7 +66,10 @@ class NodeLabel extends PureComponent {
{...sharedProps}
/>
)}
<span {...nodeLabelProps}>{label}</span>
<span {...nodeLabelProps}>
{label}
{labelCount && <span className="node-label-count">({labelCount})</span>}
</span>
</label>
)
}
Expand Down
5 changes: 3 additions & 2 deletions types/react-dropdown-tree-select.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// tslint:disable:interface-name
declare module 'react-dropdown-tree-select' {
declare module '@sm2dev/react-dropdown-tree-select' {
import * as React from 'react'

export type TreeData = Object | TreeNodeProps[]
Expand Down Expand Up @@ -39,7 +39,7 @@ declare module 'react-dropdown-tree-select' {
*
* Calls the handler with the current node object and all selected nodes (if any)
*/
onChange?: (currentNode: TreeNode, selectedNodes: TreeNode[]) => void
onChange?: (currentNode: TreeNode, selectedNodes: TreeNode[], allNodes: TreeNode[]) => void
/** Fired on click of the action */
onAction?: (currentNode: TreeNode, currentAction: NodeAction) => void
/** Fires when a node is expanded or collapsed.
Expand Down Expand Up @@ -123,6 +123,7 @@ declare module 'react-dropdown-tree-select' {
export interface TreeNode {
/** Checkbox label */
label: string
labelCount?: string
/** Checkbox value */
value: string
/** Initial state of checkbox. if true, checkbox is selected and corresponding pill is rendered. */
Expand Down
Loading