Skip to content

Commit

Permalink
Restore RTL support
Browse files Browse the repository at this point in the history
  • Loading branch information
jassmith committed Jul 4, 2022
1 parent df7f4e1 commit 49f7b8c
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,52 @@ export const AddData: React.VFC = () => {
},
};

export const RightToLeft: React.VFC = () => {
const { cols, getCellContent, setCellValue, onColumnResize } = useMockDataGenerator(60, false);

const getCellContentMangled = React.useCallback<typeof getCellContent>(
item => {
const [col, _row] = item;
if (col !== 0) return getCellContent(item);
return {
kind: GridCellKind.Text,
allowOverlay: true,
data: "讗谞讬 讙讚注讜谉, 诪讜诪讞讛 诇讗驻诇讬拽爪讬讜转 讙诇讬讬讚.",
displayData: "讗谞讬 讙讚注讜谉, 诪讜诪讞讛 诇讗驻诇讬拽爪讬讜转 讙诇讬讬讚.",
allowWrapping: true,
};
},
[getCellContent]
);

return (
<BeautifulWrapper
title="Right "
description={
<>
<Description>The data editor automatically detects RTL in text cells and respects it.</Description>
</>
}>
<DataEditor
{...defaultProps}
getCellContent={getCellContentMangled}
columns={cols}
onColumnResize={onColumnResize}
getCellsForSelection={true}
rowMarkers={"both"}
onPaste={true}
onCellEdited={setCellValue}
rows={1000}
/>
</BeautifulWrapper>
);
};
(RightToLeft as any).parameters = {
options: {
showPanel: false,
},
};

export const ValidateData: React.VFC = () => {
const { cols, getCellContent, setCellValue, getCellsForSelection } = useMockDataGenerator(60, false);

Expand Down
16 changes: 15 additions & 1 deletion packages/core/src/data-grid/data-grid-lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -416,9 +416,19 @@ export function drawTextCell(

const bias = getMiddleCenterBias(ctx, theme);

const isRtl = direction(data) === "rtl";

if (contentAlign === undefined && isRtl) {
contentAlign = "right";
}

if (isRtl) {
ctx.direction = "rtl";
}

if (data.length > 0) {
let changed = false;
if (contentAlign === undefined && direction(data) === "rtl") {
if (contentAlign === "right") {
// Use right alignment as default for RTL text
ctx.textAlign = "right";
changed = true;
Expand Down Expand Up @@ -470,6 +480,10 @@ export function drawTextCell(
// Reset alignment to default
ctx.textAlign = "start";
}

if (isRtl) {
ctx.direction = "inherit";
}
}
}

Expand Down

0 comments on commit 49f7b8c

Please sign in to comment.