From 0cb618be58b29521b358d6ec629529addacd59f7 Mon Sep 17 00:00:00 2001 From: ozh Date: Wed, 9 Sep 2026 15:12:45 +0800 Subject: [PATCH] =?UTF-8?q?fix(family):=20showModal=20editable=20=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E6=89=A9=E5=B1=95=E4=B8=8E=E6=B5=8B=E8=AF=95=E7=A9=BA?= =?UTF-8?q?=E5=80=BC=E6=96=AD=E8=A8=80=EF=BC=8Ctsc=20--noEmit=20=E6=B8=85?= =?UTF-8?q?=E9=9B=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/family/index.tsx | 55 +++++++++++++++++++------------- tests/unit/family/family.spec.ts | 16 ++++++---- 2 files changed, 42 insertions(+), 29 deletions(-) diff --git a/src/pages/family/index.tsx b/src/pages/family/index.tsx index 91f9bdb..e1fe8c0 100644 --- a/src/pages/family/index.tsx +++ b/src/pages/family/index.tsx @@ -10,6 +10,19 @@ import { navigateTo } from '@/utils/navigation' import { showToast } from '@/utils/toast' import styles from './index.module.scss' +// Taro 类型未跟进 wx.showModal 的 editable 扩展(微信 2.17.1+),运行时真实存在 +interface EditableModalOption extends Taro.showModal.Option { + editable?: boolean + placeholderText?: string +} + +interface EditableModalResult extends Taro.showModal.SuccessCallbackResult { + content?: string +} + +const showEditableModal = (option: EditableModalOption) => + Taro.showModal(option as Taro.showModal.Option) as Promise + export default function Family() { const [list, setList] = useState([]) const [showCreateModal, setShowCreateModal] = useState(false) @@ -30,37 +43,35 @@ export default function Family() { const goDetail = (id: string) => navigateTo(`${ROUTES.FAMILY_DETAIL}?id=${id}`) const createFamily = () => { - Taro.showModal({ + showEditableModal({ title: '新建家庭', editable: true, - placeholderText: '请输入家庭名', - success: async ({ confirm, content }) => { - if (!confirm) return - try { - await addFamily(content ?? '') - setShowCreateModal(false) - await reload() - } catch (error) { - showToast(error instanceof Error ? error.message : '创建失败') - } + placeholderText: '请输入家庭名' + }).then(async ({ confirm, content }) => { + if (!confirm) return + try { + await addFamily(content ?? '') + setShowCreateModal(false) + await reload() + } catch (error) { + showToast(error instanceof Error ? error.message : '创建失败') } }) } const joinByCode = () => { - Taro.showModal({ + showEditableModal({ title: '加入家庭', editable: true, - placeholderText: '请输入家庭编号', - success: async ({ confirm, content }) => { - if (!confirm) return - try { - await joinFamily(content ?? '') - setShowCreateModal(false) - await reload() - } catch (error) { - showToast(error instanceof Error ? error.message : '加入失败') - } + placeholderText: '请输入家庭编号' + }).then(async ({ confirm, content }) => { + if (!confirm) return + try { + await joinFamily(content ?? '') + setShowCreateModal(false) + await reload() + } catch (error) { + showToast(error instanceof Error ? error.message : '加入失败') } }) } diff --git a/tests/unit/family/family.spec.ts b/tests/unit/family/family.spec.ts index 2974a1a..b154375 100644 --- a/tests/unit/family/family.spec.ts +++ b/tests/unit/family/family.spec.ts @@ -68,10 +68,11 @@ describe('家庭圈 mock 数据层', () => { it('getFamilyDetail 返回家庭 + 成员 + 关爱对象', async () => { await getFamilies() const detail = await getFamilyDetail('846443196293189') - expect(detail.family.name).toBe('JollyPlace') - expect(detail.members.length).toBe(2) - expect(detail.cared.length).toBe(1) - expect(detail.cared[0].name).toBe('Jay') + expect(detail).not.toBeNull() + expect(detail!.family.name).toBe('JollyPlace') + expect(detail!.members.length).toBe(2) + expect(detail!.cared.length).toBe(1) + expect(detail!.cared[0].name).toBe('Jay') }) it('getFamilyDetail 不存在返回 null', async () => { @@ -94,9 +95,10 @@ describe('家庭圈 mock 数据层', () => { const saved = await addCared(caredInput()) expect(saved.id).toBeTruthy() const detail = await getFamilyDetail('735201884550361') - expect(detail.family.caredCount).toBe(1) - expect(detail.family.caredLabel).toBe('王奶奶') - expect(detail.cared[0].contacts).toEqual([]) + expect(detail).not.toBeNull() + expect(detail!.family.caredCount).toBe(1) + expect(detail!.family.caredLabel).toBe('王奶奶') + expect(detail!.cared[0].contacts).toEqual([]) }) it('addCared 达上限抛错', async () => {