Skip to content

Commit

Permalink
fix: fix ArrayField focus
Browse files Browse the repository at this point in the history
  • Loading branch information
Hein Jeong authored and cwoolum committed Sep 22, 2022
1 parent 2d7d1d3 commit 5c93079
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,11 @@ function ArrayField({
const { tokens } = useTheme();
const [selectedBadgeIndex, setSelectedBadgeIndex] = React.useState();
const [isEditing, setIsEditing] = React.useState();
React.useEffect(() => {
if (isEditing) {
inputFieldRef?.current?.focus();
}
}, [isEditing]);
const removeItem = async (removeIndex) => {
const newItems = items.filter((value, index) => index !== removeIndex);
await onChange(newItems);
Expand Down Expand Up @@ -770,7 +775,6 @@ function ArrayField({
setSelectedBadgeIndex(index);
setFieldValue(items[index]);
setIsEditing(true);
inputFieldRef?.current?.focus();
}}
>
{value}
Expand Down Expand Up @@ -1027,6 +1031,11 @@ function ArrayField({
const { tokens } = useTheme();
const [selectedBadgeIndex, setSelectedBadgeIndex] = React.useState();
const [isEditing, setIsEditing] = React.useState();
React.useEffect(() => {
if (isEditing) {
inputFieldRef?.current?.focus();
}
}, [isEditing]);
const removeItem = async (removeIndex) => {
const newItems = items.filter((value, index) => index !== removeIndex);
await onChange(newItems);
Expand Down Expand Up @@ -1102,7 +1111,6 @@ function ArrayField({
setSelectedBadgeIndex(index);
setFieldValue(items[index]);
setIsEditing(true);
inputFieldRef?.current?.focus();
}}
>
{value}
Expand Down Expand Up @@ -2834,6 +2842,11 @@ function ArrayField({
const { tokens } = useTheme();
const [selectedBadgeIndex, setSelectedBadgeIndex] = React.useState();
const [isEditing, setIsEditing] = React.useState();
React.useEffect(() => {
if (isEditing) {
inputFieldRef?.current?.focus();
}
}, [isEditing]);
const removeItem = async (removeIndex) => {
const newItems = items.filter((value, index) => index !== removeIndex);
await onChange(newItems);
Expand Down Expand Up @@ -2909,7 +2922,6 @@ function ArrayField({
setSelectedBadgeIndex(index);
setFieldValue(items[index]);
setIsEditing(true);
inputFieldRef?.current?.focus();
}}
>
{value}
Expand Down Expand Up @@ -3487,6 +3499,11 @@ function ArrayField({
const { tokens } = useTheme();
const [selectedBadgeIndex, setSelectedBadgeIndex] = React.useState();
const [isEditing, setIsEditing] = React.useState();
React.useEffect(() => {
if (isEditing) {
inputFieldRef?.current?.focus();
}
}, [isEditing]);
const removeItem = async (removeIndex) => {
const newItems = items.filter((value, index) => index !== removeIndex);
await onChange(newItems);
Expand Down Expand Up @@ -3562,7 +3579,6 @@ function ArrayField({
setSelectedBadgeIndex(index);
setFieldValue(items[index]);
setIsEditing(true);
inputFieldRef?.current?.focus();
}}
>
{value}
Expand Down
48 changes: 33 additions & 15 deletions packages/codegen-ui-react/lib/utils/forms/array-field-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
getCurrentValueName,
getSetNameIdentifier,
} from '../../forms/form-state';
import { addUseEffectWrapper } from '../generate-react-hooks';

export const generateArrayFieldComponent = () => {
const iconPath = 'M10 10l5.09-5.09L10 10l5.09 5.09L10 10zm0 0L4.91 4.91 10 10l-5.09 5.09L10 10z';
Expand Down Expand Up @@ -100,6 +101,38 @@ export const generateArrayFieldComponent = () => {
NodeFlags.Const,
),
),

addUseEffectWrapper(
[
factory.createIfStatement(
factory.createIdentifier('isEditing'),
factory.createBlock(
[
factory.createExpressionStatement(
factory.createCallChain(
factory.createPropertyAccessChain(
factory.createPropertyAccessChain(
factory.createIdentifier('inputFieldRef'),
factory.createToken(SyntaxKind.QuestionDotToken),
factory.createIdentifier('current'),
),
factory.createToken(SyntaxKind.QuestionDotToken),
factory.createIdentifier('focus'),
),
undefined,
undefined,
[],
),
),
],
true,
),
undefined,
),
],
['isEditing'],
),

factory.createVariableStatement(
undefined,
factory.createVariableDeclarationList(
Expand Down Expand Up @@ -709,21 +742,6 @@ export const generateArrayFieldComponent = () => {
[factory.createTrue()],
),
),
factory.createExpressionStatement(
factory.createCallExpression(
factory.createPropertyAccessChain(
factory.createPropertyAccessChain(
factory.createIdentifier('inputFieldRef'),
factory.createToken(SyntaxKind.QuestionDotToken),
factory.createIdentifier('current'),
),
factory.createToken(SyntaxKind.QuestionDotToken),
factory.createIdentifier('focus'),
),
undefined,
[],
),
),
],
true,
),
Expand Down

0 comments on commit 5c93079

Please sign in to comment.