Skip to content

Commit

Permalink
chore: extract function
Browse files Browse the repository at this point in the history
  • Loading branch information
oasis-cloud committed Dec 4, 2024
1 parent d16bb67 commit 9a10f48
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 30 deletions.
8 changes: 8 additions & 0 deletions src/packages/hoverbuttonitem/hoverbuttonitem.scss
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@
display: flex;
flex-direction: column;
align-items: center;

.nut-icon {
display: block;
width: 14px;
height: 14px;
font-size: 14px;
}
}
}

Expand All @@ -37,6 +44,7 @@
display: block;
width: 20px;
height: 20px;
font-size: 20px;
}
}

Expand Down
17 changes: 3 additions & 14 deletions src/packages/hoverbuttonitem/hoverbuttonitem.taro.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import classNames from 'classnames'
import { BaseEventOrig, ITouchEvent, View } from '@tarojs/components'
import { BasicComponent, ComponentDefaults } from '@/utils/typings'
import { harmony, harmonyAndRn } from '@/utils/platform-taro'
import { getIcon } from '@/packages/hoverbuttonitem/utils'

export interface HoverButtonItemProps extends BasicComponent {
icon?: React.ReactNode
Expand Down Expand Up @@ -51,15 +52,7 @@ export const HoverButtonItem = (props: Partial<HoverButtonItemProps>) => {

const renderBody = () => {
if (icon && !children)
return (
<View className={`${classPrefix}-icon`}>
{React.cloneElement(icon, {
className: 'nut-icon',
size: 20,
...nativeProps,
})}
</View>
)
return <View className={`${classPrefix}-icon`}>{getIcon(icon)}</View>
if (icon && children) {
return (
<>
Expand All @@ -68,11 +61,7 @@ export const HoverButtonItem = (props: Partial<HoverButtonItemProps>) => {
[`${classPrefix}-text-icon`]: true,
})}
/>
{React.cloneElement(icon, {
className: 'nut-icon',
size: 14,
...nativeProps,
})}
{getIcon(icon)}
<View
className={classNames({
[`${classPrefix}-text`]: true,
Expand Down
19 changes: 3 additions & 16 deletions src/packages/hoverbuttonitem/hoverbuttonitem.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react'
import classNames from 'classnames'
import { BasicComponent, ComponentDefaults } from '@/utils/typings'
import { getIcon } from '@/packages/hoverbuttonitem/utils'

export interface HoverButtonItemProps extends BasicComponent {
className?: string
Expand Down Expand Up @@ -31,16 +32,7 @@ export const HoverButtonItem = (props: Partial<HoverButtonItemProps>) => {

const renderBody = () => {
if (icon && !children)
return (
<div className={`${classPrefix}-icon`}>
{React.isValidElement(icon)
? React.cloneElement(icon, {
// @ts-ignore
size: 20,
})
: icon}
</div>
)
return <div className={`${classPrefix}-icon`}>{getIcon(icon)}</div>
if (icon && children) {
return (
<>
Expand All @@ -49,12 +41,7 @@ export const HoverButtonItem = (props: Partial<HoverButtonItemProps>) => {
[`${classPrefix}-text-icon`]: true,
})}
/>
{React.isValidElement(icon)
? React.cloneElement(icon, {
// @ts-ignore
size: 14,
})
: icon}
{getIcon(icon)}
<div
className={classNames({
[`${classPrefix}-text`]: true,
Expand Down
10 changes: 10 additions & 0 deletions src/packages/hoverbuttonitem/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React from 'react'

export const getIcon = (icon: React.ReactNode) => {
return React.isValidElement(icon)
? React.cloneElement(icon, {
// @ts-ignore
className: 'nut-icon',
})
: icon
}

0 comments on commit 9a10f48

Please sign in to comment.