Skip to content

Commit 06d19c8

Browse files
杨国璇杨国璇
authored andcommitted
feat: optimzie code
1 parent f35e208 commit 06d19c8

File tree

2 files changed

+54
-15
lines changed
  • src/RowExpandEditor

2 files changed

+54
-15
lines changed

src/RowExpandEditor/RowExpandMultipleSelectEditor/index.js

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import React from 'react';
2+
import PropTypes from 'prop-types';
23
import { getColumnOptions } from 'dtable-utils';
34
import classnames from 'classnames';
45
import { KeyCodes, DELETED_OPTION_BACKGROUND_COLOR, DELETED_OPTION_TIPS } from '../../constants';
56
import MultipleSelectEditor from '../../MultipleSelectEditor';
67
import { getLocale } from '../../lang';
8+
import ObjectUtils from '../../utils/object-utils';
79

810
import './index.css';
911

@@ -26,8 +28,8 @@ class RowExpandMultipleSelectEditor extends React.Component {
2628
}
2729

2830
UNSAFE_componentWillReceiveProps(nextProps) {
29-
const { value } = nextProps;
30-
if (value !== this.props.value) {
31+
const { value, column } = nextProps;
32+
if (value !== this.props.value || !ObjectUtils.isSameObject(column, this.props.column)) {
3133
this.options = this.getOptions(nextProps);
3234
this.setState({ value });
3335
}
@@ -80,7 +82,8 @@ class RowExpandMultipleSelectEditor extends React.Component {
8082
onToggleSelect = (e) => {
8183
e.preventDefault();
8284
e.stopPropagation();
83-
this.props.updateTabIndex(this.props.columnIndex);
85+
const { updateTabIndex, columnIndex } = this.props;
86+
updateTabIndex && updateTabIndex(columnIndex);
8487
this.toggleSingleSelect(true);
8588
};
8689

@@ -89,7 +92,8 @@ class RowExpandMultipleSelectEditor extends React.Component {
8992
};
9093

9194
onFocus = () => {
92-
this.props.updateTabIndex(this.props.columnIndex);
95+
const { updateTabIndex, columnIndex } = this.props;
96+
updateTabIndex && updateTabIndex(columnIndex);
9397
};
9498

9599
onChange = (option) => {
@@ -148,7 +152,7 @@ class RowExpandMultipleSelectEditor extends React.Component {
148152
};
149153

150154
renderOptions = () => {
151-
const { isEditorFocus, classNamePrefix } = this.props;
155+
const { isEditorFocus, classNamePrefix, placeholder } = this.props;
152156
const options = this.getMultipleSelectList();
153157

154158
return (
@@ -160,12 +164,13 @@ class RowExpandMultipleSelectEditor extends React.Component {
160164
className={classnames('dtable-ui dtable-ui-row-expand-select-editor custom-select', { 'focus': isEditorFocus, [`${classNamePrefix}-select-editor`]: classNamePrefix })}
161165
>
162166
<div className={classnames('dtable-ui-row-expand-select-editor-inner', { [`${classNamePrefix}-select-editor-inner`]: classNamePrefix })}>
163-
<div>
167+
<div className={classnames('', { [`${classNamePrefix}-select-editor-inner-container`]: classNamePrefix })}>
164168
{options.length > 0 &&
165169
<div className={classnames('dtable-ui-row-expand-select-options', { [`${classNamePrefix}-select-editor-options`]: classNamePrefix })}>
166170
{options}
167171
</div>
168172
}
173+
{options.length === 0 && placeholder && (<>{placeholder}</>)}
169174
</div>
170175
<i aria-hidden="true" className="dtable-font dtable-icon-down3"></i>
171176
</div>
@@ -201,4 +206,19 @@ class RowExpandMultipleSelectEditor extends React.Component {
201206

202207
}
203208

209+
RowExpandMultipleSelectEditor.propTypes = {
210+
column: PropTypes.object,
211+
value: PropTypes.array,
212+
valueKey: PropTypes.string,
213+
isSupportNewOption: PropTypes.bool,
214+
onAddNewOption: PropTypes.func,
215+
isEditorFocus: PropTypes.bool,
216+
classNamePrefix: PropTypes.string,
217+
placeholder: PropTypes.any,
218+
columnIndex: PropTypes.number,
219+
updateTabIndex: PropTypes.func,
220+
onEditorOpen: PropTypes.func,
221+
onEditorClose: PropTypes.func,
222+
};
223+
204224
export default RowExpandMultipleSelectEditor;

src/RowExpandEditor/RowExpandSingleSelectorEditor/index.js

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import React from 'react';
2+
import PropTypes from 'prop-types';
23
import { getColumnOptions } from 'dtable-utils';
34
import { KeyCodes, DELETED_OPTION_BACKGROUND_COLOR, DELETED_OPTION_TIPS } from '../../constants';
45
import classnames from 'classnames';
56
import SingleSelectEditor from '../../SingleSelectEditor';
67
import { getLocale } from '../../lang';
8+
import ObjectUtils from '../../utils/object-utils';
79

810
import './index.css';
911

@@ -27,8 +29,8 @@ class RowExpandSingleSelectEditor extends React.Component {
2729
}
2830

2931
UNSAFE_componentWillReceiveProps(nextProps) {
30-
const { value } = nextProps;
31-
if (value !== this.props.value) {
32+
const { value, column } = nextProps;
33+
if (value !== this.props.value || !ObjectUtils.isSameObject(column, this.props.column)) {
3234
this.options = this.getOptions(nextProps);
3335
this.setState({ value, showSelectPopover: false });
3436
}
@@ -88,7 +90,7 @@ class RowExpandSingleSelectEditor extends React.Component {
8890

8991
hideDropDownMenu = (event) => {
9092
if (!event.target) return;
91-
if (!this.singleSelectContainer.contains(event.target) && this.state.showSelectPopover) {
93+
if (!this.ref.contains(event.target) && this.state.showSelectPopover) {
9294
const singleSelectEditor = document.getElementsByClassName('dtable-ui-select-editor-container')[0];
9395
if (singleSelectEditor && singleSelectEditor.contains(event.target)) return;
9496
this.toggleSingleSelect(false);
@@ -116,7 +118,7 @@ class RowExpandSingleSelectEditor extends React.Component {
116118
};
117119

118120
renderOption = () => {
119-
const { isEditorFocus } = this.props;
121+
const { isEditorFocus, classNamePrefix, placeholder } = this.props;
120122
const { value } = this.state;
121123
const option = this.options.find(o => o[this.key] === value);
122124
const optionStyle = option ?
@@ -130,15 +132,16 @@ class RowExpandSingleSelectEditor extends React.Component {
130132
onFocus={this.onFocus}
131133
onClick={this.onToggleSelect}
132134
ref={ref => this.selectRef = ref}
133-
className={classnames('dtable-ui dtable-ui-row-expand-select-editor custom-select', { 'focus': isEditorFocus })}
135+
className={classnames('dtable-ui dtable-ui-row-expand-select-editor custom-select', { 'focus': isEditorFocus, [`${classNamePrefix}-select-editor`]: classNamePrefix })}
134136
>
135-
<div className="dtable-ui-row-expand-select-editor-inner">
136-
<div>
137+
<div className={classnames('dtable-ui-row-expand-select-editor-inner', { [`${classNamePrefix}-select-editor-inner`]: classNamePrefix })}>
138+
<div className={classnames('', { [`${classNamePrefix}-select-editor-inner-container`]: classNamePrefix })}>
137139
{value && (
138140
<div className="dtable-ui-select-option" style={optionStyle} title={optionName}>
139141
{optionName}
140142
</div>
141143
)}
144+
{!value && placeholder && (<>{placeholder}</>)}
142145
</div>
143146
<i aria-hidden="true" className="dtable-font dtable-icon-down3"></i>
144147
</div>
@@ -147,10 +150,10 @@ class RowExpandSingleSelectEditor extends React.Component {
147150
};
148151

149152
render() {
150-
const { isSupportNewOption, onAddNewOption, column } = this.props;
153+
const { isSupportNewOption, onAddNewOption, column, classNamePrefix } = this.props;
151154
const { showSelectPopover, value } = this.state;
152155
return (
153-
<div className="position-relative w-100" ref={ref => this.singleSelectContainer = ref}>
156+
<div className="position-relative w-100" ref={ref => this.ref = ref}>
154157
{this.renderOption()}
155158
<span ref={ref => this.targetRef = ref}></span>
156159
{showSelectPopover && (
@@ -161,6 +164,7 @@ class RowExpandSingleSelectEditor extends React.Component {
161164
value={value}
162165
valueKey={this.key}
163166
target={this.targetRef}
167+
classNamePrefix={classNamePrefix}
164168
onCommit={this.onChange}
165169
isSupportNewOption={isSupportNewOption}
166170
onAddNewOption={onAddNewOption}
@@ -173,4 +177,19 @@ class RowExpandSingleSelectEditor extends React.Component {
173177

174178
}
175179

180+
RowExpandSingleSelectEditor.propTypes = {
181+
column: PropTypes.object,
182+
value: PropTypes.array,
183+
valueKey: PropTypes.string,
184+
isSupportNewOption: PropTypes.bool,
185+
onAddNewOption: PropTypes.func,
186+
isEditorFocus: PropTypes.bool,
187+
classNamePrefix: PropTypes.string,
188+
placeholder: PropTypes.any,
189+
columnIndex: PropTypes.number,
190+
updateTabIndex: PropTypes.func,
191+
onEditorOpen: PropTypes.func,
192+
onEditorClose: PropTypes.func,
193+
};
194+
176195
export default RowExpandSingleSelectEditor;

0 commit comments

Comments
 (0)