Switch

版本要求:
@ray-js/ray≥0.5.10

描述

开关选择器,用于在两种互斥状态间切换。

属性

属性类型必填默认值说明
checkedboolean否-当前是否选中
disabledboolean否false是否禁用
colorstring否#007AFF开关颜色,同 CSS 的 color
type"switch" | "checkbox"否switch样式类型,可选 switch、checkbox
namestring否-表单控件名称,提交表单时作为键名
否-选中状态改变时触发,detail 含 value

合法值

type
值说明最低版本
switch开关样式-
checkbox复选框样式-

示例代码

基础用法

import React from 'react';
import { Switch, View } from '@ray-js/ray';

export default function () {
  return (
    <View style={{ padding: '20px' }}>
      <Switch
        onChange={(e) => {
          console.log('Switch 改变:', e.detail.value);
        }}
      />
    </View>
  );
}

不同状态

import React, { useState } from 'react';
import { Switch, View, Text } from '@ray-js/ray';

export default function () {
  const [checked, setChecked] = useState(false);

  return (
    <View style={{ padding: '20px' }}>
      <View style={{ marginBottom: '16px' }}>
        <Text>受控开关:</Text>
        <Switch
          checked={checked}
          onChange={(e) => {
            console.log('Switch 改变:', e.detail.value);
            setChecked(e.detail.value);
          }}
          style={{ marginTop: '8px' }}
        />
        <Text style={{ marginTop: '8px' }}>当前状态: {checked ? '开' : '关'}</Text>
      </View>
      <View style={{ marginBottom: '16px' }}>
        <Text>自定义颜色:</Text>
        <Switch color="#ff0000" checked style={{ marginTop: '8px' }} />
      </View>
      <View style={{ marginBottom: '16px' }}>
        <Text>禁用状态:</Text>
        <Switch disabled style={{ marginTop: '8px' }} />
      </View>
      <View style={{ marginBottom: '16px' }}>
        <Text>Checkbox 类型:</Text>
        <Switch type="checkbox" checked style={{ marginTop: '8px' }} />
      </View>
    </View>
  );
}