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

Enhance ExternalInput: Pass filters to mapProp for custom data mapping #714

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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 packages/core/components/ExternalInput/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const ExternalInput = ({
readOnly?: boolean;
}) => {
const {
mapProp = (val: any) => val,
mapProp = (val: any,filters: object) => val,
mapRow = (val: any) => val,
filterFields,
} = field || {};
Expand All @@ -41,8 +41,8 @@ export const ExternalInput = ({
const [isLoading, setIsLoading] = useState(true);

const hasFilterFields = !!filterFields;

const [filters, setFilters] = useState(field.initialFilters || {});
// @ts-ignore
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We ideally shouldn't be introducing @ts-ignore, if at all possible

const [filters, setFilters] = useState(field.initialFilters || value?.[name] || {});
const [filtersToggled, setFiltersToggled] = useState(hasFilterFields);

const mappedData = useMemo(() => {
Expand Down Expand Up @@ -244,7 +244,7 @@ export const ExternalInput = ({
style={{ whiteSpace: "nowrap" }}
className={getClassNameModal("tr")}
onClick={() => {
onChange(mapProp(data[i]));
onChange(mapProp(data[i],filters));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be formatted, otherwise it will fail linting. You can run npm run format to address it.


setOpen(false);
}}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/types/Fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export type ExternalField<
query: string;
filters: Record<string, any>;
}) => Promise<any[] | null>;
mapProp?: (value: any) => Props;
mapProp?: (value: any,filters:any) => Props;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The second param would probably be better as an object, and we may as well use the correct type for filters -

Suggested change
mapProp?: (value: any,filters:any) => Props;
mapProp?: (value: any, options: { filters: Record<string, any> }) => Props;

mapRow?: (value: any) => Record<string, string | number>;
getItemSummary?: (item: Props, index?: number) => string;
showSearch?: boolean;
Expand Down