DropDown with ShowClear enabled show clear button on load

UI Components for React
Post Reply
Yayati
Posts: 5
Joined: 09 Sep 2020, 10:07

09 Sep 2020, 11:17

I am using PrimeReact's Dropdown control with showClear enabled.
Ideally the control should show clear button only when some option in selected but the control shows clear on control load also even when no value is selected.
Once the user selects some option then tries to clear the selected option then it works fine but the issue is in onload.

Code: Select all

export default class selectCityComponent extends React.Component<any, any> {
	constructor(props: any) {
		super(props);
		this.state = {
			cities: [],
			city: ''
		};
	}
	
	public componentDidMount() {
		this.getCityOptions().then(results => {
			this.setState({ cities: results });
		});
	}
	
	public render(){
		return (
			<Dropdown
				value={this.state.city}
				options={this.state.cities}
				optionLabel="CITY_NAME"
				optionValue="CITY_NAME"
				onChange={(e) => this.setState({ city: e.value })}
				placeholder="Select City" 
			/>
		);
	}
}


mert.sincan
Posts: 5281
Joined: 29 Jun 2013, 12:38

10 Oct 2020, 22:31

Hi,

Could you please try it with 5.0 version? I couldn't replicate it at https://www.primefaces.org/primereact/s ... #/dropdown

Best Regards,

Yayati
Posts: 5
Joined: 09 Sep 2020, 10:07

03 Dec 2020, 11:46

This was actually my mistake in the code. the following correction in initializing state resolved the issue.

Code: Select all

this.state = {
	cities: [],
	city: null
};

mert.sincan
Posts: 5281
Joined: 29 Jun 2013, 12:38

12 Dec 2020, 03:14

Hi,
Thanks a lot for the update!

Best Regards,

Post Reply

Return to “PrimeReact”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 4 guests