I'm building a form with many TextField elements.
In the form, there are other elements than TextField elements along with TextField elements.
The problem is that they look quite different from TextFields.
Take a look at the example below.
https://codesandbox.io/s/material-ui-demo-forked-p8s87?file=/demo.js:488-520
As you can see, InputLabel doesn't work well with the element other than Input, while FormLabel doesn't look like the right alternative to InputLabel.
What I exactly want is to replace Input as Link, other elements staying the same as with Input. What is the best practice to achieve this?
Main difference with InputLabel and FormLabel is that the InputLabel component adds an animation for us. The FormLabel component does not.
There are four different inputs that we can use in Material-UI, InputBase Input OutlinedInput and FilledInput.
The FormControl component in Material-UI is a wrapper class for an input component. It is good to use a form control when you are using any input components in Material-UI, like a checkbox, radio button, or a switch.
Hence it is good practice to use FormControl with TextField if you don't need some animation on your input control in form like "on focus", "leave focus" etc.
For more details understanding refere this link
Why dont you use Button
You can use it like this:
import { Button } from "#material-ui/core";
<FormControl>
<FormLabel shrink>hi</FormLabel>
<Button color="primary" href="/link">hello</Button>
</FormControl>
<FormControl>
<FormLabel shrink>hi</FormLabel>
<Button variant="contained" color="primary" href="/link">hello</Button>
</FormControl>
<FormControl>
<FormLabel shrink>hi</FormLabel>
<Button variant="outlined" color="primary" href="/link">hello</Button>
</FormControl>
You can play around with color & variant but it'll give you a nice look for Link
Personally recommend you to check out Rsuite Library for other such UI components.
Related
We have some legacy code that I'm working to refactor styling in.
Right now, there are 3+ files (each representing a page within a wizard) with a styling constant at the top of each file feeding into TextFields and Buttons. Each of these constants have mostly the same content.
I'd like to put the common styling in another, separate file and then apply those styles across each page in the wizard that uses them.
Initially, I thought about using themes, but the use case may not be conducive to it. In each of the three files/pages, I'm seeing pairs of TextFields with different styles applied to them:
<div>
<TextField
id="upload-name"
label="Upload Name"
value={this.state.label}
onChange={this.handleChange('label')}
margin="normal"
variant="outlined"
className="form-input"
InputLabelProps={{
shrink: true,
}}
/>
</div>
<div>
<TextField
id="upload-description"
label="Description"
value={this.state.description}
onChange={this.handleChange('description')}
multiline={true}
margin="normal"
variant="outlined"
className="form-input"
InputLabelProps={{
shrink: true,
}}
InputProps={{
classes:{
root: classes.textArea
}
}}
/>
</div>
Specifically, the lower TextField's getting two different classes in two different ways. I've been trying to figure out if there's a way of passing all classes needed from a CSS module but neither the approach above nor root: styles.textArea seem to work.
I have an array that is getting data from an API, but I want to style each button differently so that the background colours differ. I am also using Material UI... Here is my code -
{options.map((data, id) => (
<Button className='p-3 md:w-[25wh] md:h-[25vh]' onClick={handleClickAnswer} fullWidth variant="contained">{decode(data)}</Button>
))}
Please tell me how to style them
There are several ways in which you can assign different styles to different buttons. It can be done majorly using string literals.
Few examples,
If you are using bootstrap/tailwind and your styling will majorly consist of that, then you can something like
{options.map((data, id) => (
<Button
className=`p-${id} md:w-[25wh] md:h-[25vh]`
onClick={handleClickAnswer}
fullWidth
variant="contained">
{decode(data)}
</Button>
))}
Notice p-${id} would get modified for each particular element in the array
Even if bootstrap isn't being used, you can use the above method, create custom classNames to assign styles to each button separately.
If you need to specifically assign a style to the first/last element, you can use first child selector (or) last child selector.
I use ngbootstrap for popovers, but I want to override all the default styles it comes with. I have a form that should be displayed as a popover on a button click which has its own styles.
When I use [ngbPopover] it renders an element with the default class of 'popover' applied, instead of overriding each of its properties to align with my expectation, is it possible to remove it all together while rendering on the page and then I could use a custom class with popoverClass property.
<ng-template #popContent><user-form></user-form></ng-template>
<button type="button" class="btn btn-outline-secondary" [ngbPopover]="popContent">
I've got markup and bindings in my popover!
</button>
Looking into the popover source code I see lots of classes nailed without a chance to change them. I suppose the only promising approach would be exclude the css for the popover component from the import.
How to do it depends on how you import the Bootstrap css
I am trying to add a custom css class directive to my button, but whenever I do that, the angular material design goes away and the button goes back to its default ugly state. How can I make sure that my custom css class gets added to the button without it removing the mat-buutton effects?
<button
mat-button
[class]="todoItems.completed ? 'delete': null"
(click)="deleteTodo(todoItems._id)"
[disabled]="!todoItems.completed">
<mat-icon >delete</mat-icon>
</button>
Here is the css
.delete:hover {
color: red
}
I want my class to be applied only if the completed property is true and when that property is true the delete button will turn red when I hover on it.
The hovering part works fine but the problem I have is that the material design disappears and it turns into the default style of button
mat-button applies its own css classes to the <button> at run time.
When you are doing [class]="todoItems.completed ? 'delete': null" it is hard overridding these classes.
To append your class to the existing ones, you need to use ngClass instead.
In https://github.com/ant-design/ant-design, how it the recommend method of adding spacing (margin) between buttons?
In semantic-ui, default margins defined within the library CSS. In bootstrap, one can use button groups to add spacing between buttons.
Is adding custom css or inline css the recommended way of achieving margins between buttons? Ideally, I want avoid writing any css when using a css framework
You can use https://ant.design/components/space/ to put spaces between your buttons or any other elements.
You can achieve that by simply wrapping your <Button> with <Space>
import { Button, Space } from 'antd';
<Space>
<Button>Default</Button>
<Button type="primary">Primary</Button>
</Space>
Two quick and dirty tricks
If you just want a tiny space in between, you can change
</Button><Button>
or
</Button>
<Button>
into
</Button> <Button>
and it will solve the problem
Alternatively put a {' '} in between, e.g. </Button> {' '} <Button>
I use styled components and components become really as components no global css, images and icons.
I define styles like this:
import styled from 'styled-components'
export default styled.div`
> button + button {
margin-left: 16px;
}
`
usually in a separate file for example: Controls.js
Then I use it as a component where it knows how to place the buttons (space between is 16px)
import React from 'react'
import Controls from './Controls'
import {Button} from 'antd'
export const Form = () =>
<Controls>
<Button type="primary" icon="check">
Accpet
</Button>
<Button type="danger" icon="close">
Cencel
</Button>
</Controls>
You can use ant design grid system.
Then use gutter property for Row component. That will add spacing between grids.
Put your buttons inside of a div. It will act as a button group.