Skip to content

Radio 单选框

单选框用于在多个选项中选取一个。

前置条件

在react项目入口文件中引入样式,默认为src/main.tsx

tsx
import 'ono-react-element/dist/style/Radio.css'

单个单选框用法

tsx
import React, { useState } from'react'
import { Radio } from 'ono-react-element'

function App() {
  const [checked, setChecked] = useState(false)

  return (
    <div style={{ width: '100%' }}>
      <Radio
        value="javascript"
        checked={checked}
        onChange={e => setChecked(e.target.checked)}
      >
        JavaScript
      </Radio>
    </div>
  )
}

export default App;

多个单选框用法

tsx
import React, { useState } from'react'
import { RadioGroup } from 'ono-react-element'

function App() {
  const [language, setLanguage] = useState<string>('html')

  const languageList = [
    {
      value: 'html',
      label: 'HTML'
    },
    { value: 'css', label: 'CSS' },
    { value: 'js', label: 'JavaScript' }
  ]

  return (
    <div style={{ width: '100%' }}>
      <RadioGroup
        value={language}
        options={languageList}
        onChange={value => setLanguage(value as string)}
      />
    </div>
  )
}

export default App;

自定义多个单选框用法

tsx
import React, { useState } from'react'
import { RadioGroup } from 'ono-react-element'

function App() {
  const [language, setLanguage] = useState<string>('vue')

  const languageList = [
    {
      label: 'vue',
      value: 'vue'
    },
    {
      label: 'react',
      value: 'react'
    },
    {
      label: 'angular',
      value: 'angular'
    }
  ]

  return (
    <div style={{ width: '100%' }}>
      <RadioGroup
        radioW={24}
        value={language}
        options={languageList}
        checkedColor="#f00"
        unCheckedColor="#fff"
        labelPosition="left"
        style={{
          fontSize: '24px',
          fontWeight: 700,
          gap: 8,
          flexDirection: 'column'
        }}
        radioGap={16}
        onChange={value => setLanguage(value as string)}
      />
    </div>
  )
}

export default App;

API

Radio

参数说明类型默认值是否必填
value关联 Radio 选项的值T-
nameRadio 组的namestring-
checked指定当前是否选中boolean-
childrenlabel中显示的文字boolean-
disabled是否禁用booleanfalse
radioWRadio的宽度string|number16
radioGapRadio和label之间的gapstring|number4
classNameRadio和label父元素的类名string-
styleRadio和label父元素的样式CSSProperties-
checkColorRadio选中时的颜色string'#532ce1'
unCheckColorRadio未选中时的颜色string'transparent'
onChange当 Radio 的值发送改变时触发(e: React.ChangeEvent<HTMLInputElement>) => void-

RadioGroup

参数说明类型默认值是否必填
value关联 Radio 选项的值T-
nameRadio 组的namestring-
optionsRadio 选项列表RadioItemType-
radioWRadio的宽度string|number16
radioGapRadio和label之间的gapstring|number4
styleRadioGroup的样式CSSProperties-
classNameRadioGroup的样式的类名string-
checkColorRadio选中时的颜色string'#532ce1'
unCheckColorRadio未选中时的颜色string'transparent'
labelPositionRadio的标签位置'left'|'right''left'
onChange当 Radio 的值发送改变时触发(value: T) => void-

RadioItemType

参数说明类型默认值是否必填
valueRadio 选项的值T-
labelRadio 选项的文字string-
disabled禁用函数boolean|(value: string|number|boolean) => boolean-