import { useEffect, useMemo, useState } from 'react'; import { FieldArray, useFieldArray, useFormContext } from 'react-hook-form'; import { Table, Button, Typography, Space, Popover, Tag, Flex, Select, Form } from 'antd'; import { CloseCircleFilled, DownOutlined } from '@ant-design/icons'; import { ColumnsType } from 'antd/es/table'; import type { DragEndEvent } from '@dnd-kit/core'; import { DndContext } from '@dnd-kit/core'; import { restrictToVerticalAxis } from '@dnd-kit/modifiers'; import { SortableContext, verticalListSortingStrategy } from '@dnd-kit/sortable'; import { useLineActionStore } from '@l-clutch/core'; import { FormValues } from '.'; import { useSelectAction } from './useSelectAction'; import { ActionListRow } from './ActionListRow'; import { ActionListFooter } from './ActionListFooter'; import { AreaTemplate, AREA_TEMPLATES, AreaTemplateSelectModal } from './AreaTemplate'; export const ActionList = () => { const { control, watch, formState: { isLoading }, getFieldState, setValue, } = useFormContext(); const { id: selectedId, setIdList, setId: setSelectedId } = useSelectAction(); const areas = watch(`areas`); const background = watch(`background`); const errors = getFieldState('areas')?.error; const { fields, append, remove, move, insert } = useFieldArray({ control, name: 'areas', }); useEffect(() => { setIdList(fields.map((field) => field.id)); }, [fields, setIdList]); const lineActionTypes = useLineActionStore((state) => state.types); const onDragEnd = ({ active, over }: DragEndEvent) => { if (active.id === over?.id) return; const activeIndex = fields.findIndex((i) => i.id === active.id); const overIndex = fields.findIndex((i) => i.id === over?.id); move(activeIndex, overIndex); }; const [isTemplateModalOpen, setTemplateModalOpen] = useState(false); const [selectedTemplateValue, setSelectedTemplateValue] = useState(1); const selectedTemplate = useMemo( () => AREA_TEMPLATES.find(({ value }) => value === selectedTemplateValue)?.template, [selectedTemplateValue], ); const columns = useMemo>>( () => [ { key: 'sort', }, { title: 'ラベル', dataIndex: 'action', key: 'label', render: ({ label }) => (label?.length > 0 ? label : 'ラベルなし'), }, { title: 'タイプ', dataIndex: 'action', key: 'type', render: ({ type }) => (type ? lineActionTypes[type].label : 'アクションなし'), }, { title: 'アクション', dataIndex: 'action', key: 'action', render: (_action, area, index) => ( { if (!background) return; const bounds = { x: Math.floor(ratioBounds.x * background.width), y: Math.floor(ratioBounds.y * background.height), width: Math.floor(ratioBounds.width * background.width), height: Math.floor(ratioBounds.height * background.height), }; setValue(`areas.${index}.bounds`, bounds, { shouldDirty: true }); }} /> ) } > ), }, { title: errors ? 'エラー' : undefined, key: 'error', render: (_action, _area, index) => errors?.[index] && (

下記のエラーがあります。

    {errors[index].bounds !== undefined && (
  • アクション領域
  • )} {errors[index].action !== undefined && (
  • アクション設定
  • )}
} >
), }, ], [remove, setSelectedId, errors, background, selectedTemplate], // データが更新されないため、areasを渡す ); return ( <> アクションリスト