Skip to content

feat: radio adaptation #2721

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@
"author": "dsj"
},
{
"version": "2.0.0",
"version": "3.0.0",
"name": "Radio",
"type": "component",
"cName": "单选按钮",
Expand All @@ -599,7 +599,7 @@
"author": "oasis"
},
{
"version": "2.0.0",
"version": "3.0.0",
"name": "RadioGroup",
"type": "component",
"cName": "单选按钮组",
Expand Down
5 changes: 1 addition & 4 deletions src/packages/radio/demos/taro/demo8.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ import { Checklist } from '@nutui/icons-react-taro'

const Demo8 = () => {
return (
<Radio
icon={<Checklist />}
activeIcon={<Checklist style={{ color: 'red' }} />}
>
<Radio icon={<Checklist />} activeIcon={<Checklist color="red" />}>
自定义图标
</Radio>
)
Expand Down
4 changes: 2 additions & 2 deletions src/packages/radio/demos/taro/demo9.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ const Demo9 = () => {
>
<Radio
icon={<Checklist />}
activeIcon={<Checklist style={{ color: 'red' }} />}
activeIcon={<Checklist color="red" />}
value="1"
>
自定义图标
</Radio>
<Radio
icon={<Checklist />}
activeIcon={<Checklist style={{ color: 'red' }} />}
activeIcon={<Checklist color="red" />}
value="2"
>
自定义图标
Expand Down
14 changes: 12 additions & 2 deletions src/packages/radio/index.taro.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import { Radio } from './radio.taro'
import React from 'react'
import { Radio, RadioProps } from './radio.taro'
import { RadioGroup } from '../radiogroup/radiogroup.taro'

export type { RadioProps } from './radio.taro'
export type { RadioShape, RadioPosition } from './types'

export default Radio
type CompoundedComponent = React.FC<
Partial<RadioProps> & Omit<React.HTMLAttributes<HTMLDivElement>, 'onChange'>
> & {
Group: typeof RadioGroup
}
const InnerRadio = Radio as CompoundedComponent
InnerRadio.Group = RadioGroup

export default InnerRadio
14 changes: 12 additions & 2 deletions src/packages/radio/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { Radio } from './radio'
import React from 'react'
import { Radio, RadioProps } from './radio'
import { RadioGroup } from '../radiogroup/radiogroup'

export type { RadioProps } from './radio'
export type { RadioShape, RadioPosition } from './types'
export default Radio
type CompoundedComponent = React.FC<
Partial<RadioProps> & Omit<React.HTMLAttributes<HTMLDivElement>, 'onChange'>
> & {
Group: typeof RadioGroup
}
const InnerRadio = Radio as CompoundedComponent
InnerRadio.Group = RadioGroup

export default InnerRadio
1 change: 1 addition & 0 deletions src/packages/radio/radio.scss
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
border: 1px solid $color-text-disabled;
}
}

[dir='rtl'] .nut-radio,
.nut-rtl .nut-radio {
&:last-child {
Expand Down
25 changes: 10 additions & 15 deletions src/packages/radio/radio.taro.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import React, { FunctionComponent, useContext } from 'react'
import React, { FC, useContext } from 'react'
import {
CheckChecked,
CheckNormal,
CheckDisabled,
CheckNormal,
} from '@nutui/icons-react-taro'
import classNames from 'classnames'
import { View, ITouchEvent } from '@tarojs/components'
import classNames, { Mapping } from 'classnames'
import { ITouchEvent, View } from '@tarojs/components'
import RadioContext from '../radiogroup/context'
import RadioGroup from '@/packages/radiogroup/index.taro'
import { BasicComponent, ComponentDefaults } from '@/utils/typings'
Expand Down Expand Up @@ -34,9 +34,9 @@ const defaultProps = {
activeIcon: null,
onChange: (checked: boolean) => {},
} as RadioProps
export const Radio: FunctionComponent<
export const Radio: FC<
Partial<RadioProps> & Omit<React.HTMLAttributes<HTMLDivElement>, 'onChange'>
> & { Group: typeof RadioGroup } = (props) => {
> = (props) => {
const classPrefix = 'nut-radio'
const {
children,
Expand Down Expand Up @@ -80,7 +80,7 @@ export const Radio: FunctionComponent<
}
}
}
const color = () => {
const color = (): Mapping => {
return {
[`${classPrefix}-icon-disabled`]: disabled,
[`${classPrefix}-icon`]: !checkedStatement,
Expand Down Expand Up @@ -118,7 +118,7 @@ export const Radio: FunctionComponent<
return (
<>
{renderIcon()}
<div className={labelcls}>{children}</div>
<View className={labelcls}>{children}</View>
</>
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codebase verification

需要完成剩余的View组件替换

在代码库中发现还有未替换的div元素:

  • src/packages/radio/radio.tsx 中有3处需要替换:
    • <div className={labelcls}>{children}</div>
    • <div className={buttoncls}>{children}</div>
    • <div className={cls} style={style} onClick={handleClick} {...rest}>
  • src/packages/radio/demo.tsx 中有1处需要替换:
    • <div className="demo">

为确保Taro跨平台兼容性,这些div元素也需要替换为View组件。

🔗 Analysis chain

验证View组件替换的兼容性

div元素替换为Taro的View组件是必要的改动,但需要确保在不同平台上的表现一致。

Also applies to: 130-130, 152-152


Let's check the content of these files to understand if there are any remaining div elements that need to be replaced.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# 检查是否还有遗留的div元素
rg -l 'div' src/packages/radio/

Length of output: 88


Script:

#!/bin/bash
# Check the context of div usage in radio.tsx and demo.tsx
echo "=== radio.tsx ==="
rg -C 2 'div' src/packages/radio/radio.tsx

echo -e "\n=== demo.tsx ==="
rg -C 2 'div' src/packages/radio/demo.tsx

Length of output: 755

)
}
Expand All @@ -127,7 +127,7 @@ export const Radio: FunctionComponent<
[`${classPrefix}-button-active`]: checkedStatement,
[`${classPrefix}-button-disabled`]: disabled,
})
return <div className={buttoncls}>{children}</div>
return <View className={buttoncls}>{children}</View>
}
const renderByShape = (shape: RadioShape) => {
return shape === 'button' ? renderButton() : renderLabel()
Expand All @@ -149,12 +149,7 @@ export const Radio: FunctionComponent<
className
)
return (
<View
className={cls}
style={style}
onClick={handleClick}
// {...rest}
>
<View className={cls} style={style} onClick={handleClick}>
{renderRadioItem()}
</View>
)
Expand Down
2 changes: 1 addition & 1 deletion src/packages/radio/radio.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const defaultProps = {
} as RadioProps
export const Radio: FunctionComponent<
Partial<RadioProps> & Omit<React.HTMLAttributes<HTMLDivElement>, 'onChange'>
> & { Group: typeof RadioGroup } = (props) => {
> = (props) => {
const classPrefix = 'nut-radio'
const {
children,
Expand Down
7 changes: 4 additions & 3 deletions src/packages/radiogroup/radiogroup.taro.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { useCallback } from 'react'
import classNames from 'classnames'
import { View } from '@tarojs/components'
import {
RadioGroupDirection,
RadioGroupOption,
Expand Down Expand Up @@ -53,7 +54,7 @@ export const RadioGroup = React.forwardRef(
const cls = classNames(
classPrefix,
{
[`${classPrefix}-${props.direction}`]: props.direction,
[`${classPrefix}-${direction}`]: direction,
},
className
)
Expand Down Expand Up @@ -97,9 +98,9 @@ export const RadioGroup = React.forwardRef(
},
}}
>
<div className={cls} {...rest}>
<View className={cls} {...rest}>
{options?.length ? renderOptionsChildren() : children}
</div>
</View>
</RadioContext.Provider>
)
}
Expand Down
2 changes: 1 addition & 1 deletion src/packages/radiogroup/radiogroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export const RadioGroup = React.forwardRef(
const cls = classNames(
classPrefix,
{
[`${classPrefix}-${props.direction}`]: props.direction,
[`${classPrefix}-${direction}`]: direction,
},
className
)
Expand Down
Loading