我最终使用了 React Select组件API 以下是我如何使用 Aphrodite 将自定义样式注入 React Select 组件的各个部分的示例。
// Node Modules
import { css } from 'aphrodite';
import PropTypes from 'prop-types';
import React, { Fragment } from 'react';
import ReactSelect, { components } from 'react-select';
// Styles
import styles from './SelectStyles';
import { fontStyles } from 'styles';
// Assets
import caretImg from 'images/downCaret.svg';
class Select extends React.Component {
render() {
const { noOptionsMessage, onChange, options, value } = this.props;
const Control = props => (
<components.Control {...props} className={css(styles.selectControl)} />
);
const DropdownIndicator = props => {
return (
components.DropdownIndicator && (
<components.DropdownIndicator {...props}>
<img src={caretImg} height="6px" alt="▼" className={css(styles.caret)} />
</components.DropdownIndicator>
)
);
};
const IndicatorSeparator = ({ innerProps }) => {
return <span className={css(styles.indicatorSeparator)} {...innerProps} />;
};
const Menu = props => {
return (
<Fragment>
<components.Menu {...props} className={css(styles.menu)}>
{props.children}
</components.Menu>
</Fragment>
);
};
const Option = props => (
<components.Option
{...props}
className={css(
fontStyles.regular,
props.isSelected ? styles.menuOptionSelected : styles.menuOption,
)}
/>
);
const ValueContainer = ({ children, ...props }) => (
<components.ValueContainer {...props}>{children}</components.ValueContainer>
);
return (
<ReactSelect
className={css(fontStyles.semibold, styles.selectContainer)}
components={{
Control,
DropdownIndicator,
IndicatorSeparator,
Menu,
Option,
ValueContainer,
}}
isSearchable={true}
noOptionsMessage={() => noOptionsMessage}
onChange={onChange}
options={options}
value={value}
/>
);
}
}
Select.propTypes = {
noOptionsMessage: PropTypes.string,
onChange: PropTypes.func.isRequired,
options: PropTypes.arrayOf(PropTypes.object),
value: PropTypes.object,
};
export default Select;