|
|
@ -1,62 +1,236 @@
|
|
|
|
import { inject, defineComponent, VNode } from 'vue';
|
|
|
|
import {
|
|
|
|
import omit from 'omit.js';
|
|
|
|
defineComponent,
|
|
|
|
|
|
|
|
ExtractPropTypes,
|
|
|
|
|
|
|
|
ref,
|
|
|
|
|
|
|
|
reactive,
|
|
|
|
|
|
|
|
VNode,
|
|
|
|
|
|
|
|
onUpdated,
|
|
|
|
|
|
|
|
onBeforeUpdate,
|
|
|
|
|
|
|
|
onMounted,
|
|
|
|
|
|
|
|
} from 'vue';
|
|
|
|
|
|
|
|
import { initDefaultProps, getPropsSlot, findDOMNode } from '../_util/props-util';
|
|
|
|
|
|
|
|
import { withInstall } from '../_util/type';
|
|
|
|
|
|
|
|
import { getOffsetLeft } from './util';
|
|
|
|
|
|
|
|
import classNames from '../_util/classNames';
|
|
|
|
import PropTypes from '../_util/vue-types';
|
|
|
|
import PropTypes from '../_util/vue-types';
|
|
|
|
import { getOptionProps, getComponent } from '../_util/props-util';
|
|
|
|
import KeyCode from '../_util/KeyCode';
|
|
|
|
import { defaultConfigProvider } from '../config-provider';
|
|
|
|
|
|
|
|
import VcRate from '../vc-rate';
|
|
|
|
|
|
|
|
import StarFilled from '@ant-design/icons-vue/StarFilled';
|
|
|
|
import StarFilled from '@ant-design/icons-vue/StarFilled';
|
|
|
|
import Tooltip from '../tooltip';
|
|
|
|
import Tooltip from '../tooltip';
|
|
|
|
import { withInstall } from '../_util/type';
|
|
|
|
import useConfigInject from '../_util/hooks/useConfigInject';
|
|
|
|
|
|
|
|
|
|
|
|
export const RateProps = {
|
|
|
|
import Star from './Star';
|
|
|
|
|
|
|
|
import { useRef } from '../_util/hooks/useRef';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const rateProps = {
|
|
|
|
prefixCls: PropTypes.string,
|
|
|
|
prefixCls: PropTypes.string,
|
|
|
|
count: PropTypes.number,
|
|
|
|
count: PropTypes.number,
|
|
|
|
value: PropTypes.number,
|
|
|
|
value: PropTypes.number,
|
|
|
|
defaultValue: PropTypes.number,
|
|
|
|
|
|
|
|
allowHalf: PropTypes.looseBool,
|
|
|
|
allowHalf: PropTypes.looseBool,
|
|
|
|
allowClear: PropTypes.looseBool,
|
|
|
|
allowClear: PropTypes.looseBool,
|
|
|
|
tooltips: PropTypes.arrayOf(PropTypes.string),
|
|
|
|
tooltips: PropTypes.arrayOf(PropTypes.string),
|
|
|
|
disabled: PropTypes.looseBool,
|
|
|
|
disabled: PropTypes.looseBool,
|
|
|
|
character: PropTypes.any,
|
|
|
|
character: PropTypes.any,
|
|
|
|
autofocus: PropTypes.looseBool,
|
|
|
|
autofocus: PropTypes.looseBool,
|
|
|
|
|
|
|
|
tabindex: PropTypes.number,
|
|
|
|
|
|
|
|
direction: PropTypes.string,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export type RateProps = Partial<ExtractPropTypes<typeof rateProps>>;
|
|
|
|
|
|
|
|
|
|
|
|
const Rate = defineComponent({
|
|
|
|
const Rate = defineComponent({
|
|
|
|
name: 'ARate',
|
|
|
|
name: 'ARate',
|
|
|
|
props: RateProps,
|
|
|
|
props: initDefaultProps(rateProps, {
|
|
|
|
setup() {
|
|
|
|
value: 0,
|
|
|
|
return {
|
|
|
|
count: 5,
|
|
|
|
configProvider: inject('configProvider', defaultConfigProvider),
|
|
|
|
allowHalf: false,
|
|
|
|
|
|
|
|
allowClear: true,
|
|
|
|
|
|
|
|
prefixCls: 'ant-rate',
|
|
|
|
|
|
|
|
tabindex: 0,
|
|
|
|
|
|
|
|
character: '★',
|
|
|
|
|
|
|
|
direction: 'ltr',
|
|
|
|
|
|
|
|
}),
|
|
|
|
|
|
|
|
emits: ['hoverChange', 'update:value', 'change', 'focus', 'blur', 'keydown'],
|
|
|
|
|
|
|
|
setup(props, { slots, attrs, emit, expose }) {
|
|
|
|
|
|
|
|
const { prefixCls, direction } = useConfigInject('rate', props);
|
|
|
|
|
|
|
|
const rateRef = ref();
|
|
|
|
|
|
|
|
const [setRef, starRefs] = useRef();
|
|
|
|
|
|
|
|
const state = reactive({
|
|
|
|
|
|
|
|
sValue: props.value,
|
|
|
|
|
|
|
|
focused: false,
|
|
|
|
|
|
|
|
cleanedValue: null,
|
|
|
|
|
|
|
|
hoverValue: undefined,
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const getStarDOM = index => {
|
|
|
|
|
|
|
|
return findDOMNode(starRefs[index]);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
},
|
|
|
|
const getStarValue = (index, x) => {
|
|
|
|
methods: {
|
|
|
|
const reverse = direction.value === 'rtl';
|
|
|
|
characterRender(node: VNode, { index }) {
|
|
|
|
let value = index + 1;
|
|
|
|
const { tooltips } = this.$props;
|
|
|
|
if (props.allowHalf) {
|
|
|
|
|
|
|
|
const starEle = getStarDOM(index);
|
|
|
|
|
|
|
|
const leftDis = getOffsetLeft(starEle);
|
|
|
|
|
|
|
|
const width = starEle.clientWidth;
|
|
|
|
|
|
|
|
if (reverse && x - leftDis > width / 2) {
|
|
|
|
|
|
|
|
value -= 0.5;
|
|
|
|
|
|
|
|
} else if (!reverse && x - leftDis < width / 2) {
|
|
|
|
|
|
|
|
value -= 0.5;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
const changeValue = (value: number) => {
|
|
|
|
|
|
|
|
state.sValue = value;
|
|
|
|
|
|
|
|
emit('update:value', value);
|
|
|
|
|
|
|
|
emit('change', value);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const onHover = (e: MouseEvent, index) => {
|
|
|
|
|
|
|
|
const hoverValue = getStarValue(index, e.pageX);
|
|
|
|
|
|
|
|
if (hoverValue !== state.cleanedValue) {
|
|
|
|
|
|
|
|
state.hoverValue = hoverValue;
|
|
|
|
|
|
|
|
state.cleanedValue = null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
emit('hoverChange', hoverValue);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
const onMouseLeave = () => {
|
|
|
|
|
|
|
|
state.hoverValue = undefined;
|
|
|
|
|
|
|
|
state.cleanedValue = null;
|
|
|
|
|
|
|
|
emit('hoverChange', undefined);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
const onClick = (event: MouseEvent, index) => {
|
|
|
|
|
|
|
|
const { allowClear } = props;
|
|
|
|
|
|
|
|
const newValue = getStarValue(index, event.pageX);
|
|
|
|
|
|
|
|
let isReset = false;
|
|
|
|
|
|
|
|
if (allowClear) {
|
|
|
|
|
|
|
|
isReset = newValue === state.sValue;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
onMouseLeave();
|
|
|
|
|
|
|
|
changeValue(isReset ? 0 : newValue);
|
|
|
|
|
|
|
|
state.cleanedValue = isReset ? newValue : null;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
const onFocus = () => {
|
|
|
|
|
|
|
|
state.focused = true;
|
|
|
|
|
|
|
|
emit('focus');
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
const onBlur = () => {
|
|
|
|
|
|
|
|
state.focused = false;
|
|
|
|
|
|
|
|
emit('blur');
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
const onKeyDown = event => {
|
|
|
|
|
|
|
|
const { keyCode } = event;
|
|
|
|
|
|
|
|
const { count, allowHalf } = props;
|
|
|
|
|
|
|
|
const reverse = direction.value === 'rtl';
|
|
|
|
|
|
|
|
if (keyCode === KeyCode.RIGHT && state.sValue < count && !reverse) {
|
|
|
|
|
|
|
|
if (allowHalf) {
|
|
|
|
|
|
|
|
state.sValue += 0.5;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
state.sValue += 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
changeValue(state.sValue);
|
|
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
} else if (keyCode === KeyCode.LEFT && state.sValue > 0 && !reverse) {
|
|
|
|
|
|
|
|
if (allowHalf) {
|
|
|
|
|
|
|
|
state.sValue -= 0.5;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
state.sValue -= 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
changeValue(state.sValue);
|
|
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
} else if (keyCode === KeyCode.RIGHT && state.sValue > 0 && reverse) {
|
|
|
|
|
|
|
|
if (allowHalf) {
|
|
|
|
|
|
|
|
state.sValue -= 0.5;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
state.sValue -= 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
changeValue(state.sValue);
|
|
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
} else if (keyCode === KeyCode.LEFT && state.sValue < count && reverse) {
|
|
|
|
|
|
|
|
if (allowHalf) {
|
|
|
|
|
|
|
|
state.sValue += 0.5;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
state.sValue += 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
changeValue(state.sValue);
|
|
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
emit('keydown', event);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const focus = () => {
|
|
|
|
|
|
|
|
if (!props.disabled) {
|
|
|
|
|
|
|
|
rateRef.value.focus();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
const blur = () => {
|
|
|
|
|
|
|
|
if (!props.disabled) {
|
|
|
|
|
|
|
|
rateRef.value.blur();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
expose({
|
|
|
|
|
|
|
|
focus,
|
|
|
|
|
|
|
|
blur,
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
onMounted(() => {
|
|
|
|
|
|
|
|
const { autoFocus, disabled } = props;
|
|
|
|
|
|
|
|
if (autoFocus && !disabled) {
|
|
|
|
|
|
|
|
focus();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const characterRender = (node: VNode, { index }) => {
|
|
|
|
|
|
|
|
const { tooltips } = props;
|
|
|
|
if (!tooltips) return node;
|
|
|
|
if (!tooltips) return node;
|
|
|
|
return <Tooltip title={tooltips[index]}>{node}</Tooltip>;
|
|
|
|
return <Tooltip title={tooltips[index]}>{node}</Tooltip>;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
focus() {
|
|
|
|
const character = getPropsSlot(slots, props, 'character') || <StarFilled />;
|
|
|
|
(this.$refs.refRate as HTMLUListElement).focus();
|
|
|
|
|
|
|
|
},
|
|
|
|
return () => {
|
|
|
|
blur() {
|
|
|
|
const { count, allowHalf, disabled, tabindex } = props;
|
|
|
|
(this.$refs.refRate as HTMLUListElement).blur();
|
|
|
|
const { class: className, style } = attrs;
|
|
|
|
},
|
|
|
|
const stars = [];
|
|
|
|
},
|
|
|
|
const disabledClass = disabled ? `${prefixCls.value}-disabled` : '';
|
|
|
|
render() {
|
|
|
|
for (let index = 0; index < count; index++) {
|
|
|
|
const { prefixCls: customizePrefixCls, ...restProps } = getOptionProps(this);
|
|
|
|
stars.push(
|
|
|
|
const { getPrefixCls } = this.configProvider;
|
|
|
|
<Star
|
|
|
|
const prefixCls = getPrefixCls('rate', customizePrefixCls);
|
|
|
|
ref={setRef}
|
|
|
|
|
|
|
|
key={index}
|
|
|
|
const character = getComponent(this, 'character') || <StarFilled />;
|
|
|
|
index={index}
|
|
|
|
const rateProps = {
|
|
|
|
count={count}
|
|
|
|
character,
|
|
|
|
disabled={disabled}
|
|
|
|
characterRender: this.characterRender,
|
|
|
|
prefixCls={`${prefixCls.value}-star`}
|
|
|
|
prefixCls,
|
|
|
|
allowHalf={allowHalf}
|
|
|
|
...omit(restProps, ['tooltips']),
|
|
|
|
value={state.hoverValue === undefined ? state.sValue : state.hoverValue}
|
|
|
|
...this.$attrs,
|
|
|
|
onClick={onClick}
|
|
|
|
ref: 'refRate',
|
|
|
|
onHover={onHover}
|
|
|
|
};
|
|
|
|
character={character}
|
|
|
|
return <VcRate {...rateProps} />;
|
|
|
|
characterRender={characterRender}
|
|
|
|
|
|
|
|
focused={state.focused}
|
|
|
|
|
|
|
|
/>,
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
const rateClassName = classNames(prefixCls.value, disabledClass, className, {
|
|
|
|
|
|
|
|
[`${prefixCls.value}-rtl`]: direction.value === 'rtl',
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
|
|
|
<ul
|
|
|
|
|
|
|
|
{...attrs}
|
|
|
|
|
|
|
|
class={rateClassName}
|
|
|
|
|
|
|
|
style={style}
|
|
|
|
|
|
|
|
onMouseleave={disabled ? null : onMouseLeave}
|
|
|
|
|
|
|
|
tabindex={disabled ? -1 : tabindex}
|
|
|
|
|
|
|
|
onFocus={disabled ? null : onFocus}
|
|
|
|
|
|
|
|
onBlur={disabled ? null : onBlur}
|
|
|
|
|
|
|
|
onKeydown={disabled ? null : onKeyDown}
|
|
|
|
|
|
|
|
ref={rateRef}
|
|
|
|
|
|
|
|
role="radiogroup"
|
|
|
|
|
|
|
|
>
|
|
|
|
|
|
|
|
{stars}
|
|
|
|
|
|
|
|
</ul>
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
};
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|