-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: collapse component added showClose prop and closeIcon slot (#214)
* feat: collapse component added showClose prop and closeIcon slot * chore: temp commit * feat: added collapse wrapper to support accordion * test: updated unit test * docs: updated collapse document * test: updated snap
- Loading branch information
1 parent
28b383e
commit 488638c
Showing
20 changed files
with
349 additions
and
56 deletions.
There are no files selected for viewing
14 changes: 7 additions & 7 deletions
14
components/Collapse/__test__/__snapshots__/collapse.spec.ts.snap
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,15 +1,15 @@ | ||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
||
exports[`Test: KCollapse > props: content 1`] = `"<div class=\\"k-collapse k-collapse--base\\"><div class=\\"k-collapse--title k-collapse--title__dark k-collapse--title__show\\" aria-hidden=\\"true\\"> <div role=\\"\\" aria-hidden=\\"true\\" class=\\"k-icon--base k-icon--base__dark \\"><div class=\\"i-carbon-chevron-right rotate-90 k-icon-transition \\" style=\\"width: 24px; height: 24px;\\"></div></div></div> <div class=\\"k-collapse--content\\"><div class=\\"k-collapse--line\\"></div> foo</div></div>"`; | ||
exports[`Test: KCollapse > props: content 1`] = `"<div class=\\"k-collapse--base\\"><div class=\\"k-collapse--title k-collapse--title__dark\\" aria-hidden=\\"true\\"> <div role=\\"\\" aria-hidden=\\"true\\" class=\\"k-icon--base k-icon--base__dark \\"><div class=\\"i-carbon-chevron-right rotate-90 k-icon-transition \\" style=\\"width: 24px; height: 24px;\\"></div></div></div> <div class=\\"\\" style=\\"opacity: 0;\\"><div class=\\"k-collapse--line\\"></div> foo</div></div>"`; | ||
exports[`Test: KCollapse > props: show 1`] = `"<div class=\\"k-collapse k-collapse--base\\"><div class=\\"k-collapse--title k-collapse--title__dark k-collapse--title__show\\" aria-hidden=\\"true\\"> <div role=\\"\\" aria-hidden=\\"true\\" class=\\"k-icon--base k-icon--base__dark \\"><div class=\\"i-carbon-chevron-right rotate-90 k-icon-transition \\" style=\\"width: 24px; height: 24px;\\"></div></div></div> <div class=\\"k-collapse--content\\" style=\\"animation: __svelte_2259128677_0 300ms linear 0ms 1 both;\\"><div class=\\"k-collapse--line\\"></div> foo</div></div>"`; | ||
exports[`Test: KCollapse > props: show 1`] = `"<div class=\\"k-collapse--base\\"><div class=\\"k-collapse--title k-collapse--title__dark\\" aria-hidden=\\"true\\"> <div role=\\"\\" aria-hidden=\\"true\\" class=\\"k-icon--base k-icon--base__dark \\"><div class=\\"i-carbon-chevron-right rotate-90 k-icon-transition \\" style=\\"width: 24px; height: 24px;\\"></div></div></div> <div class=\\"\\" style=\\"opacity: 0; animation: __svelte_2259128677_0 300ms linear 0ms 1 both;\\"><div class=\\"k-collapse--line\\"></div> foo</div></div>"`; | ||
exports[`Test: KCollapse > props: showClose 1`] = `"<div class=\\"k-collapse k-collapse--base\\"><div class=\\"k-collapse--title k-collapse--title__dark\\" aria-hidden=\\"true\\">title-content </div> </div>"`; | ||
exports[`Test: KCollapse > props: showClose 1`] = `"<div class=\\"k-collapse--base\\"><div class=\\"k-collapse--title k-collapse--title__dark\\" aria-hidden=\\"true\\">title-content </div> </div>"`; | ||
exports[`Test: KCollapse > props: title 1`] = `"<div class=\\"k-collapse k-collapse--base\\"><div class=\\"k-collapse--title k-collapse--title__dark\\" aria-hidden=\\"true\\">title-content <div role=\\"\\" aria-hidden=\\"true\\" class=\\"k-icon--base k-icon--base__dark \\"><div class=\\"i-carbon-chevron-right k-icon-transition \\" style=\\"width: 24px; height: 24px;\\"></div></div></div> </div>"`; | ||
exports[`Test: KCollapse > props: title 1`] = `"<div class=\\"k-collapse--base\\"><div class=\\"k-collapse--title k-collapse--title__dark\\" aria-hidden=\\"true\\">title-content <div role=\\"\\" aria-hidden=\\"true\\" class=\\"k-icon--base k-icon--base__dark \\"><div class=\\"i-carbon-chevron-right k-icon-transition \\" style=\\"width: 24px; height: 24px;\\"></div></div></div> </div>"`; | ||
exports[`Test: KCollapse > slot: closeIcon 1`] = `"<div class=\\"k-collapse k-collapse--base\\"><div class=\\"k-collapse--title k-collapse--title__dark\\" aria-hidden=\\"true\\"> <div slot=\\"closeIcon\\" class=\\"flex\\"><div>🍓 closeIcon</div></div></div> </div>"`; | ||
exports[`Test: KCollapse > slot: closeIcon 1`] = `"<div class=\\"k-collapse--base\\"><div class=\\"k-collapse--title k-collapse--title__dark\\" aria-hidden=\\"true\\"> <div slot=\\"closeIcon\\" class=\\"flex\\"><div>🍓 closeIcon</div></div></div> </div>"`; | ||
exports[`Test: KCollapse > slot: content 1`] = `"<div class=\\"k-collapse k-collapse--base\\"><div class=\\"k-collapse--title k-collapse--title__dark k-collapse--title__show\\" aria-hidden=\\"true\\"> <div role=\\"\\" aria-hidden=\\"true\\" class=\\"k-icon--base k-icon--base__dark \\"><div class=\\"i-carbon-chevron-right rotate-90 k-icon-transition \\" style=\\"width: 24px; height: 24px;\\"></div></div></div> <div class=\\"k-collapse--content\\"><div class=\\"k-collapse--line\\"></div> <div slot=\\"content\\" class=\\"flex\\"><div id=\\"custom_content\\">🍞 Bread! Bread! We want to bread!</div></div></div></div>"`; | ||
exports[`Test: KCollapse > slot: content 1`] = `"<div class=\\"k-collapse--base\\"><div class=\\"k-collapse--title k-collapse--title__dark\\" aria-hidden=\\"true\\"> <div role=\\"\\" aria-hidden=\\"true\\" class=\\"k-icon--base k-icon--base__dark \\"><div class=\\"i-carbon-chevron-right rotate-90 k-icon-transition \\" style=\\"width: 24px; height: 24px;\\"></div></div></div> <div class=\\"\\" style=\\"opacity: 0;\\"><div class=\\"k-collapse--line\\"></div> <div slot=\\"content\\" class=\\"flex\\"><div id=\\"custom_content\\">🍞 Bread! Bread! We want to bread!</div></div></div></div>"`; | ||
exports[`Test: KCollapse > slot: title 1`] = `"<div class=\\"k-collapse k-collapse--base\\"><div class=\\"k-collapse--title k-collapse--title__dark\\" aria-hidden=\\"true\\"><div slot=\\"title\\" class=\\"flex\\"><div id=\\"custom_title\\">🍓 Germinal</div></div> <div role=\\"\\" aria-hidden=\\"true\\" class=\\"k-icon--base k-icon--base__dark \\"><div class=\\"i-carbon-chevron-right k-icon-transition \\" style=\\"width: 24px; height: 24px;\\"></div></div></div> </div>"`; | ||
exports[`Test: KCollapse > slot: title 1`] = `"<div class=\\"k-collapse--base\\"><div class=\\"k-collapse--title k-collapse--title__dark\\" aria-hidden=\\"true\\"><div slot=\\"title\\" class=\\"flex\\"><div id=\\"custom_title\\">🍓 Germinal</div></div> <div role=\\"\\" aria-hidden=\\"true\\" class=\\"k-icon--base k-icon--base__dark \\"><div class=\\"i-carbon-chevron-right k-icon-transition \\" style=\\"width: 24px; height: 24px;\\"></div></div></div> </div>"`; |
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
3 changes: 3 additions & 0 deletions
3
components/CollapseWrapper/__test__/__snapshots__/collapse-wrapper.spec.ts.snap
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,3 @@ | ||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
||
exports[`Test: KCollapseWrapper > props: cls 1`] = `"<div class=\\"k-collapse-wrapper k-collapse-wrapper--base k-collapse-wrapper--test\\"></div>"`; |
30 changes: 30 additions & 0 deletions
30
components/CollapseWrapper/__test__/collapse-wrapper.spec.ts
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,30 @@ | ||
import { afterEach, expect, test, describe, beforeEach } from 'vitest'; | ||
import KCollapseWrapper from '../src'; | ||
|
||
let host: HTMLElement; | ||
|
||
const initHost = () => { | ||
host = document.createElement('div'); | ||
host.setAttribute('id', 'host'); | ||
document.body.appendChild(host); | ||
}; | ||
beforeEach(() => { | ||
initHost(); | ||
}); | ||
afterEach(() => { | ||
host.remove(); | ||
}); | ||
|
||
describe('Test: KCollapseWrapper', () => { | ||
test('props: cls', async () => { | ||
const instance = new KCollapseWrapper({ | ||
target: host, | ||
props: { | ||
cls: 'k-collapse-wrapper--test' | ||
} | ||
}); | ||
expect(instance).toBeTruthy(); | ||
expect((host as HTMLElement)!.innerHTML.includes('k-collapse-wrapper--test')).toBeTruthy(); | ||
expect(host.innerHTML).matchSnapshot(); | ||
}); | ||
}); |
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,42 @@ | ||
{ | ||
"name": "@ikun-ui/collapse-wrapper", | ||
"version": "0.0.9-beta.5", | ||
"type": "module", | ||
"main": "./src/index.ts", | ||
"types": "src/index.ts", | ||
"keywords": [ | ||
"svelte", | ||
"svelte3", | ||
"web component", | ||
"component", | ||
"react", | ||
"vue", | ||
"svelte-kit", | ||
"dx" | ||
], | ||
"scripts": { | ||
"build": "npm run build:js && npm run build:svelte", | ||
"build:js": "tsc -p . --outDir dist/ --rootDir src/", | ||
"build:svelte": "svelte-strip strip src/ dist", | ||
"publish:npm": "pnpm publish --no-git-checks --access public" | ||
}, | ||
"publishConfig": { | ||
"access": "public", | ||
"main": "dist/index.js", | ||
"module": "dist/index.js", | ||
"svelte": "dist/index.js", | ||
"types": "src/index.ts" | ||
}, | ||
"dependencies": { | ||
"@ikun-ui/icon": "workspace:*", | ||
"@ikun-ui/utils": "workspace:*", | ||
"baiwusanyu-utils": "^1.0.14", | ||
"clsx": "^2.0.0" | ||
}, | ||
"devDependencies": { | ||
"@tsconfig/svelte": "^5.0.0", | ||
"svelte-strip": "^2.0.0", | ||
"tslib": "^2.6.1", | ||
"typescript": "^5.1.6" | ||
} | ||
} |
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,50 @@ | ||
<script lang="ts"> | ||
import { getPrefixCls, collapseWrapperKey } from '@ikun-ui/utils'; | ||
import { clsx } from 'clsx'; | ||
import { onMount, setContext } from 'svelte'; | ||
import type { collapseMapType, KCollapseWrapperProps } from './types'; | ||
export let cls: KCollapseWrapperProps['cls'] = ''; | ||
export let accordion: KCollapseWrapperProps['accordion'] = false; | ||
export let attrs: KCollapseWrapperProps['attrs'] = {}; | ||
const collapseMap: collapseMapType = {}; | ||
const setCollapseMap = (key: string, cb: () => void) => { | ||
collapseMap[key] = cb; | ||
}; | ||
const closeCollapse = (key: string) => { | ||
Object.keys(collapseMap).forEach((k) => { | ||
if (k !== key) { | ||
collapseMap[k](); | ||
} | ||
}); | ||
}; | ||
if (accordion) { | ||
setContext(collapseWrapperKey, { | ||
setCollapseMap, | ||
closeCollapse | ||
}); | ||
} | ||
let wrapperRef: HTMLElement | null = null; | ||
onMount(() => { | ||
if (wrapperRef) { | ||
let lastCollapse = [...wrapperRef.querySelectorAll('.k-collapse--base--wrapper')].pop(); | ||
lastCollapse && ((lastCollapse as HTMLElement).style.border = '0'); | ||
} | ||
}); | ||
const prefixCls = getPrefixCls('collapse-wrapper'); | ||
$: cnames = clsx( | ||
prefixCls, | ||
{ | ||
[`${prefixCls}--base`]: true | ||
}, | ||
cls | ||
); | ||
</script> | ||
|
||
<div class={cnames} {...$$restProps} {...attrs} bind:this={wrapperRef}> | ||
<slot /> | ||
</div> |
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,5 @@ | ||
/// <reference types="./types" /> | ||
import CollapseWrapper from './index.svelte'; | ||
export { CollapseWrapper as KCollapseWrapper }; | ||
|
||
export default CollapseWrapper; |
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,7 @@ | ||
/// <reference types="svelte" /> | ||
export type KCollapseWrapperProps = { | ||
accordion: boolean; | ||
cls: string; | ||
attrs: Record<string, string>; | ||
}; | ||
export type collapseMapType = Record<string, () => void>; |
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,11 @@ | ||
{ | ||
"extends": "@tsconfig/svelte/tsconfig.json", | ||
|
||
"compilerOptions": { | ||
"noImplicitAny": true, | ||
"strict": true, | ||
"declaration": true | ||
}, | ||
"include": ["src/**/*.ts", "src/**/*.svelte"], | ||
"exclude": ["node_modules/*", "**/*.spec.ts"] | ||
} |
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.