-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: TableEditableCell to be a separate component
- Loading branch information
1 parent
06a2ecd
commit c709efe
Showing
5 changed files
with
166 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
packages/blade/src/components/Table/TableEditableCell.native.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
143
packages/blade/src/components/Table/TableEditableCell.web.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters