# 按钮 Button

按钮是一种命令组件，可发起一个即时操作。

Source: https://sd-design.js.org/components/button/

### 基本用法

按钮分为 `primary` - **主要按钮**、`secondary` - **次要按钮（默认）**、`dashed` - **虚线按钮**、`outline` - **线形按钮**、`text` - **文本按钮**五种类型。

示例代码

文件: src/button-basic.vue

```vue
<template>
  <sd-space>
    <sd-button type="primary">Primary</sd-button>
    <sd-button>Secondary</sd-button>
    <sd-button type="dashed">Dashed</sd-button>
    <sd-button type="outline">Outline</sd-button>
    <sd-button type="text">Text</sd-button>
  </sd-space>
</template>
```

### 禁用状态

按钮的禁用状态。

示例代码

文件: src/button-disabled.vue

```vue
<template>
  <sd-space direction="vertical">
    <sd-space>
      <sd-button type="primary" disabled>Primary</sd-button>
      <sd-button disabled>Default</sd-button>
      <sd-button type="dashed" disabled>Dashed</sd-button>
      <sd-button type="outline" disabled>Outline</sd-button>
      <sd-button type="text" disabled>Text</sd-button>
    </sd-space>
    <sd-space>
      <sd-button type="primary" status="success" disabled>Primary</sd-button>
      <sd-button status="success" disabled>Default</sd-button>
      <sd-button type="dashed" status="success" disabled>Dashed</sd-button>
      <sd-button type="outline" status="success" disabled>Outline</sd-button>
      <sd-button type="text" status="success" disabled>Text</sd-button>
    </sd-space>
    <sd-space>
      <sd-button type="primary" status="warning" disabled>Primary</sd-button>
      <sd-button status="warning" disabled>Default</sd-button>
      <sd-button type="dashed" status="warning" disabled>Dashed</sd-button>
      <sd-button type="outline" status="warning" disabled>Outline</sd-button>
      <sd-button type="text" status="warning" disabled>Text</sd-button>
    </sd-space>
    <sd-space>
      <sd-button type="primary" status="danger" disabled>Primary</sd-button>
      <sd-button status="danger" disabled>Default</sd-button>
      <sd-button type="dashed" status="danger" disabled>Dashed</sd-button>
      <sd-button type="outline" status="danger" disabled>Outline</sd-button>
      <sd-button type="text" status="danger" disabled>Text</sd-button>
    </sd-space>
  </sd-space>
</template>
```

### 组合按钮

通过 `<sd-button-group>` 组件使按钮以组合方式出现。可用在同级多项操作中。

示例代码

文件: src/button-group.vue

```vue
<template>
  <sd-space direction="vertical">
    <sd-button-group>
      <sd-button>Publish</sd-button>
      <sd-button>
        <template #icon>
          <icon-down />
        </template>
      </sd-button>
    </sd-button-group>
    <sd-button-group>
      <sd-button>Publish</sd-button>
      <sd-button>
        <template #icon>
          <icon-more />
        </template>
      </sd-button>
    </sd-button-group>
    <sd-button-group>
      <sd-button type="primary">
        <icon-left />
        Prev
      </sd-button>
      <sd-button type="primary">
        Next
        <icon-right />
      </sd-button>
    </sd-button-group>
    <sd-space size="large">
      <sd-button-group type="primary">
        <sd-button> copy </sd-button>
        <sd-button> cut </sd-button>
        <sd-button> find </sd-button>
      </sd-button-group>
      <sd-button-group type="primary" status="warning">
        <sd-button>
          <template #icon><icon-heart-fill /></template>
        </sd-button>
        <sd-button>
          <template #icon><icon-star-fill /></template>
        </sd-button>
        <sd-button>
          <template #icon><icon-thumb-up-fill /></template>
        </sd-button>
      </sd-button-group>
      <sd-button-group size="small" disabled>
        <sd-button> prev </sd-button>
        <sd-button> next </sd-button>
      </sd-button-group>
    </sd-space>
  </sd-space>
</template>
```

### 图标按钮

按钮可以嵌入图标。在只设置图标时，按钮的宽高相等。

示例代码

文件: src/button-icon.vue

```vue
<template>
  <sd-space>
    <sd-button type="primary">
      <template #icon>
        <icon-plus />
      </template>
    </sd-button>
    <sd-button type="primary">
      <template #icon>
        <icon-delete />
      </template>
      <!-- Use the default slot to avoid extra spaces -->
      <template #default>Delete</template>
    </sd-button>
  </sd-space>
</template>

<script setup lang="ts">
  import { IconPlus, IconDelete } from '@sdata/web-vue/es/icon/index.js';
</script>
```

### 加载中状态

通过设置 `loading` 可以让按钮处于加载中状态。处于加载中状态的按钮不会触发点击事件。

示例代码

文件: src/button-loading.vue

```vue
<template>
  <sd-space>
    <sd-button type="primary" loading>Primary</sd-button>
    <sd-button loading>Default</sd-button>
    <sd-button type="dashed" loading>Dashed</sd-button>
    <sd-button type="primary" :loading="loading1" @click="handleClick1">Click Me</sd-button>
    <sd-button type="primary" :loading="loading2" @click="handleClick2">
      <template #icon>
        <icon-plus />
      </template>
      Click Me
    </sd-button>
  </sd-space>
</template>

<script setup lang="ts">
  import { ref } from 'vue';

  import { IconPlus } from '@sdata/web-vue/es/icon/index.js';

  const loading1 = ref(false);
  const loading2 = ref(false);

  function handleClick1() {
    loading1.value = !loading1.value;
  }

  function handleClick2() {
    loading2.value = !loading2.value;
  }
</script>
```

### 长按钮

通过设置 `long` 属性，使按钮的宽度跟随容器的宽度。

示例代码

文件: src/button-long.vue

```vue
<template>
  <sd-space class="wrapper" direction="vertical">
    <sd-button type="primary" long>Primary</sd-button>
    <sd-button long>Default</sd-button>
    <sd-button type="dashed" long>Dashed</sd-button>
    <sd-button type="outline" long>Outline</sd-button>
    <sd-button type="text" long>Text</sd-button>
  </sd-space>
</template>

<style scoped lang="scss">
  .wrapper {
    width: 400px;
    padding: 20px;
    border: 1px solid var(--color-border);
    border-radius: 4px;
  }
</style>
```

### 按钮形状

按钮分为 `square` - **长方形（默认）**、`circle` - **圆形**、`round` - **全圆角**三种形状。

示例代码

文件: src/button-shape.vue

```vue
<template>
  <sd-space>
    <sd-button type="primary">Square</sd-button>
    <sd-button type="primary" shape="round">Round</sd-button>
    <sd-button type="primary">
      <template #icon>
        <icon-plus />
      </template>
    </sd-button>
    <sd-button type="primary" shape="circle">
      <icon-plus />
    </sd-button>
  </sd-space>
</template>
<script setup lang="ts">
  import { IconPlus } from '@sdata/web-vue/es/icon/index.js';
</script>
```

### 按钮尺寸

按钮分为 `mini`、`small`、`medium`、`large` 四种尺寸。高度分别为：`24px`、`28px`、`32px`、`36px`。推荐（默认）尺寸为 `medium`。可在不同场景及不同业务需求选择适合尺寸。

示例代码

文件: src/button-size.vue

```vue
<template>
  <sd-space>
    <sd-button type="primary" size="mini">Mini</sd-button>
    <sd-button type="primary" size="small">Small</sd-button>
    <sd-button type="primary">Medium</sd-button>
    <sd-button type="primary" size="large">Large</sd-button>
  </sd-space>
</template>
```

### 按钮状态

按钮的状态分为 `normal` - **正常（默认）**、`success` - **成功**、`warning` - **警告**、`danger` - **危险**四种，可以与按钮类型同时使用。

示例代码

文件: src/button-status.vue

```vue
<template>
  <sd-space direction="vertical">
    <sd-space>
      <sd-button type="primary" status="success">Primary</sd-button>
      <sd-button status="success">Default</sd-button>
      <sd-button type="dashed" status="success">Dashed</sd-button>
      <sd-button type="outline" status="success">Outline</sd-button>
      <sd-button type="text" status="success">Text</sd-button>
    </sd-space>
    <sd-space>
      <sd-button type="primary" status="warning">Primary</sd-button>
      <sd-button status="warning">Default</sd-button>
      <sd-button type="dashed" status="warning">Dashed</sd-button>
      <sd-button type="outline" status="warning">Outline</sd-button>
      <sd-button type="text" status="warning">Text</sd-button>
    </sd-space>
    <sd-space>
      <sd-button type="primary" status="danger">Primary</sd-button>
      <sd-button status="danger">Default</sd-button>
      <sd-button type="dashed" status="danger">Dashed</sd-button>
      <sd-button type="outline" status="danger">Outline</sd-button>
      <sd-button type="text" status="danger">Text</sd-button>
    </sd-space>
  </sd-space>
</template>
```

## API

### `<button>` Props

| 参数名 | 描述 | 类型 | 默认值 |
| --- | --- | --- | :-: |
| type | 按钮的类型，分为五种：次要按钮、主要按钮、虚框按钮、线性按钮、文字按钮。 | `ButtonTypes` | `'secondary'` |
| shape | 按钮的形状 | `BorderShape` | `-` |
| status | 按钮的状态 | `'normal' \| 'warning' \| 'success' \| 'danger'` | `'normal'` |
| size | 按钮的尺寸 | `'mini' \| 'small' \| 'medium' \| 'large'` | `'medium'` |
| long | 按钮的宽度是否随容器自适应。 | `boolean` | `false` |
| loading | 按钮是否为加载中状态 | `boolean` | `false` |
| disabled | 按钮是否禁用 | `boolean` | `false` |
| html-type | 设置 `button` 的原生 `type` 属性，可选值参考 [HTML标准](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attr-type '_blank') | `HTMLButtonElement['type']` | `'button'` |
| autofocus | 设置 `button` 的原生 `autofocus` 属性，可选值参考 [HTML标准](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attr-type '_blank') | `boolean` | `false` |
| href | 设置跳转链接。设置此属性时，按钮渲染为a标签。 | `string` | `-` |

### `<button>` Events

| 事件名 | 描述           | 参数             |
| ------ | -------------- | ---------------- |
| click  | 点击按钮时触发 | ev: `MouseEvent` |

### `<button>` Slots

| 插槽名 | 描述 | 参数 |
| ------ | :--: | ---- |
| icon   | 图标 | -    |

### `<button-group>` Props

| 参数名 | 描述 | 类型 | 默认值 |
| --- | --- | --- | :-: |
| type | 按钮的类型，分为五种：次要按钮、主要按钮、虚框按钮、线性按钮、文字按钮。 | `ButtonTypes` | `-` |
| status | 按钮的状态 | `'normal' \| 'warning' \| 'success' \| 'danger'` | `-` |
| shape | 按钮的形状 | `BorderShape` | `-` |
| size | 按钮的尺寸 | `'mini' \| 'small' \| 'medium' \| 'large'` | `-` |
| disabled | 全部子按钮是否禁用 | `boolean` | `false` |