-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Step.js
146 lines (121 loc) · 3.39 KB
/
Step.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import cx from 'clsx'
import _ from 'lodash'
import PropTypes from 'prop-types'
import * as React from 'react'
import {
childrenUtils,
createShorthandFactory,
customPropTypes,
getComponentType,
getUnhandledProps,
useKeyOnly,
useEventCallback,
} from '../../lib'
import Icon from '../Icon'
import StepContent from './StepContent'
import StepDescription from './StepDescription'
import StepGroup from './StepGroup'
import StepTitle from './StepTitle'
/**
* A step shows the completion status of an activity in a series of activities.
*/
const Step = React.forwardRef(function (props, ref) {
const {
active,
children,
className,
completed,
content,
description,
disabled,
href,
onClick,
icon,
link,
title,
} = props
const handleClick = useEventCallback((e) => {
if (!disabled) {
_.invoke(props, 'onClick', e, props)
}
})
const classes = cx(
useKeyOnly(active, 'active'),
useKeyOnly(completed, 'completed'),
useKeyOnly(disabled, 'disabled'),
useKeyOnly(link, 'link'),
'step',
className,
)
const rest = getUnhandledProps(Step, props)
const ElementType = getComponentType(props, {
getDefault: () => {
if (onClick) {
return 'a'
}
},
})
if (!childrenUtils.isNil(children)) {
return (
<ElementType {...rest} className={classes} href={href} onClick={handleClick} ref={ref}>
{children}
</ElementType>
)
}
if (!childrenUtils.isNil(content)) {
return (
<ElementType {...rest} className={classes} href={href} onClick={handleClick} ref={ref}>
{content}
</ElementType>
)
}
return (
<ElementType {...rest} className={classes} href={href} onClick={handleClick} ref={ref}>
{Icon.create(icon, { autoGenerateKey: false })}
{StepContent.create({ description, title }, { autoGenerateKey: false })}
</ElementType>
)
})
Step.displayName = 'Step'
Step.propTypes = {
/** An element type to render as (string or function). */
as: PropTypes.elementType,
/** A step can be highlighted as active. */
active: PropTypes.bool,
/** Primary content. */
children: PropTypes.node,
/** Additional classes. */
className: PropTypes.string,
/** A step can show that a user has completed it. */
completed: PropTypes.bool,
/** Shorthand for primary content. */
content: customPropTypes.contentShorthand,
/** Shorthand for StepDescription. */
description: customPropTypes.itemShorthand,
/** Show that the Loader is inactive. */
disabled: PropTypes.bool,
/** Render as an `a` tag instead of a `div` and adds the href attribute. */
href: PropTypes.string,
/** Shorthand for Icon. */
icon: customPropTypes.itemShorthand,
/** A step can be link. */
link: PropTypes.bool,
/**
* Called on click. When passed, the component will render as an `a`
* tag by default instead of a `div`.
*
* @param {SyntheticEvent} event - React's original SyntheticEvent.
* @param {object} data - All props.
*/
onClick: PropTypes.func,
/** A step can show a ordered sequence of steps. Passed from StepGroup. */
ordered: PropTypes.bool,
/** Shorthand for StepTitle. */
title: customPropTypes.itemShorthand,
}
Step.Content = StepContent
Step.Description = StepDescription
Step.Group = StepGroup
Step.Title = StepTitle
Step.create = createShorthandFactory(Step, (content) => ({ content }))
export default Step