-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
jaskang
committed
Jun 30, 2023
1 parent
5b90c8a
commit f802128
Showing
9 changed files
with
173 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,116 @@ | ||
import { classed } from '@tw-classed/core' | ||
import { | ||
cloneVNode, | ||
computed, | ||
defineComponent, | ||
type EmitsOptions, | ||
type ExtractPropTypes, | ||
type ExtractPublicPropTypes, | ||
type PropType, | ||
ref, | ||
type SlotsType, | ||
toRef, | ||
type VNode, | ||
} from 'vue' | ||
|
||
import { useControllable } from '@/hooks/controllable' | ||
import { useTheme } from '@/theme' | ||
import { type OptionItem, type OptionValue, toMultipleVal } from '@/utils/option' | ||
|
||
import { SelectorIcon } from '../Icon' | ||
|
||
const props = { | ||
color: { | ||
type: String as PropType<Color>, | ||
value: [String, Number, Array] as PropType<OptionValue | Array<OptionValue>>, | ||
options: { | ||
type: Array as PropType<OptionItem[]>, | ||
default: () => [], | ||
}, | ||
loading: Boolean, | ||
disabled: Boolean, | ||
// 多选 | ||
multiple: Boolean, | ||
} | ||
|
||
export type ListProps = ExtractPropTypes<typeof props> | ||
|
||
export type ListPublicProps = ExtractPublicPropTypes<typeof props> | ||
|
||
export type ListCssVars = { | ||
'--t-btn-text-color': string | ||
'--t-btn-border-color': string | ||
'--t-btn-bg': string | ||
|
||
'--t-btn-text-color-hover': string | ||
'--t-btn-border-color-hover': string | ||
'--t-btn-bg-hover': string | ||
|
||
'--t-btn-ring-color': string | ||
'--t-list-accent-color': string | ||
'--t-list-ring-color': string | ||
} | ||
|
||
export const List = defineComponent({ | ||
name: 'TList', | ||
props, | ||
emits: { | ||
click: (payload: MouseEvent) => true, | ||
'update:value': (val: Required<ListProps>['value']) => true, | ||
change: (val: Required<ListProps>['value']) => true, | ||
}, | ||
slots: Object as SlotsType<{ | ||
default: () => VNode | ||
icon: () => VNode | ||
}>, | ||
setup(props, { slots, emit }) { | ||
setup(props, { slots, emit, attrs }) { | ||
const { colors } = useTheme() | ||
|
||
const { cssVars, cls } = useStyle(() => { | ||
return { | ||
...props, | ||
} | ||
}) | ||
const [val, setVal] = useControllable( | ||
() => (props.multiple ? toMultipleVal(props.value) : props.value), | ||
val => { | ||
emit('update:value', val) | ||
emit('change', val) | ||
}, | ||
props.multiple ? [] : undefined | ||
) | ||
|
||
const cssVars = computed<ListCssVars>(() => ({ | ||
'--t-list-accent-color': colors.value.primary[500], | ||
'--t-list-ring-color': colors.value.primary[500], | ||
})) | ||
|
||
const hasIcon = computed(() => !!slots.icon || props.loading) | ||
const onClick = (e: MouseEvent) => { | ||
if (!props.disabled) { | ||
emit('click', e) | ||
const itemClickHandler = (item: OptionItem) => { | ||
if (item.disabled) return | ||
if (props.multiple) { | ||
const set = new Set((val.value as any[]) || []) | ||
if (set.has(item.value)) { | ||
set.delete(item.value) | ||
} else { | ||
set.add(item.value) | ||
} | ||
setVal(Array.from(set)) | ||
} else { | ||
setVal(item.value) | ||
} | ||
} | ||
const isActived = (item: OptionItem) => { | ||
if (props.multiple) { | ||
return (val.value as any[]).includes(item.value) | ||
} | ||
return item.value === val.value | ||
} | ||
return () => ( | ||
<div style={cssVars.value} class={cls.value}> | ||
{slots.default?.()} | ||
</div> | ||
<ul | ||
style={cssVars.value} | ||
class="w-full overflow-auto rounded-md bg-white py-1 text-sm shadow-md ring-1 ring-black ring-opacity-5 focus:outline-none " | ||
tabindex="-1" | ||
> | ||
{props.options.length > 0 ? ( | ||
props.options.map(item => ( | ||
<li | ||
class={[ | ||
'relative flex cursor-default select-none px-3 py-2 transition-colors ease-in-out', | ||
isActived(item) | ||
? 'bg-[--t-list-accent-color] font-semibold text-white' | ||
: item.disabled | ||
? 'font-normal text-gray-400' | ||
: 'font-normal text-gray-700 hover:bg-gray-100', | ||
]} | ||
onClick={() => itemClickHandler(item)} | ||
> | ||
<span class="block truncate font-normal"> {item.label} </span> | ||
</li> | ||
)) | ||
) : ( | ||
<li>没有数据</li> | ||
)} | ||
</ul> | ||
) | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
f802128
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
elenext – ./
elenext-git-main-jaskang.vercel.app
elenext.vercel.app
elenext-jaskang.vercel.app
elenext.jaskang.vip