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

[base-ui][useInput] Align ExternalProps naming #38849

Merged
merged 2 commits into from
Sep 11, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions docs/pages/base-ui/api/use-input.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@
},
"getInputProps": {
"type": {
"name": "<TOther extends Record<string, any> = {}>(externalProps?: TOther) => UseInputInputSlotProps<TOther>",
"description": "<TOther extends Record<string, any> = {}>(externalProps?: TOther) => UseInputInputSlotProps<TOther>"
"name": "<ExternalProps extends Record<string, any> = {}>(externalProps?: ExternalProps) => UseInputInputSlotProps<ExternalProps>",
"description": "<ExternalProps extends Record<string, any> = {}>(externalProps?: ExternalProps) => UseInputInputSlotProps<ExternalProps>"
},
"required": true
},
"getRootProps": {
"type": {
"name": "<TOther extends Record<string, any> = {}>(externalProps?: TOther) => UseInputRootSlotProps<TOther>",
"description": "<TOther extends Record<string, any> = {}>(externalProps?: TOther) => UseInputRootSlotProps<TOther>"
"name": "<ExternalProps extends Record<string, any> = {}>(externalProps?: ExternalProps) => UseInputRootSlotProps<ExternalProps>",
"description": "<ExternalProps extends Record<string, any> = {}>(externalProps?: ExternalProps) => UseInputRootSlotProps<ExternalProps>"
},
"required": true
},
Expand Down
20 changes: 20 additions & 0 deletions packages/mui-base/src/useInput/useInput.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,24 @@ describe('useInput', () => {
expect(handleFocus.callCount).to.equal(1);
});
});

describe('external props', () => {
it('prop getter functions should forward arbitrary props to the corresponding slot', () => {
const rootRef = React.createRef<HTMLDivElement>();

function Input() {
const { getRootProps, getInputProps } = useInput();
return (
<div {...getRootProps({ 'data-testid': 'test-root-slot', ref: rootRef })}>
<input {...getInputProps({ 'data-testid': 'test-input-slot' })} />
</div>
);
}
const { getByRole } = render(<Input />);

expect(rootRef.current).to.have.attribute('data-testid', 'test-root-slot');

expect(getByRole('textbox')).to.have.attribute('data-testid', 'test-input-slot');
});
});
});
19 changes: 10 additions & 9 deletions packages/mui-base/src/useInput/useInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
*
* - [useInput API](https://mui.com/base-ui/react-input/hooks-api/#use-input)
*/
export function useInput(parameters: UseInputParameters): UseInputReturnValue {
export function useInput(parameters: UseInputParameters = {}): UseInputReturnValue {
const {
defaultValue: defaultValueProp,
disabled: disabledProp = false,
Expand Down Expand Up @@ -164,9 +164,9 @@ export function useInput(parameters: UseInputParameters): UseInputReturnValue {
otherHandlers.onClick?.(event);
};

const getRootProps = <TOther extends Record<string, any> = {}>(
externalProps: TOther = {} as TOther,
): UseInputRootSlotProps<TOther> => {
const getRootProps = <ExternalProps extends Record<string, any> = {}>(
externalProps: ExternalProps = {} as ExternalProps,
): UseInputRootSlotProps<ExternalProps> => {
// onBlur, onChange and onFocus are forwarded to the input slot.
const propsEventHandlers = extractEventHandlers(parameters, ['onBlur', 'onChange', 'onFocus']);
const externalEventHandlers = { ...propsEventHandlers, ...extractEventHandlers(externalProps) };
Expand All @@ -178,9 +178,9 @@ export function useInput(parameters: UseInputParameters): UseInputReturnValue {
};
};

const getInputProps = <TOther extends Record<string, any> = {}>(
externalProps: TOther = {} as TOther,
): UseInputInputSlotProps<TOther> => {
const getInputProps = <ExternalProps extends Record<string, any> = {}>(
externalProps: ExternalProps = {} as ExternalProps,
): UseInputInputSlotProps<ExternalProps> => {
const propsEventHandlers: Record<string, React.EventHandler<any> | undefined> = {
onBlur,
onChange,
Expand All @@ -190,7 +190,6 @@ export function useInput(parameters: UseInputParameters): UseInputReturnValue {
const externalEventHandlers = { ...propsEventHandlers, ...extractEventHandlers(externalProps) };

const mergedEventHandlers = {
...externalProps,
...externalEventHandlers,
onBlur: handleBlur(externalEventHandlers),
onChange: handleChange(externalEventHandlers),
Expand All @@ -201,10 +200,12 @@ export function useInput(parameters: UseInputParameters): UseInputReturnValue {
...mergedEventHandlers,
'aria-invalid': error || undefined,
defaultValue: defaultValue as string | number | readonly string[] | undefined,
ref: handleInputRef,
value: value as string | number | readonly string[] | undefined,
required,
disabled,
...externalProps,
ref: handleInputRef,
...mergedEventHandlers,
};
};

Expand Down
21 changes: 12 additions & 9 deletions packages/mui-base/src/useInput/useInput.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ export interface UseInputRootSlotOwnProps {
onClick: React.MouseEventHandler | undefined;
}

export type UseInputRootSlotProps<TOther = {}> = Omit<
TOther,
export type UseInputRootSlotProps<ExternalProps = {}> = Omit<
ExternalProps,
keyof UseInputRootSlotOwnProps | 'onBlur' | 'onChange' | 'onFocus'
> &
UseInputRootSlotOwnProps;
Expand All @@ -51,7 +51,10 @@ export interface UseInputInputSlotOwnProps {
disabled: boolean;
}

export type UseInputInputSlotProps<TOther = {}> = Omit<TOther, keyof UseInputInputSlotOwnProps> &
export type UseInputInputSlotProps<ExternalProps = {}> = Omit<
ExternalProps,
keyof UseInputInputSlotOwnProps
> &
UseInputInputSlotOwnProps;

export interface UseInputReturnValue {
Expand All @@ -76,17 +79,17 @@ export interface UseInputReturnValue {
* @param externalProps props for the input slot
* @returns props that should be spread on the input slot
*/
getInputProps: <TOther extends Record<string, any> = {}>(
externalProps?: TOther,
) => UseInputInputSlotProps<TOther>;
getInputProps: <ExternalProps extends Record<string, any> = {}>(
externalProps?: ExternalProps,
) => UseInputInputSlotProps<ExternalProps>;
/**
* Resolver for the root slot's props.
* @param externalProps props for the root slot
* @returns props that should be spread on the root slot
*/
getRootProps: <TOther extends Record<string, any> = {}>(
externalProps?: TOther,
) => UseInputRootSlotProps<TOther>;
getRootProps: <ExternalProps extends Record<string, any> = {}>(
externalProps?: ExternalProps,
) => UseInputRootSlotProps<ExternalProps>;
inputRef: React.RefCallback<HTMLInputElement | HTMLTextAreaElement> | null;
/**
* If `true`, the `input` will indicate that it's required.
Expand Down