fix(family): showModal editable 类型扩展与测试空值断言,tsc --noEmit 清零
This commit is contained in:
parent
5627be98f7
commit
0cb618be58
|
|
@ -10,6 +10,19 @@ import { navigateTo } from '@/utils/navigation'
|
||||||
import { showToast } from '@/utils/toast'
|
import { showToast } from '@/utils/toast'
|
||||||
import styles from './index.module.scss'
|
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<EditableModalResult>
|
||||||
|
|
||||||
export default function Family() {
|
export default function Family() {
|
||||||
const [list, setList] = useState<Family[]>([])
|
const [list, setList] = useState<Family[]>([])
|
||||||
const [showCreateModal, setShowCreateModal] = useState(false)
|
const [showCreateModal, setShowCreateModal] = useState(false)
|
||||||
|
|
@ -30,37 +43,35 @@ export default function Family() {
|
||||||
const goDetail = (id: string) => navigateTo(`${ROUTES.FAMILY_DETAIL}?id=${id}`)
|
const goDetail = (id: string) => navigateTo(`${ROUTES.FAMILY_DETAIL}?id=${id}`)
|
||||||
|
|
||||||
const createFamily = () => {
|
const createFamily = () => {
|
||||||
Taro.showModal({
|
showEditableModal({
|
||||||
title: '新建家庭',
|
title: '新建家庭',
|
||||||
editable: true,
|
editable: true,
|
||||||
placeholderText: '请输入家庭名',
|
placeholderText: '请输入家庭名'
|
||||||
success: async ({ confirm, content }) => {
|
}).then(async ({ confirm, content }) => {
|
||||||
if (!confirm) return
|
if (!confirm) return
|
||||||
try {
|
try {
|
||||||
await addFamily(content ?? '')
|
await addFamily(content ?? '')
|
||||||
setShowCreateModal(false)
|
setShowCreateModal(false)
|
||||||
await reload()
|
await reload()
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
showToast(error instanceof Error ? error.message : '创建失败')
|
showToast(error instanceof Error ? error.message : '创建失败')
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const joinByCode = () => {
|
const joinByCode = () => {
|
||||||
Taro.showModal({
|
showEditableModal({
|
||||||
title: '加入家庭',
|
title: '加入家庭',
|
||||||
editable: true,
|
editable: true,
|
||||||
placeholderText: '请输入家庭编号',
|
placeholderText: '请输入家庭编号'
|
||||||
success: async ({ confirm, content }) => {
|
}).then(async ({ confirm, content }) => {
|
||||||
if (!confirm) return
|
if (!confirm) return
|
||||||
try {
|
try {
|
||||||
await joinFamily(content ?? '')
|
await joinFamily(content ?? '')
|
||||||
setShowCreateModal(false)
|
setShowCreateModal(false)
|
||||||
await reload()
|
await reload()
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
showToast(error instanceof Error ? error.message : '加入失败')
|
showToast(error instanceof Error ? error.message : '加入失败')
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -68,10 +68,11 @@ describe('家庭圈 mock 数据层', () => {
|
||||||
it('getFamilyDetail 返回家庭 + 成员 + 关爱对象', async () => {
|
it('getFamilyDetail 返回家庭 + 成员 + 关爱对象', async () => {
|
||||||
await getFamilies()
|
await getFamilies()
|
||||||
const detail = await getFamilyDetail('846443196293189')
|
const detail = await getFamilyDetail('846443196293189')
|
||||||
expect(detail.family.name).toBe('JollyPlace')
|
expect(detail).not.toBeNull()
|
||||||
expect(detail.members.length).toBe(2)
|
expect(detail!.family.name).toBe('JollyPlace')
|
||||||
expect(detail.cared.length).toBe(1)
|
expect(detail!.members.length).toBe(2)
|
||||||
expect(detail.cared[0].name).toBe('Jay')
|
expect(detail!.cared.length).toBe(1)
|
||||||
|
expect(detail!.cared[0].name).toBe('Jay')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('getFamilyDetail 不存在返回 null', async () => {
|
it('getFamilyDetail 不存在返回 null', async () => {
|
||||||
|
|
@ -94,9 +95,10 @@ describe('家庭圈 mock 数据层', () => {
|
||||||
const saved = await addCared(caredInput())
|
const saved = await addCared(caredInput())
|
||||||
expect(saved.id).toBeTruthy()
|
expect(saved.id).toBeTruthy()
|
||||||
const detail = await getFamilyDetail('735201884550361')
|
const detail = await getFamilyDetail('735201884550361')
|
||||||
expect(detail.family.caredCount).toBe(1)
|
expect(detail).not.toBeNull()
|
||||||
expect(detail.family.caredLabel).toBe('王奶奶')
|
expect(detail!.family.caredCount).toBe(1)
|
||||||
expect(detail.cared[0].contacts).toEqual([])
|
expect(detail!.family.caredLabel).toBe('王奶奶')
|
||||||
|
expect(detail!.cared[0].contacts).toEqual([])
|
||||||
})
|
})
|
||||||
|
|
||||||
it('addCared 达上限抛错', async () => {
|
it('addCared 达上限抛错', async () => {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue