Skip to content

Commit

Permalink
fix(TMC-28581): UI Form display fix with hint and required asterisk (#…
Browse files Browse the repository at this point in the history
…5319)

* fix(TMC-28581): UI Form display fix with hint and required asterisk

* change required * to css

---------

Co-authored-by: Geoffroy Baccarini <[email protected]>
  • Loading branch information
Gbacc and Geoffroy Baccarini authored May 29, 2024
1 parent bcb24b1 commit 5b7240e
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/weak-boats-destroy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@talend/design-system": patch
---

Form field label property "required" can now be overriden by passing props
5 changes: 5 additions & 0 deletions .changeset/wet-sloths-talk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@talend/react-forms": patch
---

UI Form fields with both hint and required asterisk are now displayed correctly
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ const Field = forwardRef(

const LabelComponent = hideLabel ? (
<VisuallyHidden>
<Label {...labelProps} htmlFor={fieldId} required={required} />
<Label htmlFor={fieldId} required={required} {...labelProps} />
</VisuallyHidden>
) : (
<Label {...labelProps} htmlFor={fieldId} required={required} />
<Label htmlFor={fieldId} required={required} {...labelProps} />
);

const Description = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function FieldTemplate(props) {
const title = (
<Form.Label
htmlFor={props.id}
{...getLabelProps(props.label, props.labelProps, props.hint)}
{...getLabelProps(props.label, props.labelProps, props.hint, props.required)}
required={props.required}
/>
);
Expand Down
2 changes: 1 addition & 1 deletion packages/forms/src/UIForm/fields/File/File.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ const FileWidget = props => {
<SkeletonInput />
) : (
<Form.File
label={getLabelProps(title, labelProps, schema.hint)}
label={getLabelProps(title, labelProps, schema.hint, required)}
required={required}
accept={accept}
autoFocus={autoFocus}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export default function Select({
readOnly={readOnly}
value={value}
required={schema.required}
label={getLabelProps(title, labelProps, schema.hint)}
label={getLabelProps(title, labelProps, schema.hint, schema.required)}
description={errorMessage || description}
hasError={!isValid}
aria-invalid={!isValid}
Expand Down
2 changes: 1 addition & 1 deletion packages/forms/src/UIForm/fields/Text/Text.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export default function Text(props) {
readOnly,
type,
value,
label: getLabelProps(title, labelProps, schema.hint),
label: getLabelProps(title, labelProps, schema.hint, schema.required),
required: schema.required,
description: errorMessage || description,
hasError: !isValid,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default function TextArea({
<Form.Textarea
id={id}
autoFocus={autoFocus}
label={getLabelProps(title, labelProps, schema.hint)}
label={getLabelProps(title, labelProps, schema.hint, schema.required)}
required={schema.required}
disabled={disabled || valueIsUpdating}
placeholder={placeholder}
Expand Down
11 changes: 8 additions & 3 deletions packages/forms/src/UIForm/utils/labels.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import classnames from 'classnames';

import { ButtonIcon, Popover, StackHorizontal } from '@talend/design-system';

export const getLabelProps = (title, labelProps, hint) => {
import styles from './labels.module.scss';

export const getLabelProps = (title, labelProps, hint, required) => {
if (!hint) {
return {
children: title,
Expand All @@ -9,8 +13,8 @@ export const getLabelProps = (title, labelProps, hint) => {
}
return {
children: (
<StackHorizontal gap="XS" align="center">
{title}
<StackHorizontal gap="XXS" align="center">
<span className={classnames({ [styles.required]: required })}>{title}</span>
<Popover
position={hint.overlayPlacement || 'auto'}
data-test={hint['data-test']}
Expand All @@ -28,5 +32,6 @@ export const getLabelProps = (title, labelProps, hint) => {
</StackHorizontal>
),
...labelProps,
required: false,
};
};
4 changes: 4 additions & 0 deletions packages/forms/src/UIForm/utils/labels.module.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.required:after {
content: '*';
display: inline;
}
9 changes: 6 additions & 3 deletions packages/forms/stories/json/fields/core-text.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"password": {
"type": "string"
}
}
},
"required": ["text", "numberMinMax"]
},
"uiSchema": [
{
Expand All @@ -32,11 +33,13 @@
"data-test": "simple.text",
"labelProps": {
"data-test": "simple.text.label"
}
},
"hint": { "overlayComponent": "This is a simple text input" }
},
{
"key": "number",
"title": "Number"
"title": "Number",
"hint": { "overlayComponent": "This is a simple text input" }
},
{
"key": "numberMinMax",
Expand Down

0 comments on commit 5b7240e

Please sign in to comment.