Switch 开关

基本用法

```schema: scope=”body” { “type”: “form”, “api”: “/api/mock2/form/saveForm”, “body”: [ { “name”: “switch”, “type”: “switch”, “label”: “开关”, “option”: “开关说明” } ] }

  1. ## 配置真假值
  2. 默认情况:
  3. - 开关打开时,表单项值为:true
  4. - 开关关闭时,表单项值为:false
  5. ```schema: scope="body"
  6. {
  7. "type": "form",
  8. "debug": true,
  9. "body": [
  10. {
  11. "name": "switch",
  12. "type": "switch",
  13. "label": "开关"
  14. }
  15. ]
  16. }

如果你想调整这个值,可以配置trueValuefalseValue

```schema: scope=”body” { “type”: “form”, “debug”: true, “body”: [ { “name”: “switch”, “type”: “switch”, “label”: “开关”, “trueValue”: 1, “falseValue”: 0 } ] }

  1. 调整开关,观察数据域变化,会发现打开后值为`1`,而关闭后为`0`
  2. ## 配置开启和关闭状态的文本
  3. > 1.1.5 版本之后支持
  4. ```schema: scope="body"
  5. {
  6. "type": "form",
  7. "body": [
  8. {
  9. "name": "switch",
  10. "type": "switch",
  11. "onText": "已开启飞行模式",
  12. "offText": "已关闭飞行模式"
  13. }
  14. ]
  15. }

默认值

和其它表单项一样,如果要设置默认值,可以使用 value 属性

```schema: scope=”body” { “type”: “form”, “debug”: true, “body”: [ { “name”: “switch”, “type”: “switch”, “label”: “开关”, “value”: false } ] }

  1. ## 不同尺寸
  2. > 2.0.0 及以上版本
  3. ```schema: scope="body"
  4. {
  5. "type": "form",
  6. "api": "/api/mock2/form/saveForm",
  7. "body": [
  8. {
  9. "name": "switch",
  10. "type": "switch",
  11. "label": ""
  12. },
  13. {
  14. "name": "switch-sm",
  15. "type": "switch",
  16. "label": "",
  17. "size": "sm"
  18. }
  19. ]
  20. }

属性表

除了支持 普通表单项属性表 中的配置以外,还支持下面一些配置

属性名 类型 默认值 说明
option string 选项说明
onText string / IconSchema 开启时开关显示的内容
offText string / IconSchema 关闭时开关显示的内容
trueValue boolean / string / number true 标识真值
falseValue boolean / string / number false 标识假值
size `”sm” \ “md”` "md" 开关大小

IconSchema 配置 | 属性名 | 类型 | 默认值 | 说明 | | ————— | ———— | ————- | —————— | | type | string | | icon | | icon | string | | icon 的类型 |

事件表

当前组件会对外派发以下事件,可以通过onEvent来监听这些事件,并通过actions来配置执行的动作,在actions中可以通过event.data.xxx事件参数变量来获取事件产生的数据,详细请查看事件动作

事件名称 事件参数 说明
change `event.data.value: string \ boolean` 开关值 开关值变化时触发

动作表

当前组件对外暴露以下特性动作,其他组件可以通过指定actionType: 动作名称componentId: 该组件id来触发这些动作,动作配置可以通过args: {动作配置项名称: xxx}来配置具体的参数,详细请查看事件动作

动作名称 动作配置 说明
setValue `value: string \ boolean` 更新的数据 更新数据