forked from react-component/select
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support custom label render (react-component#995)
* feat: support custom label render * fix: update tests & demo & doc * Update src/Select.tsx Co-authored-by: Amumu <[email protected]> * fix: demo * fix: ci lint Signed-off-by: xliez <[email protected]> * fix: hooks deps & format demo * test: 测试用例调整 * chore: fix lint Signed-off-by: xliez <[email protected]> * test: 添加测试用例 --------- Signed-off-by: xliez <[email protected]> Co-authored-by: Amumu <[email protected]>
- Loading branch information
Showing
5 changed files
with
144 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
title: custom-label | ||
nav: | ||
title: Demo | ||
path: /demo | ||
--- | ||
|
||
<code src="../examples/custom-label.tsx"></code> |
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,66 @@ | ||
/* eslint-disable no-console */ | ||
import Select, { Option } from 'rc-select'; | ||
import React from 'react'; | ||
import '../../assets/index.less'; | ||
|
||
const children = []; | ||
for (let i = 10; i < 36; i += 1) { | ||
children.push( | ||
<Option key={i.toString(36) + i} test={i}> | ||
{i.toString(36) + i} | ||
</Option>, | ||
); | ||
} | ||
|
||
const Test: React.FC = () => { | ||
const [value, setValue] = React.useState<string>('test'); | ||
|
||
return ( | ||
<div> | ||
<h2>custom label render</h2> | ||
|
||
<div> | ||
<Select | ||
placeholder="placeholder" | ||
style={{ width: 500 }} | ||
value={value} | ||
onChange={(val: string, option) => { | ||
console.log('change', val, option); | ||
setValue(val); | ||
}} | ||
onSelect={(val, option) => { | ||
console.log('selected', val, option); | ||
}} | ||
onDeselect={(val, option) => { | ||
console.log('deselected', val, option); | ||
}} | ||
tokenSeparators={[',']} | ||
labelRender={(props) => { | ||
const { label, value: _value } = props; | ||
const style: React.CSSProperties = { backgroundColor: 'red' }; | ||
if (label) { | ||
return _value; | ||
} else return <span style={style}>no this value in options</span>; | ||
}} | ||
onFocus={() => console.log('focus')} | ||
onBlur={() => console.log('blur')} | ||
> | ||
{children} | ||
</Select> | ||
</div> | ||
<p> | ||
<button | ||
type="button" | ||
onClick={() => { | ||
setValue('test'); | ||
}} | ||
> | ||
set value as test | ||
</button> | ||
</p> | ||
</div> | ||
); | ||
}; | ||
|
||
export default Test; | ||
/* 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