Skip to content

Commit

Permalink
Fjerne required prop
Browse files Browse the repository at this point in the history
  • Loading branch information
frodehansen2 committed Dec 12, 2024
1 parent a1f9b93 commit 9d54c6b
Show file tree
Hide file tree
Showing 17 changed files with 43 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { FastField, Field, FieldProps } from 'formik';
import { TestProps, TypedFormInputValidationProps, UseFastFieldProps } from '../../types';
import { getErrorPropForFormikInput } from '../../utils/typedFormErrorUtils';
import { TypedFormikFormContext } from '../typed-formik-form/TypedFormikForm';
import { inputPropsToRemove } from '../../utils/inputPropsToRemove';

export type FormikCheckboxGroupCheckboxProp = Omit<CheckboxProps, 'children' | 'name' | 'checked'> & {
label: React.ReactNode;
Expand Down Expand Up @@ -48,6 +49,7 @@ function FormikCheckboxGroup<FieldName, ErrorType>({
return (
<CheckboxGroup
{...restProps}
{...inputPropsToRemove}
value={getFieldValueArray(field.value)}
legend={legend}
className="focusableFieldset"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { FastField, Field, FieldProps } from 'formik';
import { FormError, TestProps, TypedFormInputValidationProps, UseFastFieldProps } from '../../types';
import { getErrorPropForFormikInput } from '../../utils/typedFormErrorUtils';
import { TypedFormikFormContext } from '../typed-formik-form/TypedFormikForm';
import { inputPropsToRemove } from '../../utils/inputPropsToRemove';

interface OwnProps<FieldName> extends Omit<CheckboxProps, 'name' | 'error' | 'children'> {
name: FieldName;
Expand Down Expand Up @@ -36,6 +37,7 @@ function FormikCheckbox<FieldName, ErrorType>({
<Checkbox
{...restProps}
{...field}
{...inputPropsToRemove}
error={hasError !== undefined}
checked={field.value === true}
autoComplete="off"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Field, FieldProps } from 'formik';
import { TestProps, TypedFormInputValidationProps } from '../../types';
import { getErrorPropForFormikInput } from '../../utils/typedFormErrorUtils';
import { TypedFormikFormContext } from '../typed-formik-form/TypedFormikForm';
import { inputPropsToRemove } from '../../utils/inputPropsToRemove';

interface OwnProps<FieldName> extends Omit<ConfirmationPanelProps, 'name' | 'onChange' | 'checked'> {
name: FieldName;
Expand All @@ -28,6 +29,7 @@ function FormikConfirmationCheckbox<FieldName, ErrorType>({
<ConfirmationPanel
{...restProps}
{...field}
{...inputPropsToRemove}
checked={field.value === true}
error={getErrorPropForFormikInput({ field, form, context, error })}
onChange={(evt) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react';
import { getCountries } from '../../utils/countryUtils';
import { TestProps } from '../../types';
import { Select, SelectProps } from '@navikt/ds-react';
import { inputPropsToRemove } from '../../utils/inputPropsToRemove';

interface Props extends TestProps, Omit<SelectProps, 'onChange' | 'children'> {
label: React.ReactNode;
Expand Down Expand Up @@ -56,7 +57,12 @@ class CountrySelect extends React.Component<Props> {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { onChange, name, showOnlyEuAndEftaCountries, locale, ...restProps } = this.props;
return (
<Select name={name} {...restProps} onChange={(e) => onChange(e.target.value)} autoComplete="off">
<Select
name={name}
{...restProps}
{...inputPropsToRemove}
onChange={(e) => onChange(e.target.value)}
autoComplete="off">
<option value="" />
{this.getCountryOptions(locale || 'nb')}
</Select>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { TestProps, TypedFormInputValidationProps } from '../../types';
import { getErrorPropForFormikInput } from '../../utils/typedFormErrorUtils';
import { TypedFormikFormContext } from '../typed-formik-form/TypedFormikForm';
import CountrySelect from './CountrySelect';
import { inputPropsToRemove } from '../../utils/inputPropsToRemove';

interface OwnProps<FieldName> extends Omit<SelectProps, 'name' | 'children'> {
name: FieldName;
Expand All @@ -31,6 +32,7 @@ function FormikCountrySelect<FieldName, ErrorType>({
<CountrySelect
{...restProps}
{...field}
{...inputPropsToRemove}
data-testid={testKey}
error={getErrorPropForFormikInput({ field, form, context, error })}
onChange={(value) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { getErrorPropForFormikInput } from '../../utils/typedFormErrorUtils';
import { TypedFormikFormContext } from '../typed-formik-form/TypedFormikForm';
import DateInputAndPicker from './date-input-and-picker/DateInputAndPicker';
import { ISODateString } from './dateFormatUtils';
import { inputPropsToRemove } from '../../utils/inputPropsToRemove';

export type DatepickerLimitations = {
minDate?: Date;
Expand Down Expand Up @@ -72,6 +73,7 @@ function FormikDatepicker<FieldName, ErrorType>({
value={field.value}
error={getErrorPropForFormikInput({ field, form, context, error })}
{...restProps}
{...inputPropsToRemove}
/>
);
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { FastField, Field, FieldProps } from 'formik';
import { TypedFormInputValidationProps, UseFastFieldProps } from '../../types';
import { getErrorPropForFormikInput } from '../../utils/typedFormErrorUtils';
import { TypedFormikFormContext } from '../typed-formik-form/TypedFormikForm';
import { inputPropsToRemove } from '../../utils/inputPropsToRemove';

interface OwnProps<FieldName> extends Omit<FieldsetProps, 'name'> {
name: FieldName;
Expand All @@ -29,6 +30,7 @@ function FormikInputGroup<ErrorType, FieldName>({
return (
<Fieldset
{...restProps}
{...inputPropsToRemove}
error={getErrorPropForFormikInput({ field, form, context, error })}
id={id}
tabIndex={id ? -1 : undefined}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import FormikTextField from '../formik-text-field/FormikTextField';
import { TextFieldWidths } from '../formik-text-field/FormikTextFieldUtils';
import { TypedFormikFormContext } from '../typed-formik-form/TypedFormikForm';
import { useIntl } from 'react-intl';
import { inputPropsToRemove } from '../../utils/inputPropsToRemove';

interface OwnProps<FieldName> extends Omit<TextFieldProps, 'name' | 'children' | 'width'> {
name: FieldName;
Expand Down Expand Up @@ -41,6 +42,7 @@ function FormikNumberInput<FieldName, ErrorType>({
<FormikTextField
{...restProps}
{...field}
{...inputPropsToRemove}
type="text"
width={width}
autoComplete={autoComplete || 'off'}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { FastField, Field, FieldProps } from 'formik';
import { TestProps, TypedFormInputValidationProps, UseFastFieldProps } from '../../types';
import { getErrorPropForFormikInput } from '../../utils/typedFormErrorUtils';
import { TypedFormikFormContext, TypedFormikFormContextType } from '../typed-formik-form/TypedFormikForm';
import { inputPropsToRemove } from '../../utils/inputPropsToRemove';

export type FormikRadioProp = Omit<RadioProps, 'children' | 'name'> & {
label: React.ReactNode;
Expand Down Expand Up @@ -69,6 +70,7 @@ function FormikRadioGroup<FieldName, ErrorType>({
return (
<RadioGroup
{...restProps}
{...inputPropsToRemove}
description={
restProps.description ? <BodyShort as="div">{restProps.description}</BodyShort> : undefined
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { TestProps, TypedFormInputValidationProps, UseFastFieldProps } from '../
import { getErrorPropForFormikInput } from '../../utils/typedFormErrorUtils';
import { TypedFormikFormContext } from '../typed-formik-form/TypedFormikForm';
import { Select, SelectProps } from '@navikt/ds-react';
import { inputPropsToRemove } from '../../utils/inputPropsToRemove';

interface OwnProps<FieldName> extends Omit<SelectProps, 'name'> {
name: FieldName;
Expand Down Expand Up @@ -31,6 +32,7 @@ function FormikSelect<FieldName, ErrorType>({
<Select
{...restProps}
{...field}
{...inputPropsToRemove}
error={getErrorPropForFormikInput({ field, form, context, error })}
autoComplete="off"
value={field.value === undefined ? '' : field.value}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { TextField, TextFieldProps } from '@navikt/ds-react';
import React, { useState } from 'react';
import { FastField, Field, FieldProps } from 'formik';
import { TestProps, TypedFormInputValidationProps, UseFastFieldProps } from '../../types';
import { inputPropsToRemove } from '../../utils/inputPropsToRemove';
import { getErrorPropForFormikInput } from '../../utils/typedFormErrorUtils';
import { TypedFormikFormContext } from '../typed-formik-form/TypedFormikForm';
import { getTextFieldWidthClassName, TextFieldWidths } from './FormikTextFieldUtils';
Expand Down Expand Up @@ -46,6 +47,7 @@ function FormikTextField<FieldName, ErrorType>({
<TextField
{...restProps}
{...field}
{...inputPropsToRemove}
label={<div>{label}</div>}
autoComplete={autoComplete}
className={getTextFieldWidthClassName(width, className)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { FastField, Field, FieldProps } from 'formik';
import { TestProps, TypedFormInputValidationProps, UseFastFieldProps } from '../../types';
import { getErrorPropForFormikInput } from '../../utils/typedFormErrorUtils';
import { TypedFormikFormContext } from '../typed-formik-form/TypedFormikForm';
import { inputPropsToRemove } from '../../utils/inputPropsToRemove';

interface OwnProps<FieldName> extends Omit<TextareaProps, 'name' | 'defaultValue'> {
name: FieldName;
Expand All @@ -30,6 +31,7 @@ function FormikTextarea<FieldName, ErrorType>({
<Textarea
{...restProps}
{...field}
{...inputPropsToRemove}
error={getErrorPropForFormikInput({ field, form, context, error })}
onChange={(evt) => {
form.setFieldValue(field.name, evt.target.value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { getErrorPropForFormikInput } from '../../utils/typedFormErrorUtils';
import SkjemagruppeQuestion from '../helpers/skjemagruppe-question/SkjemagruppeQuestion';
import { TypedFormikFormContext } from '../typed-formik-form/TypedFormikForm';
import TimeInput, { TimeInputLabels, TimeInputLayoutProps, TimeInputRefProps } from './TimeInput';
import { inputPropsToRemove } from '../../utils/inputPropsToRemove';

interface OwnProps<FieldName> extends Omit<TextFieldProps, 'name' | 'onChange'> {
name: FieldName;
Expand Down Expand Up @@ -82,6 +83,7 @@ function FormikTimeInput<FieldName, ErrorType>({
{...restProps}
{...field}
{...timeInputLayout}
{...inputPropsToRemove}
justifyContent="left"
time={field.value || undefined}
onChange={(time: Partial<InputTime> | undefined) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { TestProps, TypedFormInputValidationProps, YesOrNo } from '../../types';
import { inputPropsToRemove } from '../../utils/inputPropsToRemove';
import FormikRadioGroup, { FormikRadioGroupProps } from '../formik-radio-group/FormikRadioGroup';

export interface FormikYesOrNoQuestionProps<FieldName, ErrorType>
Expand All @@ -24,6 +25,7 @@ function FormikYesOrNoQuestion<FieldName, ErrorType>({
<FormikRadioGroup<FieldName, ErrorType>
data-testid={testKey}
{...restProps}
{...inputPropsToRemove}
renderHorizontal={renderHorizontal}
radios={[
{ label: yesLabel, value: YesOrNo.YES, ['data-testid']: testKey ? `${testKey}_yes` : undefined },
Expand Down
8 changes: 8 additions & 0 deletions packages/sif-common-formik-ds/src/utils/inputPropsToRemove.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/** Føringer fra Aksel om å ikke bruke noen props på inputs
*
* https://aksel.nav.no/god-praksis/artikler/obligatoriske-og-valgfrie-skjemafelter#h3bfe00453471
*
*/
export const inputPropsToRemove = {
// required: undefined,
};
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const defaultArgs: FormikRadioGroupProps<any, any> = {
description: 'Choose any animal except the catty one',
name: 'animals',
radios: mockAnimalOptions,
required: true,
};

Default.args = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Default.args = {
label: 'FormikTextField',
name: 'FormikTextField',
description: 'Some description i appropriate',
required: true,
};
Default.parameters = {
formik: {
Expand Down

0 comments on commit 9d54c6b

Please sign in to comment.