Skip to content

Commit 9d62d13

Browse files
authored
Allow to hide label for TextFields (#179)
1 parent a8fc49b commit 9d62d13

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/elements/forms/text-field.jsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const TextField = React.forwardRef(
2929
helper,
3030
variant = 'normal',
3131
size = 'default',
32+
labelSrOnly = false,
3233
tag: Tag = 'div',
3334
...inputProps
3435
},
@@ -62,7 +63,12 @@ const TextField = React.forwardRef(
6263
.from(styles)}
6364
{...inputProps}
6465
/>
65-
<label htmlFor={controlId}>{label}</label>
66+
<label
67+
htmlFor={controlId}
68+
className={classNames.use(labelSrOnly && 'sr-only')}
69+
>
70+
{label}
71+
</label>
6672
<span id={`${id}-helper`} className={styles.helper}>
6773
{helper}
6874
</span>
@@ -76,6 +82,10 @@ TextField.propTypes = {
7682
* Label for the text field
7783
*/
7884
label: PropTypes.node.isRequired,
85+
/**
86+
* Hide label and expose it only for screen readers
87+
*/
88+
labelSrOnly: PropTypes.bool,
7989
/**
8090
* Size variation. Affects height only.
8191
*/

src/elements/forms/text-field.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,19 @@ Example with disabled input:
7777
/>
7878
```
7979

80+
Example with hidden label:
81+
82+
```jsx
83+
<TextField
84+
id="hidden-label"
85+
name="hidden-label"
86+
label="Input with hidden label"
87+
labelSrOnly
88+
value="Hidden label"
89+
variant="success"
90+
/>
91+
```
92+
8093
### Size variations
8194

8295
#### Small

0 commit comments

Comments
 (0)