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

1127 filter select is mistakenly resetting value #1128

Merged
merged 2 commits into from
Aug 15, 2023
Merged
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
2 changes: 1 addition & 1 deletion api/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@devtable/api",
"version": "10.18.0",
"version": "10.18.1",
"description": "",
"main": "index.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion dashboard/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@devtable/dashboard",
"version": "10.18.0",
"version": "10.18.1",
"license": "Apache-2.0",
"publishConfig": {
"access": "public",
Expand Down
14 changes: 1 addition & 13 deletions dashboard/src/components/filter/filter-select/render.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { Select } from '@mantine/core';
import { observer } from 'mobx-react-lite';
import { useEffect } from 'react';
import { useRenderContentModelContext } from '~/contexts';
import { FilterMetaInstance, FilterSelectConfigInstance } from '~/model';
import { FilterSelectItem } from '../select-item';

interface IFilterSelect extends Omit<FilterMetaInstance, 'key' | 'type' | 'config'> {
config: FilterSelectConfigInstance;
value: $TSFixMe;
value: string;
onChange: (v: string, forceSubmit?: boolean) => void;
}

Expand All @@ -17,17 +16,6 @@ export const FilterSelect = observer(({ label, config, value, onChange }: IFilte
const { state, error } = model.getDataStuffByID(config.options_query_id);
const loading = state === 'loading';

useEffect(() => {
const { default_selection_count } = config;
if (!default_selection_count) {
return;
}
const newValue = config.options[0]?.value ?? '';

console.log('Selecting the first option by default. New value: ', newValue);
onChange(newValue, true);
}, [config.default_selection_count, config.options]); // excluding onChange from deps, since it's always re-created

return (
<Select
label={label}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Instance, types } from 'mobx-state-tree';
import { addDisposer, getParent, getRoot, Instance, types } from 'mobx-state-tree';
import { FilterBaseSelectConfigMeta } from './select-base';
import { shallowToJS } from '~/utils/shallow-to-js';
import { reaction, toJS } from 'mobx';

export const FilterSelectConfigMeta = types
.compose(
Expand Down Expand Up @@ -32,6 +33,15 @@ export const FilterSelectConfigMeta = types
getSelectOption(value: string) {
return self.options.find((o) => o.value === value);
},
get default_selection() {
if (!self.usingQuery) {
return self.default_value;
}
if (self.default_selection_count > 0 && self.options.length > 0) {
return self.options[0].value;
}
return '';
},
}))
.actions((self) => ({
setRequired(required: boolean) {
Expand All @@ -43,6 +53,24 @@ export const FilterSelectConfigMeta = types
setWidth(v: string) {
self.width = v;
},
setDefaultSelection() {
// @ts-expect-error getRoot type
const filters = getRoot(self).content.filters;
// @ts-expect-error Property 'key' does not exist on type 'IStateTreeNode<IAnyStateTreeNode>
const key = getParent(self).key;
filters.setValueByKey(key, self.default_selection);
},
}))
.actions((self) => ({
afterCreate() {
addDisposer(
self,
reaction(() => toJS(self.default_selection), self.setDefaultSelection, {
fireImmediately: true,
delay: 0,
}),
);
},
}));

export type FilterSelectConfigInstance = Instance<typeof FilterSelectConfigMeta>;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@devtable/root",
"version": "10.18.0",
"version": "10.18.1",
"private": true,
"workspaces": [
"api",
Expand Down
2 changes: 1 addition & 1 deletion settings-form/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@devtable/settings-form",
"version": "10.18.0",
"version": "10.18.1",
"license": "Apache-2.0",
"publishConfig": {
"access": "public",
Expand Down
2 changes: 1 addition & 1 deletion website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@devtable/website",
"private": true,
"license": "Apache-2.0",
"version": "10.18.0",
"version": "10.18.1",
"scripts": {
"dev": "vite",
"preview": "vite preview"
Expand Down