Skip to content

Commit

Permalink
Align ExternalProps naming
Browse files Browse the repository at this point in the history
  • Loading branch information
mj12albert committed Sep 7, 2023
1 parent 6d765df commit fe64921
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 20 deletions.
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');
});
});
});
14 changes: 7 additions & 7 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 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

0 comments on commit fe64921

Please sign in to comment.