【发布时间】:2021-03-18 08:48:41
【问题描述】:
我正在尝试将酒店搜索结果提要设为固定高度,但如果结果超出提要容器的大小,也能够滚动。
应该是这样的:mockup
但目前看起来是这样的:current
如您所见,滚动条出现在整个页面上,而不是在提要容器中。
我将整个页面包装在一个 flexbox 父容器中,然后两个直接子级是 searchBar 和 page content。 页面内容目前也是一个弹性容器,其中包含酒店搜索结果和酒店建议组件作为弹性子项。我目前正在使用带有样式的组件在 React 中编写代码,但这本质上是一个 css 问题。关于如何构建我的 css 以使其看起来像我想要的任何想法?
App.js:
//entire screen
const Wrapper = styled.div`
display: flex;
flex-direction: column;
`;
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = {
searchResults: [],
};
this.displaySearchFeed = this.displaySearchFeed.bind(this);
}
displaySearchFeed(data) {
console.log("state set");
this.setState({
searchResults: data,
});
}
render() {
return (
<Wrapper>
<SearchBar displaySearchFeed={this.displaySearchFeed} />
<HotelPageContent searchResults={this.state.searchResults} />
</Wrapper>
);
}
}
HotelPageContent.js
const ResultsContainer = styled.div`
display: flex;
`;
export default function HotelPageContent(props) {
console.log("rendered");
console.log(props.searchResults);
return (
<ResultsContainer>
<HotelSearchResults searchResults={props.searchResults} />
<HotelSuggestions />
</ResultsContainer>
);
}
HotelSearchResultsFeed.js
const Container = styled.div`
height: 100%;
flex: 3;
border-color: red;
border-width: 3px;
border-style: solid;
overflow-y: scroll;
`;
// display: grid;
// position: fixed;
export default function SearchResults(props) {
return (
<Container>
<div>Hotel Search Results</div>
{props.searchResults.length > 0
? props.searchResults.map((data, index) => {
return <HotelCard key={index} HotelData={data} />;
})
: null}
</Container>
);
}
HotelSuggestions.js
const Container = styled.div`
height: 100%;
display: grid;
flex: 1;
border-color: red;
border-width: 1px;
border-style: solid;
`;
export default function HotelSuggestions(props) {
return (
<Container>
<div>Hotel Suggestions</div>
</Container>
);
}
HotelCard.js
const Container = styled.div`
height: 10em;
display: flex;
flex-direction: row;
border-color: black;
border-width: 1px;
border-style: solid;
`;
const CenterSection = styled.div`
background-color: #80cbc4;
border: 1px solid #fff;
text-align: center;
flex: 2;
`;
const HotelImageWrapper = styled.div`
flex: 1;
`;
const HotelImage = styled.img`
height: 100%;
width: 100%;
object-fit: contain;
border-color: black;
border-width: 1px;
border-style: solid;
`;
export default function HotelCard({ HotelData }) {
return (
<Container>
{/* <HotelImage src={imgSrc} /> */}
<CenterSection>{HotelData["name"]}</CenterSection>
<div>{HotelData["rating"]}</div>
</Container>
);
}
【问题讨论】:
-
你必须用弹性盒子来做这个吗? css-grid 将是更好的解决方案。还请添加您的代码。否则没有办法修复它,我们不得不猜测......
-
你最好把你的项目上传到codesandbox
-
@tacoshy 不,我不必使用弹性盒子。
标签: javascript html css reactjs flexbox