-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathTabularCompoundInput.jsx
85 lines (74 loc) · 2.25 KB
/
TabularCompoundInput.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
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import CustomCompoundInput from './CustomCompoundInput';
import InputTableRow from './InputTableRow';
import InputTableHeader from './InputTableHeader';
import labelable from '../enhancers/labelable';
import repeatable from '../enhancers/repeatable';
import { getPath } from '../helpers/pathHelpers';
const BaseComponent = repeatable(labelable(CustomCompoundInput));
const propTypes = {
// TODO: Stop using propTypes in isInput, and in render method of cspace-ui Field component.
// Until then, propTypes need to be hoisted from the base component.
// eslint-disable-next-line react/forbid-foreign-prop-types
...BaseComponent.propTypes,
children: PropTypes.node,
repeating: PropTypes.bool,
sortableFields: PropTypes.objectOf(PropTypes.bool),
onSortInstances: PropTypes.func,
renderChildInputLabel: PropTypes.func,
};
const defaultProps = {
children: undefined,
repeating: undefined,
sortableFields: undefined,
onSortInstances: undefined,
renderChildInputLabel: undefined,
};
export default class TabularCompoundInput extends Component {
constructor() {
super();
this.handleSortButtonClick = this.handleSortButtonClick.bind(this);
}
handleSortButtonClick(event) {
const {
onSortInstances,
} = this.props;
if (onSortInstances) {
onSortInstances(getPath(this.props), event.currentTarget.dataset.name);
}
}
render() {
const {
children,
repeating,
renderChildInputLabel,
sortableFields,
...remainingProps
} = this.props;
const tableHeader = (
<InputTableHeader
embedded={repeating}
renderLabel={renderChildInputLabel}
sortableFields={sortableFields}
onSortButtonClick={this.handleSortButtonClick}
>
{children}
</InputTableHeader>
);
return (
<BaseComponent
// eslint-disable-next-line react/jsx-props-no-spreading
{...remainingProps}
label={tableHeader}
repeating={repeating}
>
<InputTableRow embedded={repeating}>
{children}
</InputTableRow>
</BaseComponent>
);
}
}
TabularCompoundInput.propTypes = propTypes;
TabularCompoundInput.defaultProps = defaultProps;