Skip to content

Commit

Permalink
feat: toast support config provider
Browse files Browse the repository at this point in the history
  • Loading branch information
Layouwen committed Dec 15, 2024
1 parent 8407aae commit 0d835d2
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
5 changes: 5 additions & 0 deletions src/components/config-provider/config-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ type Config = {
searchBar?: {
searchIcon?: ReactNode
}
toast?: {
successIcon?: ReactNode
failIcon?: ReactNode
loadingIcon?: ReactNode
}
}

export const defaultConfigRef: {
Expand Down
18 changes: 15 additions & 3 deletions src/components/toast/toast.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { GetContainer } from '../../utils/render-to-container'
import { mergeProps } from '../../utils/with-default-props'
import { PropagationEvent } from '../../utils/with-stop-propagation'
import AutoCenter from '../auto-center'
import { useConfig } from '../config-provider'
import type { MaskProps } from '../mask'
import Mask from '../mask'
import SpinLoading from '../spin-loading'
Expand Down Expand Up @@ -34,17 +35,28 @@ const defaultProps = {
export const InternalToast: FC<ToastProps> = p => {
const props = mergeProps(defaultProps, p)
const { maskClickable, content, icon, position } = props
const { toast: componentConfig = {} } = useConfig()

const iconElement = useMemo(() => {
if (icon === null || icon === undefined) return null
switch (icon) {
case 'success':
return <CheckOutline className={`${classPrefix}-icon-success`} />
return (
componentConfig.successIcon ?? (
<CheckOutline className={`${classPrefix}-icon-success`} />
)
)
case 'fail':
return <CloseOutline className={`${classPrefix}-icon-fail`} />
return (
componentConfig.failIcon ?? (
<CloseOutline className={`${classPrefix}-icon-fail`} />
)
)
case 'loading':
return (
<SpinLoading color='white' className={`${classPrefix}-loading`} />
componentConfig.loadingIcon ?? (
<SpinLoading color='white' className={`${classPrefix}-loading`} />
)
)
default:
return icon
Expand Down
7 changes: 6 additions & 1 deletion src/utils/render-imperatively.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { ReactElement } from 'react'
import React, { useEffect, useImperativeHandle, useRef, useState } from 'react'
import ConfigProvider, { getDefaultConfig } from '../components/config-provider'
import { renderToBody } from './render-to-body'

type ImperativeProps = {
Expand Down Expand Up @@ -55,7 +56,11 @@ export function renderImperatively(element: TargetElement) {
})
})
const wrapperRef = React.createRef<ImperativeHandler>()
const unmount = renderToBody(<Wrapper ref={wrapperRef} />)
const unmount = renderToBody(
<ConfigProvider {...getDefaultConfig()}>
<Wrapper ref={wrapperRef} />
</ConfigProvider>
)
return {
close: async () => {
if (!wrapperRef.current) {
Expand Down

0 comments on commit 0d835d2

Please sign in to comment.