Use <label> HTML element when possible (#7609)

This PR:

- Uses `<label>` HTML elements when possible to represent labels
- Uses the new `useId()` React hook to get an identifier to link the
label with its input; it's more suitable than generating a UUID at every
render

Fixes #7281

---------

Co-authored-by: Charles Bochet <charles@twenty.com>
This commit is contained in:
Baptiste Devessier
2024-10-13 20:04:24 +02:00
committed by GitHub
parent 284b2677be
commit 05e8f8a0b1
5 changed files with 26 additions and 14 deletions

View File

@ -1,5 +1,5 @@
import React from 'react';
import styled from '@emotion/styled'; import styled from '@emotion/styled';
import React, { useId } from 'react';
interface TextInputProps { interface TextInputProps {
label?: string; label?: string;
@ -18,7 +18,7 @@ const StyledContainer = styled.div<{ fullWidth?: boolean }>`
margin-bottom: ${({ theme }) => theme.spacing(4)}; margin-bottom: ${({ theme }) => theme.spacing(4)};
`; `;
const StyledLabel = styled.span` const StyledLabel = styled.label`
color: ${({ theme }) => theme.font.color.light}; color: ${({ theme }) => theme.font.color.light};
font-size: ${({ theme }) => theme.font.size.xs}; font-size: ${({ theme }) => theme.font.size.xs};
font-weight: ${({ theme }) => theme.font.weight.semiBold}; font-weight: ${({ theme }) => theme.font.weight.semiBold};
@ -65,12 +65,15 @@ const TextInput: React.FC<TextInputProps> = ({
placeholder, placeholder,
icon, icon,
}) => { }) => {
const inputId = useId();
return ( return (
<StyledContainer fullWidth={fullWidth}> <StyledContainer fullWidth={fullWidth}>
{label && <StyledLabel>{label}</StyledLabel>} {label && <StyledLabel htmlFor={inputId}>{label}</StyledLabel>}
<StyledInputContainer> <StyledInputContainer>
{icon && <StyledIcon>{icon}</StyledIcon>} {icon && <StyledIcon>{icon}</StyledIcon>}
<StyledInput <StyledInput
id={inputId}
type="text" type="text"
value={value} value={value}
onChange={(e) => onChange(e.target.value)} onChange={(e) => onChange(e.target.value)}

View File

@ -1,7 +1,6 @@
import styled from '@emotion/styled'; import styled from '@emotion/styled';
import * as React from 'react'; import * as React from 'react';
import { IconCheck, IconMinus } from 'twenty-ui'; import { IconCheck, IconMinus } from 'twenty-ui';
import { v4 } from 'uuid';
export enum CheckboxVariant { export enum CheckboxVariant {
Primary = 'primary', Primary = 'primary',
@ -165,7 +164,7 @@ export const Checkbox = ({
setIsInternalChecked(event.target.checked ?? false); setIsInternalChecked(event.target.checked ?? false);
}; };
const checkboxId = 'checkbox' + v4(); const checkboxId = React.useId();
return ( return (
<StyledInputContainer <StyledInputContainer

View File

@ -3,7 +3,6 @@ import { motion } from 'framer-motion';
import * as React from 'react'; import * as React from 'react';
import { RGBA } from 'twenty-ui'; import { RGBA } from 'twenty-ui';
import { v4 } from 'uuid';
import { RadioGroup } from './RadioGroup'; import { RadioGroup } from './RadioGroup';
export enum RadioSize { export enum RadioSize {
@ -134,7 +133,7 @@ export const Radio = ({
onCheckedChange?.(event.target.checked); onCheckedChange?.(event.target.checked);
}; };
const optionId = v4(); const optionId = React.useId();
return ( return (
<StyledContainer className={className} labelPosition={labelPosition}> <StyledContainer className={className} labelPosition={labelPosition}>

View File

@ -6,6 +6,7 @@ import {
ForwardedRef, ForwardedRef,
InputHTMLAttributes, InputHTMLAttributes,
forwardRef, forwardRef,
useId,
useRef, useRef,
useState, useState,
} from 'react'; } from 'react';
@ -21,7 +22,7 @@ const StyledContainer = styled.div<
width: ${({ fullWidth }) => (fullWidth ? `100%` : 'auto')}; width: ${({ fullWidth }) => (fullWidth ? `100%` : 'auto')};
`; `;
const StyledLabel = styled.span` const StyledLabel = styled.label`
color: ${({ theme }) => theme.font.color.light}; color: ${({ theme }) => theme.font.color.light};
font-size: ${({ theme }) => theme.font.size.xs}; font-size: ${({ theme }) => theme.font.size.xs};
font-weight: ${({ theme }) => theme.font.weight.semiBold}; font-weight: ${({ theme }) => theme.font.weight.semiBold};
@ -169,9 +170,15 @@ const TextInputV2Component = (
setPasswordVisible(!passwordVisible); setPasswordVisible(!passwordVisible);
}; };
const inputId = useId();
return ( return (
<StyledContainer className={className} fullWidth={fullWidth ?? false}> <StyledContainer className={className} fullWidth={fullWidth ?? false}>
{label && <StyledLabel>{label + (required ? '*' : '')}</StyledLabel>} {label && (
<StyledLabel htmlFor={inputId}>
{label + (required ? '*' : '')}
</StyledLabel>
)}
<StyledInputContainer> <StyledInputContainer>
{!!LeftIcon && ( {!!LeftIcon && (
<StyledLeftIconContainer> <StyledLeftIconContainer>
@ -181,6 +188,7 @@ const TextInputV2Component = (
</StyledLeftIconContainer> </StyledLeftIconContainer>
)} )}
<StyledInput <StyledInput
id={inputId}
data-testid={dataTestId} data-testid={dataTestId}
autoComplete={autoComplete || 'off'} autoComplete={autoComplete || 'off'}
ref={combinedRef} ref={combinedRef}

View File

@ -69,11 +69,14 @@ export const Toggle = ({
}; };
useEffect(() => { useEffect(() => {
if (value !== isOn) { setIsOn((isOn) => {
setIsOn(value ?? false); if (value !== isOn) {
} return value ?? false;
// eslint-disable-next-line react-hooks/exhaustive-deps }
}, [value]);
return isOn;
});
}, [value, setIsOn]);
return ( return (
<StyledContainer <StyledContainer