Skip to content

fix(radio): lint fixed #2635

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 1 commit into from
Oct 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
68 changes: 24 additions & 44 deletions src/packages/radio/radio.taro.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ export const Radio: FunctionComponent<
Partial<RadioProps> & Omit<React.HTMLAttributes<HTMLDivElement>, 'onChange'>
> & { Group: typeof RadioGroup } = (props) => {
const classPrefix = 'nut-radio'

const {
children,
className,
Expand Down Expand Up @@ -80,30 +79,6 @@ export const Radio: FunctionComponent<
}
}
}

const renderLabel = () => {
return (
<div
className={classNames(`${classPrefix}-label`, {
[`${classPrefix}-label-disabled`]: disabled,
})}
>
{children}
</div>
)
}
const renderButton = () => {
return (
<div
className={classNames(`${classPrefix}-button`, {
[`${classPrefix}-button-active`]: checkedStatement,
[`${classPrefix}-button-disabled`]: disabled,
})}
>
{children}
</div>
)
}
const color = () => {
return {
[`${classPrefix}-icon-disabled`]: disabled,
Expand All @@ -116,7 +91,6 @@ export const Radio: FunctionComponent<
if (disabled && !checkedStatement) {
return <CheckDisabled className={classNames(color())} />
}

if (checkedStatement) {
return React.isValidElement(activeIcon) ? (
React.cloneElement<any>(activeIcon, {
Expand All @@ -136,37 +110,43 @@ export const Radio: FunctionComponent<
<CheckNormal className={classNames(color())} />
)
}
const renderByShape = (shape: RadioShape) => {
return shape === 'button' ? (
renderButton()
) : (
const renderLabel = () => {
const labelcls = classNames(`${classPrefix}-label`, {
[`${classPrefix}-label-disabled`]: disabled,
})
return (
<>
{renderIcon()}
{renderLabel()}
<div className={labelcls}>{children}</div>
</>
)
}
const renderButton = () => {
const buttoncls = classNames(`${classPrefix}-button`, {
[`${classPrefix}-button-active`]: checkedStatement,
[`${classPrefix}-button-disabled`]: disabled,
})
return <div className={buttoncls}>{children}</div>
}
const renderByShape = (shape: RadioShape) => {
return shape === 'button' ? renderButton() : renderLabel()
}
const renderRadioItem = () => {
return renderByShape(context && context.shape ? context.shape : shape)
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

考虑使用可选链操作符

静态分析工具 Biome 建议在这行使用可选链操作符。这可以提高代码的健壮性,防止在 contextnullundefined 时出现错误。

建议修改为:

return shape === 'button' ? renderButton() : renderLabel()
🧰 Tools
🪛 Biome

[error] 135-135: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)

}
const handleClick: MouseEventHandler<HTMLDivElement> = (e) => {
if (disabled || checkedStatement) return
setCheckedStatement(!checkedStatement)
}

const cls = classNames(
classPrefix,
{
[`${classPrefix}-reverse`]: labelPosition === 'left',
},
className
)
return (
<div
className={classNames(
classPrefix,
{
[`${classPrefix}-reverse`]: labelPosition === 'left',
},
className
)}
style={style}
onClick={handleClick}
{...rest}
>
<div className={cls} style={style} onClick={handleClick} {...rest}>
{renderRadioItem()}
</div>
)
Expand Down
68 changes: 24 additions & 44 deletions src/packages/radio/radio.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ export const Radio: FunctionComponent<
Partial<RadioProps> & Omit<React.HTMLAttributes<HTMLDivElement>, 'onChange'>
> & { Group: typeof RadioGroup } = (props) => {
const classPrefix = 'nut-radio'

const {
children,
className,
Expand Down Expand Up @@ -76,30 +75,6 @@ export const Radio: FunctionComponent<
}
}
}

const renderLabel = () => {
return (
<div
className={classNames(`${classPrefix}-label`, {
[`${classPrefix}-label-disabled`]: disabled,
})}
>
{children}
</div>
)
}
const renderButton = () => {
return (
<div
className={classNames(`${classPrefix}-button`, {
[`${classPrefix}-button-active`]: checkedStatement,
[`${classPrefix}-button-disabled`]: disabled,
})}
>
{children}
</div>
)
}
const color = () => {
return {
[`${classPrefix}-icon-disabled`]: disabled,
Expand All @@ -112,7 +87,6 @@ export const Radio: FunctionComponent<
if (disabled && !checkedStatement) {
return <CheckDisabled className={classNames(color())} />
}

if (checkedStatement) {
return React.isValidElement(activeIcon) ? (
React.cloneElement<any>(activeIcon, {
Expand All @@ -132,37 +106,43 @@ export const Radio: FunctionComponent<
<CheckNormal className={classNames(color())} />
)
}
const renderByShape = (shape: RadioShape) => {
return shape === 'button' ? (
renderButton()
) : (
const renderLabel = () => {
const labelcls = classNames(`${classPrefix}-label`, {
[`${classPrefix}-label-disabled`]: disabled,
})
return (
<>
{renderIcon()}
{renderLabel()}
<div className={labelcls}>{children}</div>
</>
)
}
const renderButton = () => {
const buttoncls = classNames(`${classPrefix}-button`, {
[`${classPrefix}-button-active`]: checkedStatement,
[`${classPrefix}-button-disabled`]: disabled,
})
return <div className={buttoncls}>{children}</div>
}
const renderByShape = (shape: RadioShape) => {
return shape === 'button' ? renderButton() : renderLabel()
}
const renderRadioItem = () => {
return renderByShape(context && context.shape ? context.shape : shape)
}
const handleClick: MouseEventHandler<HTMLDivElement> = (e) => {
if (disabled || checkedStatement) return
setCheckedStatement(!checkedStatement)
}

const cls = classNames(
classPrefix,
{
[`${classPrefix}-reverse`]: labelPosition === 'left',
},
className
)
return (
<div
className={classNames(
classPrefix,
{
[`${classPrefix}-reverse`]: labelPosition === 'left',
},
className
)}
style={style}
onClick={handleClick}
{...rest}
>
<div className={cls} style={style} onClick={handleClick} {...rest}>
{renderRadioItem()}
</div>
)
Expand Down
21 changes: 10 additions & 11 deletions src/packages/radiogroup/radiogroup.taro.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ export const RadioGroup = React.forwardRef(
...rest
} = { ...defaultProps, ...props }

const cls = classNames(
classPrefix,
{
[`${classPrefix}-${props.direction}`]: props.direction,
},
className
)

const [val2State, setVal2State] = usePropsValue<string | number>({
defaultValue: props.defaultValue,
value: props.value,
Expand All @@ -72,7 +80,7 @@ export const RadioGroup = React.forwardRef(
/>
)
})
}, [options])
}, [options, labelPosition, val2State])

return (
<RadioContext.Provider
Expand All @@ -89,16 +97,7 @@ export const RadioGroup = React.forwardRef(
},
}}
>
<div
className={classNames(
classPrefix,
{
[`${classPrefix}-${props.direction}`]: props.direction,
},
className
)}
{...rest}
>
<div className={cls} {...rest}>
{options?.length ? renderOptionsChildren() : children}
</div>
</RadioContext.Provider>
Expand Down
21 changes: 10 additions & 11 deletions src/packages/radiogroup/radiogroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ export const RadioGroup = React.forwardRef(
...rest
} = { ...defaultProps, ...props }

const cls = classNames(
classPrefix,
{
[`${classPrefix}-${props.direction}`]: props.direction,
},
className
)

const [val2State, setVal2State] = usePropsValue<string | number>({
defaultValue: props.defaultValue,
value: props.value,
Expand All @@ -72,7 +80,7 @@ export const RadioGroup = React.forwardRef(
/>
)
})
}, [options])
}, [options, labelPosition, val2State])

return (
<RadioContext.Provider
Expand All @@ -89,16 +97,7 @@ export const RadioGroup = React.forwardRef(
},
}}
>
<div
className={classNames(
classPrefix,
{
[`${classPrefix}-${props.direction}`]: props.direction,
},
className
)}
{...rest}
>
<div className={cls} {...rest}>
{options?.length ? renderOptionsChildren() : children}
</div>
</RadioContext.Provider>
Expand Down
Loading