-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Expand file tree
/
Copy pathObjectWidgetTopBar.js
More file actions
132 lines (117 loc) · 3.16 KB
/
ObjectWidgetTopBar.js
File metadata and controls
132 lines (117 loc) · 3.16 KB
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
import React from 'react';
import PropTypes from 'prop-types';
import styled from '@emotion/styled';
import { css } from '@emotion/react';
import ImmutablePropTypes from 'react-immutable-proptypes';
import Icon from './Icon';
import { colors, buttons } from './styles';
import Dropdown, { StyledDropdownButton, DropdownItem } from './Dropdown';
const TopBarContainer = styled.div`
align-items: center;
background-color: ${colors.textFieldBorder};
display: flex;
justify-content: space-between;
margin: 0 -14px;
padding: 13px;
`;
const ExpandButtonContainer = styled.div`
${props =>
props.hasHeading &&
css`
display: flex;
align-items: center;
font-size: 14px;
font-weight: 500;
line-height: 1;
`};
`;
const ExpandButton = styled.button`
${buttons.button};
padding: 4px;
background-color: transparent;
color: inherit;
&:last-of-type {
margin-right: 4px;
}
`;
const AddButton = styled.button`
${buttons.button}
${buttons.widget}
${Icon} {
margin-left: 6px;
}
`;
class ObjectWidgetTopBar extends React.Component {
static propTypes = {
allowAdd: PropTypes.bool,
types: ImmutablePropTypes.list,
onAdd: PropTypes.func,
onAddType: PropTypes.func,
onCollapseToggle: PropTypes.func,
collapsed: PropTypes.bool,
heading: PropTypes.node,
label: PropTypes.string,
t: PropTypes.func.isRequired,
};
componentDidMount() {
// Manually validate PropTypes - React 19 breaking change
PropTypes.checkPropTypes(
ObjectWidgetTopBar.propTypes,
this.props,
'prop',
'ObjectWidgetTopBar',
);
}
renderAddUI() {
if (!this.props.allowAdd) {
return null;
}
if (this.props.types && this.props.types.size > 0) {
return this.renderTypesDropdown(this.props.types);
} else {
return this.renderAddButton();
}
}
renderTypesDropdown(types) {
return (
<Dropdown
renderButton={() => (
<StyledDropdownButton>
{this.props.t('editor.editorWidgets.list.addType', { item: this.props.label })}
</StyledDropdownButton>
)}
>
{types.map((type, idx) => (
<DropdownItem
key={idx}
label={type.get('label', type.get('name'))}
onClick={() => this.props.onAddType(type.get('name'))}
/>
))}
</Dropdown>
);
}
renderAddButton() {
return (
<AddButton onClick={this.props.onAdd}>
{this.props.t('editor.editorWidgets.list.add', { item: this.props.label })}
<Icon type="add" size="xsmall" />
</AddButton>
);
}
render() {
const { onCollapseToggle, collapsed, heading = null } = this.props;
return (
<TopBarContainer>
<ExpandButtonContainer hasHeading={!!heading}>
<ExpandButton onClick={onCollapseToggle} data-testid="expand-button" aria-label={collapsed ? 'expand' : 'collapse'}>
<Icon type="chevron" direction={collapsed ? 'right' : 'down'} size="small" />
</ExpandButton>
{heading}
</ExpandButtonContainer>
{this.renderAddUI()}
</TopBarContainer>
);
}
}
export default ObjectWidgetTopBar;