radio.taro.tsx 4.1 KB
Newer Older
X
xiaoyatong 已提交
1 2 3 4 5 6 7
import React, {
  FunctionComponent,
  MouseEventHandler,
  useContext,
  useEffect,
  useState,
} from 'react'
O
oasis-cloud 已提交
8
import { CheckChecked, CheckNormal } from '@nutui/icons-react-taro'
X
xiaoyatong 已提交
9 10

import RadioContext from './context'
O
oasis-cloud 已提交
11
import RadioGroup from '@/packages/radiogroup/index.taro'
X
xiaoyatong 已提交
12

O
oasis-cloud 已提交
13
import { BasicComponent, ComponentDefaults } from '@/utils/typings'
14

X
xiaoyatong 已提交
15 16 17
type Shape = 'button' | 'round'
type Position = 'right' | 'left'

O
oasis-cloud 已提交
18
export interface RadioProps extends BasicComponent {
X
xiaoyatong 已提交
19 20 21 22 23 24 25
  className: string
  style: React.CSSProperties
  disabled: boolean
  checked: boolean
  shape: Shape
  textPosition: Position
  value: string | number | boolean
O
oasis-cloud 已提交
26 27
  iconName: React.ReactNode
  iconActiveName: React.ReactNode
X
xiaoyatong 已提交
28 29 30 31 32
  iconSize: string | number
  onChange: MouseEventHandler<HTMLDivElement>
}

const defaultProps = {
33
  ...ComponentDefaults,
34 35
  className: '',
  style: {},
X
xiaoyatong 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48
  disabled: false,
  checked: false,
  shape: 'round',
  value: '',
  textPosition: 'right',
  iconName: 'check-normal',
  iconActiveName: 'check-checked',
  iconSize: 18,
  onChange: (e) => {},
} as RadioProps
export const Radio: FunctionComponent<
  Partial<RadioProps> & React.HTMLAttributes<HTMLDivElement>
> & { RadioGroup: typeof RadioGroup } = (props) => {
49 50 51 52
  const { children } = {
    ...defaultProps,
    ...props,
  }
X
xiaoyatong 已提交
53 54 55 56 57 58 59 60 61 62 63
  const {
    className,
    disabled,
    checked,
    shape,
    textPosition,
    value,
    iconName,
    iconActiveName,
    iconSize,
    onChange,
64 65
    iconClassPrefix,
    iconFontClassName,
X
xiaoyatong 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
    ...rest
  } = props
  const componentName = 'nut-radio'
  const [checkedStatement, setCheckedStatement] = useState(checked)
  const [valueStatement, setValueStatement] = useState(value)
  const [disabledStatement, setDisabledStatement] = useState(disabled)
  const context = useContext(RadioContext)
  useEffect(() => {
    setDisabledStatement(disabled)
    setValueStatement(value)
    setCheckedStatement(checked)
  }, [disabled, value, checked])

  const renderLabel = () => {
    return (
      <div
        className={`${componentName}__label ${
          disabledStatement ? `${componentName}__label--disabled` : ''
        }`}
      >
        {children}
      </div>
    )
  }
  const renderButton = () => {
    return (
      <div
        className={`${componentName}__button ${
94 95 96
          !disabledStatement &&
          checkedStatement &&
          `${componentName}__button--active`
X
xiaoyatong 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
        } ${disabledStatement ? `${componentName}__button--disabled` : ''}`}
      >
        {children}
      </div>
    )
  }
  const color = () => {
    if (disabledStatement) {
      return 'nut-radio__icon--disable'
    }
    if (checkedStatement) {
      return 'nut-radio__icon'
    }
    return 'nut-radio__icon--unchecked'
  }
  const renderIcon = () => {
    const { iconName, iconSize, iconActiveName } = props

O
oasis-cloud 已提交
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
    if (!disabledStatement && checkedStatement) {
      return React.isValidElement(iconActiveName) ? (
        React.cloneElement<any>(iconActiveName, {
          size: iconSize,
          className: color(),
        })
      ) : (
        <CheckChecked width={iconSize} height={iconSize} className={color()} />
      )
    }
    return React.isValidElement(iconName) ? (
      React.cloneElement<any>(iconName, {
        size: iconSize,
        className: color(),
      })
    ) : (
      <CheckNormal width={iconSize} height={iconSize} className={color()} />
X
xiaoyatong 已提交
132 133
    )
  }
134
  const reverseState = textPosition === 'left'
X
xiaoyatong 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
  const renderRadioItem = () => {
    if (shape === 'button') {
      return renderButton()
    }
    if (reverseState) {
      return [renderLabel(), renderIcon()]
    }
    return [renderIcon(), renderLabel()]
  }
  const handleClick: MouseEventHandler<HTMLDivElement> = (e) => {
    if (disabledStatement || checkedStatement) return
    setCheckedStatement(!checkedStatement)
    onChange && onChange(e)
    context && context.onChange(valueStatement)
  }
150

X
xiaoyatong 已提交
151
  return (
152 153 154 155 156 157 158
    <div
      className={`nut-radio ${className} ${
        reverseState ? `${componentName}--reverse` : ''
      }`}
      onClick={handleClick}
      {...rest}
    >
X
xiaoyatong 已提交
159 160 161 162 163 164 165 166
      {renderRadioItem()}
    </div>
  )
}

Radio.defaultProps = defaultProps
Radio.displayName = 'NutRadio'
Radio.RadioGroup = RadioGroup