PrimaryKeyFieldRow.tsx 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import { FC } from 'react';
  2. import { FormControlLabel, Switch, Theme, Box } from '@mui/material';
  3. import { useTranslation } from 'react-i18next';
  4. import CustomToolTip from '@/components/customToolTip/CustomToolTip';
  5. import { DataTypeEnum } from '@/consts';
  6. import { FieldType } from '../../../databases/collections/Types';
  7. import NameField from '../NameField';
  8. import DescriptionField from '../DescriptionField';
  9. import MaxLengthField from '../MaxLengthField';
  10. import PrimaryKeyTypeSelector from '../PrimaryKeyTypeSelector';
  11. import { rowStyles } from './styles';
  12. interface PrimaryKeyFieldRowProps {
  13. field: FieldType;
  14. fields: FieldType[];
  15. onFieldChange: (
  16. id: string,
  17. changes: Partial<FieldType>,
  18. isValid?: boolean
  19. ) => void;
  20. }
  21. const PrimaryKeyFieldRow: FC<PrimaryKeyFieldRowProps> = ({
  22. field,
  23. fields,
  24. onFieldChange,
  25. }) => {
  26. const { t: collectionTrans } = useTranslation('collection');
  27. const isVarChar = field.data_type === DataTypeEnum.VarChar;
  28. const toggleStyles = (theme: Theme) => ({
  29. marginLeft: theme.spacing(0.5),
  30. marginRight: theme.spacing(0.5),
  31. });
  32. return (
  33. <Box sx={rowStyles}>
  34. <NameField
  35. field={field}
  36. fields={fields}
  37. onChange={(id, name, isValid) => onFieldChange(id, { name }, isValid)}
  38. label={collectionTrans('idFieldName')}
  39. />
  40. <PrimaryKeyTypeSelector
  41. value={field.data_type}
  42. onChange={(value: DataTypeEnum) => {
  43. const changes: Partial<FieldType> = { data_type: value };
  44. if (value === DataTypeEnum.VarChar) {
  45. changes.autoID = false;
  46. }
  47. onFieldChange(field.id!, changes);
  48. }}
  49. />
  50. {isVarChar && (
  51. <MaxLengthField
  52. field={field}
  53. onChange={(id, max_length, isValid) =>
  54. onFieldChange(id, { max_length }, isValid)
  55. }
  56. />
  57. )}
  58. <DescriptionField
  59. field={field}
  60. onChange={(id, description) => onFieldChange(id, { description }, true)}
  61. sx={{ width: '64px' }}
  62. />
  63. <FormControlLabel
  64. control={
  65. <Switch
  66. checked={!!field.autoID}
  67. size="small"
  68. disabled={isVarChar}
  69. onChange={() => {
  70. onFieldChange(field.id!, { autoID: !field.autoID });
  71. }}
  72. />
  73. }
  74. label={
  75. <CustomToolTip
  76. title={collectionTrans('autoIdToggleTip')}
  77. placement="top"
  78. >
  79. <>{collectionTrans('autoId')}</>
  80. </CustomToolTip>
  81. }
  82. sx={toggleStyles}
  83. />
  84. </Box>
  85. );
  86. };
  87. export default PrimaryKeyFieldRow;