ant-design/components/rate/index.tsx

79 lines
1.7 KiB
TypeScript
Raw Normal View History

import * as React from 'react';
2018-08-07 21:07:52 +08:00
import * as PropTypes from 'prop-types';
2016-03-30 13:20:03 +08:00
import RcRate from 'rc-rate';
2018-12-28 16:04:05 +08:00
import omit from 'omit.js';
import Icon from '../icon';
2018-12-28 16:04:05 +08:00
import Tooltip from '../tooltip';
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
2016-03-30 13:20:03 +08:00
2016-08-10 09:46:56 +08:00
export interface RateProps {
prefixCls?: string;
count?: number;
value?: number;
defaultValue?: number;
allowHalf?: boolean;
allowClear?: boolean;
2016-08-10 09:46:56 +08:00
disabled?: boolean;
2018-12-28 16:04:05 +08:00
tooltips?: Array<string>;
onChange?: (value: number) => void;
onHoverChange?: (value: number) => void;
2017-03-06 14:23:38 +08:00
character?: React.ReactNode;
className?: string;
style?: React.CSSProperties;
2016-08-10 09:46:56 +08:00
}
2018-12-28 16:04:05 +08:00
interface RateNodeProps {
index: number;
}
2016-08-10 09:46:56 +08:00
export default class Rate extends React.Component<RateProps, any> {
2016-03-30 13:20:03 +08:00
static propTypes = {
prefixCls: PropTypes.string,
2017-03-06 14:23:38 +08:00
character: PropTypes.node,
2016-03-30 13:20:03 +08:00
};
2017-11-28 10:48:58 +08:00
2016-03-31 14:43:38 +08:00
static defaultProps = {
2018-09-02 18:46:53 +08:00
character: <Icon type="star" theme="filled" />,
2016-03-30 13:20:03 +08:00
};
2017-11-28 10:48:58 +08:00
private rcRate: any;
focus() {
this.rcRate.focus();
}
blur() {
this.rcRate.blur();
}
saveRate = (node: any) => {
this.rcRate = node;
2018-12-07 20:02:01 +08:00
};
2017-11-28 10:48:58 +08:00
2018-12-28 16:04:05 +08:00
characterRender = (node: React.ReactNode, { index }: RateNodeProps) => {
const { tooltips } = this.props;
if (!tooltips) return node;
return <Tooltip title={tooltips[index]}>{node}</Tooltip>;
};
renderRate = ({ getPrefixCls }: ConfigConsumerProps) => {
const { prefixCls, ...restProps } = this.props;
const rateProps = omit(restProps, ['tooltips']);
return (
<RcRate
ref={this.saveRate}
characterRender={this.characterRender}
{...rateProps}
prefixCls={getPrefixCls('rate', prefixCls)}
/>
);
};
2016-03-30 13:20:03 +08:00
render() {
2018-12-07 20:02:01 +08:00
return <ConfigConsumer>{this.renderRate}</ConfigConsumer>;
2016-03-30 13:20:03 +08:00
}
}