【发布时间】:2020-06-23 10:45:09
【问题描述】:
我正在尝试使用 react-select 库在我的网站中使用 Material UI 下拉菜单。问题是,当我使用比下拉列表宽度更长的选项时,它会影响 UI。
谁能帮帮我。
这是我的反应代码:
import React, { useState } from "react";
import "./styles.css";
import SelectDropdown from "./EditableDropdown";
export default function App() {
const [description, setDesc] = useState("");
const options = [
{
label: "a11 b33 c88 o99 t66 j44 z99",
value: "a11 b33 c88 o99 t66 j44 z99"
},
{
label: "Switches",
value: "Switches"
}
];
return (
<div style={{ width: "25%" }}>
<SelectDropdown
label="Description"
value={description}
onChange={setDesc}
options={options}
/>
</div>
);
}
这是可编辑组件的代码
import React from "react";
import {
MuiThemeProvider,
createMuiTheme,
MenuItem,
Paper,
withStyles,
TextField
} from "@material-ui/core";
import Select from "react-select";
const styles = {
input: {
padding: 0,
marginSides: "8px",
minHeight: "inherit",
lineHeight: "22px",
fontWeight: 200
},
valueContainer: {
alignItems: "center",
fontFamily: "Helvetica Neue,Arial,Helvetica,sans-serif",
fontWeight: 400,
fontSize: "14px !important"
}
};
function inputComponent({ inputRef, ...props }) {
return <div ref={inputRef} {...props} />;
}
function Control(props) {
return (
<TextField
fullWidth
InputProps={{
inputComponent,
inputProps: {
className: props.selectProps.classes.input,
children: props.children,
...props.innerProps
}
}}
{...props.selectProps.textFieldProps}
/>
);
}
function Option(props) {
return (
<MenuItem
buttonRef={props.innerRef}
selected={props.isFocused}
component="div"
style={{
fontWeight: props.isSelected ? 500 : 400,
backgroundColor: props.isSelected
? "rgba(59,234,31,0.2)"
: props.isFocused
? "#F0F0F0"
: null,
fontFamily: "Helvetica Neue,Arial,Helvetica,sans-serif",
height: "34px",
color: "#343434"
}}
{...props.innerProps}
>
{props.children}
</MenuItem>
);
}
function SingleValue(props) {
return (
<div
className={props.selectProps.classes.singleValue}
{...props.innerProps}
>
{typeof props.children !== "object" ? props.children : ""}
</div>
);
}
function ValueContainer(props) {
let valueContainerClass = props.selectProps.classes.valueContainer;
return (
<div className={valueContainerClass} ref={props.inputRef}>
{props.children}
</div>
);
}
function Menu(props) {
return (
<Paper square {...props.innerProps}>
{props.children}
</Paper>
);
}
const dropDownStyle = {
overrides: {
MuiOutlinedInput: {
root: {
padding: "12px 12px 12px 16px !important"
}
},
MuiInputBase: {
root: {
cursor: "pointer"
}
}
}
};
class SelectDropdown extends React.Component {
state = {
focused: false
};
handleTextFieldChange = ({ target: { value } }) => {
this.props.onChange({
label: value,
value
});
};
components = {
Control,
Menu,
Option,
SingleValue,
ValueContainer,
IndicatorSeparator: () => null,
DropdownIndicator: () => null
};
render() {
const { classes, options, onChange, value } = this.props;
const selectStyles = {
clearIndicator: () => ({
display: "none"
}),
noOptionsMessage: () => ({
display: "none"
})
};
return (
<MuiThemeProvider theme={createMuiTheme(dropDownStyle)}>
<Select
styles={selectStyles}
isClearable={true}
classes={classes}
onChange={onChange}
backspaceRemovesValue={true}
textFieldProps={{
label: this.props.label,
variant: "outlined",
InputLabelProps: value ? { shrink: true } : {},
onChange: this.handleTextFieldChange
}}
value={value}
components={this.components}
isSearchable={true}
placeholder=""
options={options}
/>
</MuiThemeProvider>
);
}
}
export default withStyles(styles, { withTheme: true })(SelectDropdown);
这里是沙箱中相同代码的链接:https://codesandbox.io/s/editable-dropdown-ckf1r
【问题讨论】:
-
我相信这是因为你有
<div style={{ width: "25%" }}>限制了长度。你可能想增加宽度。或者你的意思是说,当它被选中并且溢出时,你不希望它显示这种方式? -
那是前一段时间了.. 我可能已经尝试过了,但没有运气.. 无论如何,请随时在您的最后尝试.. 使用沙盒。让我知道这是否适合您。非常感谢您的关注。
-
而且我不需要那里的全宽。我需要在那里显示一个较小的输入字段。我建议你直接使用 Material-UI Select 组件,而不是你现在实现的反弹
EditableDropdown组件是你自己写的吗?
标签: css reactjs user-interface material-ui react-select