Home > Software design >  Setting dropdown width
Setting dropdown width

Time:01-05

How to set the dropdown width to the longest string length in the list of dropdown options using ? To be implement in React JS

tried with React JS but unable to get the solution

CodePudding user response:

It is automatically done when you use and . Please provide some code maybe you have something is changing it...

CodePudding user response:

I am assuming you have an array of all dropdown value like this,

const menuValue = [
'Value',
'Value2222222222222222',
'Lorem Ipsum is simply dummy text of the printing and typesetting industry',
];

let say you want to set the width to the 3rd element of the menuValue array (longest element of that array)

for that you need to set your width like,

const length =
menuValue.reduce((text, newText) =>
  newText.length > text.length ? newText : text
).length * 10;

and pass it to the width like {width=`${length}px`}

please prefer this link for better understanding.

I hope it resolves your problem.

  • Related