|
| 1 | +/** |
| 2 | + * @description collapse hook |
| 3 | + * @author 阿怪 |
| 4 | + * @date 2025/02/17 20:52 |
| 5 | + * @version v1.0.0 |
| 6 | + * |
| 7 | + * 江湖的业务千篇一律,复杂的代码好几百行。 |
| 8 | + */ |
| 9 | + |
| 10 | +import { defineHook } from '../../../runtime/defineHook'; |
| 11 | +import { props } from './api'; |
| 12 | +import { CollapseProps } from './props'; |
| 13 | +import { ref, watch } from 'vue'; |
| 14 | + |
| 15 | +export const collapseOptions = { |
| 16 | + name: 'MCollapse', |
| 17 | + props, |
| 18 | + emits: ['update:modelValue', 'change'] |
| 19 | +}; |
| 20 | + |
| 21 | +export const useCollapse = defineHook((props: Required<CollapseProps>, ctx) => { |
| 22 | + const innerValue = ref(props.modelValue); |
| 23 | + |
| 24 | + watch(() => props.modelValue, (val) => { |
| 25 | + innerValue.value = val; |
| 26 | + }); |
| 27 | + |
| 28 | + const toggle = () => { |
| 29 | + if (props.disabled) return; |
| 30 | + const newValue = !innerValue.value; |
| 31 | + innerValue.value = newValue; |
| 32 | + ctx.emit('update:modelValue', newValue); |
| 33 | + ctx.emit('change', newValue); |
| 34 | + }; |
| 35 | + |
| 36 | + const renderInit = () => { |
| 37 | + const collapseClass = [ |
| 38 | + 'm-collapse', |
| 39 | + { |
| 40 | + 'm-collapse-disabled': props.disabled, |
| 41 | + 'm-collapse-arrow-expanded': innerValue.value |
| 42 | + } |
| 43 | + ] |
| 44 | + |
| 45 | + const context = props.renderContext ? ( |
| 46 | + <div class={['m-collapse-content', { 'm-collapse-content-hidden': !innerValue.value }]}> |
| 47 | + {ctx.slots.content?.()} |
| 48 | + </div> |
| 49 | + ) : innerValue.value ? ( |
| 50 | + <div class="m-collapse-content"> |
| 51 | + {ctx.slots.content?.()} |
| 52 | + </div> |
| 53 | + ) : null; |
| 54 | + |
| 55 | + return { |
| 56 | + collapseClass, |
| 57 | + context, |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + return { |
| 62 | + toggle, |
| 63 | + renderInit |
| 64 | + }; |
| 65 | +}, collapseOptions); |
0 commit comments