-
Notifications
You must be signed in to change notification settings - Fork 446
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
antd v5: Controlled components will treat null
as empty value and null
can not be used as an option value.
#777
Open
shezhangzhang
wants to merge
7
commits into
react-component:master
Choose a base branch
from
shezhangzhang:feature/option-null-value
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
365a1b3
feat: add options null value warning.
shezhangzhang 7f3af9d
feat: add node children options null value wanrning
shezhangzhang c7c3c57
chore: add prettier 2.x because the current prettier version added by…
shezhangzhang 2a38804
feat: Select will treat null as empty value and onChange() will recie…
shezhangzhang 206b49f
docs: add null-controlled example
shezhangzhang 007c0f0
feat: modify some test cases caused by null value.
shezhangzhang ecf8223
fix: remove useless code
shezhangzhang File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,3 @@ | ||
## null-controlled | ||
|
||
<code src="../examples/null-controlled.tsx"> |
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,64 @@ | ||
/* eslint-disable no-console */ | ||
import Select, { Option } from 'rc-select'; | ||
import React from 'react'; | ||
import '../../assets/index.less'; | ||
|
||
interface NullControlledState { | ||
destroy: boolean; | ||
value: string | number; | ||
} | ||
|
||
class Controlled extends React.Component<unknown, NullControlledState> { | ||
state = { | ||
destroy: false, | ||
value: null, | ||
}; | ||
|
||
onChange = (e) => { | ||
let value; | ||
if (e && e.target) { | ||
({ value } = e.target); | ||
} else { | ||
value = e; | ||
} | ||
console.log('onChange', value); | ||
this.setState({ | ||
value, | ||
}); | ||
}; | ||
|
||
onDestroy = () => { | ||
this.setState({ | ||
destroy: true, | ||
}); | ||
}; | ||
|
||
render() { | ||
const { destroy, value } = this.state; | ||
if (destroy) { | ||
return null; | ||
} | ||
return ( | ||
<div style={{ margin: 20 }}> | ||
<h2>controlled Select: value === null</h2> | ||
<div style={{ width: 300 }}> | ||
<Select | ||
id="my-select" | ||
value={value} | ||
placeholder="placeholder" | ||
listHeight={200} | ||
style={{ width: 500 }} | ||
onChange={this.onChange} | ||
allowClear | ||
> | ||
<Option value={null}>null</Option> | ||
<Option value="correct">correct</Option> | ||
</Select> | ||
</div> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
export default Controlled; | ||
/* eslint-enable */ |
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
Oops, something went wrong.
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.
Since when
null
as value will returns [], this logic code is no looger needed. Whenvalue={null}
, the placeholder will show up correctly.因为 null 作为空值已经做了返回 [] 的处理,所以这段特殊处理此 issue 的逻辑已经不需要了,
value={null}
时 placeholder 会正确显示。