-
-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Radio): 增加按钮样式的单选(RadioButton) (#725)
- Loading branch information
Showing
6 changed files
with
139 additions
and
26 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
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 |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import React, { useState, useMemo } from 'react'; | ||
import { IProps, HTMLButtonProps } from '@uiw/utils'; | ||
import Button from '@uiw/react-button'; | ||
|
||
/** | ||
* Constructs a type by picking all properties from `HTMLInputProps` and then removing `size`. | ||
* Omit: https://www.typescriptlang.org/docs/handbook/utility-types.html#omittk | ||
*/ | ||
export interface RadioButtonProps extends IProps, Omit<HTMLButtonProps, 'size'> { | ||
size?: 'large' | 'default' | 'small'; | ||
checked?: boolean; | ||
disabled?: boolean; | ||
onChange?: (even: any) => void; | ||
} | ||
|
||
const RadioButton = React.forwardRef<any, RadioButtonProps>((props, ref) => { | ||
const { | ||
prefixCls = 'w-radio', | ||
type = 'button', | ||
disabled = false, | ||
value = '', | ||
className, | ||
style, | ||
children, | ||
size = 'small', | ||
checked: prChecked = false, | ||
onChange, | ||
...other | ||
} = props; | ||
|
||
const [checked, setChecked] = useState(prChecked); | ||
const [prevChecked, setPrevChecked] = useState<boolean>(); | ||
if (prChecked !== prevChecked) { | ||
setPrevChecked(prChecked); | ||
} | ||
useMemo(() => { | ||
if (prChecked !== prevChecked) { | ||
setChecked(prChecked); | ||
} | ||
}, [prevChecked]); | ||
|
||
const cls = [prefixCls, className, disabled ? 'disabled' : null, size ? `${prefixCls}-${size}` : null] | ||
.filter(Boolean) | ||
.join(' ') | ||
.trim(); | ||
useMemo(() => { | ||
if (checked !== props.checked) { | ||
setChecked(!!props.checked); | ||
} | ||
}, [props.checked]); | ||
console.log('props.checked', props.checked); | ||
|
||
function handleChange(e: React.MouseEvent<HTMLButtonElement, MouseEvent> & MouseEvent) { | ||
e.persist(); | ||
setChecked(!checked); | ||
onChange && onChange(value); | ||
} | ||
|
||
const label = children || value; | ||
|
||
return ( | ||
<Button | ||
{...{ ...other, className: cls, style, disabled, value }} | ||
type={checked ? 'primary' : 'light'} | ||
ref={ref} | ||
onClick={handleChange} | ||
> | ||
{label} | ||
</Button> | ||
); | ||
}); | ||
|
||
export default RadioButton; |
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,5 +1,7 @@ | ||
export * from './Radio'; | ||
export * from './RadioGroup'; | ||
export * from './RadioAbstract'; | ||
export * from './RadioButton'; | ||
export { default as Radio } from './Radio'; | ||
export { default as RadioGroup } from './RadioGroup'; | ||
export { default as RadioButton } from './RadioButton'; |
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