Skip to content

Commit

Permalink
fix: single select column cascade edit (#5563)
Browse files Browse the repository at this point in the history
* fix: single select column cascade edit

* feat: update code
  • Loading branch information
YangGuoXuan-0503 authored Jul 27, 2023
1 parent 5eadcb2 commit f8e2f72
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@
}

/* editor */
.single-select-editor-popover .popover,
.single-select-editor-popover .popover-inner {
width: fit-content;
max-width: fit-content;
}

.single-select-editor-container {
min-height: 160px;
width: 320px;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class SingleSelect extends Component {
}

updateState = () => {
// this.setState({ isShowSingleSelect: !this.state.isShowSingleSelect });
this.setState({ isShowSingleSelect: !this.state.isShowSingleSelect });
}

onCommit = (value, column) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,41 @@ class SingleSelectEditor extends Component {

constructor(props) {
super(props);
const options = this.getSelectColumnOptions();
const options = this.getSelectColumnOptions(props);
this.state = {
value: props.row[props.column.key],
searchVal: '',
highlightIndex: -1,
maxItemNum: 0,
itemHeight: 0
itemHeight: 0,
filteredOptions: options,
};
this.options = options;
this.filteredOptions = options;
this.timer = null;
this.editorKey = `single-select-editor-${props.column.key}`;
}

getSelectColumnOptions = () => {
const { column, row, columns } = this.props;
UNSAFE_componentWillReceiveProps(nextProps) {
const currentCascadeColumnValue = this.getCascadeColumnValue(this.props);
const nextCascadeColumnValue = this.getCascadeColumnValue(nextProps);
if (currentCascadeColumnValue !== nextCascadeColumnValue) {
this.options = this.getSelectColumnOptions(nextProps);
this.setState({ filteredOptions: this.options });
}
}

getCascadeColumnValue = (props) => {
const { column, row, columns } = props;
const { data } = column;
const { cascade_column_key } = data || {};
if (!cascade_column_key) return '';
const cascadeColumn = columns.find(item => item.key === cascade_column_key);
if (!cascadeColumn) return '';
return row[cascade_column_key];
}

getSelectColumnOptions = (props) => {
const { column, row, columns } = props;
let options = getSelectColumnOptions(column);
const { data } = column;
const { cascade_column_key, cascade_settings } = data || {};
Expand All @@ -41,35 +60,30 @@ class SingleSelectEditor extends Component {
return options;
}

setRef = (ref) => {
this.ref = ref;
if (!this.ref) return;
const { toggle } = this.ref;
this.ref.toggle = () => {
toggle && toggle();
this.props.onUpdateState();
};
toggle = () => {
this.ref.toggle();
this.props.onUpdateState();
}

onChangeSearch = (searchVal) => {
const { searchVal: oldSearchVal } = this.state;
if (oldSearchVal === searchVal) return;
const val = searchVal.toLowerCase();
this.filteredOptions = val ?
const filteredOptions = val ?
this.options.filter((item) => item.name && item.name.toLowerCase().indexOf(val) > -1) : this.options;
this.setState({ searchVal });
this.setState({ searchVal, filteredOptions });
}

onSelectOption = (optionID) => {
const { column } = this.props;
this.setState({ value: optionID }, () => {
this.props.onCommit({ [column.key]: optionID }, column);
this.ref.toggle();
this.toggle();
});
}

render() {
const { value } = this.state;
const { value, filteredOptions } = this.state;
const { column } = this.props;

return (
Expand All @@ -79,7 +93,8 @@ class SingleSelectEditor extends Component {
trigger="legacy"
placement="bottom-start"
hideArrow={true}
ref={this.setRef}
toggle={this.toggle}
ref={ref => this.ref = ref}
>
<div className="single-select-editor-container">
<div className="search-single-selects">
Expand All @@ -91,7 +106,7 @@ class SingleSelectEditor extends Component {
/>
</div>
<div className="single-select-editor-content">
{this.filteredOptions.map(option => {
{filteredOptions.map(option => {
const isSelected = value === option.id;
const style = {
backgroundColor: option.color,
Expand Down

0 comments on commit f8e2f72

Please sign in to comment.