Skip to content

Commit

Permalink
Fix types
Browse files Browse the repository at this point in the history
  • Loading branch information
MBilalShafi committed Oct 31, 2024
1 parent ed2a6aa commit 0f54568
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const useGridStateInitialization = <PrivateApi extends GridPrivateApiComm
apiRef: React.MutableRefObject<PrivateApi>,
) => {
const controlStateMapRef = React.useRef<
Record<string, GridControlStateItem<PrivateApi['state'], any>>
Record<string, GridControlStateItem<PrivateApi['state'], any, any>>
>({});
const [, rawForceUpdate] = React.useState<PrivateApi['state']>();

Expand Down
2 changes: 1 addition & 1 deletion packages/x-data-grid/src/models/api/gridStateApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ export interface GridStatePrivateApi<State extends GridStateCommunity> {
* @param {GridControlStateItem>} controlState The [[GridControlStateItem]] to be registered.
*/
registerControlState: <E extends keyof GridControlledStateEventLookup>(
controlState: GridControlStateItem<State, E>,
controlState: GridControlStateItem<State, any, E>,
) => void;
}
3 changes: 2 additions & 1 deletion packages/x-data-grid/src/models/controlStateItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import { GridStateCommunity } from './gridStateCommunity';

export interface GridControlStateItem<
State extends GridStateCommunity,
Args,
E extends keyof GridControlledStateEventLookup,
> {
stateId: string;
propModel?: GridEventLookup[E]['params'];
stateSelector:
| OutputSelector<State, GridControlledStateEventLookup[E]['params']>
| OutputSelector<State, Args, GridControlledStateEventLookup[E]['params']>
| ((state: State) => GridControlledStateEventLookup[E]['params']);
propOnChange?: (
model: GridControlledStateEventLookup[E]['params'],
Expand Down
3 changes: 3 additions & 0 deletions packages/x-data-grid/src/utils/createSelector.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ describe('createSelector', () => {
it('should fallback to the default behavior when no cache key is provided', () => {
const selector = createSelectorMemoized([], () => []) as OutputSelector<
GridStateCommunity,
any,
any
>;
const state = {} as GridStateCommunity;
Expand Down Expand Up @@ -48,6 +49,7 @@ describe('createSelector', () => {
it('should return different selectors for different cache keys', () => {
const selector = createSelectorMemoized([], () => []) as OutputSelector<
GridStateCommunity,
any,
any
>;
const apiRef1 = {
Expand All @@ -62,6 +64,7 @@ describe('createSelector', () => {
it('should not clear the cache of one selector when another key is passed', () => {
const selector = createSelectorMemoized([], () => []) as OutputSelector<
GridStateCommunity,
any,
any
>;
const apiRef1 = {
Expand Down
11 changes: 5 additions & 6 deletions packages/x-data-grid/src/utils/createSelector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ const reselectCreateSelector = createSelectorCreator({
},
});

type GridCreateSelectorFunction = ReturnType<typeof reselectCreateSelector> & {
type CreateSelectorFunctionWithArgs = ReturnType<typeof reselectCreateSelector> & {
selectorArgs?: any;
};

export interface OutputSelector<State, Args, Result> {
(
apiRef: React.MutableRefObject<{ state: State; instanceId: GridCoreApi['instanceId'] }>,
args: Args,
args?: Args,
): Result;
(state: State, instanceId: GridCoreApi['instanceId']): Result;
acceptsApiRef: boolean;
Expand Down Expand Up @@ -55,7 +55,6 @@ type SelectorArgs<Selectors extends ReadonlyArray<Selector<any>>, Args, Result>
// Input selectors as separate inline arguments
| [...Selectors, (...args: SelectorResultArrayWithArgs<Selectors, Args>) => Result];

// TODO v8: Rename this type to `CreateSelectorFunction`
type CreateSelectorFunction = <Selectors extends ReadonlyArray<Selector<any>>, Args, Result>(
...items: SelectorArgs<Selectors, Args, Result>
) => OutputSelector<StateFromSelectorList<Selectors>, Args, Result>;
Expand Down Expand Up @@ -153,7 +152,7 @@ export const createSelector = ((
}) as unknown as CreateSelectorFunction;

export const createSelectorMemoized: CreateSelectorFunction = (...args: any) => {
const selector = (stateOrApiRef: any, selectorArgs: any, instanceId?: any) => {
const selector = (stateOrApiRef: any, selectorArgs: any = undefined, instanceId?: any) => {
const isAPIRef = checkIsAPIRef(stateOrApiRef);
const cacheKey = isAPIRef
? stateOrApiRef.current.instanceId
Expand All @@ -179,7 +178,7 @@ export const createSelectorMemoized: CreateSelectorFunction = (...args: any) =>
selectorArgs !== undefined
? [...args.slice(0, args.length - 1), () => selectorArgs, args[args.length - 1]]
: args;
const fn: GridCreateSelectorFunction = reselectCreateSelector(...reselectArgs);
const fn: CreateSelectorFunctionWithArgs = reselectCreateSelector(...reselectArgs);
fn.selectorArgs = selectorArgs;
cacheArgs.set(args, fn);
return fn(state, selectorArgs, cacheKey);
Expand All @@ -194,7 +193,7 @@ export const createSelectorMemoized: CreateSelectorFunction = (...args: any) =>
? [...args.slice(0, args.length - 1), () => selectorArgs, args[args.length - 1]]
: args;

const fn: GridCreateSelectorFunction = reselectCreateSelector(...reselectArgs);
const fn: CreateSelectorFunctionWithArgs = reselectCreateSelector(...reselectArgs);
fn.selectorArgs = selectorArgs;

if (!cacheArgsInit) {
Expand Down

0 comments on commit 0f54568

Please sign in to comment.