Skip to content
This repository has been archived by the owner on Mar 4, 2020. It is now read-only.

feat(Tree): add Component #479

Merged
merged 79 commits into from
Nov 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
79 commits
Select commit Hold shift + click to select a range
a129e19
iniital commit for Tree
Nov 5, 2018
3ad8e46
Adding subcomponents for Tree
Nov 6, 2018
0083bd6
wip Tree component
Nov 6, 2018
da886e6
Merge branch 'master' into experiment
Nov 6, 2018
603cb0a
Merge branch 'master' into priyankar/tree-component
Nov 8, 2018
dab5e30
Merge branch 'master' into priyankar/tree-component
Nov 8, 2018
0aed791
WIP Tree Component
Nov 14, 2018
0be46d8
Merge branch 'master' into priyankar/tree-component
Nov 14, 2018
2830e9b
WIP Tree Component
Nov 14, 2018
f48eac5
WIP Tree component
Nov 14, 2018
8c62ff2
WIP Tree component
Nov 14, 2018
2e8c298
WIP Tree component
Nov 15, 2018
afc3dab
WIP Tree component
Nov 15, 2018
e7fa6c7
WIP tree component
Nov 15, 2018
c6d71d9
WIP Tree Component
Nov 15, 2018
06f9d10
WIP Tree Component
Nov 15, 2018
29cdf23
WIP Tree component
Nov 15, 2018
2a64b15
Merge branch 'master' into priyankar/tree-component
Nov 15, 2018
2f79490
Merge branch 'master' into priyankar/tree-component
Nov 16, 2018
b65bad6
Updated code to make Tree component compatible with the latest change…
Nov 17, 2018
c87593b
Adding accessibility props
Nov 18, 2018
d308663
Merge branch 'master' into priyankar/tree-component
Nov 19, 2018
1d4ae12
Resolving review comments
Nov 19, 2018
471766a
Merge branch 'master' into priyankar/tree-component
Nov 19, 2018
cc8370b
Adding support for dark and high contrast themes. Also removing unuse…
Nov 19, 2018
cd5ac8a
Fixing class names and changing name of data array
Nov 20, 2018
56d2878
Replacing items at missed places
Nov 20, 2018
c8b161a
Replacing activeContent with renderTitle
Nov 20, 2018
72a8d15
- rename `subtree` to `items`
layershifter Nov 20, 2018
4a8322b
rename ui-tree-list className to ui-tree on Tree
layershifter Nov 20, 2018
7585d70
add accessibility props
layershifter Nov 20, 2018
92e6c1b
drop activeContent from props
layershifter Nov 20, 2018
08bcf55
drop `titleStyles` and `titleVariables`
layershifter Nov 20, 2018
d160582
move `display: block` to styles
layershifter Nov 20, 2018
7bff69a
use `fragment` API instead of children's array
layershifter Nov 20, 2018
29bb42a
drop unused imports
layershifter Nov 20, 2018
61e4265
drop unused imports
layershifter Nov 20, 2018
eea9f67
rename `active` to `open`
layershifter Nov 20, 2018
36d50f9
drop broken variables
layershifter Nov 20, 2018
a46742c
simplify renderContent()
layershifter Nov 20, 2018
77ce219
use `open` in example
layershifter Nov 20, 2018
8f61b44
add keys in examples
layershifter Nov 20, 2018
699425e
fix `items` => `subtree`
layershifter Nov 20, 2018
c7847ab
use shorthand props
layershifter Nov 20, 2018
0bd865c
rename `isSubtree` to `nested`
layershifter Nov 20, 2018
a5ccef4
omit some anys
layershifter Nov 20, 2018
1031a02
order props by alphabet
layershifter Nov 20, 2018
80e4527
remove unused variables, pass `renderTitle` down
layershifter Nov 20, 2018
a5a1b76
use AutoControlledComponent
layershifter Nov 20, 2018
94b95c2
fix lint issues
layershifter Nov 20, 2018
182be92
Moving href attribute to TreeTitle
Nov 20, 2018
8acb331
Merge branch 'master' into priyankar/tree-component
Nov 20, 2018
37bbd59
Changing description to specification
Nov 20, 2018
9568118
Merge branch 'master' into priyankar/tree-component
Nov 22, 2018
a9bbca9
Adressing review comments
Nov 22, 2018
095f698
Removing reference of treeBehaviour
Nov 22, 2018
999a5d7
Also removing treeitem role
Nov 22, 2018
11be0e1
Revert "Also removing treeitem role"
Nov 22, 2018
8b8ac0d
removing treeitem role
Nov 22, 2018
4b9386b
Fixing props in example
Nov 22, 2018
0140921
Merge branch 'master' into priyankar/tree-component
Nov 22, 2018
e537350
Adding default accessibility behaviour
Nov 22, 2018
150bf92
Removing arrow if there are no children
Nov 22, 2018
ab1f9dd
Merge branch 'master' into priyankar/tree-component
Nov 22, 2018
b11ad57
Adding test cases for treeTitleBehaviour
Nov 23, 2018
4a2e15d
Removing console.log
Nov 23, 2018
8459fb2
fixing typo and method convertToBooleanIfApplicable in testHelper
mituron Nov 23, 2018
7a2a829
Merge branch 'master' into priyankar/tree-component
Nov 23, 2018
c27affd
Resolving review comments
Nov 23, 2018
6d5752a
Merge branch 'priyankar/tree-component' of github.com:stardust-ui/rea…
Nov 23, 2018
bb3ffb5
Fixing ci and asdding test cases
Nov 23, 2018
bb81dac
Merge branch 'master' into priyankar/tree-component
Nov 26, 2018
cdbfb57
Addressing comments
Nov 26, 2018
d968745
Add chasnges to changelog
Nov 27, 2018
9983e9f
Addressing review comments
Nov 27, 2018
abb1e0e
Merge branch 'master' into priyankar/tree-component
Nov 27, 2018
590f7e6
Addessing review comments
Nov 28, 2018
2c462ab
Removing nested prop from Tree
Nov 28, 2018
32737f5
Merge branch 'master' into priyankar/tree-component
Nov 28, 2018
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

## [Unreleased]

### Features
- Add `Tree` Component @priyankar205 ([#479]
(https://github.com/stardust-ui/react/pull/479))

<!--------------------------------[ v0.13.0 ]------------------------------- -->
## [v0.13.0](https://github.com/stardust-ui/react/tree/v0.13.0) (2018-11-27)
[Compare changes](https://github.com/stardust-ui/react/compare/v0.12.1...v0.13.0)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import * as React from 'react'
import { Tree } from '@stardust-ui/react'

const items = [
{
key: '1',
title: 'one',
items: [
{
key: '1',
title: 'one one',
items: [
{
key: '1',
title: 'one one one',
},
],
},
],
},
{
key: '2',
title: ' two',
items: [
{
key: '1',
title: 'two one',
},
],
},
]

const TreeExampleShorthand = () => <Tree items={items} />

export default TreeExampleShorthand
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import * as React from 'react'
import { Icon, Tree } from '@stardust-ui/react'

const items = [
{
key: '1',
title: 'one',
items: [
{
key: '2',
title: 'one one',
items: [
{
key: '3',
title: 'one one one',
},
],
},
],
},
{
key: '4',
title: 'two',
items: [
{
key: '5',
title: 'two one',
},
],
},
]

const titleRenderer = (Component, { content, open, hasSubtree, ...rest }) => (
<Component open={open} hasSubtree={hasSubtree} {...rest}>
{hasSubtree && <Icon name={open ? 'arrow down' : 'arrow right'} />}
<span>{content}</span>
</Component>
)

const TreeTitleCustomizationExample = () => <Tree items={items} renderItemTitle={titleRenderer} />

export default TreeTitleCustomizationExample
20 changes: 20 additions & 0 deletions docs/src/examples/components/Tree/Types/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import * as React from 'react'
import ComponentExample from 'docs/src/components/ComponentDoc/ComponentExample'
import ExampleSection from 'docs/src/components/ComponentDoc/ExampleSection'

const Types = () => (
<ExampleSection title="Types">
<ComponentExample
title="Default"
description="A default Tree."
examplePath="components/Tree/Types/TreeExample"
/>
<ComponentExample
title="Custom Title"
description="A Tree with customized title rendering."
examplePath="components/Tree/Types/TreeTitleCustomizationExample"
/>
</ExampleSection>
)

export default Types
10 changes: 10 additions & 0 deletions docs/src/examples/components/Tree/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import * as React from 'react'
import Types from './Types'

const TreeExamples = () => (
<div>
<Types />
</div>
)

export default TreeExamples
80 changes: 80 additions & 0 deletions src/components/Tree/Tree.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import * as _ from 'lodash'
import * as PropTypes from 'prop-types'
import * as React from 'react'

import TreeItem from './TreeItem'
import { UIComponent, childrenExist } from '../../lib'
import { ShorthandValue, ShorthandRenderFunction } from '../../../types/utils'
import { Accessibility } from '../../lib/accessibility/types'
import { defaultBehavior } from '../../lib/accessibility'
import { commonUIComponentPropTypes, childrenComponentPropTypes } from '../../lib/commonPropTypes'
import { UIComponentProps, ChildrenComponentProps } from '../../lib/commonPropInterfaces'
import * as customPropTypes from '../../lib/customPropTypes'

export interface TreeProps extends UIComponentProps<any, any>, ChildrenComponentProps {
/**
* Accessibility behavior if overridden by the user.
* @default defaultBehavior
*/
accessibility?: Accessibility

/** Shorthand array of props for Tree. */
items: ShorthandValue[]

/**
* A custom render function for the title slot.
*
* @param {React.ReactType} Component - The computed component for this slot.
* @param {object} props - The computed props for this slot.
* @param {ReactNode|ReactNodeArray} children - The computed children for this slot.
*/
renderItemTitle?: ShorthandRenderFunction
}

/**
* Allows users to display data organised in tree-hierarchy.
*/
class Tree extends UIComponent<TreeProps> {
static create: Function

static className = 'ui-tree'

static displayName = 'Tree'

static propTypes = {
...commonUIComponentPropTypes,
...childrenComponentPropTypes,
accessibility: PropTypes.func,
items: customPropTypes.collectionShorthand,
renderItemTitle: PropTypes.func,
}
sophieH29 marked this conversation as resolved.
Show resolved Hide resolved

public static defaultProps = {
as: 'ul',
kuzhelov marked this conversation as resolved.
Show resolved Hide resolved
accessibility: defaultBehavior,
}

renderContent() {
const { items, renderItemTitle } = this.props

return _.map(items, item =>
TreeItem.create(item, {
defaultProps: {
renderItemTitle,
},
}),
)
}

renderComponent({ ElementType, classes, accessibility, rest, styles, variables }) {
const { children } = this.props

return (
<ElementType className={classes.root} {...accessibility.attributes.root} {...rest}>
{childrenExist(children) ? children : this.renderContent()}
</ElementType>
)
}
}

export default Tree
130 changes: 130 additions & 0 deletions src/components/Tree/TreeItem.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import * as _ from 'lodash'
import * as PropTypes from 'prop-types'
import * as React from 'react'

import Tree from './Tree'
import TreeTitle from './TreeTitle'
import { defaultBehavior } from '../../lib/accessibility'
import { Accessibility } from '../../lib/accessibility/types'

import {
AutoControlledComponent,
childrenExist,
customPropTypes,
createShorthandFactory,
} from '../../lib'
import { ShorthandRenderFunction, ShorthandValue } from 'utils'
import { commonUIComponentPropTypes, childrenComponentPropTypes } from '../../lib/commonPropTypes'
import {
UIComponentProps,
ChildrenComponentProps,
ContentComponentProps,
} from '../../lib/commonPropInterfaces'

export interface TreeItemProps
extends UIComponentProps<any, any>,
ChildrenComponentProps,
ContentComponentProps {
/**
* Accessibility behavior if overridden by the user.
* @default defaultBehavior
*/
accessibility?: Accessibility

/** Initial open value. */
defaultOpen?: boolean

/** Array of props for sub tree. */
items?: ShorthandValue[]

/** Whether or not the subtree of the item is in the open state. */
open?: boolean

/**
* A custom render iterator for rendering each Accordion panel title.
* The default component, props, and children are available for each panel title.
*
* @param {React.ReactType} Component - The computed component for this slot.
* @param {object} props - The computed props for this slot.
* @param {ReactNode|ReactNodeArray} children - The computed children for this slot.
*/
renderItemTitle?: ShorthandRenderFunction

/** Properties for TreeTitle. */
title?: ShorthandValue
}

export interface TreeItemState {
open?: boolean
}

class TreeItem extends AutoControlledComponent<TreeItemProps, TreeItemState> {
static create: Function

static className = 'ui-tree__item'

static displayName = 'TreeItem'

static autoControlledProps = ['open']

static propTypes = {
...commonUIComponentPropTypes,
...childrenComponentPropTypes,
accessibility: PropTypes.func,
defaultOpen: PropTypes.bool,
items: customPropTypes.collectionShorthand,
open: PropTypes.bool,
renderItemTitle: PropTypes.func,
title: customPropTypes.itemShorthand,
}

public static defaultProps = {
as: 'li',
accessibility: defaultBehavior,
}

handleTitleOverrides = predefinedProps => ({
onClick: (e, titleProps) => {
priyankar205 marked this conversation as resolved.
Show resolved Hide resolved
e.preventDefault()
this.trySetState({
open: !this.state.open,
})
_.invoke(predefinedProps, 'onClick', e, titleProps)
},
})

renderContent() {
const { items, title, renderItemTitle } = this.props
const { open } = this.state

const hasSubtree = !!(items && items.length)

return (
<>
{TreeTitle.create(title, {
defaultProps: {
open,
hasSubtree,
},
render: renderItemTitle,
overrideProps: this.handleTitleOverrides,
})}
{hasSubtree && open && <Tree items={items} renderItemTitle={renderItemTitle} />}
</>
)
}

renderComponent({ ElementType, accessibility, classes, rest, styles, variables }) {
const { children } = this.props

return (
<ElementType className={classes.root} {...accessibility.attributes.root} {...rest}>
{childrenExist(children) ? children : this.renderContent()}
</ElementType>
)
}
}

TreeItem.create = createShorthandFactory(TreeItem, 'title')

export default TreeItem
Loading