/*! * Copyright (c) Microsoft. All rights reserved. * Licensed under the MIT license. See LICENSE file in the project. */ import type { RenameArgs } from '@datashaper/schema' import type { Step } from '@datashaper/workflow' import styled from '@essex/styled-components' import type { IDropdownOption } from '@fluentui/react' import { ActionButton, Icon, IconButton, Label, TextField, } from '@fluentui/react' import type ColumnTable from 'arquero/dist/types/table/column-table' import { memo, useMemo } from 'react' import { EMPTY_OBJECT } from '../../../../empty.js' import { useColumnNames, useOnDeleteStepColumn, useSimpleDropdownOptions, useStepInputTable, } from '../../../../hooks/index.js' import { TableColumnDropdown } from '../../../controls/index.js' import { narrowerDropdownStyles } from '../styles.js' import type { StepFormProps } from '../types.js' import { useDisabled, useHandleAddButtonClick, useHandleColumnChange, } from './RenameForm.hooks.js' /** * Provides inputs for a RenameStep. */ export const RenameForm: React.FC> = memo( function RenameForm({ step, workflow, input, table, onChange }) { const dataTable = useStepInputTable(step, workflow, input, table) const handleColumnChange = useHandleColumnChange(step, onChange) const handleColumnDelete = useOnDeleteStepColumn(step, onChange) const handleButtonClick = useHandleAddButtonClick(step, dataTable, onChange) const columnPairs = useColumnPairs( dataTable, step, handleColumnChange, handleColumnDelete, ) const disabled = useDisabled(step, dataTable) return ( {columnPairs} Add rename ) }, ) function useColumnPairs( table: ColumnTable | undefined, step: Step, onChange: (previous: string, oldName: string, newName: string) => void, onDelete: (name: string) => void, ) { return useMemo(() => { const { columns } = step.args return Object.entries(columns || EMPTY_OBJECT).map((column, index) => { const [oldname] = column return ( ) }) }, [table, step, onChange, onDelete]) } const ColumnPair: React.FC<{ table: ColumnTable | undefined column: [string, string] step: Step onChange: (previous: string, oldName: string, newName: string) => void onDelete: (name: string) => void }> = memo(function ColumnPair({ table, column, step, onChange, onDelete }) { const [oldname, newname] = column const columnFilter = (name: string) => { if (name === oldname) { return true } if (step.args.columns?.[name]) { return false } return true } const handleColumnChange = ( _e: React.FormEvent, opt?: IDropdownOption | undefined, ) => onChange(oldname, (opt?.key as string) || oldname, newname) const handleTextChange = ( _e: React.FormEvent, newValue?: string, ) => { onChange(oldname, oldname, newValue ?? '') } const handleDeleteClick = () => onDelete(oldname) const columns = useColumnNames(table, columnFilter) const options = useSimpleDropdownOptions(columns) return ( ) }) const Container = styled.div` display: flex; flex-direction: column; ` const ColumnPairs = styled.div` display: flex; flex-direction: column; gap: 5px; ` const ColumnPairContainer = styled.div` display: flex; justify-content: flex-start; align-items: center; ` const addIconProps = { iconName: 'Add' } const deleteIconProps = { iconName: 'Delete' }