From c5bc5c0154d7d1d3c368aa3f16c0d99d5bdb2309 Mon Sep 17 00:00:00 2001 From: hanyuxinting Date: Tue, 14 Mar 2023 18:48:56 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20rate=20=E7=BB=84=E4=BB=B6=E6=8B=86?= =?UTF-8?q?=E5=88=86icon?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/packages/rate/rate.taro.tsx | 66 ++++++++++----------------- src/packages/rate/rate.tsx | 79 +++++++++++++++------------------ 2 files changed, 60 insertions(+), 85 deletions(-) diff --git a/src/packages/rate/rate.taro.tsx b/src/packages/rate/rate.taro.tsx index 4b7be48..da72103 100644 --- a/src/packages/rate/rate.taro.tsx +++ b/src/packages/rate/rate.taro.tsx @@ -1,7 +1,7 @@ import React, { FunctionComponent, useEffect, useState } from 'react' import classNames from 'classnames' +import { StarFillN } from '@nutui/icons-react' import bem from '@/utils/bem' -import Icon from '@/packages/icon' import { BasicComponent, ComponentDefaults } from '@/utils/typings' @@ -12,8 +12,7 @@ export interface RateProps extends BasicComponent { iconSize: string | number activeColor: string voidColor: string - checkedIcon: string - uncheckedIcon: string + checkedIcon: React.ReactNode disabled: boolean readonly: boolean allowHalf: boolean @@ -29,8 +28,7 @@ const defaultProps = { iconSize: 18, activeColor: '', voidColor: '', - checkedIcon: 'star-fill-n', - uncheckedIcon: 'star-n', + checkedIcon: null, disabled: false, readonly: false, allowHalf: false, @@ -47,14 +45,11 @@ export const Rate: FunctionComponent> = (props) => { activeColor, voidColor, checkedIcon, - uncheckedIcon, disabled, readonly, allowHalf, spacing, onChange, - iconClassPrefix, - iconFontClassName, } = { ...defaultProps, ...props, @@ -81,6 +76,18 @@ export const Rate: FunctionComponent> = (props) => { return Number.isNaN(Number(value)) ? String(value) : `${value}px` } + const renderIcon = (size: string | number = iconSize, color: string) => { + return React.isValidElement(checkedIcon) ? ( + React.cloneElement(checkedIcon, { + ...checkedIcon.props, + width: size, + height: size, + color, + }) + ) : ( + + ) + } const onClick = (e: React.MouseEvent, index: number) => { e.preventDefault() e.stopPropagation() @@ -91,7 +98,6 @@ export const Rate: FunctionComponent> = (props) => { } value = Math.max(value, Number(minimizeValue)) setScore(value) - onChange && onChange(value) } const onHalfClick = (event: any, n: number) => { @@ -111,45 +117,21 @@ export const Rate: FunctionComponent> = (props) => { onClick={(event) => onClick(event, n)} style={{ marginRight: pxCheck(spacing) }} > - score, })} - name={n <= score ? checkedIcon : uncheckedIcon} - color={n <= score ? activeColor : voidColor} - /> + > + {renderIcon(iconSize, n <= score ? activeColor : voidColor)} + {allowHalf && score > n - 1 && ( - onHalfClick(event, n)} - className={`${bi('icon')} ${bi('icon--half')}`} - color={n <= score ? activeColor : voidColor} - size={iconSize} - name={checkedIcon} - /> - )} - {/* {allowHalf && score > n - 1 && ( - + > + {renderIcon(iconSize, n <= score ? activeColor : voidColor)} + )} - {allowHalf && score < n - 1 && ( - - )} */} ) })} diff --git a/src/packages/rate/rate.tsx b/src/packages/rate/rate.tsx index 0b96eb9..f7b4d92 100644 --- a/src/packages/rate/rate.tsx +++ b/src/packages/rate/rate.tsx @@ -76,6 +76,19 @@ export const Rate: FunctionComponent> = (props) => { return Number.isNaN(Number(value)) ? String(value) : `${value}px` } + const renderIcon = (size: string | number = iconSize, color: string) => { + return React.isValidElement(checkedIcon) ? ( + React.cloneElement(checkedIcon, { + ...checkedIcon.props, + width: size, + height: size, + color, + }) + ) : ( + + ) + } + const onClick = (e: React.MouseEvent, index: number) => { e.preventDefault() e.stopPropagation() @@ -83,17 +96,20 @@ export const Rate: FunctionComponent> = (props) => { let value = 0 if (!(index === 1 && score === index)) { value = index - if (allowHalf) { - if ((e?.target as Element).className.includes('__icon--half')) { - value -= 0.5 - } - } } value = Math.max(value, Number(minimizeValue)) setScore(value) + onChange && onChange(value) + } + const onHalfClick = (event: any, n: number) => { + event.preventDefault() + event.stopPropagation() + const value = Math.max(Number(minimizeValue), n - 0.5) + setScore(value) onChange && onChange(value) } + return (
{countArray.map((n) => { @@ -104,44 +120,21 @@ export const Rate: FunctionComponent> = (props) => { onClick={(event) => onClick(event, n)} style={{ marginRight: pxCheck(spacing) }} > - <> - {checkedIcon || ( - score, - })} - /> - )} - {(allowHalf && score > n - 1 && ( -
- {checkedIcon || ( - - )} -
- )) || - (allowHalf && score < n - 1 && ( -
- {checkedIcon || ( - - )} -
- ))} - +
score, + })} + > + {renderIcon(iconSize, n <= score ? activeColor : voidColor)} +
+ {allowHalf && score > n - 1 && ( +
onHalfClick(event, n)} + > + {renderIcon(iconSize, n <= score ? activeColor : voidColor)} +
+ )}
) })} -- GitLab