Skip to content

Commit

Permalink
feat: add additional button for multilang array of strings and onblur…
Browse files Browse the repository at this point in the history
… support
  • Loading branch information
jeffreiffers committed Jan 9, 2025
1 parent 73b8b81 commit 4ccb796
Showing 1 changed file with 4 additions and 26 deletions.
30 changes: 4 additions & 26 deletions libs/ui/src/lib/formik-language-fieldset/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

import { ReactNode, useState } from 'react';
import { localization } from '@catalog-frontend/utils';
import { Fieldset, Box, Paragraph, Textfield, ErrorMessage, Chip, Chip } from '@digdir/designsystemet-react';
import { Fieldset, Box, Paragraph, Textfield, ErrorMessage, Chip } from '@digdir/designsystemet-react';
import { FastField, useFormikContext } from 'formik';

import styles from './formik-language-fieldset.module.scss';
import { ISOLanguage, LocalizedStrings } from '@catalog-frontend/types';
import { TextareaWithPrefix } from '../textarea-with-prefix';
import _ from 'lodash';
import { AddButton, DeleteButton } from '../button';
import _ from 'lodash';

Expand All @@ -20,7 +19,6 @@ type LanuguageFieldsetProps = {
requiredLanguages?: Omit<ISOLanguage, 'no'>[];
as?: typeof Textfield | typeof TextareaWithPrefix;
multiple?: boolean;
multiple?: boolean;
};

const allowedLanguages = Object.freeze<ISOLanguage[]>(['nb', 'nn', 'en']);
Expand All @@ -33,10 +31,8 @@ export const FormikLanguageFieldset = ({
requiredLanguages,
as: renderAs = Textfield,
multiple = false,
multiple = false,
}: LanuguageFieldsetProps) => {
const { errors, values, values, getFieldMeta, setFieldValue } = useFormikContext<Record<string, LocalizedStrings>>();
const [textValue, setTextValue] = useState<Record<string, string>>({});
const { errors, values, getFieldMeta, setFieldValue } = useFormikContext<Record<string, LocalizedStrings>>();
const [textValue, setTextValue] = useState<Record<string, string>>({});

const handleAddLanguage = (lang: string) => {
Expand All @@ -51,24 +47,6 @@ export const FormikLanguageFieldset = ({
setTextValue((prev) => ({ ...prev, ...{ [lang]: value } }));
};

const handleAddTextValue = (code: string, lang: string) => {
if(code === 'Enter') {
const textValues = [...(values?.[name]?.[lang] as string[]), textValue[lang]];
setFieldValue(`${name}.${lang}`, textValues);
setTextValue((prev) => ({ ...prev, ...{ [lang]: '' } }));
}
};

const handleRemoveTextValue = (index: number, lang: string) => {
const textValues = [...(values?.[name]?.[lang] as string[])];
textValues.splice(index, 1);
setFieldValue(`${name}.${lang}`, textValues);
};

const handleOnChangeTextValue = (value: string, lang: string) => {
setTextValue((prev) => ({ ...prev, ...{ [lang]: value } }));
};

const handleAddTextValue = (lang: string) => {
if (Boolean(textValue[lang]) === true) {
const textValues = [...(values?.[name]?.[lang] as string[]), textValue[lang]];
Expand Down Expand Up @@ -123,8 +101,8 @@ export const FormikLanguageFieldset = ({
/>

<DeleteButton
variant='tertiary'
onClick={() => handleRemoveLanguage(lang)}
onKeyDown={(e) => { if(e.code === 'Enter') { handleAddTextValue(lang) } }}
onBlur={() => handleAddTextValue(lang)}
/>
</Box>
<Chip.Group size='sm'>
Expand Down

0 comments on commit 4ccb796

Please sign in to comment.