Skip to content

Commit 142c508

Browse files
committed
fix(ui-form-field): fix misaligned text when size is exactly at the breakpoint
Also add a test so clicking on the label selects the right control
1 parent d380aa7 commit 142c508

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

packages/ui-form-field/src/FormField/__new-tests__/FormField.test.tsx

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import { runAxeCheck } from '@instructure/ui-axe-check'
2929
import '@testing-library/jest-dom'
3030

3131
import { FormField } from '../index'
32+
import { userEvent } from '@testing-library/user-event'
3233

3334
describe('<FormField />', () => {
3435
let consoleWarningMock: ReturnType<typeof vi.spyOn>
@@ -65,9 +66,22 @@ describe('<FormField />', () => {
6566

6667
it('should meet a11y standards', async () => {
6768
const { container } = render(<FormField label="foo" id="bar" />)
68-
6969
const axeCheck = await runAxeCheck(container)
70-
7170
expect(axeCheck).toBe(true)
7271
})
72+
73+
it('should focus the control with the supplied id', async () => {
74+
const user = userEvent.setup()
75+
render(
76+
<FormField label="labelText" id="foo">
77+
<input />
78+
<input id="foo" />
79+
<input />
80+
</FormField>
81+
)
82+
const label = screen.getByText('labelText').closest('label')!
83+
await user.click(label)
84+
const input = document.getElementById('foo')!
85+
expect(input).toHaveFocus()
86+
})
7387
})

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ class FormField extends Component<FormFieldProps> {
6868
label={this.props.label}
6969
vAlign={this.props.vAlign}
7070
as="label"
71+
// This makes the control in focus when the label is clicked
72+
// This is needed to prevent the wrong element to be focused, e.g.
73+
// multi selects Tag-s
7174
htmlFor={this.props.id}
7275
elementRef={this.handleRef}
7376
margin={this.props.margin}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ const generateStyle = (
107107
// when inline add a small padding between the label and the control
108108
paddingRight: componentTheme.inlinePadding,
109109
// and use the horizontal alignment prop
110-
[`@media screen and (min-width: ${componentTheme.stackedOrInlineBreakpoint})`]:
110+
[`@media screen and (width >= ${componentTheme.stackedOrInlineBreakpoint})`]:
111111
{
112112
textAlign: labelAlign
113113
}
@@ -138,7 +138,7 @@ const generateStyle = (
138138
verticalAlign: 'middle', // removes margin in inline layouts
139139
gridTemplateColumns: gridTemplateColumns,
140140
gridTemplateAreas: gridTemplateAreas,
141-
[`@media screen and (max-width: ${componentTheme.stackedOrInlineBreakpoint})`]:
141+
[`@media screen and (width < ${componentTheme.stackedOrInlineBreakpoint})`]:
142142
{
143143
// for small screens use the stacked layout
144144
gridTemplateColumns: '100%',
@@ -171,7 +171,7 @@ const generateStyle = (
171171
...(hasMessages && hasNewErrorMsgAndIsGroup && { marginTop: '0.375rem' }),
172172
...(isInlineLayout &&
173173
inline && {
174-
[`@media screen and (min-width: ${componentTheme.stackedOrInlineBreakpoint})`]:
174+
[`@media screen and (width >= ${componentTheme.stackedOrInlineBreakpoint})`]:
175175
{
176176
justifySelf: 'start'
177177
}

0 commit comments

Comments
 (0)