私が作業しているデザイナーは、複数列のテキストセットの行が本当に好きであると思います。それはかなり単純な電子ブックのためですが、私が高さを取得すると、時々正しくレンダリングされ、それ以外の時には正しく表示されません。参照用画像:React:テキスト要素の高さを取得する
だから、私は何をターゲットにしています(とも時間の一部を働いていること)をループにレンダリングヘッダー用であり、3つのヘッダの一番高いの高さを設定します。上記の画像では、関数は同様のビューをレンダリングするはずですが、ヘッダーのそれぞれがマージンを保持している必要があります。代わりに、マージンが削減されるように、すべて同じサイズに計算されています。残念ながら、孤児が1つしかないヘッダーでは(通常)高さが正しく計算されているようです。誰か良いアイデアはありますか?私のコードは以下の通りです。
Columns.js(updateHeightここで重要な機能である) -
import React from 'react'
import { branch } from 'baobab-react/higher-order'
import classNames from 'classnames'
import _parseInt from 'lodash.parseint'
import _filter from 'lodash.filter'
import InlineSVG from 'react-inlinesvg'
// Column Components
import ImageContainer from './columns/ImageContainer'
import IconContainer from './columns/IconContainer'
import ColumnHeader from './columns/ColumnHeader'
import Content from './columns/Content'
class Columns extends React.Component {
\t constructor(props, context) {
\t \t super(props, context)
\t \t this.state = {
\t \t \t content: 0,
\t \t \t header: 0,
\t \t }
\t }
\t updateHeight(height, component) {
\t \t if (window.innerWidth > 768 && window.innerHeight > 768) {
\t \t \t if (height > this.state[component]) {
\t \t \t \t this.setState({ [component]: height })
\t \t \t } else {
\t \t \t \t return false
\t \t \t }
\t \t } else {
\t \t \t return false
\t \t }
\t }
\t getItems(column, index) {
\t \t let data = column[0] || column
\t \t const icon = data.icon ? '/assets/icons/' + data.icon : null
\t \t const image = data.bioImage ? '/assets/bio-photos/' + data.bioImage : null
\t \t const ref = `column${ index }`
\t \t return (
\t \t \t <div className="column" key={ index } ref={ ref }>
\t \t \t \t { image ? <ImageContainer title={ data.title } image={ image } /> : null }
\t \t \t \t { icon ? <IconContainer icon={ icon } /> : null }
\t \t \t \t <ColumnHeader
\t \t \t \t \t title={ data.title }
\t \t \t \t \t subtitle={ data.subtitle }
\t \t \t \t \t index={ index }
\t \t \t \t \t updateHeight={ this.updateHeight.bind(this) }
\t \t \t \t \t height={ index === undefined ? 'auto' : this.state.header } />
\t \t \t \t <Content
\t \t \t \t \t content={ data.content }
\t \t \t \t \t updateHeight={ this.updateHeight.bind(this) }
\t \t \t \t \t height={ index === undefined ? 'auto' : this.state.content } />
\t \t \t \t { this.props.type === 'team'
\t \t \t \t \t ? <div className="team__social"><InlineSVG src="/assets/icons/linkedin.svg"></InlineSVG></div>
\t \t \t \t \t : null
\t \t \t \t }
\t \t \t </div>
\t \t)
\t }
\t render() {
\t \t const data = this.props.data.columns || this.props.data
\t \t const type = this.props.type
\t \t const count = data.length || 1
\t \t let columns = count > 1 ? data.map((column, index) => this.getItems(column, index)) : this.getItems(data, 1)
\t \t let columnClasses = classNames({
\t \t \t 'columns': true,
\t \t \t [`columns--${count}`]: true,
\t \t \t 'columns--icons': type === 'icons' ? true : false,
\t \t \t 'columns--team': type === 'team' ? true : false,
\t \t })
\t \t return (
\t \t \t <div className={ columnClasses }>
\t \t \t \t { columns }
\t \t \t </div>
\t \t)
\t }
}
export default branch(Columns, {
\t cursors: {
\t \t navOpen: ['navOpen'],
\t }
})
ColumnHeader.js -
import React from 'react'
import { branch } from 'baobab-react/higher-order'
class ColumnHeader extends React.Component {
\t constructor(props, context) {
\t \t super(props, context)
\t }
\t componentDidMount() {
\t \t this.props.updateHeight(this.refs.header.offsetHeight, 'header')
\t }
\t
\t render() {
\t \t let { title, subtitle } = this.props
\t \t let height = (this.props.height === 0) ? 'auto' : this.props.height
\t \t return (
\t \t \t <div className="column__header" ref="header" style={{ height: height }}>
\t \t \t \t <h1 className="title">{ title }</h1>
\t \t \t \t { subtitle ? (<h2 className="subtitle">{ subtitle }</h2>) : null }
\t \t \t </div>
\t \t)
\t }
}
export default ColumnHeader
写真のすべての要素(アイコン、ヘッダー、サブヘッダー、テキスト)は、同じ高さ(必要に応じて)のように見え、内容は内側に一列に並んでいます。あなたは正確に何が違うのですか? – wintvelt
各ヘッダー項目に1remの余白があるため、上記のようにしか表示されません。したがって、高さは実際には右側の2つのヘッダーを切り捨てていますが、マージンはそれらが配置されている全体のスペースが68ピクセルのようなものになるようにそれらをプロップしていますが、ヘッダーの実際の高さは42ピクセルヘッダーの実際の高さを下回ります。現実的には、3つはすべて高さが68ピクセルでなければなりません。たとえば、それぞれが正しく動作する場合は1remのマージンがあります。 – thesublimeobject
申し訳ありませんが、私はまだそれを見ません。 CSSの高さ=コンテンツ+パディング+マージン。 heightが常に68pxの場合、マージンが異なっていても、すべてのヘッダーで同じ高さになります。そして、すべての要素が整列します。間違ったレイアウトの写真をもっとシェアできますか? – wintvelt