-
Notifications
You must be signed in to change notification settings - Fork 80
refactor hook and support ref #47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b1ad330
refactor hook and support ref
hengkx ac66dde
change lint
hengkx 2a504e4
fix test lint
hengkx f6ca784
add prettierrc and fix test lint
hengkx cc6bd1c
add more test
hengkx 7aa7e9a
fix lint
hengkx 3964eee
remove not use code
hengkx e341624
add more test
hengkx aed608a
Update index.spec.js
hengkx a0472cb
Create .eslintignore
hengkx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 @@ | ||
.eslintrc.js |
This file contains hidden or 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,8 @@ | ||
{ | ||
"endOfLine": "lf", | ||
"semi": true, | ||
"singleQuote": true, | ||
"tabWidth": 2, | ||
"trailingComma": "all", | ||
"printWidth": 100 | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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,4 +1,4 @@ | ||
import React, { Component, MouseEventHandler } from 'react'; | ||
import * as React from 'react'; | ||
import classNames from 'classnames'; | ||
|
||
export type SwitchChangeEventHandler = (checked: boolean, event: MouseEvent) => void; | ||
|
@@ -11,7 +11,7 @@ interface SwitchProps { | |
checkedChildren?: React.ReactNode; | ||
unCheckedChildren?: React.ReactNode; | ||
onChange?: SwitchChangeEventHandler; | ||
onMouseUp: MouseEventHandler<HTMLButtonElement>; | ||
onMouseUp: React.MouseEventHandler<HTMLButtonElement>; | ||
onClick?: SwitchClickEventHandler; | ||
tabIndex?: number; | ||
checked?: boolean; | ||
|
@@ -22,143 +22,115 @@ interface SwitchProps { | |
title?: string; | ||
} | ||
|
||
interface SwitchState { | ||
checked: boolean; | ||
} | ||
|
||
class Switch extends Component<SwitchProps, SwitchState> { | ||
private node: React.RefObject<HTMLButtonElement>; | ||
const Switch = React.forwardRef<HTMLButtonElement, SwitchProps>((props, ref) => { | ||
const mergedRef = (ref as any) || React.createRef<HTMLButtonElement>(); | ||
|
||
static defaultProps = { | ||
prefixCls: 'rc-switch', | ||
checkedChildren: null, | ||
unCheckedChildren: null, | ||
className: '', | ||
defaultChecked: false, | ||
}; | ||
|
||
constructor(props) { | ||
super(props); | ||
let checked = false; | ||
if ('checked' in props) { | ||
checked = !!props.checked; | ||
} else { | ||
checked = !!props.defaultChecked; | ||
} | ||
this.state = { checked }; | ||
this.node = React.createRef(); | ||
let initChecked = false; | ||
if ('checked' in props) { | ||
initChecked = !!props.checked; | ||
} else { | ||
initChecked = !!props.defaultChecked; | ||
} | ||
const [checked, setChecked] = React.useState(initChecked); | ||
|
||
componentDidMount() { | ||
const { autoFocus, disabled } = this.props; | ||
React.useEffect(() => { | ||
const { autoFocus, disabled } = props; | ||
if (autoFocus && !disabled) { | ||
this.focus(); | ||
focus(); | ||
} | ||
} | ||
}, [props.autoFocus, props.disabled]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
static getDerivedStateFromProps(nextProps) { | ||
const { checked } = nextProps; | ||
const newState: Partial<SwitchState> = {}; | ||
if ('checked' in nextProps) { | ||
newState.checked = !!checked; | ||
React.useEffect(() => { | ||
if ('checked' in props) { | ||
setChecked(!!props.checked); | ||
} | ||
return newState; | ||
} | ||
}, [props.checked]); | ||
|
||
setChecked(checked, e) { | ||
const { disabled, onChange } = this.props; | ||
const setInternalChecked = (checked, e) => { | ||
const { disabled, onChange } = props; | ||
if (disabled) { | ||
return; | ||
} | ||
if (!('checked' in this.props)) { | ||
this.setState({ | ||
checked, | ||
}); | ||
if (!('checked' in props)) { | ||
setChecked(checked); | ||
} | ||
if (onChange) { | ||
onChange(checked, e); | ||
} | ||
} | ||
}; | ||
|
||
handleClick = e => { | ||
const { checked } = this.state; | ||
const { onClick } = this.props; | ||
const handleClick = e => { | ||
const { onClick } = props; | ||
const newChecked = !checked; | ||
this.setChecked(newChecked, e); | ||
setInternalChecked(newChecked, e); | ||
if (onClick) { | ||
onClick(newChecked, e); | ||
} | ||
}; | ||
|
||
handleKeyDown = e => { | ||
const handleKeyDown = e => { | ||
if (e.keyCode === 37) { | ||
// Left | ||
this.setChecked(false, e); | ||
setInternalChecked(false, e); | ||
} else if (e.keyCode === 39) { | ||
// Right | ||
this.setChecked(true, e); | ||
setInternalChecked(true, e); | ||
} | ||
}; | ||
|
||
// Handle auto focus when click switch in Chrome | ||
handleMouseUp = e => { | ||
const { onMouseUp } = this.props; | ||
this.blur(); | ||
if (onMouseUp) { | ||
onMouseUp(e); | ||
const handleMouseUp = e => { | ||
hengkx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
(mergedRef.current as any).blur(); | ||
if (props.onMouseUp) { | ||
props.onMouseUp(e); | ||
} | ||
}; | ||
|
||
focus() { | ||
if (this.node.current) { | ||
this.node.current.focus(); | ||
} | ||
} | ||
|
||
blur() { | ||
if (this.node.current) { | ||
this.node.current.blur(); | ||
} | ||
} | ||
|
||
render() { | ||
const { | ||
className, | ||
prefixCls, | ||
disabled, | ||
loadingIcon, | ||
checkedChildren, | ||
unCheckedChildren, | ||
onChange, | ||
...restProps | ||
} = this.props; | ||
const { checked } = this.state; | ||
const switchClassName = classNames({ | ||
[className]: !!className, | ||
[prefixCls]: true, | ||
[`${prefixCls}-checked`]: checked, | ||
[`${prefixCls}-disabled`]: disabled, | ||
}); | ||
return ( | ||
<button | ||
{...restProps} | ||
type="button" | ||
role="switch" | ||
aria-checked={checked} | ||
disabled={disabled} | ||
className={switchClassName} | ||
ref={this.node} | ||
onKeyDown={this.handleKeyDown} | ||
onClick={this.handleClick} | ||
onMouseUp={this.handleMouseUp} | ||
> | ||
{loadingIcon} | ||
<span className={`${prefixCls}-inner`}> | ||
{checked ? checkedChildren : unCheckedChildren} | ||
</span> | ||
</button> | ||
); | ||
} | ||
} | ||
const { | ||
className, | ||
prefixCls, | ||
disabled, | ||
loadingIcon, | ||
checkedChildren, | ||
unCheckedChildren, | ||
onChange, | ||
...restProps | ||
} = props; | ||
|
||
const switchClassName = classNames({ | ||
[className]: !!className, | ||
[prefixCls]: true, | ||
[`${prefixCls}-checked`]: checked, | ||
[`${prefixCls}-disabled`]: disabled, | ||
}); | ||
|
||
return ( | ||
<button | ||
{...restProps} | ||
type="button" | ||
role="switch" | ||
aria-checked={checked} | ||
disabled={disabled} | ||
className={switchClassName} | ||
ref={mergedRef} | ||
onKeyDown={handleKeyDown} | ||
onClick={handleClick} | ||
onMouseUp={handleMouseUp} | ||
> | ||
{loadingIcon} | ||
<span className={`${prefixCls}-inner`}>{checked ? checkedChildren : unCheckedChildren}</span> | ||
</button> | ||
); | ||
}); | ||
|
||
Switch.displayName = 'Switch'; | ||
|
||
Switch.defaultProps = { | ||
prefixCls: 'rc-switch', | ||
checkedChildren: null, | ||
unCheckedChildren: null, | ||
className: '', | ||
defaultChecked: false, | ||
}; | ||
|
||
export default Switch; |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个
focus
是无源之水……There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我处理下 ,批量替换了this,... 竟然不报错
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
单元测试 覆盖到了,为什么会通过