From f3e9e53f3d7ee3c26af95d380a7da752e9cd7fe2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E6=99=BA=E5=AD=90?= Date: Fri, 11 Feb 2022 09:09:21 +0800 Subject: [PATCH] fix(hooks): `namespace.is` default state (#5941) --- packages/hooks/__tests__/use-namespace.spec.ts | 4 ++++ packages/hooks/use-namespace/index.ts | 9 +++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/packages/hooks/__tests__/use-namespace.spec.ts b/packages/hooks/__tests__/use-namespace.spec.ts index 23160a2583..63c72abf3f 100644 --- a/packages/hooks/__tests__/use-namespace.spec.ts +++ b/packages/hooks/__tests__/use-namespace.spec.ts @@ -18,11 +18,14 @@ const TestComp = { ns.be('content', 'active'), ns.em('content', 'active'), ns.bem('body', 'content', 'active'), + ns.is('focus'), ns.e(), // return empty string ns.m(), // return empty string ns.be(), // return empty string ns.em(), // return empty string ns.bem(), // return empty string + ns.is('hover', undefined), // return empty string + ns.is('clicked', false), // return empty string ], }, 'text' @@ -67,6 +70,7 @@ describe('use-locale', () => { 'ep-table-content__active', // be('content', 'active') 'ep-table__content--active', // em('content', 'active') 'ep-table-body__content--active', // bem('body', 'content', 'active') + 'is-focus', // is('focus') ].join('~') ) }) diff --git a/packages/hooks/use-namespace/index.ts b/packages/hooks/use-namespace/index.ts index 3e70a902f8..c6efc5eaf0 100644 --- a/packages/hooks/use-namespace/index.ts +++ b/packages/hooks/use-namespace/index.ts @@ -49,8 +49,13 @@ export const useNamespace = (block: string) => { blockSuffix && element && modifier ? _bem(unref(namespace), block, blockSuffix, element, modifier) : '' - const is = (name: string, state = true) => - name && state ? `${statePrefix}${name}` : '' + const is: { + (name: string, state: boolean | undefined): string + (name: string): string + } = (name: string, ...args: [boolean | undefined] | []) => { + const state = args.length >= 1 ? args[0]! : true + return name && state ? `${statePrefix}${name}` : '' + } return { namespace, b,