Skip to content

Commit

Permalink
refactor: TableEditableCell to be a separate component
Browse files Browse the repository at this point in the history
  • Loading branch information
chaitanyadeorukhkar committed Jul 8, 2024
1 parent 06a2ecd commit c709efe
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 144 deletions.
15 changes: 7 additions & 8 deletions packages/blade/src/components/Table/TableBody.native.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
/* eslint-disable react/no-unused-prop-types */
/* eslint-disable @typescript-eslint/no-unused-vars */
import React from 'react';
import type {
TableBodyProps,
TableCellProps,
TableEditableCellProps,
TableRowProps,
} from './types';
import type { TableBodyProps, TableCellProps, TableRowProps } from './types';
import { Text } from '~components/Typography';

const TableBody = (props: TableBodyProps): React.ReactElement => {
Expand All @@ -21,8 +16,12 @@ const TableCell = (props: TableCellProps): React.ReactElement => {
return <Text>Table Component is not available for Native mobile apps.</Text>;
};

const TableEditableCell = (props: TableEditableCellProps): React.ReactElement => {
const StyledCell = (props: TableCellProps): React.ReactElement => {
return <Text>Table Component is not available for Native mobile apps.</Text>;
};

export { TableBody, TableRow, TableCell, TableEditableCell };
const CellWrapper = (props: TableCellProps): React.ReactElement => {
return <Text>Table Component is not available for Native mobile apps.</Text>;
};

export { TableBody, TableRow, TableCell, StyledCell, CellWrapper };
141 changes: 5 additions & 136 deletions packages/blade/src/components/Table/TableBody.web.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,25 @@ import React, { useEffect } from 'react';
import { Body, Row, Cell } from '@table-library/react-table-library/table';
import styled from 'styled-components';
import { useTableContext } from './TableContext';
import { checkboxCellWidth, tableEditableCellRowDensityToInputSizeMap, tableRow } from './tokens';
import { checkboxCellWidth, tableRow } from './tokens';
import { ComponentIds } from './componentIds';
import type {
TableProps,
TableBodyProps,
TableRowProps,
TableCellProps,
TableBackgroundColors,
TableEditableCellProps,
} from './types';
import getIn from '~utils/lodashButBetter/get';
import { Text } from '~components/Typography';
import type { CheckboxProps } from '~components/Checkbox';
import { Checkbox } from '~components/Checkbox';
import { castWebType, makeMotionTime, makeSize, makeSpace } from '~utils';
import { makeMotionTime, makeSize, makeSpace } from '~utils';
import BaseBox from '~components/Box/BaseBox';
import { MetaConstants, metaAttribute } from '~utils/metaAttribute';
import { assignWithoutSideEffects } from '~utils/assignWithoutSideEffects';
import { getFocusRingStyles } from '~utils/getFocusRingStyles';
import { size } from '~tokens/global';
import { BaseInput } from '~components/Input/BaseInput';
import { Box } from '~components/Box';
import { AlertCircleIcon, CheckIcon } from '~components/Icons';
import type { MarginProps } from '~components/Box/BaseBox/types/spacingTypes';

const StyledBody = styled(Body)<{
$isSelectable: boolean;
Expand Down Expand Up @@ -152,7 +147,7 @@ const TableBody = assignWithoutSideEffects(_TableBody, {
componentId: ComponentIds.TableBody,
});

const StyledCell = styled(Cell)<{
export const StyledCell = styled(Cell)<{
$backgroundColor: TableBackgroundColors;
}>(({ theme, $backgroundColor }) => ({
'&&&': {
Expand All @@ -165,7 +160,7 @@ const StyledCell = styled(Cell)<{
},
}));

const CellWrapper = styled(BaseBox)<{
export const CellWrapper = styled(BaseBox)<{
rowDensity: NonNullable<TableProps<unknown>['rowDensity']>;
showStripedRows?: boolean;
hasPadding?: boolean;
Expand Down Expand Up @@ -226,132 +221,6 @@ const TableCell = assignWithoutSideEffects(_TableCell, {
componentId: ComponentIds.TableCell,
});

const StyledEditableCell = styled(StyledCell)<{
rowDensity: NonNullable<TableProps<unknown>['rowDensity']>;
}>(({ theme, rowDensity }) => ({
'&&&': {
'&:focus-visible': { outline: '1px solid' },
'&:focus-within': {
...(rowDensity !== 'comfortable' ? getFocusRingStyles({ theme, negativeOffset: true }) : {}),
},
},
}));

const validationStateToInputTrailingIconMap = {
none: undefined,
success: CheckIcon,
error: AlertCircleIcon,
};

const rowDensityToIsTableInputCellMapping = {
comfortable: false,
normal: true,
compact: true,
};

const getEditableInputMargin = ({
rowDensity,
}: {
rowDensity: NonNullable<TableProps<unknown>['rowDensity']>;
}): MarginProps['margin'] => {
if (rowDensity === 'comfortable') {
return ['spacing.4', 'spacing.4'];
}

return 'spacing.2';
};

const _TableEditableCell = ({
validationState = 'none',
accessibilityLabel,
autoCapitalize,
autoCompleteSuggestionType,
autoFocus,
defaultValue,
isDisabled,
isRequired,
keyboardReturnKeyType,
leadingIcon,
maxCharacters,
name,
onBlur,
onChange,
onClick,
onFocus,
onSubmit,
placeholder,
prefix,
suffix,
value,
testID,
trailingButton,
errorText,
successText,
}: TableEditableCellProps): React.ReactElement => {
const { rowDensity, showStripedRows, backgroundColor } = useTableContext();

return (
<StyledEditableCell
role="cell"
$backgroundColor={backgroundColor}
rowDensity={rowDensity}
{...metaAttribute({ name: MetaConstants.TableCell })}
>
<BaseBox className="cell-wrapper-base" display="flex" alignItems="center" height="100%">
<CellWrapper
className="cell-wrapper"
rowDensity={rowDensity}
showStripedRows={showStripedRows}
display="flex"
alignItems="center"
flex={1}
hasPadding={false}
>
<Box margin={getEditableInputMargin({ rowDensity })} width="100%">
<BaseInput
isTableInputCell={rowDensityToIsTableInputCellMapping[rowDensity]}
validationState={validationState}
id="table-editable-cell-input"
size={tableEditableCellRowDensityToInputSizeMap[rowDensity]}
type="text"
trailingIcon={validationStateToInputTrailingIconMap[validationState]}
accessibilityLabel={accessibilityLabel}
autoCapitalize={autoCapitalize}
autoCompleteSuggestionType={autoCompleteSuggestionType}
// eslint-disable-next-line jsx-a11y/no-autofocus
autoFocus={autoFocus}
defaultValue={defaultValue}
isDisabled={isDisabled}
isRequired={isRequired}
keyboardReturnKeyType={keyboardReturnKeyType}
leadingIcon={leadingIcon}
maxCharacters={maxCharacters}
name={name}
onBlur={onBlur}
onChange={onChange}
onClick={onClick}
onFocus={onFocus}
onSubmit={castWebType(onSubmit)}
placeholder={placeholder}
prefix={prefix}
suffix={suffix}
value={value}
testID={testID}
trailingButton={trailingButton}
errorText={errorText}
successText={successText}
/>
</Box>
</CellWrapper>
</BaseBox>
</StyledEditableCell>
);
};

const TableEditableCell = assignWithoutSideEffects(_TableEditableCell, {
componentId: ComponentIds.TableEditableCell,
});

const TableCheckboxCell = ({
isChecked,
onChange,
Expand Down Expand Up @@ -474,4 +343,4 @@ const TableRow = assignWithoutSideEffects(_TableRow, {
componentId: ComponentIds.TableRow,
});

export { TableBody, TableRow, TableCell, TableEditableCell };
export { TableBody, TableRow, TableCell };
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import type { TableEditableCellProps } from './types';
import { Text } from '~components/Typography';

const TableEditableCell = (props: TableEditableCellProps): React.ReactElement => {
return <Text>Table Component is not available for Native mobile apps.</Text>;
};

export { TableEditableCell };
143 changes: 143 additions & 0 deletions packages/blade/src/components/Table/TableEditableCell.web.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
import styled from 'styled-components';
import { CellWrapper, StyledCell } from './TableBody';
import { useTableContext } from './TableContext';
import type { TableEditableCellProps, TableProps } from './types';
import { tableEditableCellRowDensityToInputSizeMap } from './tokens';
import { ComponentIds } from './componentIds';
import { getFocusRingStyles } from '~utils/getFocusRingStyles';
import { AlertCircleIcon, CheckIcon } from '~components/Icons';
import type { MarginProps } from '~components/Box/BaseBox/types/spacingTypes';
import { MetaConstants, metaAttribute } from '~utils/metaAttribute';
import BaseBox from '~components/Box/BaseBox';
import { Box } from '~components/Box';
import { BaseInput } from '~components/Input/BaseInput';
import { castWebType } from '~utils';
import { assignWithoutSideEffects } from '~utils/assignWithoutSideEffects';

export const StyledEditableCell = styled(StyledCell)<{
rowDensity: NonNullable<TableProps<unknown>['rowDensity']>;
}>(({ theme, rowDensity }) => ({
'&&&': {
'&:focus-visible': { outline: '1px solid' },
'&:focus-within': {
...(rowDensity !== 'comfortable' ? getFocusRingStyles({ theme, negativeOffset: true }) : {}),
},
},
}));

export const validationStateToInputTrailingIconMap = {
none: undefined,
success: CheckIcon,
error: AlertCircleIcon,
};

export const rowDensityToIsTableInputCellMapping = {
comfortable: false,
normal: true,
compact: true,
};

export const getEditableInputMargin = ({
rowDensity,
}: {
rowDensity: NonNullable<TableProps<unknown>['rowDensity']>;
}): MarginProps['margin'] => {
if (rowDensity === 'comfortable') {
return ['spacing.4', 'spacing.4'];
}

return 'spacing.2';
};

const _TableEditableCell = ({
validationState = 'none',
accessibilityLabel,
autoCapitalize,
autoCompleteSuggestionType,
autoFocus,
defaultValue,
isDisabled,
isRequired,
keyboardReturnKeyType,
leadingIcon,
maxCharacters,
name,
onBlur,
onChange,
onClick,
onFocus,
onSubmit,
placeholder,
prefix,
suffix,
value,
testID,
trailingButton,
errorText,
successText,
}: TableEditableCellProps): React.ReactElement => {
const { rowDensity, showStripedRows, backgroundColor } = useTableContext();

return (
<StyledEditableCell
role="cell"
$backgroundColor={backgroundColor}
rowDensity={rowDensity}
{...metaAttribute({ name: MetaConstants.TableCell })}
>
<BaseBox className="cell-wrapper-base" display="flex" alignItems="center" height="100%">
<CellWrapper
className="cell-wrapper"
rowDensity={rowDensity}
showStripedRows={showStripedRows}
display="flex"
alignItems="center"
flex={1}
hasPadding={false}
>
<Box margin={getEditableInputMargin({ rowDensity })} width="100%">
<BaseInput
isTableInputCell={rowDensityToIsTableInputCellMapping[rowDensity]}
validationState={validationState}
id="table-editable-cell-input"
type="text"
size={tableEditableCellRowDensityToInputSizeMap[rowDensity]}
trailingIcon={validationStateToInputTrailingIconMap[validationState]}
accessibilityLabel={accessibilityLabel}
autoCapitalize={autoCapitalize}
autoCompleteSuggestionType={autoCompleteSuggestionType}
// eslint-disable-next-line jsx-a11y/no-autofocus
autoFocus={autoFocus}
defaultValue={defaultValue}
isDisabled={isDisabled}
isRequired={isRequired}
keyboardReturnKeyType={keyboardReturnKeyType}
leadingIcon={leadingIcon}
maxCharacters={maxCharacters}
name={name}
onBlur={onBlur}
onChange={onChange}
onClick={onClick}
onFocus={onFocus}
onSubmit={castWebType(onSubmit)}
placeholder={placeholder}
prefix={prefix}
suffix={suffix}
value={value}
testID={testID}
trailingButton={trailingButton}
errorText={errorText}
successText={successText}
/>
</Box>
</CellWrapper>
</BaseBox>
</StyledEditableCell>
);
};

const TableEditableCell = assignWithoutSideEffects(_TableEditableCell, {
componentId: ComponentIds.TableEditableCell,
});

export { TableEditableCell };
2 changes: 2 additions & 0 deletions packages/blade/src/components/Table/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ export * from './TableFooter';
export * from './TablePagination';
export * from './TableToolbar';
export * from './types';
export { TableBody, TableCell, TableRow } from './TableBody';

Check failure on line 8 in packages/blade/src/components/Table/index.ts

View workflow job for this annotation

GitHub Actions / Validate Source Code

Multiple exports of name 'TableBody'

Check failure on line 8 in packages/blade/src/components/Table/index.ts

View workflow job for this annotation

GitHub Actions / Validate Source Code

Multiple exports of name 'TableCell'

Check failure on line 8 in packages/blade/src/components/Table/index.ts

View workflow job for this annotation

GitHub Actions / Validate Source Code

Multiple exports of name 'TableRow'
export { TableEditableCell } from './TableEditableCell';

0 comments on commit c709efe

Please sign in to comment.