tangjinzhou
4 years ago
8 changed files with 393 additions and 50 deletions
@ -0,0 +1,14 @@
|
||||
import { onBeforeUpdate, readonly, ref, DeepReadonly, UnwrapRef } from 'vue'; |
||||
|
||||
export type UseRef = [(el: any) => void, DeepReadonly<UnwrapRef<any[]>>]; |
||||
|
||||
export const useRef = (): UseRef => { |
||||
const refs = ref<any>([]); |
||||
const setRef = (el: any) => { |
||||
refs.value.push(el); |
||||
}; |
||||
onBeforeUpdate(() => { |
||||
refs.value = []; |
||||
}); |
||||
return [setRef, readonly(refs)]; |
||||
}; |
@ -0,0 +1,89 @@
|
||||
import { defineComponent, computed, ExtractPropTypes } from 'vue'; |
||||
import { getPropsSlot } from '../_util/props-util'; |
||||
import PropTypes from '../_util/vue-types'; |
||||
|
||||
export const starProps = { |
||||
value: PropTypes.number, |
||||
index: PropTypes.number, |
||||
prefixCls: PropTypes.string, |
||||
allowHalf: PropTypes.looseBool, |
||||
disabled: PropTypes.looseBool, |
||||
character: PropTypes.any, |
||||
characterRender: PropTypes.func, |
||||
focused: PropTypes.looseBool, |
||||
count: PropTypes.number, |
||||
|
||||
onClick: PropTypes.func, |
||||
onHover: PropTypes.func, |
||||
}; |
||||
|
||||
export type StarProps = Partial<ExtractPropTypes<typeof starProps>>; |
||||
|
||||
export default defineComponent({ |
||||
name: 'Star', |
||||
inheritAttrs: false, |
||||
props: starProps, |
||||
setup(props, { slots, emit }) { |
||||
const onHover = e => { |
||||
const { index } = props; |
||||
emit('hover', e, index); |
||||
}; |
||||
const onClick = e => { |
||||
const { index } = props; |
||||
emit('click', e, index); |
||||
}; |
||||
const onKeyDown = e => { |
||||
const { index } = props; |
||||
if (e.keyCode === 13) { |
||||
emit('click', e, index); |
||||
} |
||||
}; |
||||
|
||||
const getClassName = computed(() => { |
||||
const { prefixCls, index, value, allowHalf, focused } = props; |
||||
const starValue = index + 1; |
||||
let className = prefixCls; |
||||
if (value === 0 && index === 0 && focused) { |
||||
className += ` ${prefixCls}-focused`; |
||||
} else if (allowHalf && value + 0.5 >= starValue && value < starValue) { |
||||
className += ` ${prefixCls}-half ${prefixCls}-active`; |
||||
if (focused) { |
||||
className += ` ${prefixCls}-focused`; |
||||
} |
||||
} else { |
||||
className += starValue <= value ? ` ${prefixCls}-full` : ` ${prefixCls}-zero`; |
||||
if (starValue === value && focused) { |
||||
className += ` ${prefixCls}-focused`; |
||||
} |
||||
} |
||||
return className; |
||||
}); |
||||
|
||||
const character = getPropsSlot(slots, props, 'character'); |
||||
|
||||
return () => { |
||||
const { disabled, prefixCls, characterRender, index, count, value } = props; |
||||
let star = ( |
||||
<li class={getClassName.value}> |
||||
<div |
||||
onClick={disabled ? null : onClick} |
||||
onKeydown={disabled ? null : onKeyDown} |
||||
onMousemove={disabled ? null : onHover} |
||||
role="radio" |
||||
aria-checked={value > index ? 'true' : 'false'} |
||||
aria-posinset={index + 1} |
||||
aria-setsize={count} |
||||
tabindex={disabled ? -1 : 0} |
||||
> |
||||
<div class={`${prefixCls}-first`}>{character}</div> |
||||
<div class={`${prefixCls}-second`}>{character}</div> |
||||
</div> |
||||
</li> |
||||
); |
||||
if (characterRender) { |
||||
star = characterRender(star, props); |
||||
} |
||||
return star; |
||||
}; |
||||
}, |
||||
}); |
@ -0,0 +1,21 @@
|
||||
.@{rate-prefix-cls} { |
||||
&-rtl { |
||||
direction: rtl; |
||||
} |
||||
|
||||
&-star { |
||||
&:not(:last-child) { |
||||
.@{rate-prefix-cls}-rtl & { |
||||
margin-right: 0; |
||||
margin-left: 8px; |
||||
} |
||||
} |
||||
|
||||
&-first { |
||||
.@{rate-prefix-cls}-rtl & { |
||||
right: 0; |
||||
left: auto; |
||||
} |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,42 @@
|
||||
/* eslint-disable import/prefer-default-export */ |
||||
|
||||
function getScroll(w: Window) { |
||||
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: HTMLElement) { |
||||
let x: number; |
||||
let y: number; |
||||
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: HTMLElement) { |
||||
const pos = getClientPosition(el); |
||||
const doc = el.ownerDocument; |
||||
// Only IE use `parentWindow`
|
||||
const w: Window = doc.defaultView || (doc as any).parentWindow; |
||||
pos.left += getScroll(w); |
||||
return pos.left; |
||||
} |
Loading…
Reference in new issue