How can different styles be seamlessly combined when customizing Fabric components?

Imagine I am enhancing a Fabric component by adding custom styles and wishing to combine any additional styles passed in through its props. Here's the solution I've devised:

const { TextField, Fabric , IButtonProps, mergeStyleSets } = window.Fabric;

const MyTextField = (props: IButtonProps) => {
  const  { styles, ...otherProps } = props;

  const myStyles = stylesProps => {
    // props.styles can be a function, an object or undefined
    const stylesAsObject = typeof (styles) === "function" ? styles(stylesProps) : styles;
    return mergeStyleSets({ root: { maxWidth: 250 }, field: { backgroundColor: "pink"}}, stylesAsObject);
  };

  return <TextField styles={myStyles} {...otherProps} />;
}

const TextFieldExample () => (<MyTextField readOnly value="My text field" styles={{field: { fontWeight: 600}}} />
  );

ReactDOM.render(<Fabric><TextFieldExample /></Fabric>, document.getElementById('content'));

This method works, yet it seems somewhat lengthy.

I wonder if there exists a version of mergeStylesets that would allow me to simply write:

const myStyles = mergeStylesets({ root: { maxWidth: 250 }, field: { backgroundColor: "pink"}}, props.styles);

Answer №1

I came across a new function called concatStyleSetsWithProps:

const { TextField, Fabric , IButtonProps, concatStyleSetsWithProps } = window.Fabric;

const MyTextField = (props: IButtonProps) => {
  return <TextField  {...props} styles={stylesProps => concatStyleSetsWithProps(props, { root: { maxWidth: 250 }, field: { backgroundColor: "pink"}}, props.styles)} />;
}

const TextFieldExample= () => (<MyTextField readOnly value="My text field" styles={{field: { fontWeight: 600}}} />);

const ExampleWrapper = () => <Fabric><TextFieldExample /></Fabric>;
ReactDOM.render(<ExampleWrapper />, document.getElementById('content'))

https://codepen.io/onlyann/pen/yLNXvPd?editors=1111

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

Utilizing Tailwind CSS for creating a responsive layout on a Next.js application

I'm just getting started with Tailwind CSS and I decided to build the user interface of my nextjs application using a "mobile first" approach like everyone suggests. I got the flex direction and background color working perfectly on mobile screens, so ...

Mastering the Art of Unit Testing React Components

In my current project, I am using the testing tools Jest and Enzyme to test a React component. This component relies on a third-party React UI library called Ant Design (Antd). To better illustrate my question, I have provided a simplified version of the c ...

Show a dropdown menu based on a certain condition in Angular

Is there a way to conditionally display select options like this? <select id="updateType" class="form-control" formControlName="updateType"> <option value="personalDetails">Personal</option> <option value="addressD ...

Transferring information between a parent and child component

I recently started learning reactjs and I encountered an issue with data communication between two components. In my application, I am using a material table to display the data. The create user form is located in a separate component. Whenever a new user ...

The issue with Nextjs where CSS does not render properly during Server Side Rendering

After upgrading React 17 to React 18 in my Next.js project, I encountered an issue where the CSS is not rendering on Server-Side Rendering. Has anyone else faced this problem and how did you resolve it? _app.tsx: import { AppProvider } from "contexts ...

"Put Jest to the test by running it with the Express

Currently, I am in the process of learning how to build an API with TypeScript and experimenting with testing it using the Jest framework. When utilizing a basic Express application like app = express() supertest(app) everything works smoothly. However, ...

To generate an npm package, you must convert several ES6 files into multiple ES5 files

I have developed various React components. Within my project folder, I have a collection of ES6 files including: components/A.js components/Button.js My goal is to import these components into my future projects in the following way: import A from &ap ...

Understanding the Issue: Why Doesn't Signing Up with an Existing Account Automatically Log In When Using the 'autoconfirm' Feature in Supabase Authentication?

When using the supabase.auth.signUp function in my code, I expected a logged-in session with "autoconfirm" enabled on the server. However, after signing up with an existing account, it didn't log me in as anticipated. Here's a snippet of the code ...

Setting up data in Firebase can be challenging due to its complex structure definition

https://i.stack.imgur.com/iclx7.png UPDATE: In my firebase structure, I have made edits to the users collection by adding a special list called ListaFavorite. This list will contain all the favorite items that users add during their session. The issue I a ...

Navigational bar with React and Next.js. Issue: Hydration unsuccessful due to inconsistencies between the initial UI and the server-rendered content

I am working on a project with Next.js and React. I've created a navbar component but I keep encountering the following error message multiple times: Error: Hydration failed because the initial UI does not match what was rendered on the server. Warni ...

Is TypeScript being converted to JavaScript with both files in the same directory?

As I begin my journey with TypeScript in my new Angular project, I find myself pondering the best approach for organizing all these JS and TS files. Currently, it appears that the transpiler is placing the .js files in the same directory as the correspondi ...

Tips for displaying Next.js dynamic paths on a static S3/CloudFront website

Help Needed with Next.js Setup on S3 and CloudFront While setting up Next.js with static website hosting on S3 and CloudFront, I have encountered an issue with dynamic routes. My directory structure is as follows: pages/ index.js about.js [id].js A ...

Using either Google Maps or a customized mapping solution in a React Native application

I am currently developing an application that allows users to input addresses and locate them on a map. I am wondering if implementing AI technology or utilizing specific libraries is necessary in order for this feature to function properly within a reac ...

The React Router test commences outside of the home page

While testing my App component, I encountered an issue during the second test. In the first test, the process begins from the home page where I click on a drink-preview to access a recipe. However, in the subsequent test, which is expected to follow the sa ...

Comparing SSR and CSR Approaches to Handling getInitialProps in _app.js with Next JS

I am currently working on developing a Next JS application that manages authentication and initial routing within the getInitialProps function. I have found that this method can be executed either server-side or client-side. My current approach involves ...

Create a dynamic list of checkboxes populated by API data and utilize React hooks to handle updates

I am looking to utilize hooks within my component that generates a list of checkboxes and allows for independent status updates for each checkbox. To accomplish this, I am currently utilizing useSelector() to retrieve the checkbox data obtained from the U ...

Combining Redux and Apollo Client in a React Application: Step-by-Step Guide

I'm currently developing a project and utilizing Apollo Client, GraphQL in React, and Redux for global state management. Although some may question the use of Redux, I find it necessary to handle user data during the login/register process and store i ...

Four unique chip/tag colors, personalized to suit your style

Currently, I have integrated two arrays into my autocomplete menu where the chip/tag color is either primary or secondary based on the array the selected component belongs to. I aim to include all four arrays in the menu (top10Songs, top10Artists, top10Fi ...

Encountering an issue in REACT (cra) where attempting to integrate jQuery into fullcalendar's "eventRender" results in the error message stating that "$el.popover is not a function."

Attempting to incorporate the popover option within calendar events, I followed the suggested method from fullcalendar docs. However, upon creating a callback function that utilizes the popover method alongside jQuery, an error emerged: TypeError: $el.p ...

Methods to validate CSS attributes specified within a class using React testing library

I am struggling to validate the CSS properties defined within a class in CSS using the React Testing Library. Unfortunately, I am facing challenges in doing so. Here are the simplified code snippets: import React from "react"; import { render, ...