'element.current.scrollHeight not returning real height (React, CSS)

I have a <div> container that will be populated with some tags as the user selects options on a dropdown. If the tags fill the space I designated for the container, I want to display a "+N" tag with the remaining number of selected options.

Expected output: the isOverflowing condition sets to true.

Actual output: isOverflowing is never set to true.

CODE

const [selectedOptions, setSelectedOptions] = useState<number[]>([]);
const [overflowing, setOverflowing] = useState(false);

const tagsRef = useRef<HTMLDivElement>(null);

const isOverflowing = (element: any) => {
  return element.current?.scrollHeight > element.current?.clientHeight; // NEVER RETURNS TRUE
};

useLayoutEffect(() => {
  if (isOverflowing(tagsRef)) {
    setOverflowing(true);
  } else {
    setOverflowing(false);
  }
}, [tagsRef, selectedOptions]);

...

return (
  <TagsWrapper ref={tagsRef} className="tagsWrapper">
    {selectedOptions.map((option, index) => (
      ...
    ))}
  </TagsWrapper>
)

CSS

.tagsWrapper {
  display: flex;
  flex-flow: row wrap;
  max-height: 32px;
  max-width: calc(100% - 50px - 16px - 4px);
  overflow: hidden;
}

Console logging the tagsRef always shows that scrollHeight and clientHeight are the same, when in fact they are not (I can see that the div is taller when I inspect it on the browser).

I've tried many combinations of max-height, height... to no avail. If the overflow property is set to visible it works, obviously, but I don't want to show the remaining tags... Is there a solution I'm not seeing?

Thanks!



Solution 1:[1]

I managed to see my own problem. The max-height and overflow properties should be on the parent element of overflowing container:

<TagsContainer>
  <TagsWrapper ref={tagsRef} className="tagsWrapper">
    {selectedOptions.map((option, index) => (
      ...
    ))}
  </TagsWrapper>
</TagsContainer>
.tagsContainer {
  max-height: 32px;
  overflow: hidden;
}
.tagsWrapper {
  display: flex;
  flex-flow: row wrap;
  ...
}

Solution 2:[2]

Are you asking how to delete multiple records or how to get multiple records from a list box? Combobox allows a single selection.

Assumptions:

  1. we are using multiselect ListBox (not Combobox) to show a list of people
  2. each item is an instance of object Person with id and name
  3. we will create a comma separated list of selected people using id
  4. add System.Linq to using statement

showing examples in c#, but concept is similar in VB.net

Person class

  public class Person {
        public int? id { get; set; }
        public string? name { get; set; }

        public override string ToString() {
            return $"{name} ({id})";
        }
    }

Fill list

    private void FillList() {
        listBox1.Items.Add(new Person { id = 1, name = "KUMAR" });
        listBox1.Items.Add(new Person { id = 2, name = "KISHAN" });
    }

Make string of selected items

    private void button2_Click(object sender, EventArgs e) {
        var l = listBox1.SelectedItems.OfType<Person>().Select(p => p.id).ToArray();
        string selectedIds = string.Join(",", l);
    }

modify DELETE

DELETE [EMP] WHERE [SNO] IN(@SNOLIST)

use resulting selectedIds in your query

cmd.Parameters.Add("@SNO", selectedIds)

Make sure to check if anything is selected before running delete.


For names use the following:

build list of names (note that each gets wrapped into ', if value has ' it gets escaped as '', so for instance O'Connor becomes O''Connor

private void button2_Click(object sender, EventArgs e) {
        var l = listBox1.SelectedItems.OfType<Person>().Select(p => ("'" + p.name?.Replace("'", "''") + "'")).ToArray();
        string selectedNames = string.Join(",", l);
}

modify DELETE

DELETE [EMP] WHERE [Name] IN (@selectedNames)

use selectedNames when adding parameter

cmd.Parameters.Add("@SNO", selectedNames)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Helena Sánchez
Solution 2