Skip to content

Commit

Permalink
Revert Select local search feature (#252)
Browse files Browse the repository at this point in the history
* Revert "fix: options didn't update (#198)"

This reverts commit e894318.

* Revert "feat: select support local search (#197)"

This reverts commit 8a6a136
  • Loading branch information
Leioy authored Jun 20, 2024
1 parent 46c10fb commit ccb6ebe
Showing 1 changed file with 13 additions and 38 deletions.
51 changes: 13 additions & 38 deletions src/components/Select/Select.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from "react";
import PropTypes from "prop-types";
import classNames from "classnames";
import { get, isEmpty, isUndefined, isFunction, isEqual } from "lodash";
import { get, isEmpty, isUndefined, isFunction } from "lodash";

import Tag from "../Tag";
import Icon from "../Icon";
Expand Down Expand Up @@ -45,7 +45,6 @@ export default class Select extends React.Component {
optionRenderer: PropTypes.func,
valueRenderer: PropTypes.func,
dorpdownRender: PropTypes.func,
disableRemoteSearch: PropTypes.bool,
showTip: PropTypes.bool,
};

Expand All @@ -63,7 +62,6 @@ export default class Select extends React.Component {
options: [],
onChange() {},
onPaging() {},
disableRemoteSearch: false,
showTip: false,
};

Expand All @@ -72,8 +70,6 @@ export default class Select extends React.Component {
value: this.props.multi ? [] : "",
inputValue: "",
inputVisible: true,
options: this.props.options ? this.props.options : [],
options_copy: this.props.options ? this.props.options : [],
};

inputRef = React.createRef();
Expand All @@ -94,12 +90,8 @@ export default class Select extends React.Component {

componentDidUpdate(prevProps, prevState) {
const { value, options } = this.props;
const equal = isEqual(prevProps.options, options);
if (prevProps.options.length !== options.length) {
this.reachBottom = false;
this.setState({ options, options_copy: options });
} else if (!equal) {
this.setState({ options, options_copy: options });
}

if (!isUndefined(value) && value !== prevState.value) {
Expand Down Expand Up @@ -312,11 +304,10 @@ export default class Select extends React.Component {
};

handleInputStatus = (visible) => {
const { value, inputValue, options } = this.state;
const { multi, searchable } = this.props;
const { value, inputValue } = this.state;
const { multi, searchable, options } = this.props;
const option = options.find((item) => item.value === value) || {};
const currentInputValue = multi ? "" : option.value || inputValue || "";

this.setState(
{ inputVisible: visible, inputValue: currentInputValue },
() => {
Expand All @@ -332,7 +323,7 @@ export default class Select extends React.Component {

handleInputChange = (e) => {
const value = e.target.value;
const { multi, searchable, onFetch, disableRemoteSearch } = this.props;
const { multi, searchable, onFetch, onChange } = this.props;

const newState = { inputValue: value };
if (!multi) {
Expand All @@ -345,17 +336,11 @@ export default class Select extends React.Component {
: `${get(this.inputValueRef, "current.clientWidth", 0) + 5}px`;
this.updateInputDOM({ width });
}
onChange(this.state.value);
});

if (isFunction(onFetch) && !disableRemoteSearch) {
if (isFunction(onFetch)) {
onFetch({ name: value });
} else if (searchable) {
const options = value
? this.state.options_copy.filter((item) =>
item.label.toLowerCase().includes(value.toLowerCase())
)
: this.state.options_copy;
this.setState({ options });
}
};

Expand All @@ -380,27 +365,18 @@ export default class Select extends React.Component {
handleClearValue = (e) => {
e.nativeEvent.stopImmediatePropagation();
e.stopPropagation();
const {
multi,
searchable,
onChange,
onFetch,
disableRemoteSearch,
} = this.props;
const { multi, searchable, onChange, onFetch } = this.props;

this.setState(
{
value: multi ? [] : "",
inputValue: "",
inputVisible: true,
visible: false,
},
() => {
onChange();
if (searchable && isFunction(onFetch) && !disableRemoteSearch) {
if (searchable && isFunction(onFetch)) {
onFetch();
} else if (searchable) {
this.setState({ options: this.state.options_copy });
}
}
);
Expand Down Expand Up @@ -439,8 +415,8 @@ export default class Select extends React.Component {
};

renderOptions = () => {
const { visible, options } = this.state;
const { isLoading, pagination = {}, onFetch } = this.props;
const { visible } = this.state;
const { options, isLoading, pagination = {}, onFetch } = this.props;
const { page = 1, total = 0, limit = 10 } = pagination;

if (!visible || !get(this.selectRef, "current")) {
Expand Down Expand Up @@ -566,8 +542,7 @@ export default class Select extends React.Component {
};

renderMultiValue = (value, i) => {
const { valueRenderer, showTip } = this.props;
const { options } = this.state;
const { valueRenderer, options, showTip } = this.props;

const option = options.find((item) => item.value === value) || {
label: value,
Expand Down Expand Up @@ -595,8 +570,8 @@ export default class Select extends React.Component {
};

renderBaseValues = () => {
const { value, inputVisible, options } = this.state;
const { multi, valueRenderer } = this.props;
const { value, inputVisible } = this.state;
const { multi, valueRenderer, options } = this.props;

if (multi) {
if (isEmpty(value)) {
Expand Down

0 comments on commit ccb6ebe

Please sign in to comment.