From a02fe15ffc0f4f9f5879a858f021e65e9d4ea00d Mon Sep 17 00:00:00 2001 From: Jeff Luyau Date: Thu, 15 Aug 2024 11:04:18 -0700 Subject: [PATCH] Conditionally render field label only if label is provided (#6833) * do not render field label if no children (string) provided * add label-less story for testing * add more examples to form story * add story with form with labeled and non-labeled components * hide value label when label is hidden for meter and progress bar Will need warning in eventual docs that this is not accessible unless the value is displayed visually externally * Improve labeling in example --------- Co-authored-by: Devon Govett --- packages/@react-spectrum/s2/src/Field.tsx | 4 + packages/@react-spectrum/s2/src/Meter.tsx | 4 +- .../@react-spectrum/s2/src/ProgressBar.tsx | 4 +- .../s2/stories/Form.stories.tsx | 160 +++++++++++++++++- 4 files changed, 167 insertions(+), 5 deletions(-) diff --git a/packages/@react-spectrum/s2/src/Field.tsx b/packages/@react-spectrum/s2/src/Field.tsx index 9cb60d5a009..805649d1d13 100644 --- a/packages/@react-spectrum/s2/src/Field.tsx +++ b/packages/@react-spectrum/s2/src/Field.tsx @@ -63,6 +63,10 @@ function FieldLabel(props: FieldLabelProps, ref: DOMRef) { labelProps.id = fallbackLabelPropsId; } + if (!props.children) { + return null; + } + return (
) { }, styles)}> {({percentage, valueText}) => ( <> - {label} - {valueText} + {label && {label}} + {label && {valueText}}
diff --git a/packages/@react-spectrum/s2/src/ProgressBar.tsx b/packages/@react-spectrum/s2/src/ProgressBar.tsx index 854841bdc6a..9c9b4949c27 100644 --- a/packages/@react-spectrum/s2/src/ProgressBar.tsx +++ b/packages/@react-spectrum/s2/src/ProgressBar.tsx @@ -127,8 +127,8 @@ function ProgressBar(props: ProgressBarProps, ref: DOMRef) { className={UNSAFE_className + wrapper({...props, size}, props.styles)}> {({percentage, valueText}) => ( <> - {label} - {valueText} + {label && {label}} + {label && {valueText}}
= { component: Form, @@ -54,3 +87,128 @@ export const Example = (args: any) => ( ); + +export const MixedForm = (args: any) => ( +
+ + + + + Soccer + Baseball + Basketball + + + Cat + Dog + Plant + + + +); + +MixedForm.parameters = { + docs: { + disable: true + } +}; + + +export const CustomLabelsExample = (args: any) => { + const [isSortAscending, setIsSortAscending] = useState(true); + return ( +
+
+ Sort order + setIsSortAscending(!isSortAscending)}> + { + isSortAscending ? : + } + + + Name + Created + +
+
+ Filter terms + + keyword 1 + keyword 2 + +
+ +
+ Color settings + + Enable color + + + +
+ +
+ Search + + Enable search + + + + search term 1 + search term 2 + + +
+
+ Search parameters + + Text + Images + Video + + + Summary + Date + Author + +
+ +
+ 28% complete + + 44% confidence + +
+ +
+
Sliders (with and without label)
+ + Help + Help content + + } /> + + Help + Help content + + } /> +
+ + + ); +}; + +CustomLabelsExample.parameters = { + docs: { + disable: true + } +};