Skip to content

Commit

Permalink
Revert "refactor(FormControl): update existing code for sub-component…
Browse files Browse the repository at this point in the history
…s to prep for migration (#5342)"

This reverts commit 39df71e.
  • Loading branch information
francinelucca committed Dec 5, 2024
1 parent 742d4a8 commit 7048d46
Show file tree
Hide file tree
Showing 13 changed files with 136 additions and 199 deletions.
5 changes: 0 additions & 5 deletions .changeset/soft-bananas-behave.md

This file was deleted.

10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,5 @@
"webpack": false,
"running": false
}
],
"packageManager": "[email protected]+sha512.c89530d37c4baa38afd43e76a077a84b9aa63840b986426584fd5c5a54ab0a0b21bb1595c851042b733784b0b43706d36a494b4d8ae1a086a762cb8d3f95942a"
]
}
6 changes: 3 additions & 3 deletions packages/react/src/FormControl/FormControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import {get} from '../constants'
import {useSlots} from '../hooks/useSlots'
import type {SxProp} from '../sx'
import {useId} from '../hooks/useId'
import {FormControlCaption} from './FormControlCaption'
import FormControlLabel from './FormControlLabel'
import FormControlLeadingVisual from './FormControlLeadingVisual'
import FormControlCaption from './_FormControlCaption'
import FormControlLabel from './_FormControlLabel'
import FormControlLeadingVisual from './_FormControlLeadingVisual'
import FormControlValidation from './_FormControlValidation'
import {FormControlContextProvider} from './_FormControlContext'
import {warning} from '../utils/warning'
Expand Down
36 changes: 0 additions & 36 deletions packages/react/src/FormControl/FormControlCaption.tsx

This file was deleted.

44 changes: 0 additions & 44 deletions packages/react/src/FormControl/FormControlLeadingVisual.tsx

This file was deleted.

15 changes: 15 additions & 0 deletions packages/react/src/FormControl/_FormControlCaption.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from 'react'
import InputCaption from '../internal/components/InputCaption'
import type {SxProp} from '../sx'
import {useFormControlContext} from './_FormControlContext'

const FormControlCaption: React.FC<React.PropsWithChildren<{id?: string} & SxProp>> = ({children, sx, id}) => {
const {captionId, disabled} = useFormControlContext()
return (
<InputCaption id={id || captionId || ''} disabled={disabled} sx={sx}>
{children}
</InputCaption>
)
}

export default FormControlCaption
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react'
import InputLabel from '../internal/components/InputLabel'
import type {SxProp} from '../sx'
import {useFormControlContext} from './_FormControlContext'
import {InputLabel} from '../internal/components/InputLabel'

export type Props = {
/**
Expand Down Expand Up @@ -49,7 +49,6 @@ const FormControlLabel: React.FC<
sx,
...props,
}

return <InputLabel {...labelProps}>{children}</InputLabel>
}

Expand Down
27 changes: 27 additions & 0 deletions packages/react/src/FormControl/_FormControlLeadingVisual.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import React from 'react'
import Box from '../Box'
import {get} from '../constants'
import type {SxProp} from '../sx'
import {useFormControlContext} from './_FormControlContext'

const FormControlLeadingVisual: React.FC<React.PropsWithChildren<SxProp>> = ({children, sx}) => {
const {disabled, captionId} = useFormControlContext()
return (
<Box
color={disabled ? 'fg.muted' : 'fg.default'}
sx={{
'> *': {
minWidth: captionId ? get('fontSizes.4') : get('fontSizes.2'),
minHeight: captionId ? get('fontSizes.4') : get('fontSizes.2'),
fill: 'currentColor',
},
...sx,
}}
ml={2}
>
{children}
</Box>
)
}

export default FormControlLeadingVisual
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {useSlots} from '../../hooks/useSlots'
import {useProvidedRefOrCreate, useId, useAnchoredPosition} from '../../hooks'
import type {OverlayProps} from '../../Overlay/Overlay'
import {StyledOverlay, heightMap} from '../../Overlay/Overlay'
import {InputLabel} from '../../internal/components/InputLabel'
import InputLabel from '../../internal/components/InputLabel'
import {invariant} from '../../utils/invariant'
import {AriaStatus} from '../../live-region'
import {useResponsiveValue} from '../../hooks/useResponsiveValue'
Expand Down
30 changes: 30 additions & 0 deletions packages/react/src/internal/components/InputCaption.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React from 'react'
import Text from '../../Text'
import type {SxProp} from '../../sx'

type Props = {
/**
* The unique identifier used to associate the caption with an input
*/
id: string
/**
* Whether the input associated with this caption is disabled
*/
disabled?: boolean
} & SxProp

const InputCaption: React.FC<React.PropsWithChildren<Props>> = ({children, disabled, id, sx}) => (
<Text
id={id}
sx={{
color: disabled ? 'var(--control-fgColor-disabled)' : 'var(--fgColor-muted)',
display: 'block',
fontSize: 0,
...sx,
}}
>
{children}
</Text>
)

export default InputCaption
75 changes: 25 additions & 50 deletions packages/react/src/internal/components/InputLabel.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react'
import styled from 'styled-components'
import {get} from '../../constants'
import sx, {type SxProp} from '../../sx'
import Box from '../../Box'
import type {SxProp} from '../../sx'
import VisuallyHidden from '../../_VisuallyHidden'

type BaseProps = SxProp & {
disabled?: boolean
Expand All @@ -23,9 +23,9 @@ export type LegendOrSpanProps = BaseProps & {
htmlFor?: undefined
}

type Props = React.PropsWithChildren<LabelProps | LegendOrSpanProps>
type Props = LabelProps | LegendOrSpanProps

function InputLabel({
const InputLabel: React.FC<React.PropsWithChildren<Props>> = ({
children,
disabled,
htmlFor,
Expand All @@ -38,62 +38,37 @@ function InputLabel({
as = 'label',
className,
...props
}: Props) {
}) => {
return (
<StyledLabel
as={as}
data-control-disabled={disabled ? '' : undefined}
data-visually-hidden={visuallyHidden ? '' : undefined}
<VisuallyHidden
isVisible={!visuallyHidden}
as={
as as 'label' /* This assertion is clearly wrong, but it's the only way TS will allow the htmlFor prop to be possibly defined */
}
htmlFor={htmlFor}
id={id}
className={className}
sx={sx}
sx={{
fontWeight: 'bold',
fontSize: 1,
display: 'block',
color: disabled ? 'fg.muted' : 'fg.default',
cursor: disabled ? 'not-allowed' : 'pointer',
alignSelf: 'flex-start',
...sx,
}}
{...props}
>
{required || requiredText ? (
<StyledRequiredText>
<span>{children}</span>
<Box display="flex" as="span">
<Box mr={1}>{children}</Box>
<span aria-hidden={requiredIndicator ? undefined : true}>{requiredText ?? '*'}</span>
</StyledRequiredText>
</Box>
) : (
children
)}
</StyledLabel>
</VisuallyHidden>
)
}

const StyledRequiredText = styled.span`
display: flex;
column-gap: ${get('space.1')};
`

const StyledLabel = styled.label`
align-self: flex-start;
display: block;
color: var(--fgColor-default);
cursor: pointer;
font-weight: 600;
font-size: ${get('fontSizes.1')};
&:where([data-control-disabled]) {
color: var(--fgColor-muted);
cursor: not-allowed;
}
&:where([data-visually-hidden]) {
border: 0;
clip: rect(0 0 0 0);
clip-path: inset(50%);
height: 1px;
margin: -1px;
overflow: hidden;
padding: 0;
position: absolute;
white-space: nowrap;
width: 1px;
}
${sx}
`

export {InputLabel}
export default InputLabel
Loading

0 comments on commit 7048d46

Please sign in to comment.