forked from staccDOTsol/raydium-ui-v3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Button.tsx
44 lines (41 loc) · 1.5 KB
/
Button.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { shrinkToValue } from '@/utils/shrinkToValue'
import { ButtonProps as ChakraButtonProps, Button as ChakraButton } from '@chakra-ui/react'
import { forwardRef } from 'react'
import { MayArray, MayFunction } from '@raydium-io/raydium-sdk-v2'
/**
* migrated from V2, and have pre-defined style
*/
export interface ButtonProps extends Omit<ChakraButtonProps, 'colorScheme'> {
variant?:
| 'solid'
| 'solid-dark' // not shining eye-breaking gradient button
| 'outline'
| 'ghost'
| 'link'
| 'unstyled'
| 'capsule'
| 'capsule-radio'
| 'rect-rounded-radio'
validators?: MayArray<{
/** must return true to pass this validator */
should: MayFunction<any>
// used in "connect wallet" button, it's order is over props: disabled
forceActive?: boolean
/** items are button's setting which will apply when corresponding validator has failed */
fallbackProps?: Omit<ButtonProps, 'validators'>
}>
}
export default forwardRef(function Button({ validators, ...restProps }: ButtonProps, ref) {
const failedValidator = (Array.isArray(validators) ? validators.length > 0 : validators)
? [validators!].flat().find(({ should }) => !shrinkToValue(should))
: undefined
const mergedProps: Omit<ButtonProps, 'validators'> = failedValidator
? {
...restProps,
...failedValidator.fallbackProps,
isDisabled: true,
isActive: failedValidator.forceActive
}
: restProps
return <ChakraButton ref={ref as any} {...mergedProps} />
})