React Button コンポーネントを使用すると、React アプリでアクションをトリガーするクリック可能な要素を有効にできます。ボタンのバリアントの設定方法、ラップされた要素のスタイルの構成方法、およびサイズの定義方法を完全に制御できます。Button コンポーネントは、React Button クリックされたコールバック、React ボタンの切り替え、React ボタンの無効化などを通じて柔軟性を提供します。
EXAMPLE
TSX
ButtonOverviewStyle.css
index.css
import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import './ButtonOverviewStyle.css';
import { IgrButton, IgrButtonModule } from "@infragistics/igniteui-react";
import 'igniteui-webcomponents/themes/light/bootstrap.css';
IgrButtonModule.register();
export default class ButtonOverview extends React.Component<any, any> {
constructor(props: any) {
super(props);
}
public render(): JSX.Element {
return (
<div className="container sample">
<div className="button-container">
<IgrButton variant="flat"><span>Flat</span></IgrButton>
<IgrButton variant="contained"><span>Contained</span></IgrButton>
<IgrButton variant="outlined"><span>Outlined</span></IgrButton>
</div>
</div>
);
}
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<ButtonOverview/>);
tsx
.button-container {
display: flex;
justify-content: space-evenly;
margin-top: 20px;
}
css
このサンプルが気に入りましたか? 完全な Ignite UI for Reactツールキットにアクセスして、すばやく独自のアプリの作成を開始します。無料でダウンロードできます。
使用方法
まず、次のコマンドを実行して、対応する Ignite UI for React npm パッケージをインストールする必要があります:
npm install igniteui-react
cmd
次に、以下のように、IgrButton
とそれに必要な CSS をインポートし、そのモジュールを登録する必要があります:
import { IgrButtonModule, IgrButton } from 'igniteui-react';
import 'igniteui-webcomponents/themes/light/bootstrap.css';
IgrButtonModule.register();
tsx
<IgrButton />
tsx
Prefix / Suffix
IgrButton
コンポーネントの prefix
スロットと suffix
スロットを使用すると、ボタンのメイン コンテンツの前後に異なるコンテンツを追加できます。
<IgrButton type="button" variant="contained">
<span slot="prefix">+</span>Click me<span slot="suffix">-</span>
</IgrButton>
tsx
タイプ
href
属性が設定されている場合、ボタン コンポーネントはその内部構造を <button>
から <a>
タイプの要素に変更します。その場合、ボタンは通常のリンクと考えることができます。href
属性を設定すると、rel
、target
および download
属性も設定できます。
ボタン コンポーネントが実際の <button>
要素を内部で使用する場合、プロパティを次のいずれかの値に設定することで、その displayType
を指定できます。
Submit
- フォーム データを送信する場合
reset
- フォーム データを初期値にリセットする場合
button
- ウェブページのどこかにカスタム機能を備えたボタンを追加する場合
Contained ボタン
variant
を使用して、コンポーネント テンプレートにシンプルな contained ボタンを追加します。バリアントを設定しない場合、デフォルトでは contained に設定されることに注意してください。
<IgrButton variant="contained"><span>Contained</span></IgrButton>
tsx
import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import { IgrButton, IgrButtonModule } from "@infragistics/igniteui-react";
import 'igniteui-webcomponents/themes/light/bootstrap.css';
IgrButtonModule.register();
export default class ButtonContained extends React.Component<any, any> {
constructor(props: any) {
super(props);
}
public render(): JSX.Element {
return (
<div className="container sample">
<IgrButton variant="contained"><span>Contained</span></IgrButton>
</div>
);
}
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<ButtonContained/>);
tsx
Outlined ボタン
outlined
ボタンを作成するために必要なのは、variant
プロパティの値を変更することだけです。
<IgrButton variant="outlined"><span>Outlined</span></IgrButton>
tsx
import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import { IgrButton, IgrButtonModule } from "@infragistics/igniteui-react";
import 'igniteui-webcomponents/themes/light/bootstrap.css';
IgrButtonModule.register();
export default class ButtonOutlined extends React.Component<any, any> {
constructor(props: any) {
super(props);
}
public render(): JSX.Element {
return (
<div className="container sample">
<IgrButton variant="outlined"><span>Outlined</span></IgrButton>
</div>
);
}
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<ButtonOutlined/>);
tsx
Flat ボタン
同様に、flat
バリアントに切り替えることができます。
<IgrButton variant="flat"><span>Flat</span></IgrButton>
tsx
import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import { IgrButton, IgrButtonModule } from "@infragistics/igniteui-react";
import 'igniteui-webcomponents/themes/light/bootstrap.css';
IgrButtonModule.register();
export default class ButtonFlat extends React.Component<any, any> {
constructor(props: any) {
super(props);
}
public render(): JSX.Element {
return (
<div className="container sample">
<IgrButton variant="flat"><span>Flat</span></IgrButton>
</div>
);
}
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<ButtonFlat/>);
tsx
Floating Action ボタン
variant
プロパティを fab
に設定することで、フローティング アクション ボタンを作成できます。
<IgrButton variant="fab"><span>Fab</span></IgrButton>
tsx
import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import { IgrButton, IgrButtonModule } from "@infragistics/igniteui-react";
import 'igniteui-webcomponents/themes/light/bootstrap.css';
IgrButtonModule.register();
export default class ButtonFab extends React.Component<any, any> {
constructor(props: any) {
super(props);
}
public render(): JSX.Element {
return (
<div className="container sample">
<IgrButton variant="fab">
<span slot="prefix">+</span>
<span>Add</span>
</IgrButton>
</div>
);
}
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<ButtonFab/>);
tsx
ボタンのサイズ設定
ユーザーは、CSS 変数 --ig-size
を使用して button
コンポーネントのサイズを変更できます。次の例では、すべてのサイズ値を表示するためのラジオ ボタンをいくつか追加します。このようにして、選択されるたびにボタンの size プロパティを変更します。
import { IgrButton, IgrRadio, IgrRadioGroup, IgrButtonModule, IgrRadioModule, IgrRadioGroupModule } from 'igniteui-react';
<IgrRadioGroup alignment="horizontal" style={{display: 'flex', margin: '0 auto', width: '15%'}}>
<IgrRadio name="size" value="small" labelPosition="after" checked={true} change={this.onRadioChange}>
<span>Small</span>
</IgrRadio>
<IgrRadio name="size" value="medium" labelPosition="after" change={this.onRadioChange}>
<span>Medium</span>
</IgrRadio>
<IgrRadio name="size" value="large" labelPosition="after" change={this.onRadioChange}>
<span>Large</span>
</IgrRadio>
</IgrRadioGroup>
<div>
<IgrButton ref={this.flatButtonRef} className="flat-btn" variant="flat"><span>Flat</span></IgrButton>
<IgrButton ref={this.containedButtonRef} className="contained-btn" variant="contained"><span>Contained</span></IgrButton>
<IgrButton ref={this.outlinedButtonRef} className="outlined-btn" variant="outlined"><span>Outlined</span></IgrButton>
<IgrButton ref={this.fabButtonRef} className="fab-btn" variant="fab"><span>Like</span></IgrButton>
</div>
public onRadioChange(e: any) {
this.flatButton.size = e.value;
this.containedButton.size = e.value;
this.outlinedButton.size = e.value;
this.fabButton.size = e.value;
}
tsx
上記のコードを実装した結果は、次のようになります:
EXAMPLE
TSX
ButtonSizingStyle.css
index.css
import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import './ButtonSizingStyle.css';
import { IgrButton, IgrRadio, IgrRadioGroup, IgrButtonModule, IgrRadioModule, IgrRadioGroupModule } from "@infragistics/igniteui-react";
import 'igniteui-webcomponents/themes/light/bootstrap.css';
IgrButtonModule.register();
IgrRadioGroupModule.register();
IgrRadioModule.register();
export default class ButtonSize extends React.Component<any, any> {
constructor(props: any) {
super(props);
this.onRadioChange = this.onRadioChange.bind(this);
this.state = { size: "medium"};
}
public render(): JSX.Element {
return (
<div className="container sample">
<IgrRadioGroup alignment="horizontal" style={{display: 'flex', margin: '0 auto', width: '15%'}}>
<IgrRadio name="size" value="small" labelPosition="after" checked={true} change={this.onRadioChange}>
<span>Small</span>
</IgrRadio>
<IgrRadio name="size" value="medium" labelPosition="after" change={this.onRadioChange}>
<span>Medium</span>
</IgrRadio>
<IgrRadio name="size" value="large" labelPosition="after" change={this.onRadioChange}>
<span>Large</span>
</IgrRadio>
</IgrRadioGroup>
<div className="button-container">
<IgrButton className={'size-' + this.state.size} variant="flat"><span>Flat</span></IgrButton>
<IgrButton className={'size-' + this.state.size} variant="contained"><span>Contained</span></IgrButton>
<IgrButton className={'size-' + this.state.size} variant="outlined"><span>Outlined</span></IgrButton>
<IgrButton className={'size-' + this.state.size} variant="fab"><span>Like</span></IgrButton>
</div>
</div>
);
}
public onRadioChange(e: any) {
if (e.checked == true) {
this.setState({ size: e.value });
}
}
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<ButtonSize/>);
tsx
.button-container {
display: flex;
justify-content: space-evenly;
margin-top: 20px;
}
.size-small {
--ig-size: var(--ig-size-small);
}
.size-medium {
--ig-size: var(--ig-size-medium);
}
.size-large {
--ig-size: var(--ig-size-large);
}
css
ダウンロード
download
プロパティを設定すると、リンクされた URL に移動する代わりに、保存するように求められます。
<IgrButton
href=""
variant="contained"
download="url"
target="_blank" >
<span>Download</span>
</IgrButton>
tsx
import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import { IgrButton, IgrButtonModule } from "@infragistics/igniteui-react";
import 'igniteui-webcomponents/themes/light/bootstrap.css';
IgrButtonModule.register();
export default class ButtonDownload extends React.Component<any, any> {
constructor(props: any) {
super(props);
}
public render(): JSX.Element {
return (
<div className="container sample">
<IgrButton href="" variant="contained" download="url" target="_blank" ><span>Download</span></IgrButton>
</div>
);
}
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<ButtonDownload/>);
tsx
スタイル設定
IgrButton
コンポーネントは、スタイル設定に使用できる 3 つの CSS パーツを公開します。
名前 |
説明 |
base |
igc-button コンポーネントのネイティブ ボタン要素。 |
prefix |
igc-button コンポーネントのプレフィックス コンテナー。 |
suffix |
igc-button コンポーネントのサフィックス コンテナー。 |
base
CSS パーツを使用すると、ラップされた要素 (<button>
または <a>
) のスタイルを設定できます。
igc-button::part(base) {
background-color: var(--ig-primary-500);
color: var(--ig-primary-500-contrast);
padding: 18px;
}
css
EXAMPLE
TSX
ButtonStyle.css
index.css
import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import './ButtonStyle.css';
import { IgrButton, IgrButtonModule } from "@infragistics/igniteui-react";
import 'igniteui-webcomponents/themes/light/bootstrap.css';
IgrButtonModule.register();
export default class ButtonStyling extends React.Component<any, any> {
constructor(props: any) {
super(props);
}
public render(): JSX.Element {
return (
<div className="container sample">
<IgrButton variant="contained"><span>Contained</span></IgrButton>
</div>
);
}
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<ButtonStyling/>);
tsx
igc-button::part(base) {
color: #011627;
}
igc-button::part(base)::before {
background-color: #e99221;
padding: 18px;
}
css
API リファレンス
その他のリソース