forked from ant-design/ant-design-mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ErrorBlock): add
createErrorBlock
function for generating more…
… light-weighted or customized ErrorBlock
- Loading branch information
Showing
10 changed files
with
250 additions
and
84 deletions.
There are no files selected for viewing
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,68 @@ | ||
import React, { FC, ReactElement, ReactNode } from 'react' | ||
import classNames from 'classnames' | ||
import { mergeProps } from '../../utils/with-default-props' | ||
import { NativeProps, withNativeProps } from '../../utils/native-props' | ||
import { useConfig } from '../config-provider' | ||
import type { ErrorBlockStatus, ImageRecord } from '.' | ||
|
||
const classPrefix = `adm-error-block` | ||
|
||
export type ErrorBlockProps = { | ||
status?: ErrorBlockStatus | ||
title?: ReactNode | ||
image?: string | ReactElement | ||
description?: ReactNode | ||
fullPage?: boolean | ||
children?: React.ReactNode | ||
} & NativeProps< | ||
| '--image-height' | ||
| '--image-height-full-page' | ||
| '--image-width' | ||
| '--image-width-full-page' | ||
> | ||
|
||
const defaultProps = { | ||
status: 'default', | ||
} | ||
|
||
export function createErrorBlock(imageRecord: ImageRecord) { | ||
const ErrorBlock: FC<ErrorBlockProps> = p => { | ||
const props = mergeProps(defaultProps, p) | ||
const { locale } = useConfig() | ||
const contentPack = locale.ErrorBlock[props.status] | ||
const des = | ||
'description' in props ? props.description : contentPack.description | ||
const title = 'title' in props ? props.title : contentPack.title | ||
const image: ReactNode = props.image ?? imageRecord[props.status] | ||
const imageNode = | ||
typeof image === 'string' ? ( | ||
<img src={image} alt='error block image' /> | ||
) : ( | ||
image | ||
) | ||
|
||
return withNativeProps( | ||
props, | ||
<div | ||
className={classNames(classPrefix, { | ||
[`${classPrefix}-full-page`]: props.fullPage, | ||
})} | ||
> | ||
<div className={`${classPrefix}-image`}>{imageNode}</div> | ||
<div className={`${classPrefix}-description`}> | ||
{title && ( | ||
<div className={`${classPrefix}-description-title`}>{title}</div> | ||
)} | ||
{des && ( | ||
<div className={`${classPrefix}-description-subtitle`}>{des}</div> | ||
)} | ||
</div> | ||
|
||
{props.children && ( | ||
<div className={`${classPrefix}-content`}>{props.children}</div> | ||
)} | ||
</div> | ||
) | ||
} | ||
return ErrorBlock | ||
} |
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,23 @@ | ||
import React from 'react' | ||
import { createErrorBlock, Space } from 'antd-mobile' | ||
import { DemoBlock } from 'demos' | ||
import { defaultImage } from 'antd-mobile/es/components/error-block/images' | ||
|
||
const ErrorBlock = createErrorBlock({ | ||
'default': defaultImage, | ||
'empty': | ||
'https://gw.alipayobjects.com/zos/bmw-prod/7a2970f8-9247-4196-b3b3-2d0218c18b59.svg', | ||
}) | ||
|
||
export default () => { | ||
return ( | ||
<> | ||
<DemoBlock title='四种状态'> | ||
<Space block direction='vertical' style={{ '--gap': '16px' }}> | ||
<ErrorBlock status='default' /> | ||
<ErrorBlock status='empty' /> | ||
</Space> | ||
</DemoBlock> | ||
</> | ||
) | ||
} |
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,4 @@ | ||
.myErrorBlockIcon { | ||
font-size: 48px; | ||
color: var(--adm-color-weak); | ||
} |
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,23 @@ | ||
import React from 'react' | ||
import { createErrorBlock, Space } from 'antd-mobile' | ||
import { DemoBlock } from 'demos' | ||
import { FileWrongOutline, SearchOutline } from 'antd-mobile-icons' | ||
import styles from './demo-4.less' | ||
|
||
const ErrorBlock = createErrorBlock({ | ||
'default': <FileWrongOutline className={styles.myErrorBlockIcon} />, | ||
'empty': <SearchOutline className={styles.myErrorBlockIcon} />, | ||
}) | ||
|
||
export default () => { | ||
return ( | ||
<> | ||
<DemoBlock title='四种状态'> | ||
<Space block direction='vertical' style={{ '--gap': '16px' }}> | ||
<ErrorBlock status='default' /> | ||
<ErrorBlock status='empty' /> | ||
</Space> | ||
</DemoBlock> | ||
</> | ||
) | ||
} |
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,67 +1,20 @@ | ||
import React, { FC, ReactElement, ReactNode } from 'react' | ||
import classNames from 'classnames' | ||
import { mergeProps } from '../../utils/with-default-props' | ||
import { NativeProps, withNativeProps } from '../../utils/native-props' | ||
import { useConfig } from '../config-provider' | ||
import { imageRecord } from './images' | ||
import { createErrorBlock } from './create-error-block' | ||
import { ReactElement } from 'react' | ||
import { | ||
busyImage, | ||
defaultImage, | ||
disconnectedImage, | ||
emptyImage, | ||
} from './images' | ||
|
||
const classPrefix = `adm-error-block` | ||
|
||
export type ErrorBlockProps = { | ||
status?: 'default' | 'disconnected' | 'empty' | 'busy' | ||
title?: ReactNode | ||
image?: string | ReactElement | ||
description?: ReactNode | ||
fullPage?: boolean | ||
children?: React.ReactNode | ||
} & NativeProps< | ||
| '--image-height' | ||
| '--image-height-full-page' | ||
| '--image-width' | ||
| '--image-width-full-page' | ||
> | ||
|
||
const defaultProps = { | ||
status: 'default', | ||
const imageRecord: Record< | ||
'default' | 'disconnected' | 'empty' | 'busy', | ||
ReactElement | ||
> = { | ||
'default': defaultImage, | ||
'disconnected': disconnectedImage, | ||
'empty': emptyImage, | ||
'busy': busyImage, | ||
} | ||
|
||
export const ErrorBlock: FC<ErrorBlockProps> = p => { | ||
const props = mergeProps(defaultProps, p) | ||
const { locale } = useConfig() | ||
const contentPack = locale.ErrorBlock[props.status] | ||
const des = | ||
'description' in props ? props.description : contentPack.description | ||
const title = 'title' in props ? props.title : contentPack.title | ||
let imageNode: ReactNode = imageRecord[props.status] | ||
|
||
if (props.image) { | ||
if (typeof props.image === 'string') { | ||
imageNode = <img src={props.image} alt='error block image' /> | ||
} else { | ||
imageNode = props.image | ||
} | ||
} | ||
|
||
return withNativeProps( | ||
props, | ||
<div | ||
className={classNames(classPrefix, { | ||
[`${classPrefix}-full-page`]: props.fullPage, | ||
})} | ||
> | ||
<div className={`${classPrefix}-image`}>{imageNode}</div> | ||
<div className={`${classPrefix}-description`}> | ||
{title && ( | ||
<div className={`${classPrefix}-description-title`}>{title}</div> | ||
)} | ||
{des && ( | ||
<div className={`${classPrefix}-description-subtitle`}>{des}</div> | ||
)} | ||
</div> | ||
|
||
{props.children && ( | ||
<div className={`${classPrefix}-content`}>{props.children}</div> | ||
)} | ||
</div> | ||
) | ||
} | ||
export const ErrorBlock = createErrorBlock(imageRecord) |
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,4 @@ | ||
import { ReactElement } from 'react' | ||
import { defaultImage } from './default' | ||
import { disconnectedImage } from './disconnected' | ||
import { emptyImage } from './empty' | ||
import { busyImage } from './busy' | ||
|
||
export const imageRecord: Record< | ||
'default' | 'disconnected' | 'empty' | 'busy', | ||
ReactElement | ||
> = { | ||
'default': defaultImage, | ||
'disconnected': disconnectedImage, | ||
'empty': emptyImage, | ||
'busy': busyImage, | ||
} | ||
export { defaultImage } from './default' | ||
export { disconnectedImage } from './disconnected' | ||
export { emptyImage } from './empty' | ||
export { busyImage } from './busy' |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,12 @@ | ||
import './error-block.less' | ||
import { ReactNode } from 'react' | ||
import { ErrorBlock } from './error-block' | ||
|
||
export type { ErrorBlockProps } from './error-block' | ||
export { createErrorBlock } from './create-error-block' | ||
|
||
export type { ErrorBlockProps } from './create-error-block' | ||
|
||
export type ErrorBlockStatus = 'default' | 'disconnected' | 'empty' | 'busy' | ||
export type ImageRecord = Partial<Record<ErrorBlockStatus, string | ReactNode>> | ||
|
||
export default ErrorBlock |
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