Descripción general del botón React
The React Button Component lets you enable clickable elements that trigger actions in your React app. You get full control over how you set button variants, configure styles for the wrapped element, and define sizes. The Button Component also gives flexibility through the React Button clicked callback, toggle the React button, disable the React button, and more.
React Button Example
Uso
Primero, debes instalar el paquete npm Ignite UI for React correspondiente ejecutando el siguiente comando:
npm install igniteui-react
Entonces tendrás que importar elIgrButton CSS necesario y su contenido, así:
import { IgrButton } from 'igniteui-react';
import 'igniteui-webcomponents/themes/light/bootstrap.css';
<IgrButton />
Prefijo sufijo
With prefix and suffix slots of the IgrButton component, we can add different content before and after the main content of the button.
<IgrButton type="button" variant="contained">
<span slot="prefix">+</span>Click me<span slot="suffix">-</span>
</IgrButton>
Tipo
The button component will change its internal structure from a <button> to an <a> type element when the href attribute is set. In that case the button can be thought of as a regular link. Setting the href attribute will allow you to also set the rel, target and download attributes.
In the case when the button component uses an actual <button> element internally, we can specify its displayType by setting the property to any of the following values:
Submit- when we want to submit the form datareset- when we want to reset form data to its initial valuesbutton- when we want to add button with a custom functionality anywhere on a webpage
Variantes de botones
Botón contenido
Use the variant attribute to add a simple contained button in your component template. Note that if you do not set variant, by default it will be set to contained.
<IgrButton variant="contained"><span>Contained</span></IgrButton>
Botón delineado
All you have to do to create an outlined button is to change the value of the variant property:
<IgrButton variant="outlined"><span>Outlined</span></IgrButton>
Botón plano
Analogically, we can switch to flat variant.
<IgrButton variant="flat"><span>Flat</span></IgrButton>
Botón de acción flotante
We can create a floating action button by setting the variant property to fab:
<IgrButton variant="fab"><span>Fab</span></IgrButton>
Tamaño del botón
Users can change the size of the IgrButton using the --ig-size CSS variable. In the following example, we will add some radio buttons to display all size values. This way whenever one gets selected, we will change the size of the button.
import { IgrButton, IgrRadio, IgrRadioGroup } from 'igniteui-react';
const [size, setSize] = useState("small");
const onRadioChange = (e: IgrRadioChangeEventArgs) => {
setSize(e.detail.value);
};
<IgrRadioGroup alignment="horizontal" style={{ display: "flex", margin: "0 auto", width: "15%" }}>
<IgrRadio name="size" value="small" labelPosition="after" checked={size === "small"} onChange={onRadioChange}>
<span>Small</span>
</IgrRadio>
<IgrRadio name="size" value="medium" labelPosition="after" onChange={onRadioChange}>
<span>Medium</span>
</IgrRadio>
<IgrRadio name="size" value="large" labelPosition="after" onChange={onRadioChange}>
<span>Large</span>
</IgrRadio>
</IgrRadioGroup>
<div className="button-container">
<IgrButton className={"size-" + size} variant="flat">
<span>Flat</span>
</IgrButton>
<IgrButton className={"size-" + size} variant="contained">
<span>Contained</span>
</IgrButton>
<IgrButton className={"size-" + size} variant="outlined">
<span>Outlined</span>
</IgrButton>
<IgrButton className={"size-" + size} variant="fab">
<span>Like</span>
</IgrButton>
</div>
El resultado de implementar el código anterior debería verse similar al siguiente:
Descargar
Setting the download property will prompt the user to save the linked URL instead of navigating to it.
<IgrButton
href=""
variant="contained"
download="url"
target="_blank" >
<span>Download</span>
</IgrButton>
Estilismo
The IgrButton exposes three CSS parts which we can use for styling:
| Nombre | Descripción |
|---|---|
base |
El elemento button nativo del componente igc-button. |
prefix |
Contenedor de prefijos del componente igc-button. |
suffix |
Contenedor de sufijos del componente igc-button. |
The base CSS part allows us to style the wrapped element (<button> or <a>).
igc-button::part(base) {
background-color: var(--ig-primary-500);
color: var(--ig-primary-500-contrast);
padding: 18px;
}