123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- import { defineComponent } from 'vue';
- import styles from './index.module.less';
- import { Cell, Collapse, CollapseItem } from 'vant';
- import iconNoCheck from './image/icon-nocheck.png';
- import iconImm from './image/icon-imm.png';
- import iconCheck from './image/icon-check.png';
- import iconCheckDisabled from './image/icon-check-disabled.png';
- export const onMenuChange = (item: any, status?: string) => {
- if (item.status === 'disabled') {
- return;
- }
- // 判断父级元素
- if (status) {
- item.status = status;
- } else {
- if (item.status === 'disabled') {
- return;
- }
- if (item.status === 'checked') {
- item.status = 'nochecked';
- } else {
- item.status = 'checked';
- }
- }
- if (Array.isArray(item.materialList)) {
- // console.log(item.status, 'item.status', status);
- item.materialList.forEach((material: any) => {
- if (material.status === 'disabled') {
- return;
- }
- if (item.status === 'nochecked') {
- material.status = 'nochecked';
- } else {
- material.status = 'checked';
- }
- });
- }
- if (Array.isArray(item.children)) {
- item.children.forEach((child: any) => {
- onMenuChange(child, item.status);
- });
- }
- };
- export const onParentChangeStatus = (parent: any, childName = 'children') => {
- let arrLength = 0;
- let checkedLength = 0;
- let indeterminateLength = 0;
- if (parent.materialList && parent.materialList.length > 0) {
- parent.materialList.forEach((item: any) => {
- if (item.status !== 'disabled') {
- arrLength += 1;
- }
- if (item.status === 'checked') {
- checkedLength += 1;
- }
- });
- }
- if (Array.isArray(parent[childName])) {
- parent[childName].forEach((item: any) => {
- if (item.status !== 'disabled') {
- arrLength += 1;
- }
- if (item.status === 'checked') {
- checkedLength += 1;
- }
- if (item.status === 'indeterminate') {
- indeterminateLength += 1;
- }
- });
- }
- if (checkedLength >= arrLength) {
- parent.status = 'checked';
- } else if (checkedLength > 0) {
- parent.status = 'indeterminate';
- } else {
- parent.status = 'nochecked';
- }
- // 只能目录才会大于0,素材是不会有
- if (indeterminateLength > 0) {
- parent.status = 'indeterminate';
- }
- };
- const ChildNode = defineComponent({
- name: 'child-node',
- props: {
- list: {
- type: Array,
- default: () => []
- },
- collapse: {
- type: String,
- default: ''
- }
- },
- emits: ['update:collapse', 'menuChange', 'materialChange'],
- setup(props, { emit }) {
- return () => (
- <Collapse
- modelValue={props.collapse}
- onUpdate:modelValue={(val: any) => {
- emit('update:collapse', val);
- }}
- border={false}
- accordion>
- {props.list?.map((point: any) => (
- <CollapseItem
- clickable={false}
- center
- class={styles.collapseChild}
- name={point.id}>
- {{
- title: () => (
- <div
- class={[
- styles.itemTitle,
- props.collapse === point.id ? styles.itemTitleActive : ''
- ]}>
- <i class={[styles.arrow]}></i>
- {point.name}
- </div>
- ),
- default: () => (
- <>
- {Array.isArray(point?.materialList) &&
- point.materialList.map((n: any) => (
- <Cell>
- {{
- title: () => n.name,
- value: () => (
- <img
- src={
- n.status === 'disabled'
- ? iconCheckDisabled
- : n.status === 'checked'
- ? iconCheck
- : iconNoCheck
- }
- class={[styles.radioBtn]}
- onClick={(e: any) => {
- e.stopPropagation();
- if (n.status === 'disabled') {
- return;
- }
- if (n.status === 'checked') {
- n.status = 'nochecked';
- } else {
- n.status = 'checked';
- }
- onParentChangeStatus(point);
- emit('menuChange');
- }}
- />
- )
- }}
- </Cell>
- ))}
- {Array.isArray(point?.children) && (
- <ChildNode
- list={point.children}
- collapse={point.collapse}
- onUpdate:collapse={val => {
- point.collapse = val;
- }}
- onMenuChange={() => {
- onParentChangeStatus(point);
- emit('menuChange');
- }}
- />
- )}
- </>
- ),
- 'right-icon': () => (
- <img
- src={
- point.status === 'indeterminate'
- ? iconImm
- : point.status === 'disabled'
- ? iconCheckDisabled
- : point.status === 'checked'
- ? iconCheck
- : iconNoCheck
- }
- class={[styles.radioBtn]}
- onClick={(e: any) => {
- e.stopPropagation();
- onMenuChange(point);
- emit('menuChange');
- }}
- />
- )
- }}
- </CollapseItem>
- ))}
- </Collapse>
- );
- }
- });
- export default ChildNode;
|