Dropdown option containing two columns of Fluent UI text elements

Is there a way to customize a dropdown option in Fluent UI with two columns?

The issue I'm facing is that I need single-digit numbers to align properly with 2-digit or 3-digit numbers:

Link to see the problem visually

I believe having two columns and using "flex-end" for number alignment in the first column, while keeping text aligned normally in the second, could be a potential solution. Any thoughts on this approach?

This is the code snippet I use to map the dropdown options:

    let mainGroupOptions = Object.keys(dropDownOptions)
        .map(key => {
            return { key, text: dropDownOptions[key].value + " - " + dropDownOptions[key].label, value: dropDownOptions[key].value };
        });

Below is the HTML code for the Dropdown component:

                        <Dropdown
                            label="Main Group*"
                            options={mainGroupOptions}
                            selectedKey={mainGroupSelected}
                            errorMessage={errors.mainGroup ? "Main group required" : ''}
                            onChange={(e, option) => { setMainGroupSelected(option.key); deleteErrors(errors, errors.mainGroup) }}>
                        </Dropdown>

Answer №1

Not entirely certain about the specific format requirements, but hopefully this will provide some guidance in the right direction. The key here is to customize the styles in order to achieve the desired outcome.

import {useState} from 'react';
import {
    Dropdown,
    makeStyles,
    Option,
  } from "@fluentui/react-components";

  const useStyles=makeStyles({
    option:{
        width:"1200px",
    },
    left:{
        float:"left",
        textAlign:"right",
        width:"20%",
    },
    right:{
        float:"right",
        textAlign:"left",
        width:"70%",
        marginLeft:"10px"
    },
  });

  let mainGroupOptions = [
    {
      key: '0',
      value:"General"
    },
    {
      key: '00',
      value:"Chamber of Commerce Law and Industry Organization ..."
    },
    {
      key: '0000',
      value:"General"
    },
  ];

export default function FormatSelect(){
    const c=useStyles();
    const {mainGroupSelected, setMainGroupSelected} = useState(0);
    return (
        <Dropdown
            label="Main Group*"
            selectedKey={mainGroupSelected}
            onChange={(e, option) => { setMainGroupSelected(option.key); deleteErrors(errors, errors.mainGroup) }}
        >
            {mainGroupOptions.map((option)=>(
                <Option key={option.key} >
                    <div className={c.option}>
                        <div className={c.left}>{option.key}</div>
                        <div className={c.right}>{option.value}</div>
                    </div>
                </Option>
            ))}
        </Dropdown>
    );
  }

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 is the best approach to restructuring route endpoints into an ORM framework?

As a newcomer to Angular, I find myself uncertain about what exactly qualifies as an endpoint and how it can be transformed into ORM. My current understanding suggests that an endpoint is where data is retrieved from a server, whether it's through an ...

Unlock the Power of Typography in React with Material UI Styled Components

Exploring the world of material UI is a new experience for me. I am currently in the process of creating a styled component using Typography. Below is what I have attempted so far: import styled from 'styled-components'; import { FormGroup, ...

GitLab's CI/CD pipeline is unable to detect ts-node even though it has already been

As a newcomer to setting up GitLab CI, I am currently venturing into implementing a small web application using the MERN pipeline (MongoDB, express, react, Nodejs). My goal is to create a simple project akin to google drive where users can store and manage ...

Error: Unable to locate the reference for "foo" while utilizing TypeScript in combination with Webpack

My Chrome extension is functioning properly when using JavaScript alone. However, when attempting to incorporate TypeScript with Webpack, I encountered an issue where the function foo could not be found. Uncaught ReferenceError: foo is not defined Here ...

A function in Typescript is created to handle diverse input types in a generic manner

My goal is to create a function that can handle various input types for abstraction purposes. type ContentA = string type ContentB = number type InputA = { name: 'method_a' content: ContentA } type InputB = { name: 'method_b' con ...

Having difficulty transferring types to and from a custom module?

I'm currently faced with an issue while working on a typescript module within a larger monorepo. I am having difficulty importing types from one package into another via node modules. The types are located at ./types, and my package.json contains a ke ...

Choose the property category

Is there a more efficient way to specify the type of a property in TypeScript without resorting to casting? Take a look at this example: interface Overlay { type: "modal" | "drawer" other?: number } const rec = { obj1: { ty ...

Setting up Jest to run in WebStorm for a Vue project with TypeScript can be done through

I am struggling to run all my tests within WebStorm. I set up a project with Babel, TypeScript, and Vue using vue-cli 3.0.0-rc3. My run configuration looks like this: https://i.stack.imgur.com/7H0x3.png Unfortunately, I encountered the following error: ...

Converting hierarchical JSON to nested dropdowns in Laravel

I am currently facing some issues with Nested Set... When I execute the following code: $selects = Category::defaultOrder()->withDepth()->get()->toTree(); It returns the following JSON: https://pastebin.com/Cw4TbWyA What I am trying to achieve ...

Changing the function to operate asynchronously

How can I convert the following code into an asynchronous function? It is currently returning referralUrl as undefined: controller async createReferralUrls() { this.referralUrl = await this.referralService.generateReferralUrl(this.userData.referral ...

Ways to decrease the size of this item while maintaining its child components?

Here is an object that I am working with: { "name": "A", "children": [ { "name": "B", "open": false, "registry": true, "children": [ { ...

Having difficulty retrieving the file from Google Drive through googleapis

I'm currently attempting to retrieve a file from Google Drive using the Googleapis V3, but I keep encountering an error message stating: 'Property 'on' does not exist on type 'GaxiosPromise<Schema$File>'. Below is the c ...

The element type 'ReactElement<any>' does not match a JSX element constructor function. 'undefined' cannot be assigned to type 'Element | null'

After attempting the suggested fix of deleting node_modules/ and yarn.lock, then reinstalling everything, I still cannot resolve the issue. I am currently developing a basic router that renders children based on a certain prop: import React, { Fragment } ...

The front end is displaying an empty object, although it is showing up when logged in the console using ngFor

I'm currently enrolled in a tutorial course and I've hit a roadblock at this stage. I want to display the properties of the Product on both the console.log and the web page. Strangely, the console.log is showing the product correctly, but the fro ...

Is there a way to transfer data to a different component in React without relying on a hierarchical parent-child relationship?

I am struggling to pass the data from the "SearchingData" Component to the "Search" Component. The SearchingData component is a child of the Search component. I need to transfer the data from the variable named "datacame" to the Search Component. Can som ...

The webpack "externals" option is reporting an error due to an unexpected character "@" being used

I've come across several discussions regarding the issue of "Unexpected character '@' in webpack, but none of them address the specific problem I'm facing with the externals option. I've used this feature in the past without any is ...

Nativescript encountered an error due to an undefined variable called FIRAuth

I'm currently working on a project using Nativescript. While everything runs smoothly with Firebase on the local emulator, I encounter errors when testing the application on my iPhone. The specific error message is: CONSOLE LOG file:///app/vendor.js ...

Obtaining the names of class decorators in TypeScript source code

I've been utilizing the TypeScript compiler API to analyze source code and extract class references. Within the Node object that represents the class definition, there is an array of decorators, but I'm struggling to retrieve the name of each de ...

Is it possible to continuously re-render a React Functional Component with Axios and useState/useEffect?

Seeking assistance with creating a React Functional Component using Typescript to fetch data from an API and pass it to another component. However, encountering the error message "Error: Too many re-renders. React limits the number of renders to prevent an ...

Determining the return type depending on an optional generic parameter

I'm encountering difficulty translating the numerous function overloading examples from SO to my specific scenario: const createAccessor = <T, >(defaultValue: T) => { const value = defaultValue function fetch(): T; function fetc ...