feat(rate): clsPrefix

This commit is contained in:
07akioni 2021-04-16 23:55:42 +08:00
parent 56017475af
commit 41f263d7b8
3 changed files with 51 additions and 36 deletions

View File

@ -1,2 +1,2 @@
/* istanbul ignore file */
export { default as NElement } from './src/Element' export { default as NElement } from './src/Element'
export type { ElementProps } from './src/Element'

View File

@ -1,2 +1,2 @@
/* istanbul ignore file */
export { default as NRate } from './src/Rate' export { default as NRate } from './src/Rate'
export type { RateProps } from './src/Rate'

View File

@ -10,18 +10,16 @@ import {
} from 'vue' } from 'vue'
import { useMergedState } from 'vooks' import { useMergedState } from 'vooks'
import { NBaseIcon } from '../../_internal' import { NBaseIcon } from '../../_internal'
import { useTheme, useFormItem } from '../../_mixins' import { useTheme, useFormItem, useConfig } from '../../_mixins'
import type { ThemeProps } from '../../_mixins' import type { ThemeProps } from '../../_mixins'
import { call } from '../../_utils' import { call } from '../../_utils'
import type { MaybeArray } from '../../_utils' import type { ExtractPublicPropTypes, MaybeArray } from '../../_utils'
import { rateLight } from '../styles' import { rateLight } from '../styles'
import type { RateTheme } from '../styles' import type { RateTheme } from '../styles'
import style from './styles/index.cssr' import style from './styles/index.cssr'
import StarIcon from './StarIcon' import StarIcon from './StarIcon'
export default defineComponent({ const rateProps = {
name: 'Rate',
props: {
...(useTheme.props as ThemeProps<RateTheme>), ...(useTheme.props as ThemeProps<RateTheme>),
count: { count: {
type: Number, type: Number,
@ -46,9 +44,23 @@ export default defineComponent({
onUpdateValue: [Function, Array] as PropType< onUpdateValue: [Function, Array] as PropType<
MaybeArray<(value: number) => void> MaybeArray<(value: number) => void>
> >
}, } as const
export type RateProps = ExtractPublicPropTypes<typeof rateProps>
export default defineComponent({
name: 'Rate',
props: rateProps,
setup (props) { setup (props) {
const themeRef = useTheme('Rate', 'Rate', style, rateLight, props) const { mergedClsPrefix } = useConfig(props)
const themeRef = useTheme(
'Rate',
'Rate',
style,
rateLight,
props,
mergedClsPrefix
)
const controlledValueRef = toRef(props, 'value') const controlledValueRef = toRef(props, 'value')
const uncontrolledValueRef = ref(props.defaultValue) const uncontrolledValueRef = ref(props.defaultValue)
const hoverIndexRef = ref<number | null>(null) const hoverIndexRef = ref<number | null>(null)
@ -76,6 +88,7 @@ export default defineComponent({
doUpdateValue(index + 1) doUpdateValue(index + 1)
} }
return { return {
cPrefix: mergedClsPrefix,
mergedValue: useMergedState(controlledValueRef, uncontrolledValueRef), mergedValue: useMergedState(controlledValueRef, uncontrolledValueRef),
hoverIndex: hoverIndexRef, hoverIndex: hoverIndexRef,
handleMouseEnter, handleMouseEnter,
@ -96,10 +109,10 @@ export default defineComponent({
} }
}, },
render () { render () {
const { hoverIndex, mergedValue } = this const { hoverIndex, mergedValue, cPrefix } = this
return ( return (
<div <div
class="n-rate" class={`${cPrefix}-rate`}
style={this.cssVars as CSSProperties} style={this.cssVars as CSSProperties}
onMouseleave={this.handleMouseLeave} onMouseleave={this.handleMouseLeave}
> >
@ -107,9 +120,9 @@ export default defineComponent({
<div <div
key={index} key={index}
class={[ class={[
'n-rate__item', `${cPrefix}-rate__item`,
{ {
'n-rate__item--active': [`${cPrefix}-rate__item--active`]:
hoverIndex !== null hoverIndex !== null
? index <= hoverIndex ? index <= hoverIndex
: index < mergedValue : index < mergedValue
@ -118,7 +131,9 @@ export default defineComponent({
onClick={() => this.handleClick(index)} onClick={() => this.handleClick(index)}
onMouseenter={() => this.handleMouseEnter(index)} onMouseenter={() => this.handleMouseEnter(index)}
> >
<NBaseIcon>{{ default: () => StarIcon }}</NBaseIcon> <NBaseIcon clsPrefix={cPrefix}>
{{ default: () => StarIcon }}
</NBaseIcon>
</div> </div>
))} ))}
</div> </div>