Skip to content

Commit

Permalink
Added dynamic value field between
Browse files Browse the repository at this point in the history
- input
- textarea
  • Loading branch information
dharmesh-hemaram committed Aug 25, 2024
1 parent 9ed97c6 commit 42d0853
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ const ActionTable = ({ actions }: ActionProps) => {
accessorKey: 'value',
meta: {
list: 'value',
as: 'textarea',
},
},
{
Expand Down Expand Up @@ -131,6 +130,9 @@ const ActionTable = ({ actions }: ActionProps) => {
updateData: (selectedActionId: RANDOM_UUID, columnId: string, value: any) => {
dispatch(updateAction({ selectedActionId, name: columnId, value }));
},
updateValueFieldTypes: (selectedActionId: RANDOM_UUID, valueFieldType: 'input' | 'textarea') => {
dispatch(updateAction({ selectedActionId, name: 'valueFieldType', value: valueFieldType }));
},
},
});

Expand Down
27 changes: 24 additions & 3 deletions apps/acf-options-page/src/app/configs/action/editable-cell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { getFieldNameValue } from '@apps/acf-options-page/src/util/element';
import { Action } from '@dhruv-techapps/acf-common';
import { ColumnDef } from '@tanstack/react-table';
import { useEffect, useRef, useState } from 'react';
import { Form } from 'react-bootstrap';
import { Button, Form, InputGroup, OverlayTrigger, Tooltip } from 'react-bootstrap';

export const defaultColumn: Partial<ColumnDef<Action>> = {
cell: Cell,
Expand All @@ -14,6 +14,7 @@ function Cell({ getValue, row: { original }, column: { id, columnDef }, table })
const [value, setValue] = useState(initialValue);
const [isInvalid, setIsInvalid] = useState(false);
const inputRef = useRef<HTMLInputElement>(null);
const [valueFieldType, setValueFieldType] = useState<'input' | 'textarea'>(original.valueFieldType || 'input');

const onBlur = (e) => {
const update = getFieldNameValue(e, { [id]: initialValue });
Expand All @@ -33,14 +34,14 @@ function Cell({ getValue, row: { original }, column: { id, columnDef }, table })
setValue(initialValue);
}, [initialValue, id, original.error]);

return (
const getInput = (as: any) => (
<Form.Control
ref={inputRef}
aria-label={meta?.ariaLabel}
type={meta?.type}
value={value || ''}
name={id}
as={meta?.as}
as={as}
rows={1}
onChange={onChange}
onBlur={onBlur}
Expand All @@ -51,4 +52,24 @@ function Cell({ getValue, row: { original }, column: { id, columnDef }, table })
autoComplete='off'
/>
);

const onValueFieldTypeChange = () => {
const newFieldType = valueFieldType === 'input' ? 'textarea' : 'input';
setValueFieldType(newFieldType);
table.options.meta?.updateValueFieldTypes(original.id, newFieldType);
};

if (id === 'value') {
return (
<InputGroup>
<OverlayTrigger overlay={<Tooltip id={id}>{valueFieldType === 'input' ? `Switch to Textarea` : 'Switch to Input'}</Tooltip>}>
<Button type='button' variant='outline-secondary' id='action-field-type' onClick={onValueFieldTypeChange}>
{valueFieldType === 'input' ? 'I' : 'T'}
</Button>
</OverlayTrigger>
{getInput(valueFieldType)}
</InputGroup>
);
}
return <>{getInput(meta?.as)}</>;
}
3 changes: 2 additions & 1 deletion libs/acf/common/src/lib/model/action-model.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Addon } from './addon-model';
import { RANDOM_UUID } from '../common';
import { Addon } from './addon-model';
import { RETRY_OPTIONS } from './setting-model';

// Action Condition
Expand Down Expand Up @@ -73,6 +73,7 @@ export type Action = {
settings?: ActionSettings;
status?: string;
error?: string[];
valueFieldType?: 'text' | 'textarea';
};

export const getDefaultAction = (): Action => ({
Expand Down

0 comments on commit 42d0853

Please sign in to comment.