mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
types(Rate): use tsx (#8162)
This commit is contained in:
parent
6c36fadf59
commit
fc7de3658d
@ -13,7 +13,13 @@ import Icon from '../icon';
|
|||||||
|
|
||||||
const [createComponent, bem] = createNamespace('rate');
|
const [createComponent, bem] = createNamespace('rate');
|
||||||
|
|
||||||
function getRateStatus(value, index, allowHalf) {
|
type RateStatus = 'full' | 'half' | 'void';
|
||||||
|
|
||||||
|
function getRateStatus(
|
||||||
|
value: number,
|
||||||
|
index: number,
|
||||||
|
allowHalf: boolean
|
||||||
|
): RateStatus {
|
||||||
if (value >= index) {
|
if (value >= index) {
|
||||||
return 'full';
|
return 'full';
|
||||||
}
|
}
|
||||||
@ -59,7 +65,7 @@ export default createComponent({
|
|||||||
emits: ['change', 'update:modelValue'],
|
emits: ['change', 'update:modelValue'],
|
||||||
|
|
||||||
setup(props, { emit }) {
|
setup(props, { emit }) {
|
||||||
let ranges;
|
let ranges: Array<{ left: number; score: number }>;
|
||||||
|
|
||||||
const touch = useTouch();
|
const touch = useTouch();
|
||||||
const [itemRefs, setItemRefs] = useRefs();
|
const [itemRefs, setItemRefs] = useRefs();
|
||||||
@ -68,21 +74,21 @@ export default createComponent({
|
|||||||
props.readonly || props.disabled || !props.touchable;
|
props.readonly || props.disabled || !props.touchable;
|
||||||
|
|
||||||
const list = computed(() => {
|
const list = computed(() => {
|
||||||
const list = [];
|
const list: RateStatus[] = [];
|
||||||
for (let i = 1; i <= props.count; i++) {
|
for (let i = 1; i <= props.count; i++) {
|
||||||
list.push(getRateStatus(props.modelValue, i, props.allowHalf));
|
list.push(getRateStatus(props.modelValue, i, props.allowHalf));
|
||||||
}
|
}
|
||||||
return list;
|
return list;
|
||||||
});
|
});
|
||||||
|
|
||||||
const select = (index) => {
|
const select = (index: number) => {
|
||||||
if (!props.disabled && !props.readonly && index !== props.modelValue) {
|
if (!props.disabled && !props.readonly && index !== props.modelValue) {
|
||||||
emit('update:modelValue', index);
|
emit('update:modelValue', index);
|
||||||
emit('change', index);
|
emit('change', index);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const getScoreByPosition = (x) => {
|
const getScoreByPosition = (x: number) => {
|
||||||
for (let i = ranges.length - 1; i > 0; i--) {
|
for (let i = ranges.length - 1; i > 0; i--) {
|
||||||
if (x > ranges[i].left) {
|
if (x > ranges[i].left) {
|
||||||
return ranges[i].score;
|
return ranges[i].score;
|
||||||
@ -91,7 +97,7 @@ export default createComponent({
|
|||||||
return props.allowHalf ? 0.5 : 1;
|
return props.allowHalf ? 0.5 : 1;
|
||||||
};
|
};
|
||||||
|
|
||||||
const onTouchStart = (event) => {
|
const onTouchStart = (event: TouchEvent) => {
|
||||||
if (untouchable()) {
|
if (untouchable()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -113,7 +119,7 @@ export default createComponent({
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const onTouchMove = (event) => {
|
const onTouchMove = (event: TouchEvent) => {
|
||||||
if (untouchable()) {
|
if (untouchable()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -127,7 +133,7 @@ export default createComponent({
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const renderStar = (status, index) => {
|
const renderStar = (status: RateStatus, index: number) => {
|
||||||
const {
|
const {
|
||||||
icon,
|
icon,
|
||||||
size,
|
size,
|
||||||
@ -159,10 +165,10 @@ export default createComponent({
|
|||||||
role="radio"
|
role="radio"
|
||||||
style={style}
|
style={style}
|
||||||
class={bem('item')}
|
class={bem('item')}
|
||||||
tabindex="0"
|
tabindex={0}
|
||||||
aria-setsize={count}
|
aria-setsize={+count}
|
||||||
aria-posinset={score}
|
aria-posinset={score}
|
||||||
aria-checked={String(!isVoid)}
|
aria-checked={!isVoid}
|
||||||
>
|
>
|
||||||
<Icon
|
<Icon
|
||||||
size={size}
|
size={size}
|
||||||
@ -201,7 +207,7 @@ export default createComponent({
|
|||||||
readonly: props.readonly,
|
readonly: props.readonly,
|
||||||
disabled: props.disabled,
|
disabled: props.disabled,
|
||||||
})}
|
})}
|
||||||
tabindex="0"
|
tabindex={0}
|
||||||
onTouchstart={onTouchStart}
|
onTouchstart={onTouchStart}
|
||||||
onTouchmove={onTouchMove}
|
onTouchmove={onTouchMove}
|
||||||
>
|
>
|
Loading…
x
Reference in New Issue
Block a user