Make the color of the chosen item black while keeping the placeholder's color grey within React Picky

Utilizing react picky for the multiselect dropdown feature. Attempting to maintain the placeholder color as grey while displaying black text after selection.

Markup

<Picky multiple={true}
       includeFilter={true}
       onChange={this.onRulesSelectChange}
       placeholder={"Please Select"} />

In the CSS, attempting to override picky's placeholder class only affects the selected text as well, causing both to turn grey.

CSS

.picky__placeholder{
    font-family: CSePRoman, Helvetica Neue, Helvetica, Arial, sans-serif;
    font-size: 1rem;
    color: grey;
    line-height: 21px;
}

Current State

Placeholder -> Grey

Selected Text -> Grey

Desired Outcome

Placeholder -> Grey

Selected text -> Black

Thanks in Advance!

Answer №1

When choosing a class name that remains unchanged on an element regardless of selection status, you can keep the initial unselected value grey by following these steps.

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      arrayValue: []
    };
    this.onRulesSelectChange = this.onRulesSelectChange.bind(this);
  }

  //Update the state with the new array value
  onRulesSelectChange(value) {
    this.setState({ arrayValue: value });
  }

  render() {
    return (
      <div className="container">
        <div className="row">
          <div className="col">
            <h3>Multi select</h3>
            <Picky
              //Assign a class based on selected values
              className={
                this.state.arrayValue.length > 0 ? "items-selected" : "items-not-selected"
              }
              value={this.state.arrayValue}
              options={??} //Insert your options here 
              multiple={true}
              includeFilter={true}
              onChange={this.onRulesSelectChange}
              placeholder={"Please Select"}
            />
          </div>
        </div>
      </div>
    );
  }
}

Add the following CSS rule to your stylesheet:

.not-selected span.picky__placeholder {
    color: grey
}

View the live code sandbox example at: https://codesandbox.io/s/0zz0q9670

Answer №2

When working with React, you have the ability to apply dynamic styling to your components directly within JSX using the "className" attribute

.foo { color: grey;}

.bar { color: black;}

To implement this in React:

....
import styles from './styles'; // reference your CSS file

<div className={classNames({[styles.foo]: true, [styles.bar]: 
true})}>
  Your text...
</div>

This approach allows for flexible and dynamic application of styles based on conditions or user interactions.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

I could use a hand here - I'm getting an error message that says "Module not found: 'react-dev-utils/WatchMissingNodeModulesPlugin'

Error: The 'react-dev-utils/WatchMissingNodeModulesPlugin' module cannot be found Require stack: - C:\Users\stromboli\IdeaProjects\projects-ui\config\webpack.config.js - C:\Users\stromboli\IdeaProjects ...

Arranging arrows strategically along a line and ensuring they are positioned correctly above all div elements

I'm really struggling with this issue. I have a situation where I need to center a downward pointing arrow within some divs. It should be positioned in the middle, on a brown line, and on top of everything else. The last div should also include the a ...

What is the best way to choose a slug page using the useRouter in Next

Is there a way to include all my posts in this array so I can use them for conditional styling in the header component? Currently, the header is white on most pages, but dark on the homepage, 404 page, and project pages. However, I am experiencing issues ...

Using the <APpProvider> and <APP> components in my application causes the app to crash with the Context API

[Whenever I include the code <AppProvider> <App/> </AppProvider>, my app crashes and the pages become blank without any error or warning message. ](https://i.sstatic.net/Gb3dT.png) Please see image description here After adding the syn ...

I need assistance setting up a Facebook page feed using Angular.js. I want to display the posts in a list of cards and include a fullscreen image gallery. Is

Hey there! I'm in the process of developing an app that pulls Facebook page posts and showcases them with custom CSS. The app is functioning smoothly with two controllers, DashCtrl and MainCtrl, each working fine on its own. However, when trying to tr ...

Applying padding to an absolute div inside a Bootstrap 4 parent container does not function correctly

I'm trying to create a div with absolute position inside of another div with relative position using Bootstrap 4 like this: .p-r{ padding:8px; background-color:#ddd; height:100px; } .p-a{ bottom:3px; background-color:#000 } <link re ...

When the object reference remains the same, useState will accept updates

My understanding was that useState required a new value to accept updates: To demonstrate this, I have created a codesandbox. In my scenario, I have a nested data structure like the following: const state = { name: "parent", data: { ...

Enable divs to be interactively chosen

I have created two selectable divs that function like buttons. By using the left and right arrow keys, I am able to select one of the divs with this code: document.addEventListener("keydown", keyDownDocument, false); function keyDownDocument(e) { var k ...

Troubleshooting Problems with Adjusting Left Margin Using JQuery

function adjust_width() { $('#cont').toggle(function(){ $('#cont').animate({marginLeft:'0%'}); },function(){ $('#cont').animate({marginLeft:'18.4%'}); }); } The code above is in ...

The onClick event is not functioning properly with React's select and option elements

Looking for a way to get the option value from each region? const region = ['Africa','America','Asia','Europe','Oceania']; <div className="options"> <select> ...

Submitting a form via axios using AxiosInstance for the POST request

I'm trying to figure out how to send a binary file using axios post request- let data= await axios.post(url, formDataObj,config) The formDataObj is an object of FormData and the config is an object that contains headers for the API So far, everythin ...

Jest remains verdant even in cases where Expected does not match Received

it('User is already present as a supplier', (done) => { const store = mockStore({}, [{ type: 'get_user', data: { } }]); return store.dispatch(userGetAction({ role: 'supplier' }, () => {})).then(() => { t ...

Using the "margin: auto" property to center the text box

I am having trouble centering the search box. I tried adding marginLeft: "auto",marginRight: "auto" to the search class but it's not working. Can you please advise me on how to fix this issue? Below is my code snippet and a link to the sandbox: https ...

Tips for utilizing an Array in React Lifecycle:

Working with Mongo/Meteor 1.3/React has presented an interesting challenge for me. In a basic scenario, I have utilized a wrapper React component to fetch data from a Mongo collection and create an Array. However, when passing this Array to the Child compo ...

Managing data in React and JavaScript: Clearing fields upon successful sign up and redirecting to login page after receiving a 200 status code

Here is the code for my SignUp react function. After receiving a response of 200 upon clicking the Sign up button, I want to clear the text in all three fields and redirect the user back to the login page. I'm new to web development so any assistance ...

Adding a layer of middleware (insights) to enhance the functionality of Instantsearch in

Our search functionality using Algolia/Instantsearch/React/Nextjs is working well, but we have yet to set up the Insights middleware. We have implemented custom widgets for more specific control over how results are displayed as shown below. For our cust ...

Is it possible to implement a while loop in React?

Issue: I am facing a challenge while attempting to generate an array of 4 elements from a list using a while loop, but it seems to result in an infinite loop. const [options, setOptions] = useState([]); const fetchElements = () => { while(options. ...

What is the best way to trigger a JavaScript onclick event for a button that is located within a dropdown menu on an HTML page

I've implemented a feature that resizes the font size within a text area based on the selected font size. It was functioning flawlessly... until I decided to place the font size selection in a drop-down menu, which caused the feature to stop working. ...

Default cursor for Internet Explorer: automatic

I have a container div that holds all of my website's content. I want this container to change the cursor to a pointer when clicked on, but I don't want the elements inside the container to inherit this cursor style: https://i.sstatic.net/8joJo. ...

Issues with scrolling, styling in css, and Vue challenges

I'm a beginner in this topic, currently working with Vue and CSS. I've managed to create a responsive menu, but I'm facing difficulty in implementing scroll only for the drop-down menu. The issue I'm encountering is that when I scroll, ...