Skip to content

Commit

Permalink
[FormControl][base] Do not use optional fields in useFormControlConte…
Browse files Browse the repository at this point in the history
…xt's return value (mui#37037)
  • Loading branch information
michaldudak authored Apr 27, 2023
1 parent f4f3164 commit 498f55b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 31 deletions.
19 changes: 10 additions & 9 deletions docs/pages/base/api/use-form-control-context.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
{
"parameters": {},
"returnValue": {
"disabled": { "type": { "name": "boolean", "description": "boolean" }, "required": true },
"error": { "type": { "name": "boolean", "description": "boolean" }, "required": true },
"filled": { "type": { "name": "boolean", "description": "boolean" }, "required": true },
"focused": { "type": { "name": "boolean", "description": "boolean" }, "required": true },
"onBlur": {
"type": { "name": "() => void", "description": "() => void" },
"required": true
},
"onFocus": {
"type": { "name": "() => void", "description": "() => void" },
"required": true
},
"disabled": { "type": { "name": "boolean", "description": "boolean" }, "default": "false" },
"error": { "type": { "name": "boolean", "description": "boolean" }, "default": "false" },
"onChange": {
"type": {
"name": "React.ChangeEventHandler<NativeFormControlElement>",
"description": "React.ChangeEventHandler<NativeFormControlElement>"
}
},
"required": true
},
"onFocus": {
"type": { "name": "() => void", "description": "() => void" },
"required": true
},
"required": { "type": { "name": "boolean", "description": "boolean" }, "default": "false" },
"value": { "type": { "name": "unknown", "description": "unknown" } }
"required": { "type": { "name": "boolean", "description": "boolean" }, "required": true },
"value": { "type": { "name": "unknown", "description": "unknown" }, "required": true }
},
"name": "useFormControlContext",
"filename": "/packages/mui-base/src/FormControl/useFormControlContext.ts",
Expand Down
60 changes: 38 additions & 22 deletions packages/mui-base/src/FormControl/FormControl.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,28 +86,44 @@ export type FormControlOwnerState = Simplify<
}
>;

type ContextFromPropsKey = 'disabled' | 'error' | 'onChange' | 'required' | 'value';

export type FormControlState = Simplify<
Pick<FormControlProps, ContextFromPropsKey> & {
/**
* If `true`, the form element has some value.
*/
filled: boolean;
/**
* If `true`, the form element is focused and not disabled.
*/
focused: boolean;
/**
* Callback fired when the form element has lost focus.
*/
onBlur: () => void;
/**
* Callback fired when the form element receives focus.
*/
onFocus: () => void;
}
>;
export type FormControlState = {
/**
* If `true`, the label, input and helper text should be displayed in a disabled state.
*/
disabled: boolean;
/**
* If `true`, the label is displayed in an error state.
*/
error: boolean;
/**
* If `true`, the form element has some value.
*/
filled: boolean;
/**
* If `true`, the form element is focused and not disabled.
*/
focused: boolean;
/**
* Callback fired when the form element has lost focus.
*/
onBlur: () => void;
/**
* Callback fired when the form element's value is modified.
*/
onChange: React.ChangeEventHandler<NativeFormControlElement>;
/**
* Callback fired when the form element receives focus.
*/
onFocus: () => void;
/**
* If `true`, the label will indicate that the `input` is required.
*/
required: boolean;
/**
* The value of the form element.
*/
value: unknown;
};

export type FormControlRootSlotProps = {
children: React.ReactNode | ((state: FormControlState) => React.ReactNode);
Expand Down

0 comments on commit 498f55b

Please sign in to comment.