1
This commit is contained in:
+9
@@ -0,0 +1,9 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2014-present yiminghe
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
+121
@@ -0,0 +1,121 @@
|
||||
# rc-rate
|
||||
|
||||
React Rate Component
|
||||
|
||||
[![NPM version][npm-image]][npm-url]
|
||||
[![npm download][download-image]][download-url]
|
||||
[![build status][github-actions-image]][github-actions-url]
|
||||
[![Codecov][codecov-image]][codecov-url]
|
||||
[![bundle size][bundlephobia-image]][bundlephobia-url]
|
||||
[![dumi][dumi-image]][dumi-url]
|
||||
|
||||
[npm-image]: http://img.shields.io/npm/v/rc-rate.svg?style=flat-square
|
||||
[npm-url]: http://npmjs.org/package/rc-rate
|
||||
[github-actions-image]: https://github.com/react-component/rate/workflows/CI/badge.svg
|
||||
[github-actions-url]: https://github.com/react-component/rate/actions
|
||||
[codecov-image]: https://img.shields.io/codecov/c/github/react-component/rate/master.svg?style=flat-square
|
||||
[codecov-url]: https://codecov.io/gh/react-component/rate/branch/master
|
||||
[david-url]: https://david-dm.org/react-component/rate
|
||||
[david-image]: https://david-dm.org/react-component/rate/status.svg?style=flat-square
|
||||
[david-dev-url]: https://david-dm.org/react-component/rate?type=dev
|
||||
[david-dev-image]: https://david-dm.org/react-component/rate/dev-status.svg?style=flat-square
|
||||
[download-image]: https://img.shields.io/npm/dm/rc-rate.svg?style=flat-square
|
||||
[download-url]: https://npmjs.org/package/rc-rate
|
||||
[bundlephobia-url]: https://bundlephobia.com/result?p=rc-rate
|
||||
[bundlephobia-image]: https://badgen.net/bundlephobia/minzip/rc-rate
|
||||
[dumi-url]: https://github.com/umijs/dumi
|
||||
[dumi-image]: https://img.shields.io/badge/docs%20by-dumi-blue?style=flat-square
|
||||
|
||||
## Screenshots
|
||||
|
||||
<img src="https://img.alicdn.com/tps/TB1ijlpLVXXXXb8XpXXXXXXXXXX-466-172.png" width="288"/>
|
||||
|
||||
## Changelog
|
||||
|
||||
- [CHANGELOG](./CHANGELOG.md)
|
||||
|
||||
## Development
|
||||
|
||||
```
|
||||
npm install
|
||||
npm start
|
||||
```
|
||||
|
||||
## Example
|
||||
|
||||
- Local: http://localhost:9001/
|
||||
|
||||
- Online: http://react-component.github.io/rate/
|
||||
|
||||
## install
|
||||
|
||||
[](https://npmjs.org/package/rc-rate)
|
||||
|
||||
## Usage
|
||||
|
||||
```js
|
||||
import React from 'react';
|
||||
import ReactDOM from 'react-dom';
|
||||
import Rate from '@rc-component/rate';
|
||||
|
||||
ReactDOM.render(
|
||||
<Rate />,
|
||||
document.getElementById('root')
|
||||
)
|
||||
```
|
||||
|
||||
### with [styled-components](https://github.com/styled-components/styled-components)
|
||||
```js
|
||||
import React from 'react';
|
||||
import ReactDOM from 'react-dom';
|
||||
import Rate from '@rc-component/rate';
|
||||
import styled from 'styled-components';
|
||||
|
||||
const StyledRate = styled(Rate)`
|
||||
&.rc-rate {
|
||||
font-size: ${({ size }) => size}px;
|
||||
}
|
||||
`
|
||||
|
||||
ReactDOM.render(
|
||||
<StyledRate size="24" />,
|
||||
document.getElementById('root')
|
||||
)
|
||||
```
|
||||
|
||||
## API
|
||||
|
||||
### props
|
||||
|
||||
| name | type | default | description |
|
||||
| ------------- | --------------------------------- | ------------- | ----------------------------------------------------- |
|
||||
| count | number | 5 | Star numbers |
|
||||
| value | number | - | Controlled value |
|
||||
| defaultValue | number | 0 | Initial value |
|
||||
| allowHalf | boolean | false | Support half star |
|
||||
| allowClear | boolean | true | Reset when click again |
|
||||
| style | object | {} | |
|
||||
| onChange | function | (value) => {} | `onChange` will be triggered when click |
|
||||
| onHoverChange | function | (value) => {} | `onHoverChange` will be triggered when hover on stars |
|
||||
| character | ReactNode \| (props) => ReactNode | ★ | The each character of rate |
|
||||
| disabled | boolean | false | |
|
||||
| direction | string | `ltr` | The direction of rate |
|
||||
|
||||
## Test Case
|
||||
|
||||
```
|
||||
npm test
|
||||
npm run chrome-test
|
||||
```
|
||||
|
||||
## Coverage
|
||||
|
||||
```
|
||||
npm run coverage
|
||||
```
|
||||
|
||||
open coverage/ dir
|
||||
|
||||
## License
|
||||
|
||||
rc-rate is released under the MIT license.
|
||||
+97
@@ -0,0 +1,97 @@
|
||||
.rc-rate {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
list-style: none;
|
||||
font-size: 18px;
|
||||
display: inline-block;
|
||||
vertical-align: middle;
|
||||
font-weight: normal;
|
||||
font-style: normal;
|
||||
outline: none;
|
||||
}
|
||||
.rc-rate-rtl {
|
||||
direction: rtl;
|
||||
}
|
||||
.rc-rate-disabled .rc-rate-star {
|
||||
cursor: default;
|
||||
}
|
||||
.rc-rate-disabled .rc-rate-star:before,
|
||||
.rc-rate-disabled .rc-rate-star-content:before {
|
||||
cursor: default;
|
||||
}
|
||||
.rc-rate-disabled .rc-rate-star:hover {
|
||||
transform: scale(1);
|
||||
}
|
||||
.rc-rate-star {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
display: inline-block;
|
||||
margin-right: 8px;
|
||||
position: relative;
|
||||
transition: all 0.3s;
|
||||
color: #e9e9e9;
|
||||
cursor: pointer;
|
||||
line-height: 1.5;
|
||||
}
|
||||
.rc-rate-rtl .rc-rate-star {
|
||||
margin-right: 0;
|
||||
margin-left: 8px;
|
||||
float: right;
|
||||
}
|
||||
.rc-rate-star-first,
|
||||
.rc-rate-star-second {
|
||||
transition: all 0.3s;
|
||||
}
|
||||
.rc-rate-star-focused,
|
||||
.rc-rate-star:hover {
|
||||
transform: scale(1.1);
|
||||
}
|
||||
.rc-rate-star-first {
|
||||
position: absolute;
|
||||
left: 0;
|
||||
top: 0;
|
||||
width: 50%;
|
||||
height: 100%;
|
||||
overflow: hidden;
|
||||
opacity: 0;
|
||||
}
|
||||
.rc-rate-rtl .rc-rate-star-first {
|
||||
right: 0;
|
||||
left: auto;
|
||||
}
|
||||
.rc-rate-star-half .rc-rate-star-first,
|
||||
.rc-rate-star-half .rc-rate-star-second {
|
||||
opacity: 1;
|
||||
}
|
||||
.rc-rate-star-half .rc-rate-star-first,
|
||||
.rc-rate-star-full .rc-rate-star-second {
|
||||
color: #f5a623;
|
||||
}
|
||||
.rc-rate-star-half:hover .rc-rate-star-first,
|
||||
.rc-rate-star-full:hover .rc-rate-star-second {
|
||||
color: #f8c165;
|
||||
}
|
||||
@font-face {
|
||||
font-family: 'anticon';
|
||||
src: url('//at.alicdn.com/t/font_r5u29ls31bgldi.eot');
|
||||
/* IE9*/
|
||||
src: url('//at.alicdn.com/t/font_r5u29ls31bgldi.eot?#iefix') format('embedded-opentype'), /* IE6-IE8 */ url('//at.alicdn.com/t/font_r5u29ls31bgldi.woff') format('woff'), /* chrome、firefox */ url('//at.alicdn.com/t/font_r5u29ls31bgldi.ttf') format('truetype'), /* chrome、firefox、opera、Safari, Android, iOS 4.2+*/ url('//at.alicdn.com/t/font_r5u29ls31bgldi.svg#iconfont') format('svg');
|
||||
/* iOS 4.1- */
|
||||
}
|
||||
.anticon {
|
||||
font-style: normal;
|
||||
vertical-align: baseline;
|
||||
text-align: center;
|
||||
text-transform: none;
|
||||
line-height: 1;
|
||||
text-rendering: optimizeLegibility;
|
||||
-webkit-font-smoothing: antialiased;
|
||||
-moz-osx-font-smoothing: grayscale;
|
||||
}
|
||||
.anticon:before {
|
||||
display: block;
|
||||
font-family: "anticon" !important;
|
||||
}
|
||||
.anticon-star:before {
|
||||
content: "\e660";
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
import React from 'react';
|
||||
import type { StarProps } from './Star';
|
||||
export interface RateProps extends Pick<StarProps, 'count' | 'character' | 'characterRender' | 'allowHalf' | 'disabled'> {
|
||||
value?: number;
|
||||
defaultValue?: number;
|
||||
allowClear?: boolean;
|
||||
style?: React.CSSProperties;
|
||||
prefixCls?: string;
|
||||
onChange?: (value: number) => void;
|
||||
onHoverChange?: (value: number) => void;
|
||||
className?: string;
|
||||
tabIndex?: number;
|
||||
onFocus?: () => void;
|
||||
onBlur?: () => void;
|
||||
onKeyDown?: React.KeyboardEventHandler<HTMLUListElement>;
|
||||
onMouseEnter?: React.MouseEventHandler<HTMLUListElement>;
|
||||
onMouseLeave?: React.MouseEventHandler<HTMLUListElement>;
|
||||
id?: string;
|
||||
autoFocus?: boolean;
|
||||
direction?: string;
|
||||
/**
|
||||
* Is keyboard control enabled.
|
||||
* @default true
|
||||
*/
|
||||
keyboard?: boolean;
|
||||
}
|
||||
export interface RateRef {
|
||||
focus: VoidFunction;
|
||||
blur: VoidFunction;
|
||||
}
|
||||
declare const _default: React.ForwardRefExoticComponent<RateProps & React.RefAttributes<RateRef>>;
|
||||
export default _default;
|
||||
+194
@@ -0,0 +1,194 @@
|
||||
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
|
||||
import useControlledState from "@rc-component/util/es/hooks/useControlledState";
|
||||
import KeyCode from "@rc-component/util/es/KeyCode";
|
||||
import pickAttrs from "@rc-component/util/es/pickAttrs";
|
||||
import { clsx } from 'clsx';
|
||||
import React from 'react';
|
||||
import Star from "./Star";
|
||||
import useRefs from "./useRefs";
|
||||
import { getOffsetLeft } from "./util";
|
||||
function Rate(props, ref) {
|
||||
const {
|
||||
// Base
|
||||
prefixCls = 'rc-rate',
|
||||
className,
|
||||
// Value
|
||||
defaultValue,
|
||||
value: propValue,
|
||||
count = 5,
|
||||
allowHalf = false,
|
||||
allowClear = true,
|
||||
keyboard = true,
|
||||
// Display
|
||||
character = '★',
|
||||
characterRender,
|
||||
// Meta
|
||||
disabled,
|
||||
direction = 'ltr',
|
||||
tabIndex = 0,
|
||||
autoFocus,
|
||||
// Events
|
||||
onHoverChange,
|
||||
onChange,
|
||||
onFocus,
|
||||
onBlur,
|
||||
onKeyDown,
|
||||
onMouseLeave,
|
||||
...restProps
|
||||
} = props;
|
||||
const [getStarRef, setStarRef] = useRefs();
|
||||
const rateRef = React.useRef(null);
|
||||
|
||||
// ============================ Ref =============================
|
||||
const triggerFocus = () => {
|
||||
if (!disabled) {
|
||||
rateRef.current?.focus();
|
||||
}
|
||||
};
|
||||
React.useImperativeHandle(ref, () => ({
|
||||
focus: triggerFocus,
|
||||
blur: () => {
|
||||
if (!disabled) {
|
||||
rateRef.current?.blur();
|
||||
}
|
||||
}
|
||||
}));
|
||||
|
||||
// =========================== Value ============================
|
||||
const [value, setValue] = useControlledState(defaultValue || 0, propValue);
|
||||
const [cleanedValue, setCleanedValue] = useControlledState(null);
|
||||
const getStarValue = (index, x) => {
|
||||
const reverse = direction === 'rtl';
|
||||
let starValue = index + 1;
|
||||
if (allowHalf) {
|
||||
const starEle = getStarRef(index);
|
||||
const leftDis = getOffsetLeft(starEle);
|
||||
const width = starEle.clientWidth;
|
||||
if (reverse && x - leftDis > width / 2) {
|
||||
starValue -= 0.5;
|
||||
} else if (!reverse && x - leftDis < width / 2) {
|
||||
starValue -= 0.5;
|
||||
}
|
||||
}
|
||||
return starValue;
|
||||
};
|
||||
|
||||
// >>>>> Change
|
||||
const changeValue = nextValue => {
|
||||
setValue(nextValue);
|
||||
onChange?.(nextValue);
|
||||
};
|
||||
|
||||
// =========================== Focus ============================
|
||||
const [focused, setFocused] = React.useState(false);
|
||||
const onInternalFocus = () => {
|
||||
setFocused(true);
|
||||
onFocus?.();
|
||||
};
|
||||
const onInternalBlur = () => {
|
||||
setFocused(false);
|
||||
onBlur?.();
|
||||
};
|
||||
|
||||
// =========================== Hover ============================
|
||||
const [hoverValue, setHoverValue] = React.useState(null);
|
||||
const onHover = (event, index) => {
|
||||
const nextHoverValue = getStarValue(index, event.pageX);
|
||||
if (nextHoverValue !== cleanedValue) {
|
||||
setHoverValue(nextHoverValue);
|
||||
setCleanedValue(null);
|
||||
}
|
||||
onHoverChange?.(nextHoverValue);
|
||||
};
|
||||
const onMouseLeaveCallback = event => {
|
||||
if (!disabled) {
|
||||
setHoverValue(null);
|
||||
setCleanedValue(null);
|
||||
onHoverChange?.(undefined);
|
||||
}
|
||||
if (event) {
|
||||
onMouseLeave?.(event);
|
||||
}
|
||||
};
|
||||
|
||||
// =========================== Click ============================
|
||||
const onClick = (event, index) => {
|
||||
const newValue = getStarValue(index, event.pageX);
|
||||
let isReset = false;
|
||||
if (allowClear) {
|
||||
isReset = newValue === value;
|
||||
}
|
||||
onMouseLeaveCallback();
|
||||
changeValue(isReset ? 0 : newValue);
|
||||
setCleanedValue(isReset ? newValue : null);
|
||||
};
|
||||
const onInternalKeyDown = event => {
|
||||
const {
|
||||
keyCode
|
||||
} = event;
|
||||
const reverse = direction === 'rtl';
|
||||
const step = allowHalf ? 0.5 : 1;
|
||||
if (keyboard) {
|
||||
if (keyCode === KeyCode.RIGHT && value < count && !reverse) {
|
||||
changeValue(value + step);
|
||||
event.preventDefault();
|
||||
} else if (keyCode === KeyCode.LEFT && value > 0 && !reverse) {
|
||||
changeValue(value - step);
|
||||
event.preventDefault();
|
||||
} else if (keyCode === KeyCode.RIGHT && value > 0 && reverse) {
|
||||
changeValue(value - step);
|
||||
event.preventDefault();
|
||||
} else if (keyCode === KeyCode.LEFT && value < count && reverse) {
|
||||
changeValue(value + step);
|
||||
event.preventDefault();
|
||||
}
|
||||
}
|
||||
onKeyDown?.(event);
|
||||
};
|
||||
|
||||
// =========================== Effect ===========================
|
||||
|
||||
React.useEffect(() => {
|
||||
if (autoFocus && !disabled) {
|
||||
triggerFocus();
|
||||
}
|
||||
}, []);
|
||||
|
||||
// =========================== Render ===========================
|
||||
// >>> Star
|
||||
const starNodes = new Array(count).fill(0).map((item, index) => /*#__PURE__*/React.createElement(Star, {
|
||||
ref: setStarRef(index),
|
||||
index: index,
|
||||
count: count,
|
||||
disabled: disabled,
|
||||
prefixCls: `${prefixCls}-star`,
|
||||
allowHalf: allowHalf,
|
||||
value: hoverValue === null ? value : hoverValue,
|
||||
onClick: onClick,
|
||||
onHover: onHover,
|
||||
key: item || index,
|
||||
character: character,
|
||||
characterRender: characterRender,
|
||||
focused: focused
|
||||
}));
|
||||
const classString = clsx(prefixCls, className, {
|
||||
[`${prefixCls}-disabled`]: disabled,
|
||||
[`${prefixCls}-rtl`]: direction === 'rtl'
|
||||
});
|
||||
|
||||
// >>> Node
|
||||
return /*#__PURE__*/React.createElement("ul", _extends({
|
||||
className: classString,
|
||||
onMouseLeave: onMouseLeaveCallback,
|
||||
tabIndex: disabled ? -1 : tabIndex,
|
||||
onFocus: disabled ? null : onInternalFocus,
|
||||
onBlur: disabled ? null : onInternalBlur,
|
||||
onKeyDown: disabled ? null : onInternalKeyDown,
|
||||
ref: rateRef
|
||||
}, pickAttrs(restProps, {
|
||||
aria: true,
|
||||
data: true,
|
||||
attr: true
|
||||
})), starNodes);
|
||||
}
|
||||
export default /*#__PURE__*/React.forwardRef(Rate);
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
import React from 'react';
|
||||
export interface StarProps {
|
||||
value?: number;
|
||||
index?: number;
|
||||
prefixCls?: string;
|
||||
allowHalf?: boolean;
|
||||
disabled?: boolean;
|
||||
onHover?: (e: React.MouseEvent<HTMLDivElement>, index: number) => void;
|
||||
onClick?: (e: React.MouseEvent<HTMLDivElement> | React.KeyboardEvent<HTMLDivElement>, index: number) => void;
|
||||
character?: React.ReactNode | ((props: StarProps) => React.ReactNode);
|
||||
characterRender?: (origin: React.ReactElement, props: StarProps) => React.ReactNode;
|
||||
focused?: boolean;
|
||||
count?: number;
|
||||
}
|
||||
declare const _default: React.ForwardRefExoticComponent<StarProps & React.RefAttributes<HTMLLIElement>>;
|
||||
export default _default;
|
||||
+81
@@ -0,0 +1,81 @@
|
||||
import React from 'react';
|
||||
import KeyCode from "@rc-component/util/es/KeyCode";
|
||||
import { clsx } from 'clsx';
|
||||
function Star(props, ref) {
|
||||
const {
|
||||
disabled,
|
||||
prefixCls,
|
||||
character,
|
||||
characterRender,
|
||||
index,
|
||||
count,
|
||||
value,
|
||||
allowHalf,
|
||||
focused,
|
||||
onHover,
|
||||
onClick
|
||||
} = props;
|
||||
|
||||
// =========================== Events ===========================
|
||||
const onInternalHover = e => {
|
||||
onHover(e, index);
|
||||
};
|
||||
const onInternalClick = e => {
|
||||
onClick(e, index);
|
||||
};
|
||||
const onInternalKeyDown = e => {
|
||||
if (e.keyCode === KeyCode.ENTER) {
|
||||
onClick(e, index);
|
||||
}
|
||||
};
|
||||
|
||||
// =========================== Render ===========================
|
||||
// >>>>> ClassName
|
||||
const starValue = index + 1;
|
||||
const classNameList = new Set([prefixCls]);
|
||||
|
||||
// TODO: Current we just refactor from CC to FC. This logic seems can be optimized.
|
||||
if (value === 0 && index === 0 && focused) {
|
||||
classNameList.add(`${prefixCls}-focused`);
|
||||
} else if (allowHalf && value + 0.5 >= starValue && value < starValue) {
|
||||
classNameList.add(`${prefixCls}-half`);
|
||||
classNameList.add(`${prefixCls}-active`);
|
||||
if (focused) {
|
||||
classNameList.add(`${prefixCls}-focused`);
|
||||
}
|
||||
} else {
|
||||
if (starValue <= value) {
|
||||
classNameList.add(`${prefixCls}-full`);
|
||||
} else {
|
||||
classNameList.add(`${prefixCls}-zero`);
|
||||
}
|
||||
if (starValue === value && focused) {
|
||||
classNameList.add(`${prefixCls}-focused`);
|
||||
}
|
||||
}
|
||||
|
||||
// >>>>> Node
|
||||
const characterNode = typeof character === 'function' ? character(props) : character;
|
||||
let start = /*#__PURE__*/React.createElement("li", {
|
||||
className: clsx(Array.from(classNameList)),
|
||||
ref: ref
|
||||
}, /*#__PURE__*/React.createElement("div", {
|
||||
onClick: disabled ? null : onInternalClick,
|
||||
onKeyDown: disabled ? null : onInternalKeyDown,
|
||||
onMouseMove: disabled ? null : onInternalHover,
|
||||
role: "radio",
|
||||
"aria-checked": value > index ? 'true' : 'false',
|
||||
"aria-posinset": index + 1,
|
||||
"aria-setsize": count,
|
||||
tabIndex: disabled ? -1 : 0
|
||||
}, /*#__PURE__*/React.createElement("div", {
|
||||
className: `${prefixCls}-first`
|
||||
}, characterNode), /*#__PURE__*/React.createElement("div", {
|
||||
className: `${prefixCls}-second`
|
||||
}, characterNode)));
|
||||
if (characterRender) {
|
||||
start = characterRender(start, props);
|
||||
}
|
||||
return start;
|
||||
}
|
||||
export default /*#__PURE__*/React.forwardRef(Star);
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
import Rate from './Rate';
|
||||
export default Rate;
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
import Rate from "./Rate";
|
||||
export default Rate;
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
export default function useRefs<T>(): [
|
||||
getRef: (index: number) => T,
|
||||
setRef: (index: number) => (instance: T) => void
|
||||
];
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
import * as React from 'react';
|
||||
export default function useRefs() {
|
||||
const nodeRef = React.useRef({});
|
||||
function getRef(index) {
|
||||
return nodeRef.current[index];
|
||||
}
|
||||
function setRef(index) {
|
||||
return node => {
|
||||
nodeRef.current[index] = node;
|
||||
};
|
||||
}
|
||||
return [getRef, setRef];
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
export declare function getOffsetLeft(el: HTMLElement): number;
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
function getScroll(w) {
|
||||
let ret = w.pageXOffset;
|
||||
const method = 'scrollLeft';
|
||||
if (typeof ret !== 'number') {
|
||||
const d = w.document;
|
||||
// ie6,7,8 standard mode
|
||||
ret = d.documentElement[method];
|
||||
if (typeof ret !== 'number') {
|
||||
// quirks mode
|
||||
ret = d.body[method];
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
function getClientPosition(elem) {
|
||||
let x;
|
||||
let y;
|
||||
const doc = elem.ownerDocument;
|
||||
const {
|
||||
body
|
||||
} = doc;
|
||||
const docElem = doc && doc.documentElement;
|
||||
const box = elem.getBoundingClientRect();
|
||||
x = box.left;
|
||||
y = box.top;
|
||||
x -= docElem.clientLeft || body.clientLeft || 0;
|
||||
y -= docElem.clientTop || body.clientTop || 0;
|
||||
return {
|
||||
left: x,
|
||||
top: y
|
||||
};
|
||||
}
|
||||
export function getOffsetLeft(el) {
|
||||
const pos = getClientPosition(el);
|
||||
const doc = el.ownerDocument;
|
||||
// Only IE use `parentWindow`
|
||||
const w = doc.defaultView || doc.parentWindow;
|
||||
pos.left += getScroll(w);
|
||||
return pos.left;
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
import React from 'react';
|
||||
import type { StarProps } from './Star';
|
||||
export interface RateProps extends Pick<StarProps, 'count' | 'character' | 'characterRender' | 'allowHalf' | 'disabled'> {
|
||||
value?: number;
|
||||
defaultValue?: number;
|
||||
allowClear?: boolean;
|
||||
style?: React.CSSProperties;
|
||||
prefixCls?: string;
|
||||
onChange?: (value: number) => void;
|
||||
onHoverChange?: (value: number) => void;
|
||||
className?: string;
|
||||
tabIndex?: number;
|
||||
onFocus?: () => void;
|
||||
onBlur?: () => void;
|
||||
onKeyDown?: React.KeyboardEventHandler<HTMLUListElement>;
|
||||
onMouseEnter?: React.MouseEventHandler<HTMLUListElement>;
|
||||
onMouseLeave?: React.MouseEventHandler<HTMLUListElement>;
|
||||
id?: string;
|
||||
autoFocus?: boolean;
|
||||
direction?: string;
|
||||
/**
|
||||
* Is keyboard control enabled.
|
||||
* @default true
|
||||
*/
|
||||
keyboard?: boolean;
|
||||
}
|
||||
export interface RateRef {
|
||||
focus: VoidFunction;
|
||||
blur: VoidFunction;
|
||||
}
|
||||
declare const _default: React.ForwardRefExoticComponent<RateProps & React.RefAttributes<RateRef>>;
|
||||
export default _default;
|
||||
+201
@@ -0,0 +1,201 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.default = void 0;
|
||||
var _useControlledState = _interopRequireDefault(require("@rc-component/util/lib/hooks/useControlledState"));
|
||||
var _KeyCode = _interopRequireDefault(require("@rc-component/util/lib/KeyCode"));
|
||||
var _pickAttrs = _interopRequireDefault(require("@rc-component/util/lib/pickAttrs"));
|
||||
var _clsx = require("clsx");
|
||||
var _react = _interopRequireDefault(require("react"));
|
||||
var _Star = _interopRequireDefault(require("./Star"));
|
||||
var _useRefs = _interopRequireDefault(require("./useRefs"));
|
||||
var _util = require("./util");
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||||
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
|
||||
function Rate(props, ref) {
|
||||
const {
|
||||
// Base
|
||||
prefixCls = 'rc-rate',
|
||||
className,
|
||||
// Value
|
||||
defaultValue,
|
||||
value: propValue,
|
||||
count = 5,
|
||||
allowHalf = false,
|
||||
allowClear = true,
|
||||
keyboard = true,
|
||||
// Display
|
||||
character = '★',
|
||||
characterRender,
|
||||
// Meta
|
||||
disabled,
|
||||
direction = 'ltr',
|
||||
tabIndex = 0,
|
||||
autoFocus,
|
||||
// Events
|
||||
onHoverChange,
|
||||
onChange,
|
||||
onFocus,
|
||||
onBlur,
|
||||
onKeyDown,
|
||||
onMouseLeave,
|
||||
...restProps
|
||||
} = props;
|
||||
const [getStarRef, setStarRef] = (0, _useRefs.default)();
|
||||
const rateRef = _react.default.useRef(null);
|
||||
|
||||
// ============================ Ref =============================
|
||||
const triggerFocus = () => {
|
||||
if (!disabled) {
|
||||
rateRef.current?.focus();
|
||||
}
|
||||
};
|
||||
_react.default.useImperativeHandle(ref, () => ({
|
||||
focus: triggerFocus,
|
||||
blur: () => {
|
||||
if (!disabled) {
|
||||
rateRef.current?.blur();
|
||||
}
|
||||
}
|
||||
}));
|
||||
|
||||
// =========================== Value ============================
|
||||
const [value, setValue] = (0, _useControlledState.default)(defaultValue || 0, propValue);
|
||||
const [cleanedValue, setCleanedValue] = (0, _useControlledState.default)(null);
|
||||
const getStarValue = (index, x) => {
|
||||
const reverse = direction === 'rtl';
|
||||
let starValue = index + 1;
|
||||
if (allowHalf) {
|
||||
const starEle = getStarRef(index);
|
||||
const leftDis = (0, _util.getOffsetLeft)(starEle);
|
||||
const width = starEle.clientWidth;
|
||||
if (reverse && x - leftDis > width / 2) {
|
||||
starValue -= 0.5;
|
||||
} else if (!reverse && x - leftDis < width / 2) {
|
||||
starValue -= 0.5;
|
||||
}
|
||||
}
|
||||
return starValue;
|
||||
};
|
||||
|
||||
// >>>>> Change
|
||||
const changeValue = nextValue => {
|
||||
setValue(nextValue);
|
||||
onChange?.(nextValue);
|
||||
};
|
||||
|
||||
// =========================== Focus ============================
|
||||
const [focused, setFocused] = _react.default.useState(false);
|
||||
const onInternalFocus = () => {
|
||||
setFocused(true);
|
||||
onFocus?.();
|
||||
};
|
||||
const onInternalBlur = () => {
|
||||
setFocused(false);
|
||||
onBlur?.();
|
||||
};
|
||||
|
||||
// =========================== Hover ============================
|
||||
const [hoverValue, setHoverValue] = _react.default.useState(null);
|
||||
const onHover = (event, index) => {
|
||||
const nextHoverValue = getStarValue(index, event.pageX);
|
||||
if (nextHoverValue !== cleanedValue) {
|
||||
setHoverValue(nextHoverValue);
|
||||
setCleanedValue(null);
|
||||
}
|
||||
onHoverChange?.(nextHoverValue);
|
||||
};
|
||||
const onMouseLeaveCallback = event => {
|
||||
if (!disabled) {
|
||||
setHoverValue(null);
|
||||
setCleanedValue(null);
|
||||
onHoverChange?.(undefined);
|
||||
}
|
||||
if (event) {
|
||||
onMouseLeave?.(event);
|
||||
}
|
||||
};
|
||||
|
||||
// =========================== Click ============================
|
||||
const onClick = (event, index) => {
|
||||
const newValue = getStarValue(index, event.pageX);
|
||||
let isReset = false;
|
||||
if (allowClear) {
|
||||
isReset = newValue === value;
|
||||
}
|
||||
onMouseLeaveCallback();
|
||||
changeValue(isReset ? 0 : newValue);
|
||||
setCleanedValue(isReset ? newValue : null);
|
||||
};
|
||||
const onInternalKeyDown = event => {
|
||||
const {
|
||||
keyCode
|
||||
} = event;
|
||||
const reverse = direction === 'rtl';
|
||||
const step = allowHalf ? 0.5 : 1;
|
||||
if (keyboard) {
|
||||
if (keyCode === _KeyCode.default.RIGHT && value < count && !reverse) {
|
||||
changeValue(value + step);
|
||||
event.preventDefault();
|
||||
} else if (keyCode === _KeyCode.default.LEFT && value > 0 && !reverse) {
|
||||
changeValue(value - step);
|
||||
event.preventDefault();
|
||||
} else if (keyCode === _KeyCode.default.RIGHT && value > 0 && reverse) {
|
||||
changeValue(value - step);
|
||||
event.preventDefault();
|
||||
} else if (keyCode === _KeyCode.default.LEFT && value < count && reverse) {
|
||||
changeValue(value + step);
|
||||
event.preventDefault();
|
||||
}
|
||||
}
|
||||
onKeyDown?.(event);
|
||||
};
|
||||
|
||||
// =========================== Effect ===========================
|
||||
|
||||
_react.default.useEffect(() => {
|
||||
if (autoFocus && !disabled) {
|
||||
triggerFocus();
|
||||
}
|
||||
}, []);
|
||||
|
||||
// =========================== Render ===========================
|
||||
// >>> Star
|
||||
const starNodes = new Array(count).fill(0).map((item, index) => /*#__PURE__*/_react.default.createElement(_Star.default, {
|
||||
ref: setStarRef(index),
|
||||
index: index,
|
||||
count: count,
|
||||
disabled: disabled,
|
||||
prefixCls: `${prefixCls}-star`,
|
||||
allowHalf: allowHalf,
|
||||
value: hoverValue === null ? value : hoverValue,
|
||||
onClick: onClick,
|
||||
onHover: onHover,
|
||||
key: item || index,
|
||||
character: character,
|
||||
characterRender: characterRender,
|
||||
focused: focused
|
||||
}));
|
||||
const classString = (0, _clsx.clsx)(prefixCls, className, {
|
||||
[`${prefixCls}-disabled`]: disabled,
|
||||
[`${prefixCls}-rtl`]: direction === 'rtl'
|
||||
});
|
||||
|
||||
// >>> Node
|
||||
return /*#__PURE__*/_react.default.createElement("ul", _extends({
|
||||
className: classString,
|
||||
onMouseLeave: onMouseLeaveCallback,
|
||||
tabIndex: disabled ? -1 : tabIndex,
|
||||
onFocus: disabled ? null : onInternalFocus,
|
||||
onBlur: disabled ? null : onInternalBlur,
|
||||
onKeyDown: disabled ? null : onInternalKeyDown,
|
||||
ref: rateRef
|
||||
}, (0, _pickAttrs.default)(restProps, {
|
||||
aria: true,
|
||||
data: true,
|
||||
attr: true
|
||||
})), starNodes);
|
||||
}
|
||||
var _default = exports.default = /*#__PURE__*/_react.default.forwardRef(Rate);
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
import React from 'react';
|
||||
export interface StarProps {
|
||||
value?: number;
|
||||
index?: number;
|
||||
prefixCls?: string;
|
||||
allowHalf?: boolean;
|
||||
disabled?: boolean;
|
||||
onHover?: (e: React.MouseEvent<HTMLDivElement>, index: number) => void;
|
||||
onClick?: (e: React.MouseEvent<HTMLDivElement> | React.KeyboardEvent<HTMLDivElement>, index: number) => void;
|
||||
character?: React.ReactNode | ((props: StarProps) => React.ReactNode);
|
||||
characterRender?: (origin: React.ReactElement, props: StarProps) => React.ReactNode;
|
||||
focused?: boolean;
|
||||
count?: number;
|
||||
}
|
||||
declare const _default: React.ForwardRefExoticComponent<StarProps & React.RefAttributes<HTMLLIElement>>;
|
||||
export default _default;
|
||||
+88
@@ -0,0 +1,88 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.default = void 0;
|
||||
var _react = _interopRequireDefault(require("react"));
|
||||
var _KeyCode = _interopRequireDefault(require("@rc-component/util/lib/KeyCode"));
|
||||
var _clsx = require("clsx");
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||||
function Star(props, ref) {
|
||||
const {
|
||||
disabled,
|
||||
prefixCls,
|
||||
character,
|
||||
characterRender,
|
||||
index,
|
||||
count,
|
||||
value,
|
||||
allowHalf,
|
||||
focused,
|
||||
onHover,
|
||||
onClick
|
||||
} = props;
|
||||
|
||||
// =========================== Events ===========================
|
||||
const onInternalHover = e => {
|
||||
onHover(e, index);
|
||||
};
|
||||
const onInternalClick = e => {
|
||||
onClick(e, index);
|
||||
};
|
||||
const onInternalKeyDown = e => {
|
||||
if (e.keyCode === _KeyCode.default.ENTER) {
|
||||
onClick(e, index);
|
||||
}
|
||||
};
|
||||
|
||||
// =========================== Render ===========================
|
||||
// >>>>> ClassName
|
||||
const starValue = index + 1;
|
||||
const classNameList = new Set([prefixCls]);
|
||||
|
||||
// TODO: Current we just refactor from CC to FC. This logic seems can be optimized.
|
||||
if (value === 0 && index === 0 && focused) {
|
||||
classNameList.add(`${prefixCls}-focused`);
|
||||
} else if (allowHalf && value + 0.5 >= starValue && value < starValue) {
|
||||
classNameList.add(`${prefixCls}-half`);
|
||||
classNameList.add(`${prefixCls}-active`);
|
||||
if (focused) {
|
||||
classNameList.add(`${prefixCls}-focused`);
|
||||
}
|
||||
} else {
|
||||
if (starValue <= value) {
|
||||
classNameList.add(`${prefixCls}-full`);
|
||||
} else {
|
||||
classNameList.add(`${prefixCls}-zero`);
|
||||
}
|
||||
if (starValue === value && focused) {
|
||||
classNameList.add(`${prefixCls}-focused`);
|
||||
}
|
||||
}
|
||||
|
||||
// >>>>> Node
|
||||
const characterNode = typeof character === 'function' ? character(props) : character;
|
||||
let start = /*#__PURE__*/_react.default.createElement("li", {
|
||||
className: (0, _clsx.clsx)(Array.from(classNameList)),
|
||||
ref: ref
|
||||
}, /*#__PURE__*/_react.default.createElement("div", {
|
||||
onClick: disabled ? null : onInternalClick,
|
||||
onKeyDown: disabled ? null : onInternalKeyDown,
|
||||
onMouseMove: disabled ? null : onInternalHover,
|
||||
role: "radio",
|
||||
"aria-checked": value > index ? 'true' : 'false',
|
||||
"aria-posinset": index + 1,
|
||||
"aria-setsize": count,
|
||||
tabIndex: disabled ? -1 : 0
|
||||
}, /*#__PURE__*/_react.default.createElement("div", {
|
||||
className: `${prefixCls}-first`
|
||||
}, characterNode), /*#__PURE__*/_react.default.createElement("div", {
|
||||
className: `${prefixCls}-second`
|
||||
}, characterNode)));
|
||||
if (characterRender) {
|
||||
start = characterRender(start, props);
|
||||
}
|
||||
return start;
|
||||
}
|
||||
var _default = exports.default = /*#__PURE__*/_react.default.forwardRef(Star);
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
import Rate from './Rate';
|
||||
export default Rate;
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.default = void 0;
|
||||
var _Rate = _interopRequireDefault(require("./Rate"));
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||||
var _default = exports.default = _Rate.default;
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
export default function useRefs<T>(): [
|
||||
getRef: (index: number) => T,
|
||||
setRef: (index: number) => (instance: T) => void
|
||||
];
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.default = useRefs;
|
||||
var React = _interopRequireWildcard(require("react"));
|
||||
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
||||
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
||||
function useRefs() {
|
||||
const nodeRef = React.useRef({});
|
||||
function getRef(index) {
|
||||
return nodeRef.current[index];
|
||||
}
|
||||
function setRef(index) {
|
||||
return node => {
|
||||
nodeRef.current[index] = node;
|
||||
};
|
||||
}
|
||||
return [getRef, setRef];
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
export declare function getOffsetLeft(el: HTMLElement): number;
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.getOffsetLeft = getOffsetLeft;
|
||||
function getScroll(w) {
|
||||
let ret = w.pageXOffset;
|
||||
const method = 'scrollLeft';
|
||||
if (typeof ret !== 'number') {
|
||||
const d = w.document;
|
||||
// ie6,7,8 standard mode
|
||||
ret = d.documentElement[method];
|
||||
if (typeof ret !== 'number') {
|
||||
// quirks mode
|
||||
ret = d.body[method];
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
function getClientPosition(elem) {
|
||||
let x;
|
||||
let y;
|
||||
const doc = elem.ownerDocument;
|
||||
const {
|
||||
body
|
||||
} = doc;
|
||||
const docElem = doc && doc.documentElement;
|
||||
const box = elem.getBoundingClientRect();
|
||||
x = box.left;
|
||||
y = box.top;
|
||||
x -= docElem.clientLeft || body.clientLeft || 0;
|
||||
y -= docElem.clientTop || body.clientTop || 0;
|
||||
return {
|
||||
left: x,
|
||||
top: y
|
||||
};
|
||||
}
|
||||
function getOffsetLeft(el) {
|
||||
const pos = getClientPosition(el);
|
||||
const doc = el.ownerDocument;
|
||||
// Only IE use `parentWindow`
|
||||
const w = doc.defaultView || doc.parentWindow;
|
||||
pos.left += getScroll(w);
|
||||
return pos.left;
|
||||
}
|
||||
+77
@@ -0,0 +1,77 @@
|
||||
{
|
||||
"name": "@rc-component/rate",
|
||||
"version": "1.0.1",
|
||||
"description": "React Star Rate Component",
|
||||
"keywords": [
|
||||
"react",
|
||||
"react-component",
|
||||
"react-rate",
|
||||
"rate"
|
||||
],
|
||||
"homepage": "https://github.com/react-component/rate",
|
||||
"bugs": {
|
||||
"url": "https://github.com/react-component/rate/issues"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/react-component/rate.git"
|
||||
},
|
||||
"license": "MIT",
|
||||
"main": "./lib/index",
|
||||
"module": "./es/index",
|
||||
"files": [
|
||||
"lib",
|
||||
"es",
|
||||
"assets/*.css"
|
||||
],
|
||||
"scripts": {
|
||||
"compile": "father build && lessc assets/index.less assets/index.css",
|
||||
"coverage": "rc-test --coverage",
|
||||
"docs:build": "dumi build",
|
||||
"docs:deploy": "gh-pages -d dist",
|
||||
"lint": "eslint src/ --ext .ts,.tsx,.jsx,.js,.md",
|
||||
"now-build": "npm run docs:build",
|
||||
"prepare": "dumi setup",
|
||||
"prepublishOnly": "npm run compile && rc-np",
|
||||
"prettier": "prettier --write \"**/*.{ts,tsx,js,jsx,json,md}\"",
|
||||
"postpublish": "npm run docs:build && npm run docs:deploy",
|
||||
"start": "dumi dev",
|
||||
"test": "rc-test",
|
||||
"tsc": "bunx tsc --noEmit"
|
||||
},
|
||||
"dependencies": {
|
||||
"@rc-component/util": "^1.3.0",
|
||||
"clsx": "^2.1.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@rc-component/father-plugin": "^2.1.3",
|
||||
"@rc-component/np": "^1.0.0",
|
||||
"@rc-component/tooltip": "^1.2.1",
|
||||
"@types/jest": "^29.5.1",
|
||||
"@types/node": "^24.5.2",
|
||||
"@types/react": "^17.0.15",
|
||||
"@types/react-dom": "^17.0.9",
|
||||
"@umijs/fabric": "^3.0.0",
|
||||
"cheerio": "1.0.0-rc.12",
|
||||
"cross-env": "^7.0.0",
|
||||
"dumi": "^2.1.2",
|
||||
"enzyme": "^3.1.1",
|
||||
"enzyme-adapter-react-16": "^1.15.6",
|
||||
"enzyme-to-json": "^3.1.2",
|
||||
"eslint": "^7.1.0",
|
||||
"father": "^4.0.0",
|
||||
"gh-pages": "^3.1.0",
|
||||
"less": "^3.0.0",
|
||||
"rc-test": "^7.0.15",
|
||||
"react": "^16.0.0",
|
||||
"react-dom": "^16.0.0",
|
||||
"typescript": "^5.0.4"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"react": ">=16.9.0",
|
||||
"react-dom": ">=16.9.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=8.x"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user