From 8cc86282cee69f32e6a6108275c14ec719aa1ab6 Mon Sep 17 00:00:00 2001
From: selvagsz
Date: Mon, 9 Apr 2018 23:49:32 +0800
Subject: [PATCH 1/5] selectionOptionComponent as render props
---
src/DefaultOption.js | 5 +++++
src/Select.js | 3 ++-
src/SelectTrigger.js | 13 ++++---------
3 files changed, 11 insertions(+), 10 deletions(-)
create mode 100644 src/DefaultOption.js
diff --git a/src/DefaultOption.js b/src/DefaultOption.js
new file mode 100644
index 0000000..b7aedda
--- /dev/null
+++ b/src/DefaultOption.js
@@ -0,0 +1,5 @@
+import React, { Component } from 'react';
+
+export default function SelectedOption({ option, select }) {
+ return option;
+}
diff --git a/src/Select.js b/src/Select.js
index c2f14c8..d770697 100644
--- a/src/Select.js
+++ b/src/Select.js
@@ -4,6 +4,7 @@ import cx from 'classnames';
import Dropdown from './Dropdown';
import SelectTrigger from './SelectTrigger';
import DropdownMenu from './DropdownMenu';
+import DefaultOption from './DefaultOption';
import {
matcher,
isOptGroup,
@@ -434,7 +435,7 @@ Select.defaultProps = {
triggerComponent: SelectTrigger,
triggerLHSComponent: null,
triggerRHSComponent: null,
- selectedOptionComponent: null,
+ selectedOptionComponent: DefaultOption,
beforeOptionsComponent: null,
afterOptionsComponent: null,
matcher: matcher,
diff --git a/src/SelectTrigger.js b/src/SelectTrigger.js
index 3e5c9fb..6b0fa7f 100644
--- a/src/SelectTrigger.js
+++ b/src/SelectTrigger.js
@@ -3,7 +3,7 @@ import RenderOption from './RenderOption';
import TriggerWrapper from './TriggerWrapper';
export default function Trigger({
- selectedOption,
+ selectedOption: option,
optionLabelPath,
selectedOptionComponent,
placeholder,
@@ -11,15 +11,10 @@ export default function Trigger({
...rest
}) {
return (
-
+
- {selectedOption ? (
-
+ {option ? (
+ selectedOptionComponent({ option, select })
) : (
{placeholder}
)}
From 2c2449c5e9f38991e82eb1bb4fd74608c7ebedfb Mon Sep 17 00:00:00 2001
From: selvagsz
Date: Tue, 10 Apr 2018 01:03:41 +0800
Subject: [PATCH 2/5] render function as children pattern for
---
src/DropdownMenu.js | 4 +--
src/Option.js | 58 ++++++++++++++----------------
src/Options.js | 88 +++++++++++++++++++++++++++------------------
src/Select.js | 14 ++++++--
src/index.js | 3 +-
5 files changed, 96 insertions(+), 71 deletions(-)
diff --git a/src/DropdownMenu.js b/src/DropdownMenu.js
index 92faa16..4c1c72a 100644
--- a/src/DropdownMenu.js
+++ b/src/DropdownMenu.js
@@ -1,7 +1,6 @@
import React, { Component } from 'react';
import cx from 'classnames';
import { renderComponent } from './utils';
-import Options from './Options';
export default class DropdownMenu extends Component {
componentWillMount() {
@@ -21,6 +20,7 @@ export default class DropdownMenu extends Component {
render() {
let {
+ children,
className,
select,
handleKeyDown,
@@ -43,7 +43,7 @@ export default class DropdownMenu extends Component {
>
{beforeOptionsComponent && renderComponent(beforeOptionsComponent, { select })}
-
+ {children}
{afterOptionsComponent && renderComponent(afterOptionsComponent, { select })}
diff --git a/src/Option.js b/src/Option.js
index 09ddc82..bfff081 100644
--- a/src/Option.js
+++ b/src/Option.js
@@ -1,36 +1,30 @@
import React, { Component } from 'react';
import cx from 'classnames';
-import RenderOption from './RenderOption';
-export default class Option extends Component {
- render() {
- let {
- option,
- select,
- disabled,
- optionIndex,
- optionLabelPath,
- optionComponent,
- isHighlighted,
- onOptionClick,
- } = this.props;
- let isDisabled = disabled || option.disabled;
- return (
-
-
-
- );
- }
+export default function Option({
+ option,
+ disabled,
+ isHighlighted,
+ optionIndex,
+ onOptionClick,
+ children,
+}) {
+ return (
+ {
+ onOptionClick(option);
+ }
+ )
+ }
+ >
+ {children}
+
+ );
}
diff --git a/src/Options.js b/src/Options.js
index c6ed9a7..2c0c85c 100644
--- a/src/Options.js
+++ b/src/Options.js
@@ -38,47 +38,67 @@ export default class Options extends Component {
}
}
- renderOptions(options, optGroupDisabled = false) {
- let { select, optionLabelPath, optionComponent, highlightedOption, onOptionClick } = this.props;
- return options.map((option, index) => {
- let optionIndex = getOptionIndex(this.props.options, option);
- if (isOptGroup(option)) {
- return (
-
-
{option.label}
- {this.renderOptions(option.options, option.disabled)}
-
- );
+ // renderOptions(options, optGroupDisabled = false) {
+ // let { select, optionLabelPath, optionComponent, highlightedOption, onOptionClick } = this.props;
+ // return options.map((option, index) => {
+ // let optionIndex = getOptionIndex(this.props.options, option);
+ // if (isOptGroup(option)) {
+ // return (
+ //
+ // {option.label}
+ // {this.renderOptions(option.options, option.disabled)}
+ //
+ // );
+ // }
+ // return (
+ // {
+ // onOptionClick(option);
+ // }}
+ // />
+ // );
+ // });
+ // }
+
+ renderOptions() {
+ let { children, options, highlightedOption, onOptionClick } = this.props;
+ return React.Children.map(children, (child, index) => {
+ let optionIndex = getOptionIndex(options, options[index]);
+ if (child === null) {
+ return null;
+ }
+
+ if (child.type === Option) {
+ return React.cloneElement(child, {
+ ...child.props,
+ isHighlighted: child.props.option === highlightedOption,
+ optionIndex,
+ onOptionClick,
+ });
}
- return (
-