Show all span elements in a map except for the last one

Within my ReactJS application, I have implemented a mapping function to iterate through an Object. In between each element generated from the mapping process, I am including a span containing a simple care symbol.

The following code snippet demonstrates this:

  renderSecurityModes = (securityKey: string): JSX.Element => {
    const { user } = this.props;
    return (
      <div key={securityKey} className="users-overview-screen__no-last-child">
        {user && user.security && user.security[securityKey as SecurityMode] && (
          <Fragment>
            <span className="font-weight-medium mr-3 text-uppercase">{securityKey}</span>
            <span className="text-border-gray mx-3">|</span>
          </Fragment>
        )}
      </div>
    );
  };

THE ISSUE: I aim to have the caret symbol displayed between the mapped elements, except for the last one where it should be hidden using display: none.

To achieve this, I added a specific class targeting the last child of each mapped item. However, due to the nature of the mapping process, every caret is being identified as the last-child:

Below is the associated SCSS code:

.users-overview-screen__no-last-child {
  span:last-child {
      display: none;
  }
}

DOM STRUCTURE:

<div class="display-flex>
  <span>Mapped Item 1</span>
  <div class=“users-overview-screen__no-last-child”>
    <span>|</span>
  </div>
  <span>Mapped Item 2</span>
  <div class=“users-overview-screen__no-last-child”>
    <span>|</span>
  </div>
    <span>Mapped Item 3</span>
  <div class=“users-overview-screen__no-last-child”>
    <span>|</span>
  </div>
</div>

In this context, I specifically want the caret symbol to not appear on the very last mapped item, such as in "Mapped Item 3". However, with the current implementation of my SCSS, no caret symbol shows up at all.

Does anyone have any suggestions on how to instruct the class to only exclude the absolute last mapped item?

Answer №1

If you want the most optimal solution, consider this:

.users-overview-screen__no-last-child:last-child span:last-child {
    display: none;
}

Answer №2

Take a look at the updated code snippet below:

.display-flex div:last-child span {
    display: none;
}
<div class="display-flex">
  <span>Mapped Item 1</span>
  <div class=“users-overview-screen__no-last-child”>
    <span>|</span>
  </div>
  <span>Mapped Item 2</span>
  <div class=“users-overview-screen__no-last-child”>
    <span>|</span>
  </div>
    <span>Mapped Item 3</span>
  <div class=“users-overview-screen__no-last-child”>
    <span>|</span>
  </div>
</div>

Answer №3

Give this a shot:

.flexible-display div:last-child{
  visibility: hidden;
}

Answer №4

When it comes to styling, you have a few options available. Here are two of them:

.users-overview-screen__no-last-child {
    &:last-of-type { /* targets the last element wrapping the item */
        &:last-child { /* removes the last element of the last item*/
            display: none;
        }
    }
}

However, I believe the most effective approach would be to apply a border-right and padding to each span item, while removing the carets or separators for a cleaner look. This method is more straightforward and visually appealing:

.display-flex {
    display: flex;
}

span {
    padding: 0 10px;
    box-sizing: border-box;
    border-right: 1px solid black;
}
    
span:last-of-type {
  border-right: none;
}
<div class="display-flex">
  <span>Mapped Item 1</span>
  <span>Mapped Item 2</span>
  <span>Mapped Item 3</span>
</div>

The above CSS code in SCSS format:

.display-flex {
    display: flex;

    span {
        padding: 0 10px;
        box-sizing: border-box;
        border-right: 1px solid black;
    }

    &:last-of-type {
      border-right: none;
    }
}

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

What causes @typescript-eslint to retain old types/files in its cache and prevent successful compilation?

When I kick off my Typescript application using tsc -b -w, I always encounter an issue with @typescript-eslint not reacting to file changes accurately. It flags invalid types/syntax errors where there are none. Restarting the process sometimes doesn't ...

Using ReactJS and Ruby on Rails to implement an AJAX delete request

There is a component named Items residing inside a parent component called ItemsContainer. A click on a button within the Items component triggers an Ajax function to delete that specific Item. However, I am encountering a 500 error message at the moment ...

Creating a versatile function in TypeScript for performing the OR operation: A step-by-step guide

Is there a way in TypeScript to create a function that can perform an OR operation for any number of arguments passed? I currently have a function that works for 2 arguments. However, I need to make it work for any number of arguments. export const perfo ...

When iterating through a table, an error occurs stating that the property "rows" is not available on type HTMLElement (

Issue Error TS2339 - Property 'rows' does not exist on type HTMLElement when looping through table in Angular 7 Encountering error when trying to loop through HTML table in Angular 7 Currently working with Angular 7 and facing an error while ...

Shape with smoothed corners

Is there a way to create rectangles with rounded corners using CSS, SVG, or other methods while maintaining straight horizontal and vertical sides? I attempted to use border-radius for the rounded corners, but found that it created curved edges on both ho ...

Challenge with sending CSV file as attachment on response in Express not resolving

Hey there, I've been facing an issue with sending a response from a post request that includes downloading an attachment. Although the response is correct, I don't see any download or save-as pop-up. I have verified that the headers are set corre ...

Images appear as plain text in the preview of VUE 3 (with TypeScript)

Currently, I am developing a Portfolio website and have encountered an issue. While everything runs smoothly with npm run dev, the problem arises when I use npm run preview. In this scenario, some of the images within the files named 3dModellingFiles.ts do ...

The useCallback hooks persist outdated values when the list is refreshed

Why am I not getting the expected values every time the function onRefresh is called using Hooks? Example when onRefresh is called twice: Expected values: true 0 20 false true 0 20 false Received values: false 0 0 false false 20 20 false Initial st ...

Locked Position Feature in Material-UI Data Grid

Is there a way to make a column sticky in Mui x data grid using the community version? I know it can be done in the pro version, but I prefer to stick with the community version. I have attempted to override the original CSS without success. Any suggestion ...

Using Boolean functions in ngStyle allows for dynamic styling of elements in Angular templates

<div *ngFor= “ let singleorder of order.order”> <p [ngStyle]=" 'color':(singleorder.status === 'CONFIRM' )? 'green' : 'red' , 'background' : (singleorder.status === ' ...

Guide on updating Redux state using the callback function of react-token-auth

I've been working on a project with react-token-auth recently, where it is configured and exported from a top-level function. The challenge I'm facing now is how to store the user's name, email, etc. in Redux for easy access across the entir ...

When my script is located in the head of the HTML page, I am unable to

My goal is to make my JavaScript code function properly when I insert it into either the head or body elements of an HTML document. Let's look at some examples: First, I insert the script into the body as shown in this example (works correctly): ...

The overflow:hidden feature is ineffective in Firefox, yet functions properly in both IE and Chrome browsers

I'm in the process of creating a webshop and facing an issue with my sidebar due to varying product counts on different pages. For now, I have implemented the following workaround: padding-bottom: 5000px; margin-bottom: -5000px; overflow: ...

Learn how to cycle through three different texts that appear in the same spot using smooth transitions

I am working with three different rows that contain the Typography component. My goal is to display the first row, followed by the second, then the third, and back to the first one in a continuous loop. All three rows should be shown in the same location, ...

Unusual ways that backgrounds and borders behave while incorporating CSS transitions for changes in height, positions, and transformations

I was under the impression that I had come up with a brilliant idea... I wanted to create a button with a hover effect where the background would do a wipe transition (top to bottom, left to right) using soft color transitions. My plan was to achieve this ...

Using MatTableDataSource in a different Angular component

I am working with two components, namely Order and OrderDetail. Within the Order component, I have a MatTableDataSource that gets populated from a service. OrderComponent Prior to the constructor listData: MatTableDataSource<DocumentDetailModel>; ...

What could be the reason my SVG images are not displaying?

My website contains SVGs that are not loading consistently. I acquired some from freeicons/icon8 and created a couple using Inkscape. Initially, I inserted them using HTML but switched to CSS after doing research online on how to ensure they load properly ...

Missing Calendar functionality in React MUI Datepicker/Calendar

Currently, I am attempting to utilize the MUI's always-on onscreen calendar component. However, I encountered an issue stating that there is no such component in the pickers folder. Interestingly, I came across some solutions mentioning that Material ...

PrimeFaces: Achieving Conditional Coloring Based on Status (Passed/Failed)

Attempting to dynamically change the color of a column based on its status value (Passed/Failed/InProgress) using jQuery. I tried copying and pasting the table into jsfiddle, where it worked. However, when implemented in the actual XHTML file, the jQuery ...

Adding custom validations for React Material UI controls is a breeze with these simple steps

My ReactJs application requires validation on all fields, such as name, phone number, email, password, and confirm password. Validation messages should be displayed when invalid data is entered in these fields. import React from 'react'; imp ...