Skip to content

Commit

Permalink
Feat(web-react): Add Label component to form components
Browse files Browse the repository at this point in the history
- Checkbox, FileUploader, Modal, Radio, Select, TextFieldBase, Slider, Toggle components are updated to use Label component
  • Loading branch information
pavelklibani committed Feb 3, 2025
1 parent 3db3c48 commit 451e6cf
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 28 deletions.
13 changes: 10 additions & 3 deletions packages/web-react/src/components/Checkbox/Checkbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useStyleProps } from '../../hooks';
import { SpiritCheckboxProps } from '../../types';
import { HelperText, ValidationText, useAriaIds } from '../Field';
import { useValidationTextRole } from '../Field/useValidationTextRole';
import { Label } from '../Label';
import { useCheckboxStyleProps } from './useCheckboxStyleProps';

/* We need an exception for components exported with forwardRef */
Expand Down Expand Up @@ -33,7 +34,11 @@ const _Checkbox = (props: SpiritCheckboxProps, ref: ForwardedRef<HTMLInputElemen
});

return (
<label {...styleProps} htmlFor={id} className={classNames(classProps.root, styleProps.className)}>
<Label
htmlFor={id}
UNSAFE_style={styleProps.style}
UNSAFE_className={classNames(classProps.root, styleProps.className)}
>
<input
{...otherProps}
aria-describedby={ids.join(' ')}
Expand All @@ -47,7 +52,9 @@ const _Checkbox = (props: SpiritCheckboxProps, ref: ForwardedRef<HTMLInputElemen
ref={ref}
/>
<span className={classProps.text}>
<span className={classProps.label}>{label}</span>
<Label elementType="span" UNSAFE_className={classProps.label}>
{label}
</Label>
<HelperText
className={classProps.helperText}
elementType="span"
Expand All @@ -66,7 +73,7 @@ const _Checkbox = (props: SpiritCheckboxProps, ref: ForwardedRef<HTMLInputElemen
/>
)}
</span>
</label>
</Label>
);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { SpiritFileUploaderInputProps } from '../../types';
import { HelperText, ValidationText, useAriaIds } from '../Field';
import { useValidationTextRole } from '../Field/useValidationTextRole';
import { Icon } from '../Icon';
import { Label } from '../Label';
import { DEFAULT_FILE_QUEUE_LIMIT, DEFAULT_FILE_SIZE_LIMIT } from './constants';
import { useFileUploaderInput } from './useFileUploaderInput';
import { useFileUploaderStyleProps } from './useFileUploaderStyleProps';
Expand Down Expand Up @@ -85,9 +86,9 @@ const FileUploaderInput = (props: SpiritFileUploaderInputProps) => {
onDrop={!isDisabled && isDragAndDropSupported ? onDrop : undefined}
className={classNames(classProps.input.root, styleProps.className)}
>
<label htmlFor={id} className={classProps.input.label}>
<Label htmlFor={id} UNSAFE_className={classProps.input.label}>
{label}
</label>
</Label>
<input
aria-describedby={ids.join(' ')}
type="file"
Expand All @@ -102,11 +103,11 @@ const FileUploaderInput = (props: SpiritFileUploaderInputProps) => {
/>
<div ref={dropZoneRef} className={classProps.input.dropZone.root}>
<Icon name={iconName} aria-hidden="true" />
<label htmlFor={id} className={classProps.input.dropZone.label}>
<Label htmlFor={id} UNSAFE_className={classProps.input.dropZone.label}>
<span className={classProps.input.link}>{linkText}</span>
&nbsp;
<span className={classProps.input.dropLabel}>{labelText}</span>
</label>
</Label>
<HelperText
className={classProps.input.helper}
id={`${id}__helperText`}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
Checkbox,
Grid,
GridItem,
Label,
Modal,
ModalBody,
ModalDialog,
Expand Down Expand Up @@ -237,17 +238,17 @@ const ModalScrollingLongContent = () => {
}
/>
<Grid UNSAFE_style={{ columnGap: 'var(--spirit-space-600)' }}>
<label
className="GridItem"
<Label
UNSAFE_className="GridItem"
htmlFor={`custom-height-${breakpoint}`}
style={{
UNSAFE_style={{
['--grid-item-column-start' as string]: 1,
['--grid-item-column-end' as string]: 6,
['--grid-item-column-end-tablet' as string]: 4,
}}
>
Height
</label>
</Label>
<GridItem
columnStart={{ mobile: 6, tablet: 4 }}
columnEnd={{ mobile: 13, tablet: 7 }}
Expand All @@ -274,17 +275,17 @@ const ModalScrollingLongContent = () => {
/>
</Grid>
<Grid UNSAFE_style={{ columnGap: 'var(--spirit-space-600)' }}>
<label
className="GridItem"
<Label
UNSAFE_className="GridItem"
htmlFor={`custom-max-height-${breakpoint}`}
style={{
UNSAFE_style={{
['--grid-item-column-start' as string]: 1,
['--grid-item-column-end' as string]: 6,
['--grid-item-column-end-tablet' as string]: 4,
}}
>
Max height
</label>
</Label>
<GridItem
columnStart={{ mobile: 6, tablet: 4 }}
columnEnd={{ mobile: 13, tablet: 7 }}
Expand Down
15 changes: 11 additions & 4 deletions packages/web-react/src/components/Radio/Radio.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
'use client';

import classNames from 'classnames';
import React, { forwardRef, ForwardedRef } from 'react';
import React, { ForwardedRef, forwardRef } from 'react';
import { useStyleProps } from '../../hooks';
import { SpiritRadioProps } from '../../types';
import { HelperText, useAriaIds } from '../Field';
import { Label } from '../Label';
import { useRadioStyleProps } from './useRadioStyleProps';

/* We need an exception for components exported with forwardRef */
Expand All @@ -27,7 +28,11 @@ const _Radio = (props: SpiritRadioProps, ref: ForwardedRef<HTMLInputElement>): J
const [ids, register] = useAriaIds(ariaDescribedBy);

return (
<label htmlFor={id} {...styleProps} className={classNames(classProps.root, styleProps.className)}>
<Label
htmlFor={id}
UNSAFE_style={styleProps.style}
UNSAFE_className={classNames(classProps.root, styleProps.className)}
>
<input
{...otherProps}
aria-describedby={ids.join(' ')}
Expand All @@ -41,7 +46,9 @@ const _Radio = (props: SpiritRadioProps, ref: ForwardedRef<HTMLInputElement>): J
ref={ref}
/>
<span className={classProps.text}>
<span className={classProps.label}>{label}</span>
<Label elementType="span" UNSAFE_className={classProps.label}>
{label}
</Label>
<HelperText
className={classProps.helperText}
elementType="span"
Expand All @@ -50,7 +57,7 @@ const _Radio = (props: SpiritRadioProps, ref: ForwardedRef<HTMLInputElement>): J
helperText={helperText}
/>
</span>
</label>
</Label>
);
};

Expand Down
5 changes: 3 additions & 2 deletions packages/web-react/src/components/Select/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { SpiritSelectProps } from '../../types';
import { HelperText, ValidationText, useAriaIds } from '../Field';
import { useValidationTextRole } from '../Field/useValidationTextRole';
import { Icon } from '../Icon';
import { Label } from '../Label';
import { useSelectStyleProps } from './useSelectStyleProps';

/* We need an exception for components exported with forwardRef */
Expand Down Expand Up @@ -37,9 +38,9 @@ const _Select = (props: SpiritSelectProps, ref: ForwardedRef<HTMLSelectElement>)

return (
<div {...styleProps} className={classNames(classProps.root, styleProps.className)}>
<label htmlFor={id} className={classProps.label}>
<Label htmlFor={id} UNSAFE_className={classProps.label}>
{label}
</label>
</Label>
<div className={classProps.container}>
<select
{...transferProps}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useStyleProps } from '../../hooks';
import { SpiritTextFieldBaseProps, TextFieldBasePasswordToggleProps } from '../../types';
import { HelperText, ValidationText, useAriaIds } from '../Field';
import { useValidationTextRole } from '../Field/useValidationTextRole';
import { Label } from '../Label';
import TextFieldBaseInput from './TextFieldBaseInput';
import { useTextFieldBaseStyleProps } from './useTextFieldBaseStyleProps';
import withPasswordToggle from './withPasswordToggle';
Expand Down Expand Up @@ -36,9 +37,9 @@ const _TextFieldBase = (props: SpiritTextFieldBaseProps, ref: ForwardedRef<HTMLI

return (
<div {...styleProps} className={classNames(classProps.root, styleProps.className)}>
<label htmlFor={id} className={classProps.label}>
<Label htmlFor={id} UNSAFE_className={classProps.label}>
{label}
</label>
</Label>
<TextFieldBaseInputWithPasswordToggle id={id} ref={ref} aria-describedby={ids.join(' ')} {...otherProps} />
<HelperText
className={classProps.helperText}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useStyleProps } from '../../hooks';
import { SpiritSliderProps } from '../../types';
import { HelperText, ValidationText, useAriaIds } from '../Field';
import { useValidationTextRole } from '../Field/useValidationTextRole';
import { Label } from '../Label';
import { SLIDER_DEFAULT_PROPS } from './constants';
import { useSliderStyleProps } from './useSliderStyleProps';

Expand Down Expand Up @@ -53,9 +54,9 @@ const _UnstableSlider = (props: SpiritSliderProps, ref: ForwardedRef<HTMLInputEl

return (
<div {...styleProps} {...otherProps} className={classNames(classProps.root, styleProps.className)}>
<label htmlFor={id} className={classProps.label}>
<Label htmlFor={id} UNSAFE_className={classProps.label}>
{label}
</label>
</Label>
<input
aria-describedby={ids.join(' ')}
className={classProps.input}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useStyleProps } from '../../hooks';
import { SpiritToggleProps } from '../../types';
import { HelperText, useAriaIds, ValidationText } from '../Field';
import { useValidationTextRole } from '../Field/useValidationTextRole';
import { Label } from '../Label';
import { useToggleStyleProps } from './useToggleStyleProps';

/* We need an exception for components exported with forwardRef */
Expand Down Expand Up @@ -40,9 +41,15 @@ const _UNSTABLE_Toggle = (props: SpiritToggleProps, ref: ForwardedRef<HTMLInputE
};

return (
<label {...styleProps} htmlFor={id} className={classNames(classProps.root, styleProps.className)}>
<Label
htmlFor={id}
UNSAFE_style={styleProps.style}
UNSAFE_className={classNames(classProps.root, styleProps.className)}
>
<span className={classProps.text}>
<span className={classProps.label}>{label}</span>
<Label elementType="span" UNSAFE_className={classProps.label}>
{label}
</Label>
<HelperText
className={classProps.helperText}
elementType="span"
Expand Down Expand Up @@ -72,7 +79,7 @@ const _UNSTABLE_Toggle = (props: SpiritToggleProps, ref: ForwardedRef<HTMLInputE
onChange={handleOnChange}
ref={ref}
/>
</label>
</Label>
);
};

Expand Down

0 comments on commit 451e6cf

Please sign in to comment.