Skip to content

feat: add empty character prop #187

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ coverage
yarn.lock
package-lock.json
.doc/
.vscode/

# umi
.umi
Expand Down
6 changes: 6 additions & 0 deletions assets/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@
}
}

&-full &-first {
width: 100%;
color: @rate-star-color;
opacity: 1;
}

&-half &-first,
&-half &-second {
opacity: 1;
Expand Down
7 changes: 6 additions & 1 deletion src/Rate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import useRefs from './useRefs';
import { getOffsetLeft } from './util';

export interface RateProps
extends Pick<StarProps, 'count' | 'character' | 'characterRender' | 'allowHalf' | 'disabled'> {
extends Pick<
StarProps,
'count' | 'character' | 'characterRender' | 'allowHalf' | 'disabled' | 'emptyCharacter'
> {
value?: number;
defaultValue?: number;
allowClear?: boolean;
Expand Down Expand Up @@ -50,6 +53,7 @@ function Rate(props: RateProps, ref: React.Ref<RateRef>) {
// Display
character = '★',
characterRender,
emptyCharacter,

// Meta
disabled,
Expand Down Expand Up @@ -229,6 +233,7 @@ function Rate(props: RateProps, ref: React.Ref<RateRef>) {
onHover={onHover}
key={item || index}
character={character}
emptyCharacter={emptyCharacter}
characterRender={characterRender}
focused={focused}
/>
Expand Down
7 changes: 6 additions & 1 deletion src/Star.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface StarProps {
characterRender?: (origin: React.ReactElement, props: StarProps) => React.ReactNode;
focused?: boolean;
count?: number;
emptyCharacter?: React.ReactNode | ((props: StarProps) => React.ReactNode);
}

function Star(props: StarProps, ref: React.Ref<HTMLLIElement>) {
Expand All @@ -32,6 +33,7 @@ function Star(props: StarProps, ref: React.Ref<HTMLLIElement>) {
focused,
onHover,
onClick,
emptyCharacter,
} = props;

// =========================== Events ===========================
Expand Down Expand Up @@ -76,6 +78,9 @@ function Star(props: StarProps, ref: React.Ref<HTMLLIElement>) {

// >>>>> Node
const characterNode = typeof character === 'function' ? character(props) : character;
const emptyCharacterNode =
typeof emptyCharacter === 'function' ? emptyCharacter(props) : emptyCharacter;

let start: React.ReactNode = (
<li className={classNames(Array.from(classNameList))} ref={ref}>
<div
Expand All @@ -89,7 +94,7 @@ function Star(props: StarProps, ref: React.Ref<HTMLLIElement>) {
tabIndex={disabled ? -1 : 0}
>
<div className={`${prefixCls}-first`}>{characterNode}</div>
<div className={`${prefixCls}-second`}>{characterNode}</div>
<div className={`${prefixCls}-second`}>{emptyCharacterNode || characterNode}</div>
</div>
</li>
);
Expand Down