forked from jd-opensource/nutui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(collapse): script setup (jd-opensource#2907)
- Loading branch information
Showing
18 changed files
with
561 additions
and
514 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 |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<template> | ||
<view class="nut-collapse"> | ||
<slot></slot> | ||
</view> | ||
</template> | ||
<script setup lang="ts"> | ||
import { provide, ref, watch } from 'vue'; | ||
import { COLLAPSE_KEY, type CollapseValue } from './types'; | ||
defineOptions({ | ||
name: 'NutCollapse' | ||
}); | ||
export type CollapseProps = Partial<{ | ||
modelValue: CollapseValue; | ||
accordion: boolean; | ||
}>; | ||
const props = withDefaults(defineProps<CollapseProps>(), { | ||
modelValue: '', | ||
accordion: false | ||
}); | ||
const emit = defineEmits(['update:modelValue', 'change']); | ||
const innerValue = ref(props.modelValue || (props.accordion ? '' : [])); | ||
watch( | ||
() => props.modelValue, | ||
(val) => { | ||
innerValue.value = val; | ||
} | ||
); | ||
const changeVal = (val: string | number | Array<string | number>, name: string | number, status = true) => { | ||
innerValue.value = val; | ||
emit('update:modelValue', val); | ||
emit('change', val, name, status); | ||
}; | ||
const updateVal = (name: string | number) => { | ||
if (props.accordion) { | ||
if (innerValue.value === name) { | ||
changeVal('', name, false); | ||
} else { | ||
changeVal(name, name, true); | ||
} | ||
} else { | ||
if (Array.isArray(innerValue.value)) { | ||
if (innerValue.value.includes(name)) { | ||
const newValue = innerValue.value.filter((v: string | number) => v !== name); | ||
changeVal(newValue, name, false); | ||
} else { | ||
const newValue = innerValue.value.concat([name]); | ||
changeVal(newValue, name, true); | ||
} | ||
} else { | ||
console.warn('[NutUI] <Collapse> 未开启手风琴模式时 v-model 应为数组'); | ||
} | ||
} | ||
}; | ||
const isExpanded = (name: string | number) => { | ||
if (props.accordion) { | ||
return innerValue.value === name; | ||
} else if (Array.isArray(innerValue.value)) { | ||
return innerValue.value.includes(name); | ||
} | ||
return false; | ||
}; | ||
provide(COLLAPSE_KEY, { | ||
updateVal, | ||
isExpanded | ||
}); | ||
</script> |
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 |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<template> | ||
<view class="nut-collapse"> | ||
<slot></slot> | ||
</view> | ||
</template> | ||
<script setup lang="ts"> | ||
import { provide, ref, watch } from 'vue'; | ||
import { COLLAPSE_KEY, type CollapseValue } from './types'; | ||
defineOptions({ | ||
name: 'NutCollapse' | ||
}); | ||
export type CollapseProps = Partial<{ | ||
modelValue: CollapseValue; | ||
accordion: boolean; | ||
}>; | ||
const props = withDefaults(defineProps<CollapseProps>(), { | ||
modelValue: '', | ||
accordion: false | ||
}); | ||
const emit = defineEmits(['update:modelValue', 'change']); | ||
const innerValue = ref(props.modelValue || (props.accordion ? '' : [])); | ||
watch( | ||
() => props.modelValue, | ||
(val) => { | ||
innerValue.value = val; | ||
} | ||
); | ||
const changeVal = (val: string | number | Array<string | number>, name: string | number, status = true) => { | ||
innerValue.value = val; | ||
emit('update:modelValue', val); | ||
emit('change', val, name, status); | ||
}; | ||
const updateVal = (name: string | number) => { | ||
if (props.accordion) { | ||
if (innerValue.value === name) { | ||
changeVal('', name, false); | ||
} else { | ||
changeVal(name, name, true); | ||
} | ||
} else { | ||
if (Array.isArray(innerValue.value)) { | ||
if (innerValue.value.includes(name)) { | ||
const newValue = innerValue.value.filter((v: string | number) => v !== name); | ||
changeVal(newValue, name, false); | ||
} else { | ||
const newValue = innerValue.value.concat([name]); | ||
changeVal(newValue, name, true); | ||
} | ||
} else { | ||
console.warn('[NutUI] <Collapse> 未开启手风琴模式时 v-model 应为数组'); | ||
} | ||
} | ||
}; | ||
const isExpanded = (name: string | number) => { | ||
if (props.accordion) { | ||
return innerValue.value === name; | ||
} else if (Array.isArray(innerValue.value)) { | ||
return innerValue.value.includes(name); | ||
} | ||
return false; | ||
}; | ||
provide(COLLAPSE_KEY, { | ||
updateVal, | ||
isExpanded | ||
}); | ||
</script> |
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
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import Collapse from './collapse.taro.vue'; | ||
import type { ComponentPublicInstance } from 'vue'; | ||
import { withInstall } from '@/packages/utils'; | ||
|
||
withInstall(Collapse); | ||
|
||
export type { CollapseProps } from './collapse.taro.vue'; | ||
|
||
export type { CollapseValue } from './types'; | ||
|
||
export type CollapseInstance = ComponentPublicInstance & InstanceType<typeof Collapse>; | ||
|
||
export { Collapse, Collapse as default }; |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import Collapse from './collapse.vue'; | ||
import type { ComponentPublicInstance } from 'vue'; | ||
import { withInstall } from '@/packages/utils'; | ||
|
||
withInstall(Collapse); | ||
|
||
export type { CollapseProps } from './collapse.vue'; | ||
|
||
export type { CollapseValue } from './types'; | ||
|
||
export type CollapseInstance = ComponentPublicInstance & InstanceType<typeof Collapse>; | ||
|
||
export { Collapse, Collapse as default }; |
Oops, something went wrong.