-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[STTYPES-6] [UILISTS-92] Add typings for SearchAndSort and friends (#51)
* Connected sources * logger, doh * more deps * remove apollo dep * Searching AND sorting, wow!
- Loading branch information
1 parent
77f3250
commit 61a05c1
Showing
37 changed files
with
773 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { ReactNode } from 'react'; | ||
|
||
export interface ColumnManagerProps { | ||
/** The render-prop function that will receive the relevant props for implementing toggleable columns */ | ||
children: (props: { | ||
/** An array of visible column keys that can be passed directly down to the `<MultiColumnList>` */ | ||
visibleColumns: string[]; | ||
/** Renders a <MenuSection> that wraps renderCheckboxes for toggling columns. This makes it easy to implement inside e.g. a pane action menu. */ | ||
renderColumnsMenu: ReactNode; | ||
/** A method that toggles the visiblity for a given column – e.g. `toggleColumn('email')` */ | ||
toggleColumn: (key: string) => void; | ||
}) => ReactNode; | ||
/** An object that maps keys to labels. The order of the keys will determine the default order of the columns. */ | ||
columnMapping: Record<string, ReactNode>; | ||
/** An array of keys to exclude from the list of toggleable columns */ | ||
excludeKeys?: string[]; | ||
/** | ||
* The unique ID is used to generate the storage key for persisting the visible columns in sessionStorage. | ||
* The ID will also be used as a prefixed ID for any UI that is passed down to the render-prop function. | ||
*/ | ||
id: string; | ||
/** Whether or not to persist the visible columns in sessionStorage */ | ||
persist?: boolean; | ||
} | ||
|
||
/** | ||
* A render-prop component for implementing toggleable columns in a `<MultiColumnList>`. | ||
* | ||
* @example | ||
* ``` | ||
* const columnMapping = { | ||
* status: 'Status', | ||
* name: 'Name', | ||
* barcode: 'Barcode', | ||
* username: 'Username', | ||
* email: 'Email' | ||
* }; | ||
* | ||
* <ColumnManager | ||
* id="users-list-columns" // Required | ||
* columnMapping={columnMapping} // Required | ||
* excludeKeys={['name']} // Exclude these keys from being toggleable | ||
* > | ||
* {({ visibleColumns, renderColumnsMenu, toggleColumn }) => { | ||
* // Render UI | ||
* }} | ||
* </ColumnManager> | ||
* ``` | ||
*/ | ||
export default function ColumnManager(props: ColumnManagerProps): JSX.Element; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { ReactNode } from 'react'; | ||
|
||
export interface ColumnManagerMenuProps { | ||
/** An object that maps keys to labels */ | ||
columnMapping: Record<string, ReactNode>; | ||
/** An array of keys to exclude from the list of columns */ | ||
excludeColumns?: string[]; | ||
/** Unique ID */ | ||
prefix: string; | ||
/** An array of visible column keys */ | ||
visibleColumns: string[]; | ||
} | ||
|
||
/** | ||
* Renders an action menu section for toggling columns. Provided by `<ColumnManager>` as `renderColumnsMenu`. | ||
*/ | ||
export default function ColumnManagerMenu(props: ColumnManagerMenuProps): JSX.Element; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export { default as ColumnManager, ColumnManagerProps } from './ColumnManager'; | ||
export { default as ColumnManagerMenu, ColumnManagerMenuProps } from './ColumnManagerMenu'; | ||
export { default as useColumnManager } from './useColumnManager'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { ReactNode } from 'react'; | ||
|
||
export default function useColumnManager( | ||
prefix: string, | ||
columnMapping: Record<string, ReactNode>, | ||
persist?: boolean, | ||
visibleColumnsProp?: string[], | ||
): { | ||
visibleColumns: string[]; | ||
toggleColumn: (key: string) => void; | ||
}; |
16 changes: 16 additions & 0 deletions
16
smart-components/lib/SearchAndSort/ConnectedSource/ApolloConnectedSource.d.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import Logger from '../../../../util/logger'; | ||
import { ApolloConnectedSourceProps, ApolloError, ConnectedSource } from './ConnectedSource'; | ||
|
||
export default class ApolloConnectedSource implements ConnectedSource { | ||
constructor(props: ApolloConnectedSourceProps, logger: Logger, resourceName?: string); | ||
records(): unknown[]; | ||
resultCount(): number; | ||
totalCount(): number | null | undefined; | ||
pending(): boolean; | ||
loaded(): boolean; | ||
failure(): ApolloError | null | undefined; | ||
|
||
failureMessage(): string; | ||
fetchMore(increment: number): void; | ||
successfulMutations(): unknown[]; | ||
} |
132 changes: 132 additions & 0 deletions
132
smart-components/lib/SearchAndSort/ConnectedSource/ConnectedSource.d.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
import Logger from '../../../../util/logger'; | ||
import { QueryFunction } from '../makeQueryFunction'; | ||
|
||
type ApolloError = { | ||
message: string; | ||
graphQLErrors: unknown[]; | ||
networkError: Error | null; | ||
extraInfo: any; | ||
}; | ||
|
||
export interface ApolloConnectedSourceProps { | ||
apolloResource?: string; | ||
apolloRecordsKey: string; | ||
queryFunction: QueryFunction; | ||
parentData: { | ||
// properties directly on parentData | ||
loading?: boolean; | ||
successfulMutations?: unknown[]; | ||
error?: ApolloError; | ||
fetchMore(params: { | ||
variables: { | ||
cql: string; | ||
offset: number; | ||
limit: number; | ||
}; | ||
// record key is `apolloResource` | ||
updateQuery: ( | ||
prev: Record<string, { records: unknown[] }>, | ||
params: { fetchMoreResult: unknown }, | ||
) => Record<string, { records: unknown[] }>; | ||
}): void; | ||
} & { | ||
// all within resource name (within recordsObj) | ||
[resourceName: string]: { | ||
totalRecords?: number; | ||
} & { | ||
// equals apolloRecordsKey | ||
[recordsKey: string]: unknown[]; | ||
}; | ||
}; | ||
parentResources: { | ||
query: Record<string, unknown>; | ||
}; | ||
} | ||
|
||
type StripesError = { | ||
dataKey?: string; | ||
httpStatus?: unknown; | ||
message?: string; | ||
module?: unknown; | ||
resource?: unknown; | ||
throwErrors?: unknown; | ||
}; | ||
|
||
type StripesResourceType = | ||
| ({ | ||
resultCount: number; | ||
notes?: string | boolean; | ||
filters?: string; | ||
} & { | ||
// called recordsObj in code | ||
[resourceName: string]: { | ||
records?: unknown[]; | ||
hasLoaded?: boolean; | ||
isPending?: boolean; | ||
failed?: StripesError; | ||
other?: unknown; | ||
successfulMutations?: unknown[]; | ||
}; | ||
}) | ||
| unknown; // these types may not be perfect; putting this to avoid errors | ||
|
||
type StripesMutatorType = | ||
| { | ||
resultCount: { | ||
replace: (count: number) => void; | ||
}; | ||
resultOffset: { | ||
replace: (offset: number) => void; | ||
}; | ||
query: { | ||
replace: (query: unknown) => void; | ||
update: (query: unknown) => void; | ||
}; | ||
} | ||
| unknown; // these types may not be perfect; putting this to avoid errors | ||
|
||
export interface StripesConnectedSourceProps { | ||
// key is from resourceName | ||
parentResources?: StripesResourceType; | ||
resources?: StripesResourceType; | ||
parentMutator?: StripesMutatorType; | ||
mutator?: StripesMutatorType; | ||
} | ||
|
||
export interface ConnectedSource { | ||
records(): unknown[]; | ||
|
||
/** Number of records retrieved so far */ | ||
resultCount(): number; | ||
/** | ||
* Number of records in the result-set, available to be retrieved. | ||
* | ||
* For {@code StripesConnectedSource}, when there are > 10k results to a search, the `totalRecords` | ||
* value comes back as `999999999`, so we use that value to indicate | ||
* that the count is, in fact, undefined vs returning null to indicate | ||
* that the count has not been calculated. | ||
*/ | ||
totalCount(): number | null | undefined; | ||
|
||
/** True only during a request, false before and after */ | ||
pending(): boolean; | ||
|
||
/** True only after a request, false before and during */ | ||
loaded(): boolean; | ||
|
||
failure(): ApolloError | StripesError | undefined | null; | ||
|
||
failureMessage(): string; | ||
|
||
fetchMore(increment: number): void; | ||
|
||
successfulMutations(): unknown[]; | ||
} | ||
|
||
export type ConnectedSourceProps = ApolloConnectedSourceProps | StripesConnectedSourceProps; | ||
|
||
export default function makeConnectedSource( | ||
props: ConnectedSourceProps, | ||
logger: Logger, | ||
resourceName?: string, | ||
): ConnectedSource | null; |
18 changes: 18 additions & 0 deletions
18
smart-components/lib/SearchAndSort/ConnectedSource/StripesConnectedSource.d.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { StripesConnectedSourceProps, ConnectedSource, StripesError } from './ConnectedSource'; | ||
import Logger from '../../../../util/logger'; | ||
|
||
export default class StripesConnectedSource implements ConnectedSource { | ||
constructor(props: StripesConnectedSourceProps, logger: Logger, resourceName?: string); | ||
update(props: StripesConnectedSourceProps, resourceName?: string): void; | ||
records(): unknown[]; | ||
resultCount(): number; | ||
totalCount(): number | null | undefined; | ||
pending(): boolean; | ||
loaded(): boolean; | ||
failure(): StripesError | null | undefined; | ||
failureMessage(): string; | ||
fetchMore(increment: number): void; | ||
fetchByBrowsePoint(browsePoint: unknown): void; | ||
fetchOffset(index: number): void; | ||
successfulMutations(): unknown[]; | ||
} |
7 changes: 7 additions & 0 deletions
7
smart-components/lib/SearchAndSort/ConnectedSource/index.d.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export { | ||
default, | ||
ConnectedSource, | ||
ConnectedSourceProps, | ||
ApolloConnectedSourceProps, | ||
StripesConnectedSourceProps, | ||
} from './ConnectedSource'; |
Oops, something went wrong.