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

[App Search] Refactor out a shared MultiInputRows component #96881

Merged
merged 11 commits into from
Apr 14, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,10 @@ export const setMockActions = (actions: object) => {
* unmount();
* });
*/
import { resetContext, Logic, LogicInput } from 'kea';
import { resetContext, LogicWrapper } from 'kea';

type LogicFile = LogicWrapper<any>;

interface LogicFile {
inputs: Array<LogicInput<Logic>>;
build(props?: object): void;
mount(): Function;
}
export class LogicMounter {
private logicFile: LogicFile;
private unmountFn!: Function;
Expand All @@ -100,24 +97,39 @@ export class LogicMounter {
}

// Reset context with optional default value overrides
public resetContext = (values?: object) => {
if (!values) {
public resetContext = (values?: object, props?: object) => {
if (!values || !Object.keys(values).length) {
resetContext({});
} else {
const path = this.logicFile.inputs[0].path as string[]; // example: ['x', 'y', 'z']
const defaults = path.reduceRight((value: object, key: string) => ({ [key]: value }), values); // example: { x: { y: { z: values } } }
let { path, key } = this.logicFile.inputs[0];

// For keyed logic files, both key and path should be functions
if (this.logicFile._isKeaWithKey) {
key = key(props);
path = path(key);
}

// Generate the correct nested defaults obj based on the file path
// example path: ['x', 'y', 'z']
// example defaults: { x: { y: { z: values } } }
const defaults = path.reduceRight(
(value: object, name: string) => ({ [name]: value }),
values
);
resetContext({ defaults });
}
};

// Automatically reset context & mount the logic file
public mount = (values?: object, props?: object) => {
this.resetContext(values);
if (props) this.logicFile.build(props);
this.resetContext(values, props);

const logicWithProps = this.logicFile.build(props);
this.unmountFn = logicWithProps.mount();

const unmount = this.logicFile.mount();
this.unmountFn = unmount;
return unmount; // Keep Kea behavior of returning an unmount fn from mount
return logicWithProps;
// NOTE: Unlike kea's mount(), this returns the current
// built logic instance with props, NOT the unmount fn
};

// Also add unmount as a class method that can be destructured on init without becoming stale later
Expand Down Expand Up @@ -146,7 +158,7 @@ export class LogicMounter {
const { listeners } = this.logicFile.inputs[0];

return typeof listeners === 'function'
? (listeners as Function)(listenersArgs) // e.g., listeners({ values, actions, props }) => ({ ... })
? listeners(listenersArgs) // e.g., listeners({ values, actions, props }) => ({ ... })
: listeners; // handles simpler logic files that just define listeners: { ... }
};
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading