Skip to content

Commit 3420f64

Browse files
committed
refactor(ui-form-field): simplify layout logic
1 parent c36346b commit 3420f64

File tree

4 files changed

+57
-31
lines changed

4 files changed

+57
-31
lines changed

packages/ui-form-field/src/FormFieldGroup/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ class FormFieldGroup extends Component<FormFieldGroupProps> {
5353
disabled: false,
5454
rowSpacing: 'medium',
5555
colSpacing: 'small',
56-
vAlign: 'middle'
56+
vAlign: 'middle',
57+
isGroup: true
5758
}
5859

5960
ref: Element | null = null

packages/ui-form-field/src/FormFieldLayout/index.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,14 @@ class FormFieldLayout extends Component<FormFieldLayoutProps> {
8787
}
8888

8989
makeStyleProps = (): FormFieldStyleProps => {
90+
const hasNewErrorMsgAndIsGroup =
91+
!!this.props.messages?.find((m) => m.type === 'newError') &&
92+
!!this.props.isGroup
9093
return {
9194
hasMessages: this.hasMessages,
92-
hasVisibleLabel: this.hasVisibleLabel
95+
hasVisibleLabel: this.hasVisibleLabel,
96+
// if true render error message above the controls (and below the label)
97+
hasNewErrorMsgAndIsGroup: hasNewErrorMsgAndIsGroup
9398
}
9499
}
95100

@@ -182,10 +187,7 @@ class FormFieldLayout extends Component<FormFieldLayoutProps> {
182187
ref={this.handleRef}
183188
>
184189
{this.renderLabel()}
185-
{
186-
hasNewErrorMsgAndIsGroup &&
187-
this.renderVisibleMessages() /* TODO fix, never happens bc its buggy*/
188-
}
190+
{hasNewErrorMsgAndIsGroup && this.renderVisibleMessages()}
189191
<span
190192
css={styles?.formFieldChildren}
191193
ref={this.handleInputContainerRef}

packages/ui-form-field/src/FormFieldLayout/props.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ const allowedProps: AllowedPropKeys = [
141141
type FormFieldStyleProps = {
142142
hasMessages: boolean
143143
hasVisibleLabel: boolean
144+
hasNewErrorMsgAndIsGroup: boolean
144145
}
145146

146147
export type {

packages/ui-form-field/src/FormFieldLayout/styles.ts

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,35 @@ import type {
2929
} from './props'
3030
import type { FormFieldLayoutTheme } from '@instructure/shared-types'
3131

32+
const generateGridLayout = (
33+
isInlineLayout: boolean,
34+
hasNewErrorMsgAndIsGroup: boolean,
35+
hasVisibleLabel: boolean,
36+
hasMessages: boolean
37+
) => {
38+
if (isInlineLayout) {
39+
if (hasNewErrorMsgAndIsGroup) {
40+
if (hasMessages) {
41+
return `${hasVisibleLabel ? ' "label messages"' : '. messages'}
42+
". controls"`
43+
} else {
44+
return `${hasVisibleLabel ? ' "label controls"' : '. controls'}`
45+
}
46+
} else {
47+
return `${hasVisibleLabel ? ' "label controls"' : '. controls'}
48+
${hasMessages ? ' ". messages"' : ''}`
49+
}
50+
}
51+
if (hasNewErrorMsgAndIsGroup) {
52+
return `${hasVisibleLabel ? ' "label"' : ''}
53+
${hasMessages ? ' "messages"' : ''}
54+
"controls"`
55+
} else {
56+
return `${hasVisibleLabel ? ' "label"' : ''}
57+
"controls"
58+
${hasMessages ? ' "messages"' : ''}`
59+
}
60+
}
3261
/**
3362
* ---
3463
* private: true
@@ -45,28 +74,23 @@ const generateStyle = (
4574
styleProps: FormFieldStyleProps
4675
): FormFieldLayoutStyle => {
4776
const { inline, layout, vAlign, labelAlign } = props
48-
const { hasMessages, hasVisibleLabel } = styleProps
49-
// This is quite ugly, we should simplify it
50-
const inlineContainerAndLabel = layout === 'inline' && !inline
77+
const { hasMessages, hasVisibleLabel, hasNewErrorMsgAndIsGroup } = styleProps
5178
const isInlineLayout = layout === 'inline'
52-
let gridTemplateColumns = '100%'
53-
// stacked layout
54-
let gridTemplateAreas = `${hasVisibleLabel ? ' "label"' : ''}
55-
"controls"
56-
${hasMessages ? ' "messages"' : ''}`
79+
const inlineContainerAndLabel = isInlineLayout && !inline
80+
// This is quite ugly, we should simplify it
81+
const gridTemplateAreas = generateGridLayout(
82+
isInlineLayout,
83+
hasNewErrorMsgAndIsGroup,
84+
hasVisibleLabel,
85+
hasMessages
86+
)
87+
let gridTemplateColumns = '100%' // stacked layout, could be a Flex
5788
if (isInlineLayout) {
5889
gridTemplateColumns = '1fr 3fr'
59-
gridTemplateAreas = `${hasVisibleLabel ? ' "label controls"' : '. controls'}
60-
${hasMessages ? '"messages messages"' : ''}`
6190
if (inline) {
6291
gridTemplateColumns = 'auto 3fr'
6392
}
6493
}
65-
if (inlineContainerAndLabel) {
66-
gridTemplateAreas = `${hasVisibleLabel ? ' "label controls"' : '. controls'}
67-
${hasMessages ? ' ". messages"' : ''}`
68-
}
69-
7094
const labelStyles = {
7195
all: 'initial',
7296
display: 'block',
@@ -77,7 +101,6 @@ const generateStyle = (
77101
fontSize: componentTheme.fontSize,
78102
lineHeight: componentTheme.lineHeight,
79103
margin: '0 0 0.75rem 0',
80-
textAlign: inlineContainerAndLabel ? 'end' : 'start',
81104
...(isInlineLayout &&
82105
inline && {
83106
paddingRight: componentTheme.inlinePadding
@@ -88,9 +111,7 @@ const generateStyle = (
88111
}),
89112
[`@media screen and (min-width: ${componentTheme.stackedOrInlineBreakpoint})`]:
90113
{
91-
...(isInlineLayout && {
92-
textAlign: labelAlign
93-
})
114+
...(isInlineLayout && { textAlign: labelAlign })
94115
}
95116
}
96117

@@ -121,12 +142,13 @@ const generateStyle = (
121142
[`@media screen and (max-width: ${componentTheme.stackedOrInlineBreakpoint})`]:
122143
{
123144
// for small screens use the stacked layout
124-
...(isInlineLayout && {
125-
gridTemplateAreas: `"label"
126-
"controls"
127-
${hasMessages ? '"messages"' : ''}`,
128-
gridTemplateColumns: '100%'
129-
})
145+
gridTemplateColumns: '100%',
146+
gridTemplateAreas: generateGridLayout(
147+
false,
148+
hasNewErrorMsgAndIsGroup,
149+
hasVisibleLabel,
150+
hasMessages
151+
)
130152
},
131153
columnGap: '0.375rem',
132154
width: '100%',

0 commit comments

Comments
 (0)