Repository navigation
fix(jsonforms-renderers): show required asterisk for conditionally required fields #112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d12e52a
fix(jsonforms): honor uiSchema options.required for red asterisk
ginaxu1 6b13375
fix(jsonforms): honor options.required on RadioControl
ginaxu1 68ab0ce
fix(jsonforms): derive required asterisk from schema via AJV
ginaxu1 85178c1
style(jsonforms-renderers): apply Prettier to required asterisk changes
ginaxu1 c067b5f
fix(jsonforms): share useIsRequired across controls and skip AJV when…
ginaxu1 b1eb25b
fix(jsonforms): harden NumberControl key/paste tests and document roo…
ginaxu1 075b98e
fix(jsonforms-renderers): keep #112 to conditional required asterisk …
ginaxu1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| import { useMemo } from 'react' | ||
| import { useJsonForms } from '@jsonforms/react' | ||
| import { isPathRequiredBySchema, schemaHasConditionalRequired } from '../utils/required' | ||
|
|
||
| /** | ||
| * Whether the control at `path` is required under the current form data. | ||
| * ORs JSONForms' static `required` prop with an AJV check that also sees | ||
| * conditional schema rules (`allOf`/`if`/`then`, etc.) that core's | ||
| * `isRequired()` ignores. | ||
| * | ||
| * Skips the AJV path when the control is hidden, already statically required, | ||
| * or the root schema has no conditional-required constructs. | ||
| */ | ||
| export function useIsRequired(path: string, staticRequired = false, visible = true): boolean { | ||
| const ctx = useJsonForms() | ||
| const rootData = ctx.core?.data | ||
| const validator = ctx.core?.validator | ||
| const rootSchema = ctx.core?.schema | ||
|
|
||
| return useMemo(() => { | ||
| if (!visible) return false | ||
| if (staticRequired) return true | ||
| if (!schemaHasConditionalRequired(rootSchema)) return false | ||
|
ginaxu1 marked this conversation as resolved.
|
||
| return isPathRequiredBySchema(path, rootData, validator) | ||
| }, [visible, staticRequired, path, rootData, validator, rootSchema]) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
packages/jsonforms-renderers/src/renderers/NumberControl.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| // @vitest-environment jsdom | ||
| import { afterEach, describe, expect, it } from 'vitest' | ||
| import { useState } from 'react' | ||
| import { cleanup, render, screen } from '@testing-library/react' | ||
| import { JsonForms } from '@jsonforms/react' | ||
| import { Theme } from '@radix-ui/themes' | ||
| import type { JsonSchema, UISchemaElement } from '@jsonforms/core' | ||
| import { radixRenderers } from './index' | ||
|
|
||
| type Data = Record<string, unknown> | ||
|
|
||
| afterEach(() => { | ||
| cleanup() | ||
| }) | ||
|
|
||
| const schema = { | ||
| type: 'object', | ||
| properties: { | ||
| reviewOutcome: { | ||
| type: 'string', | ||
| title: 'Review Outcome', | ||
| oneOf: [ | ||
| { const: 'approve', title: 'Approve' }, | ||
| { const: 'reject', title: 'Reject' }, | ||
| ], | ||
| }, | ||
| assessedAmount: { | ||
| type: 'number', | ||
| title: 'Assessed Amount', | ||
| }, | ||
| }, | ||
| allOf: [ | ||
| { | ||
| if: { | ||
| properties: { reviewOutcome: { const: 'approve' } }, | ||
| required: ['reviewOutcome'], | ||
| }, | ||
| then: { required: ['assessedAmount'] }, | ||
| }, | ||
| ], | ||
| } as JsonSchema | ||
|
|
||
| const uischema = { | ||
| type: 'VerticalLayout', | ||
| elements: [ | ||
| { type: 'Control', scope: '#/properties/reviewOutcome', options: { format: 'radio' } }, | ||
| { | ||
| type: 'Control', | ||
| scope: '#/properties/assessedAmount', | ||
| rule: { | ||
| effect: 'SHOW', | ||
| condition: { | ||
| scope: '#/properties/reviewOutcome', | ||
| schema: { const: 'approve' }, | ||
| }, | ||
| }, | ||
| }, | ||
| ], | ||
| } as UISchemaElement | ||
|
|
||
| function renderForm(seed: Data) { | ||
| function Harness() { | ||
| const [data, setData] = useState(seed) | ||
| return ( | ||
| <Theme> | ||
| <JsonForms | ||
| schema={schema} | ||
| uischema={uischema} | ||
| data={data} | ||
| renderers={radixRenderers} | ||
| onChange={({ data: next }) => setData(next as Data)} | ||
| /> | ||
| </Theme> | ||
| ) | ||
| } | ||
| return render(<Harness />) | ||
| } | ||
|
|
||
| describe('NumberControl required asterisk', () => { | ||
| it('shows a red asterisk only when the amount is conditionally required', () => { | ||
| renderForm({ reviewOutcome: 'reject' }) | ||
| expect(screen.queryByText('Assessed Amount')).toBeNull() | ||
|
|
||
| cleanup() | ||
| renderForm({ reviewOutcome: 'approve' }) | ||
| const label = screen.getByText('Assessed Amount') | ||
| expect(label.textContent).toContain('*') | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.