diff --git a/babel.config.js b/babel.config.js index f6cd1289..251c96ec 100644 --- a/babel.config.js +++ b/babel.config.js @@ -6,6 +6,17 @@ module.exports = { plugins: [ '@babel/plugin-transform-runtime', '@babel/plugin-proposal-class-properties', - '@babel/plugin-proposal-object-rest-spread' + '@babel/plugin-proposal-object-rest-spread', + [ + 'module-resolver', + { + root: ['./'], + alias: { + '@components': './src/components', + '@store': './src/store', + '@utils': './src/utils' + } + } + ] ] }; diff --git a/src/components/MTableBodyRow/index.js b/src/components/MTableBodyRow/index.js index d1fb418b..e8cc6fe3 100644 --- a/src/components/MTableBodyRow/index.js +++ b/src/components/MTableBodyRow/index.js @@ -9,10 +9,10 @@ import { TableRow } from '@material-ui/core'; // Internal -import { MTableDetailPanel } from '../m-table-detailpanel'; -import * as CommonValues from '../../utils/common-values'; -import { useDoubleClick } from '../../utils/hooks/useDoubleClick'; -import { MTableCustomIcon } from '../../components'; +import { MTableDetailPanel } from '@components/m-table-detailpanel'; +import * as CommonValues from '@utils/common-values'; +import { useDoubleClick } from '@utils/hooks/useDoubleClick'; +import { MTableCustomIcon } from '@components'; export default function MTableBodyRow(props) { const { diff --git a/src/components/MTableCell/utils.js b/src/components/MTableCell/cellUtils.js similarity index 80% rename from src/components/MTableCell/utils.js rename to src/components/MTableCell/cellUtils.js index b62fbca5..a4d10d46 100644 --- a/src/components/MTableCell/utils.js +++ b/src/components/MTableCell/cellUtils.js @@ -1,6 +1,5 @@ import React from 'react'; import parseISO from 'date-fns/parseISO'; -import * as CommonValues from '../../utils/common-values'; /* eslint-disable no-useless-escape */ export const isoDateRegex = /^\d{4}-(0[1-9]|1[0-2])-([12]\d|0[1-9]|3[01])([T\s](([01]\d|2[0-3])\:[0-5]\d|24\:00)(\:[0-5]\d([\.,]\d+)?)?([zZ]|([\+-])([01]\d|2[0-3])\:?([0-5]\d)?)?)?$/; @@ -42,37 +41,6 @@ export function getCurrencyValue(currencySetting, value) { } } -export function getStyle(props) { - const width = CommonValues.reducePercentsInCalc( - props.columnDef.tableData.width, - props.scrollWidth - ); - - let cellStyle = { - color: 'inherit', - width, - maxWidth: props.columnDef.maxWidth, - minWidth: props.columnDef.minWidth, - boxSizing: 'border-box', - fontSize: 'inherit', - fontFamily: 'inherit', - fontWeight: 'inherit' - }; - - if (typeof props.columnDef.cellStyle === 'function') { - cellStyle = { - ...cellStyle, - ...props.columnDef.cellStyle(props.value, props.rowData) - }; - } else { - cellStyle = { ...cellStyle, ...props.columnDef.cellStyle }; - } - if (props.columnDef.disableClick) { - cellStyle.cursor = 'default'; - } - return { ...props.style, ...cellStyle }; -} - export function getRenderValue(props) { const dateLocale = props.columnDef.dateSetting && props.columnDef.dateSetting.locale diff --git a/src/components/MTableCell/index.js b/src/components/MTableCell/index.js index d26897eb..830ef55e 100644 --- a/src/components/MTableCell/index.js +++ b/src/components/MTableCell/index.js @@ -1,9 +1,8 @@ -/* eslint-disable no-unused-vars */ import React from 'react'; import TableCell from '@material-ui/core/TableCell'; import PropTypes from 'prop-types'; -import { getRenderValue, getStyle } from './utils'; -/* eslint-enable no-unused-vars */ +import { getRenderValue } from './cellUtils'; +import { getStyle } from '@utils'; function MTableCell(props) { const { @@ -22,12 +21,14 @@ function MTableCell(props) { } }; + /* eslint-disable indent */ const cellAlignment = columnDef.align !== undefined ? columnDef.align : ['numeric', 'currency'].indexOf(columnDef.type) !== -1 ? 'right' : 'left'; + /* eslint-enable indent */ let renderValue = getRenderValue(props); diff --git a/src/components/MTableEditRow/index.js b/src/components/MTableEditRow/index.js index e316906f..45242604 100644 --- a/src/components/MTableEditRow/index.js +++ b/src/components/MTableEditRow/index.js @@ -3,9 +3,9 @@ import TableCell from '@material-ui/core/TableCell'; import TableRow from '@material-ui/core/TableRow'; import Typography from '@material-ui/core/Typography'; import PropTypes from 'prop-types'; -import { byString, setByString } from '../../utils'; -import * as CommonValues from '../../utils/common-values'; -import { validateInput } from '../../utils/validate'; +import { byString, setByString } from '@utils'; +import * as CommonValues from '@utils/common-values'; +import { validateInput } from '@utils/validate'; function MTableEditRow(props) { const [state, setState] = useState(() => { diff --git a/src/components/MTableSummaryRow/index.js b/src/components/MTableSummaryRow/index.js index 309814c8..a697fdfe 100644 --- a/src/components/MTableSummaryRow/index.js +++ b/src/components/MTableSummaryRow/index.js @@ -1,7 +1,7 @@ import * as React from 'react'; import { TableRow, TableCell, withStyles } from '@material-ui/core'; -import { getStyle } from '../MTableCell/utils'; -import * as CommonValues from '../../utils/common-values'; +import { getStyle } from '@utils'; +import * as CommonValues from '@utils/common-values'; import PropTypes from 'prop-types'; export function MTableSummaryRow({ diff --git a/src/material-table.js b/src/material-table.js index cb2697ab..64216812 100644 --- a/src/material-table.js +++ b/src/material-table.js @@ -8,13 +8,13 @@ import { LinearProgress } from '@material-ui/core'; import { DragDropContext, Droppable } from 'react-beautiful-dnd'; -import DataManager from './utils/data-manager'; -import * as CommonValues from './utils/common-values'; +import DataManager from '@utils/data-manager'; +import * as CommonValues from '@utils/common-values'; import { MTablePagination, MTableSteppedPagination, MTableScrollbar -} from './components'; +} from '@components'; export default class MaterialTable extends React.Component { dataManager = new DataManager(); diff --git a/src/utils/index.js b/src/utils/index.js index de7781fc..43864a87 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -1,8 +1,9 @@ +import * as CommonValues from '@utils/common-values'; + export const byString = (o, s) => { if (!s) { return; } - s = s.replace(/\[(\w+)\]/g, '.$1'); // convert indexes to properties s = s.replace(/^\./, ''); // strip a leading dot const a = s.split('.'); @@ -19,7 +20,6 @@ export const byString = (o, s) => { export const setByString = (obj, path, value) => { let schema = obj; // a moving reference to internal objects within obj - path = path.replace(/\[(\w+)\]/g, '.$1'); // convert indexes to properties path = path.replace(/^\./, ''); // strip a leading dot const pList = path.split('.'); @@ -29,6 +29,34 @@ export const setByString = (obj, path, value) => { if (!schema[elem]) schema[elem] = {}; schema = schema[elem]; } - schema[pList[len - 1]] = value; }; + +export function getStyle(props) { + const width = CommonValues.reducePercentsInCalc( + props.columnDef.tableData.width, + props.scrollWidth + ); + let cellStyle = { + color: 'inherit', + width, + maxWidth: props.columnDef.maxWidth, + minWidth: props.columnDef.minWidth, + boxSizing: 'border-box', + fontSize: 'inherit', + fontFamily: 'inherit', + fontWeight: 'inherit' + }; + if (typeof props.columnDef.cellStyle === 'function') { + cellStyle = { + ...cellStyle, + ...props.columnDef.cellStyle(props.value, props.rowData) + }; + } else { + cellStyle = { ...cellStyle, ...props.columnDef.cellStyle }; + } + if (props.columnDef.disableClick) { + cellStyle.cursor = 'default'; + } + return { ...props.style, ...cellStyle }; +}