Skip to content
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

fix(pagination): color of icon #2769

Merged
merged 3 commits into from
Nov 20, 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
13 changes: 10 additions & 3 deletions src/packages/pagination/pagination.taro.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import React, { FunctionComponent, useMemo, ReactNode } from 'react'
import React, { FunctionComponent, ReactNode, useMemo } from 'react'
import classNames from 'classnames'
import { View } from '@tarojs/components'
import { useConfig } from '@/packages/configprovider/index.taro'
import { usePropsValue } from '@/utils/use-props-value'
import { BasicComponent, ComponentDefaults } from '@/utils/typings'
import addColorForHarmony from '@/utils/add-color-for-harmony'

export interface PaginationProps extends BasicComponent {
defaultValue: number
Expand Down Expand Up @@ -117,7 +118,10 @@ export const Pagination: FunctionComponent<
)}
onClick={(e) => handleSelectPage(currentPage - 1)}
>
{prev || locale.pagination.prev}
{addColorForHarmony(
prev || locale.pagination.prev,
currentPage === 1 ? '#c2c4cc' : '#ff0f23'
)}
</View>
oasis-cloud marked this conversation as resolved.
Show resolved Hide resolved
oasis-cloud marked this conversation as resolved.
Show resolved Hide resolved
{mode === 'multi' && (
<View className={`${classPrefix}-contain`}>
Expand Down Expand Up @@ -154,7 +158,10 @@ export const Pagination: FunctionComponent<
)}
onClick={(e) => handleSelectPage(currentPage + 1)}
>
{next || locale.pagination.next}
{addColorForHarmony(
next || locale.pagination.next,
currentPage >= pageCount ? '#c2c4cc' : '#ff0f23'
)}
</View>
</>
)}
Expand Down
23 changes: 23 additions & 0 deletions src/utils/add-color-for-harmony.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import React, { ReactElement, ReactNode } from 'react'
import { harmony } from './platform-taro'

interface ColorProps {
color?: string
}

/**
* 为支持 Harmony 的 React 元素添加颜色属性
* @param maybeElement - 要处理的 React 节点
* @param color - 要添加的颜色值(如:'#ff0000')
* @returns 处理后的 React 节点
*/
function addColorForHarmony(maybeElement: ReactNode, color: string) {
if (React.isValidElement(maybeElement) && harmony()) {
return React.cloneElement<ColorProps>(maybeElement as ReactElement, {
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

建议添加类型安全检查

直接将 maybeElement 断言为 ReactElement 可能不够安全。建议添加额外的类型检查。

建议修改如下:

-    return React.cloneElement<ColorProps>(maybeElement as ReactElement, {
+    const element = maybeElement as ReactElement
+    if (!React.isValidElement(element)) {
+      return maybeElement
+    }
+    return React.cloneElement<ColorProps>(element, {
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
return React.cloneElement<ColorProps>(maybeElement as ReactElement, {
const element = maybeElement as ReactElement
if (!React.isValidElement(element)) {
return maybeElement
}
return React.cloneElement<ColorProps>(element, {

color,
})
}
return maybeElement
}

export default addColorForHarmony
Loading