Skip to content

Commit 964f9ab

Browse files
[TextField] Add focused prop (#20276)
1 parent 8ea2df8 commit 964f9ab

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-1
lines changed

docs/pages/api-docs/form-control.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ You can find one composition example below and more going to [the demos](/compon
5050
| <span class="prop-name">component</span> | <span class="prop-type">elementType</span> | <span class="prop-default">'div'</span> | The component used for the root node. Either a string to use a DOM element or a component. |
5151
| <span class="prop-name">disabled</span> | <span class="prop-type">bool</span> | <span class="prop-default">false</span> | If `true`, the label, input and helper text should be displayed in a disabled state. |
5252
| <span class="prop-name">error</span> | <span class="prop-type">bool</span> | <span class="prop-default">false</span> | If `true`, the label should be displayed in an error state. |
53+
| <span class="prop-name">focused</span> | <span class="prop-type">bool</span> | | If `true`, the component will be displayed in focused state. |
5354
| <span class="prop-name">fullWidth</span> | <span class="prop-type">bool</span> | <span class="prop-default">false</span> | If `true`, the component will take up the full width of its container. |
5455
| <span class="prop-name">hiddenLabel</span> | <span class="prop-type">bool</span> | <span class="prop-default">false</span> | If `true`, the label will be hidden. This is used to increase density for a `FilledInput`. Be sure to add `aria-label` to the `input` element. |
5556
| <span class="prop-name">margin</span> | <span class="prop-type">'none'<br>&#124;&nbsp;'dense'<br>&#124;&nbsp;'normal'</span> | <span class="prop-default">'none'</span> | If `dense` or `normal`, will adjust vertical spacing of this and contained components. |

packages/material-ui/src/FormControl/FormControl.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export interface FormControlTypeMap<P = {}, D extends React.ElementType = 'div'>
88
disabled?: boolean;
99
error?: boolean;
1010
fullWidth?: boolean;
11+
focused?: boolean;
1112
hiddenLabel?: boolean;
1213
margin?: PropTypes.Margin;
1314
required?: boolean;

packages/material-ui/src/FormControl/FormControl.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ const FormControl = React.forwardRef(function FormControl(props, ref) {
6969
disabled = false,
7070
error = false,
7171
fullWidth = false,
72+
focused: visuallyFocused,
7273
hiddenLabel = false,
7374
margin = 'none',
7475
required = false,
@@ -118,7 +119,8 @@ const FormControl = React.forwardRef(function FormControl(props, ref) {
118119
return initialFilled;
119120
});
120121

121-
const [focused, setFocused] = React.useState(false);
122+
const [_focused, setFocused] = React.useState(false);
123+
const focused = visuallyFocused !== undefined ? visuallyFocused : _focused;
122124

123125
if (disabled && focused) {
124126
setFocused(false);
@@ -229,6 +231,10 @@ FormControl.propTypes = {
229231
* If `true`, the label should be displayed in an error state.
230232
*/
231233
error: PropTypes.bool,
234+
/**
235+
* If `true`, the component will be displayed in focused state.
236+
*/
237+
focused: PropTypes.bool,
232238
/**
233239
* If `true`, the component will take up the full width of its container.
234240
*/

packages/material-ui/src/FormControl/FormControl.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,22 @@ describe('<FormControl />', () => {
109109
});
110110
});
111111

112+
describe('prop: focused', () => {
113+
it('should display input in focused state', () => {
114+
const readContext = spy();
115+
const { container } = render(
116+
<FormControl focused>
117+
<Input />
118+
<TestComponent contextCallback={readContext} />
119+
</FormControl>,
120+
);
121+
122+
expect(readContext.args[0][0]).to.have.property('focused', true);
123+
container.querySelector('input').blur();
124+
expect(readContext.args[0][0]).to.have.property('focused', true);
125+
});
126+
});
127+
112128
describe('input', () => {
113129
it('should be filled when a value is set', () => {
114130
const readContext = spy();

0 commit comments

Comments
 (0)