Skip to content

Commit

Permalink
ISSUE #5149 - make cellSelect cotrolled
Browse files Browse the repository at this point in the history
  • Loading branch information
Amantini1997 committed Oct 8, 2024
1 parent 1179cfc commit 0681fd0
Showing 1 changed file with 4 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,7 @@ interface IProps {
onChange: (event, selectedValue: string) => void;
}

interface IState {
selectedValue: string;
}

export class CellSelect extends PureComponent<IProps, IState> {
export class CellSelect extends PureComponent<IProps> {
public static defaultProps = {
value: '',
items: [],
Expand All @@ -47,20 +43,6 @@ export class CellSelect extends PureComponent<IProps, IState> {
hidden: false,
};

public state = {
selectedValue: ''
};

public componentDidUpdate(prevProps, prevState) {
if (prevProps.value !== this.props.value) {
this.setState({selectedValue: this.props.value});
}
}

public componentDidMount() {
this.setState({selectedValue: this.props.value});
}

public renderOptions = (items, TemplateComponent) => {
const labelName = this.props.labelName || 'name';

Expand Down Expand Up @@ -90,14 +72,13 @@ export class CellSelect extends PureComponent<IProps, IState> {
public handleChange = (event) => {
const selectedValue = event.target.value;

if (this.state.selectedValue !== selectedValue) {
if (this.props.value !== selectedValue) {
this.props.onChange(event, selectedValue);
}
}

public render() {
const {items, itemTemplate, disabled, placeholder, disabledPlaceholder, readOnly, inputId, name, hidden} = this.props;
const {selectedValue} = this.state;
const {items, itemTemplate, disabled, placeholder, disabledPlaceholder, readOnly, inputId, name, hidden, value} = this.props;
const hasNoOptions = !items.length;
const options = [];

Expand All @@ -119,7 +100,7 @@ export class CellSelect extends PureComponent<IProps, IState> {
hidden={hidden}
displayEmpty
input={<Input id={inputId} readOnly={readOnly} />}
value={selectedValue}
value={value}
onChange={this.handleChange}
>
{this.renderOptions(options, itemTemplate)}
Expand Down

0 comments on commit 0681fd0

Please sign in to comment.