question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Placeholder not visible for empty object as value

See original GitHub issue

When the value parameter is given an empty object, placeholder is not visible. I think, along with null and undefined values, even empty objects {} and empty lists [] should be considered as no value.

For the below code, placeholder is not seen. However, when this.state.value is set as null or undefined, placeholder is visible.

var options = [
    {value: 'apple', label: 'Apple'},
    {value: 'banana', label: 'Banana'}
];

class extends React.Component {
    constructor(props) {
        super(props);

        this.state = {
            value: {}
        }
    }

    render() {
        return (
            <Select
                placeholder="Select a fruit"
                value={this.state.value}
                options={options} />
        );
    }
}

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

28reactions
alynnlpcommented, May 7, 2019

My code used React, TypeScript with React-Select Async for controlled value, value needs to be null in order for placeholder to display the text

render() {
     const { id, value } = this.props;
     const currentValue = { label: value };     

 return (
       <SelectList
            placeholder={this.props.placeholder}
            **value={value ? currentValue : null}**
            onChange={this.setQueryValue}
            loadOptions={this.fetchAsyncOption}
            defaultOptions={false}
            inputId={id}
            isClearable={true}
       />
13reactions
lugy90commented, Jul 12, 2016

@tejasjadhav Set value as a void string then convert it to a object.


var options = [
    {value: 'apple', label: 'Apple'},
    {value: 'banana', label: 'Banana'}
];

class extends React.Component {
    constructor(props) {
        super(props);

        this.state = {
            value: ''   //   set string
        }
    }

   handleChange(option) {
       let obj ={value: option.value, label: option.label};
       this.setState({value: obj});   //  convert to obj
   }

    render() {
        return (
            <Select
                placeholder="Select a fruit"
                value={this.state.value}
                options={options} 
                onChange={handleChange}/>
        );
    }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to set value attribute for placeholder if value is empty ...
Click in the input box. Placeholder value is cleared. Click outside, placeholder value is now reset back. Click in the input box. Type...
Read more >
:placeholder-shown - CSS: Cascading Style Sheets | MDN
The :placeholder-shown CSS pseudo-class represents any or element that is currently displaying placeholder text.
Read more >
Experience editor does not show the empty placeholder ...
When we add any component in empty placeholder in experience editor and then remove the component, we do not see the the empty...
Read more >
Placeholder vs Value Attributes in HTML - GeeksforGeeks
The actual value of the placeholder is empty. Once the user does enter something, the placeholder is no longer needed.
Read more >
Zendesk Support placeholders reference
There are two primary data objects in Zendesk Support: Ticket and User. ... Note: If the subject field is empty or not visible...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found