-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathIcon.jsx
151 lines (131 loc) · 4.07 KB
/
Icon.jsx
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
147
148
149
150
151
/**
* @component
*/
import classNames from 'clsx';
import PropTypes from 'prop-types';
import React, { Component } from 'react';
import { isString } from '../../utils/is';
let displayedIconWarning = false;
/**
* Displays a FontAwesome icon.
*/
export default class Icon extends Component {
constructor(props) {
super(props);
this.onClick = this.onClick.bind(this);
}
componentDidMount() {
const { icon } = this.props;
if (!displayedIconWarning && icon && !isString(icon)) {
displayedIconWarning = true;
// eslint-disable-next-line no-console,max-len
console.warn(
'[chayns components] Icon: You are still using fortawesome SVG-icons. Consider changing to fontawesome-font-icons. https://github.com/TobitSoftware/chayns-components/blob/master/src/react-chayns-icon/README.md#deprecated'
);
}
}
onClick(e) {
const { onClick, disabled, stopPropagation } = this.props;
if (onClick && !disabled) onClick(e);
if (stopPropagation) e.stopPropagation();
}
render() {
const {
icon,
className,
onClick,
disabled,
stopPropagation,
style,
...other
} = this.props;
if (Array.isArray(icon)) {
return (
<span
className={`fa-stack ${className}`}
style={{
height: '1em',
width: '1.4em',
lineHeight: '1em',
...style,
}}
>
{icon.map((s) => (
<i
key={s}
className={`${s} fa-stack-1x`}
onClick={this.onClick}
{...other}
/>
))}
</span>
);
}
let iconName = icon;
if (typeof icon === 'object') {
iconName = `${icon.prefix} fa-${icon.iconName}`;
}
if (!isString(iconName)) return null;
const classes = classNames('react-chayns-icon', iconName, className, {
'react-chayns-icon--clickable': onClick,
'react-chayns-icon--disabled': disabled,
});
return (
<i
className={classes}
style={style}
onClick={this.onClick}
{...other}
/>
);
}
}
Icon.propTypes = {
/**
* The icon to display. Supply a string or an array of strings like this:
* `fa fa-plane`. Search for icons and their strings on
* https://fontawesome.com/icons/. For backwards compatibility you can also
* specify an icon object from the `@fortawesome`-packages, but this should
* not be used going forward.
*/
icon: PropTypes.oneOfType([
PropTypes.string.isRequired,
PropTypes.shape({
iconName: PropTypes.string.isRequired,
prefix: PropTypes.string.isRequired,
}).isRequired,
PropTypes.arrayOf(PropTypes.string.isRequired),
]).isRequired,
/**
* A classname string that will be applied to the HTML element of the icon.
*/
className: PropTypes.string,
/**
* A React style object that will be applied ot the `<i>`-element of the
* icon.
*/
style: PropTypes.objectOf(
PropTypes.oneOfType([PropTypes.number, PropTypes.string])
),
/**
* A callback that is called for the `onclick`-event on the icon.
*/
onClick: PropTypes.func,
/**
* Disables any user interaction on the icon and renders it in a disabled
* style.
*/
disabled: PropTypes.bool,
/**
* Wether to stop propagation of click events to parent elements.
*/
stopPropagation: PropTypes.bool,
};
Icon.defaultProps = {
className: '',
style: undefined,
onClick: undefined,
disabled: false,
stopPropagation: false,
};
Icon.displayName = 'Icon';