Skip to content

Commit

Permalink
fix(Forms): update emptyValue documentation and type to reflect how…
Browse files Browse the repository at this point in the history
… it's used internally (#3729)
  • Loading branch information
joakbjerk authored Jul 2, 2024
1 parent 26b45b8 commit ff755e7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ interface ErrorMessages extends CustomErrorMessages {
}

export type Props = FieldHelpProps &
FieldProps<number, undefined, ErrorMessages> & {
FieldProps<number, undefined | number, ErrorMessages> & {
inputClassName?: string
currency?: InputMaskedProps['as_currency']
currencyDisplay?: 'code' | 'symbol' | 'narrowSymbol' | 'name'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ describe('Field.Number', () => {
myFieldWithNull: null,
myFieldWithUndefined: undefined,
myFieldWithEmptyString: '',
myFieldWithZero: 0,
myFieldWitInvalidType: 'foo',
}

Expand All @@ -341,29 +342,15 @@ describe('Field.Number', () => {
expect(statuses).toHaveLength(0)
})

it('allow empty string as emptyValue', () => {
it('allow number as emptyValue', () => {
render(
<Form.Handler schema={schema} data={data}>
<Field.Number path="/myFieldWithEmptyString" />
<Field.Number path="/myFieldWithZero" />
</Form.Handler>
)

const input = document.querySelector('input')
expect(input).toHaveValue('')

const statuses = document.querySelectorAll('.dnb-form-status')
expect(statuses).toHaveLength(0)
})

it('allow empty string as empty value', () => {
render(
<Form.Handler schema={schema} data={data}>
<Field.Number path="/myFieldWithEmptyString" />
</Form.Handler>
)

const input = document.querySelector('input')
expect(input).toHaveValue('')
expect(input).toHaveValue('0')

const statuses = document.querySelectorAll('.dnb-form-status')
expect(statuses).toHaveLength(0)
Expand Down Expand Up @@ -403,6 +390,29 @@ describe('Field.Number', () => {
log.mockRestore()
})
})

it('should use emptyValue when not set in data context', () => {
const onSubmit = jest.fn()
render(
<Form.Handler data={{}} onSubmit={onSubmit}>
<Field.Number
label="Label"
value={0}
path="/myValue"
emptyValue={0}
/>
</Form.Handler>
)

const form = document.querySelector('form')
fireEvent.submit(form)

expect(onSubmit).toHaveBeenCalledTimes(1)
expect(onSubmit).toHaveBeenCalledWith(
{ myValue: 0 },
expect.anything()
)
})
})

describe('event handlers', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const dataValueProperties: PropertiesTableProps = {
},
emptyValue: {
doc: 'The value to use (in `onChange` events etc) when emptying the field. Makes it possible for instance to provide `undefined` instead of an empty string when clearing the content of a text input.',
type: 'any',
type: ['{valueType}', 'undefined'],
status: 'optional',
},
required: {
Expand Down

0 comments on commit ff755e7

Please sign in to comment.