Skip to content

Commit c7ab314

Browse files
charIeszhaoCopilot
andauthored
refactor(experience,phrases): add optional label support to input fields (#7667)
Co-authored-by: Copilot <[email protected]>
1 parent 2323ca4 commit c7ab314

File tree

23 files changed

+35
-4
lines changed

23 files changed

+35
-4
lines changed

packages/experience/src/components/InputFields/InputField/index.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { type Nullable } from '@silverhand/essentials';
1+
import { condString, type Nullable } from '@silverhand/essentials';
22
import classNames from 'classnames';
33
import type { HTMLProps, ReactElement, Ref, AnimationEvent } from 'react';
44
import {
@@ -46,11 +46,12 @@ const InputField = (
4646
onBlur,
4747
onChange,
4848
value,
49+
required = true,
4950
...props
5051
}: Props,
5152
reference: Ref<Nullable<HTMLInputElement>>
5253
) => {
53-
const { i18n } = useTranslation();
54+
const { t, i18n } = useTranslation();
5455
const innerRef = useRef<HTMLInputElement>(null);
5556

5657
useImperativeHandle(reference, () => innerRef.current);
@@ -83,6 +84,9 @@ const InputField = (
8384
}, []);
8485

8586
const isActive = Boolean(isPrefixVisible) || hasValue || isFocused;
87+
const labelWithOptionalSuffix = required
88+
? label
89+
: condString(label && t('input.label_with_optional', { label }));
8690

8791
return (
8892
<div className={className}>
@@ -127,7 +131,7 @@ const InputField = (
127131
})}
128132
</div>
129133
<NotchedBorder
130-
label={label ?? ''}
134+
label={labelWithOptionalSuffix ?? ''}
131135
isActive={isActive}
132136
isDanger={Boolean(isDanger)}
133137
isFocused={isFocused}

packages/experience/src/components/InputFields/PrimitiveProfileInputField/index.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const PrimitiveProfileInputField = ({
2626
description,
2727
isDanger,
2828
errorMessage,
29+
required,
2930
onBlur,
3031
onChange,
3132
}: Props) => {
@@ -48,6 +49,7 @@ const PrimitiveProfileInputField = ({
4849
value={value}
4950
description={description}
5051
errorMessage={errorMessage}
52+
required={required}
5153
onBlur={onBlur}
5254
onChange={onChange}
5355
/>
@@ -75,6 +77,7 @@ const PrimitiveProfileInputField = ({
7577
isDanger={isDanger}
7678
errorMessage={errorMessage}
7779
placeholder={config?.placeholder}
80+
required={required}
7881
onChange={(event) => {
7982
onChange(event.currentTarget.value);
8083
}}

packages/experience/src/components/InputFields/SelectField/index.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ type Props = {
1717
readonly label?: string;
1818
readonly placeholder?: string;
1919
readonly errorMessage?: string;
20+
// eslint-disable-next-line react/boolean-prop-naming
21+
readonly required?: boolean;
2022
readonly onBlur?: () => void;
2123
readonly onChange: (value: string) => void;
2224
};
@@ -29,6 +31,7 @@ const SelectField = ({
2931
label,
3032
placeholder,
3133
errorMessage,
34+
required,
3235
onBlur,
3336
onChange,
3437
}: Props) => {
@@ -46,6 +49,7 @@ const SelectField = ({
4649
label={label}
4750
placeholder={placeholder}
4851
isDanger={!!errorMessage}
52+
required={required}
4953
value={currentValue}
5054
onClick={() => {
5155
setIsOpen(true);

packages/experience/src/pages/Continue/ExtraProfileForm/AddressSubForm/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ const AddressSubForm = ({ field }: Props) => {
8686
label={part.label ?? t(`profile.address.${part.name}`)}
8787
value={value ?? ''}
8888
isDanger={!!errors.address?.[part.name]}
89+
required={part.required}
8990
onBlur={onBlur}
9091
onChange={(event) => {
9192
onChange(event);

packages/experience/src/pages/Continue/ExtraProfileForm/FullnameSubForm/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ const FullnameSubForm = ({ field }: Props) => {
5555
label={part.label ?? t(`profile.${part.name}`)}
5656
value={value ?? ''}
5757
isDanger={!!errors[part.name]}
58+
required={part.required}
5859
onBlur={onBlur}
5960
onChange={onChange}
6061
/>

packages/experience/src/pages/Continue/ExtraProfileForm/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ const ExtraProfileForm = ({ customProfileFields, defaultValues, onSubmit }: Prop
4646
if (field.type === CustomProfileFieldType.Fullname) {
4747
return <FullnameSubForm key={field.name} field={field} />;
4848
}
49-
const { name, type, label, description } = field;
49+
const { name, type, label, description, required } = field;
5050
return (
5151
<Controller
5252
key={name}
@@ -67,6 +67,7 @@ const ExtraProfileForm = ({ customProfileFields, defaultValues, onSubmit }: Prop
6767
{...field}
6868
label={label || getFieldLabel(name)}
6969
description={condString(description)}
70+
required={required}
7071
value={value}
7172
isDanger={!!errors[name]}
7273
errorMessage={errors[name]?.message}

packages/phrases-experience/src/locales/ar/input.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const input = {
66
confirm_password: 'تأكيد كلمة المرور',
77
search_region_code: 'رمز منطقة البحث',
88
backup_code: 'رمز النسخ الاحتياطي',
9+
label_with_optional: '(اختياري) {{label}}',
910
};
1011

1112
export default Object.freeze(input);

packages/phrases-experience/src/locales/de/input.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const input = {
66
confirm_password: 'Passwort bestätigen',
77
search_region_code: 'Suche region code',
88
backup_code: 'Backup-Code',
9+
label_with_optional: '{{label}} (Optional)',
910
};
1011

1112
export default Object.freeze(input);

packages/phrases-experience/src/locales/en/input.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const input = {
66
confirm_password: 'Confirm password',
77
search_region_code: 'Search region code',
88
backup_code: 'Backup code',
9+
label_with_optional: '{{label}} (Optional)',
910
};
1011

1112
export default Object.freeze(input);

packages/phrases-experience/src/locales/es/input.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const input = {
66
confirm_password: 'Confirmar contraseña',
77
search_region_code: 'Código de región de búsqueda',
88
backup_code: 'Código de respaldo',
9+
label_with_optional: '{{label}} (Opcional)',
910
};
1011

1112
export default Object.freeze(input);

0 commit comments

Comments
 (0)