From f8ee455985de9c47989c403f02607a9d2022c5be Mon Sep 17 00:00:00 2001 From: "a.bouhuolia" Date: Wed, 27 Jan 2021 15:02:12 +0200 Subject: [PATCH] feat: make journal auto-adjustment for entries. feat: auto-focus cells inside the table. --- client/package.json | 2 +- .../DataTableCells/AccountsListFieldCell.js | 27 ++++++++---- .../DataTableCells/ItemsListCell.js | 17 +++++++- .../Accounting/MakeJournalEntriesTable.js | 15 ++++--- .../MakeJournalFormFloatingActions.js | 3 -- client/src/containers/Accounting/utils.js | 43 ++++++++++++++++++- .../containers/Entries/ItemsEntriesTable.js | 1 + client/src/hooks/index.js | 34 ++++++++++++++- client/src/hooks/useAutofocus.js | 6 +-- 9 files changed, 121 insertions(+), 27 deletions(-) diff --git a/client/package.json b/client/package.json index a73054c21..87266a85b 100644 --- a/client/package.json +++ b/client/package.json @@ -4,7 +4,7 @@ "private": true, "dependencies": { "@babel/core": "7.8.4", - "@blueprintjs/core": "^3.23.1", + "@blueprintjs/core": "^3.38.1", "@blueprintjs/datetime": "^3.15.2", "@blueprintjs/select": "^3.11.2", "@blueprintjs/table": "^3.8.3", diff --git a/client/src/components/DataTableCells/AccountsListFieldCell.js b/client/src/components/DataTableCells/AccountsListFieldCell.js index c5c2a6dcc..58fac50c1 100644 --- a/client/src/components/DataTableCells/AccountsListFieldCell.js +++ b/client/src/components/DataTableCells/AccountsListFieldCell.js @@ -1,11 +1,14 @@ -import React, { useCallback, useMemo } from 'react'; -import AccountsSuggestField from 'components/AccountsSuggestField'; -// import AccountsSelectList from 'components/AccountsSelectList'; +import React, { useRef, useCallback, useMemo } from 'react'; import classNames from 'classnames'; +import { useCellAutoFocus } from 'hooks'; + +import AccountsSuggestField from 'components/AccountsSuggestField'; + +// import AccountsSelectList from 'components/AccountsSelectList'; import { FormGroup, Classes, Intent } from '@blueprintjs/core'; // Account cell renderer. -const AccountCellRenderer = ({ +export default function AccountCellRenderer({ column: { id, accountsDataProp, @@ -18,9 +21,14 @@ const AccountCellRenderer = ({ accounts: defaultAccounts, updateData, errors, + autoFocus, ...restPayloadProps }, -}) => { +}) { + const accountRef = useRef(); + + useCellAutoFocus(accountRef, autoFocus, id, index); + const handleAccountSelected = useCallback( (account) => { updateData(index, id, account.id); @@ -49,9 +57,12 @@ const AccountCellRenderer = ({ selectedAccountId={initialValue} filterByRootTypes={filterAccountsByRootType} filterByTypes={filterAccountsByTypes} + inputProps={{ + inputRef: (ref) => (accountRef.current = ref), + }} + openOnKeyDown={true} + blurOnSelectClose={false} /> ); -}; - -export default AccountCellRenderer; +} diff --git a/client/src/components/DataTableCells/ItemsListCell.js b/client/src/components/DataTableCells/ItemsListCell.js index ba77db9c3..77e3970fc 100644 --- a/client/src/components/DataTableCells/ItemsListCell.js +++ b/client/src/components/DataTableCells/ItemsListCell.js @@ -1,15 +1,23 @@ -import React, { useCallback } from 'react'; +import React, { useCallback, useRef } from 'react'; // import ItemsListField from 'components/ItemsListField'; import ItemsSuggestField from 'components/ItemsSuggestField'; import classNames from 'classnames'; + import { FormGroup, Classes, Intent } from '@blueprintjs/core'; +import { useCellAutoFocus } from 'hooks'; + export default function ItemsListCell({ column: { id, filterSellable, filterPurchasable }, row: { index }, cell: { value: initialValue }, - payload: { items, updateData, errors }, + payload: { items, updateData, errors, autoFocus }, }) { + const fieldRef = useRef(); + + // Auto-focus the items list input field. + useCellAutoFocus(fieldRef, autoFocus, id, index); + const handleItemSelected = useCallback( (item) => { updateData(index, id, item.id); @@ -30,6 +38,11 @@ export default function ItemsListCell({ selectedItemId={initialValue} sellable={filterSellable} purchasable={filterPurchasable} + inputProps={{ + inputRef: (ref) => (fieldRef.current = ref), + }} + openOnKeyDown={true} + blurOnSelectClose={false} /> ); diff --git a/client/src/containers/Accounting/MakeJournalEntriesTable.js b/client/src/containers/Accounting/MakeJournalEntriesTable.js index f522a8f2a..d9ecba07c 100644 --- a/client/src/containers/Accounting/MakeJournalEntriesTable.js +++ b/client/src/containers/Accounting/MakeJournalEntriesTable.js @@ -2,11 +2,7 @@ import React, { useState, useMemo, useEffect, useCallback } from 'react'; import { Button } from '@blueprintjs/core'; import { FormattedMessage as T, useIntl } from 'react-intl'; import { omit } from 'lodash'; -import classNames from 'classnames'; - -import { CLASSES } from 'common/classes'; -import DataTable from 'components/DataTable'; -import { compose, transformUpdatedRows, saveInvoke } from 'utils'; +import { compose, saveInvoke } from 'utils'; import { AccountsListFieldCell, MoneyFieldCell, @@ -21,9 +17,12 @@ import { NoteCellRenderer, } from './components'; import { DataTableEditable } from 'components'; + import withAccounts from 'containers/Accounts/withAccounts'; import withCustomers from 'containers/Customers/withCustomers'; +import { updateDataReducer } from './utils'; + /** * Make journal entries table component. */ @@ -126,8 +125,9 @@ function MakeJournalEntriesTable({ }; // Handles update datatable data. - const handleUpdateData = (rowIndex, columnIdOrObj, value) => { - const newRows = transformUpdatedRows(rows, rowIndex, columnIdOrObj, value); + const handleUpdateData = (rowIndex, columnId, value) => { + const newRows = updateDataReducer(rows, rowIndex, columnId, value); + saveInvoke( onChange, newRows @@ -185,6 +185,7 @@ function MakeJournalEntriesTable({ contact_type: 'customer', })), ], + autoFocus: ['account_id', 0], }} actions={ <> diff --git a/client/src/containers/Accounting/MakeJournalFormFloatingActions.js b/client/src/containers/Accounting/MakeJournalFormFloatingActions.js index 2be8dfa2a..4df73ae9e 100644 --- a/client/src/containers/Accounting/MakeJournalFormFloatingActions.js +++ b/client/src/containers/Accounting/MakeJournalFormFloatingActions.js @@ -91,7 +91,6 @@ export default function MakeJournalFloatingAction({