102 lines
2.5 KiB
JavaScript
102 lines
2.5 KiB
JavaScript
import { useOptions } from 'client/hooks/useOptions';
|
|
import { useStatus, useTableStatus } from 'client/hooks/useStatus';
|
|
import { useStoreValue, useTableValue } from 'client/hooks/useValue';
|
|
import { useValidation } from 'client/hooks/useValidation';
|
|
import { observer, useObserver } from 'mobx-react-lite';
|
|
import React from 'react';
|
|
import { useStores } from 'client/hooks/useStores';
|
|
import { useTableOptions } from 'client/hooks/useOptions';
|
|
|
|
export const withStoreValue = Component => ({
|
|
name,
|
|
valueName,
|
|
computedValue,
|
|
validation,
|
|
...params
|
|
}) => {
|
|
const ComponentWithStore = () => {
|
|
const { value, setCurrentValue, debouncedValue } = useStoreValue({
|
|
computedValue,
|
|
valueName,
|
|
});
|
|
const { status } = useStatus(name);
|
|
const { isValid, validateStatus, message } = useValidation({
|
|
elementName: name,
|
|
value: debouncedValue,
|
|
validation: validation || {
|
|
errorMessage: '',
|
|
validator: () => {},
|
|
},
|
|
});
|
|
const { options, filter } = useOptions(name);
|
|
|
|
return (
|
|
<Component
|
|
value={value}
|
|
setCurrentValue={setCurrentValue}
|
|
status={status}
|
|
validateStatus={validateStatus}
|
|
message={message}
|
|
options={options}
|
|
filter={filter}
|
|
{...params}
|
|
/>
|
|
);
|
|
};
|
|
return observer(ComponentWithStore);
|
|
};
|
|
|
|
export const withTableData = Table => props => {
|
|
const { name: tableName } = props;
|
|
const ObservedTable = observer(Table);
|
|
const TableWithStore = useObserver(() => {
|
|
const { calculationStore } = useStores();
|
|
const tableData = calculationStore.tables[tableName];
|
|
return (
|
|
<ObservedTable
|
|
{...props}
|
|
{...tableData}
|
|
actions={{
|
|
deleteRow: rowIndex => {
|
|
calculationStore.deleteTableRow(tableName, rowIndex);
|
|
},
|
|
}}
|
|
/>
|
|
);
|
|
});
|
|
return () => TableWithStore;
|
|
};
|
|
|
|
export const withTableValue = Component => ({
|
|
tableName,
|
|
rowIndex,
|
|
propName,
|
|
...params
|
|
}) => {
|
|
const ComponentWithStore = () => {
|
|
const { value, setCurrentValue } = useTableValue({
|
|
tableName,
|
|
rowIndex,
|
|
propName,
|
|
});
|
|
const { status } = useTableStatus({ tableName, rowIndex, propName });
|
|
const { options, filter } = useTableOptions({
|
|
tableName,
|
|
rowIndex,
|
|
propName,
|
|
});
|
|
|
|
return (
|
|
<Component
|
|
{...params}
|
|
value={value}
|
|
setCurrentValue={setCurrentValue}
|
|
status={status}
|
|
options={options}
|
|
filter={filter}
|
|
/>
|
|
);
|
|
};
|
|
return observer(ComponentWithStore);
|
|
};
|