Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FormControl][base] Do not use optional fields in useFormControlContext's return value #37037

Merged
merged 1 commit into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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