-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Feature: Add LayoutHeaderField to support headers in Layout Grids #4548
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 1 commit
Commits
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,49 @@ | ||
import { | ||
getTemplate, | ||
getUiOptions, | ||
titleId, | ||
FieldProps, | ||
FormContextType, | ||
RJSFSchema, | ||
StrictRJSFSchema, | ||
TemplatesType, | ||
} from '@rjsf/utils'; | ||
|
||
/** The `LayoutHeaderField` component renders a `TitleFieldTemplate` with an `id` derived from the `idSchema` | ||
* and whether it is `required` from the props. The `title` is derived from the props as follows: | ||
* - If there is a title in the `uiSchema`, it is displayed | ||
* - Else, if there is an explicit `title` passed in the props, it is displayed | ||
* - Otherwise, if there is a title in the `schema`, it is displayed | ||
* - Finally, the `name` prop is displayed as the title | ||
* | ||
* @param props - The `LayoutHeaderField` for the component | ||
*/ | ||
export default function LayoutHeaderField< | ||
T = any, | ||
S extends StrictRJSFSchema = RJSFSchema, | ||
F extends FormContextType = any | ||
>(props: FieldProps<T, S, F>) { | ||
const { idSchema, title, schema, uiSchema, required, registry, name } = props; | ||
const options = getUiOptions<T, S, F>(uiSchema, registry.globalUiOptions); | ||
const { title: uiTitle } = options; | ||
const { title: schemaTitle } = schema; | ||
const fieldTitle = uiTitle || title || schemaTitle || name; | ||
if (!fieldTitle) { | ||
return null; | ||
} | ||
const TitleFieldTemplate: TemplatesType<T, S, F>['TitleFieldTemplate'] = getTemplate<'TitleFieldTemplate', T, S, F>( | ||
'TitleFieldTemplate', | ||
registry, | ||
options | ||
); | ||
return ( | ||
<TitleFieldTemplate | ||
id={titleId<T>(idSchema)} | ||
title={fieldTitle} | ||
required={required} | ||
schema={schema} | ||
uiSchema={uiSchema} | ||
registry={registry} | ||
/> | ||
); | ||
} |
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,132 @@ | ||
import { titleId, FieldProps, ID_KEY, IdSchema, Registry, TitleFieldProps } from '@rjsf/utils'; | ||
import { render, screen, within } from '@testing-library/react'; | ||
import noop from 'lodash/noop'; | ||
|
||
import templates from '../src/components/templates'; | ||
import LayoutHeaderField from '../src/components/fields/LayoutHeaderField'; | ||
|
||
const TEST_ID = 'test-id'; | ||
const REQUIRED_ID = 'required-id'; | ||
|
||
const TITLE_BOLD = 'test'; | ||
const TITLE_BOLD_2 = 'test ui'; | ||
const TITLE_NORMAL = 'title'; | ||
|
||
function TestTitleField(props: TitleFieldProps) { | ||
const { id, title, required } = props; | ||
return ( | ||
<div id={id} data-testid={TEST_ID}> | ||
{title} | ||
{required && <span data-testid={REQUIRED_ID} />} | ||
</div> | ||
); | ||
} | ||
|
||
describe('LayoutHeaderField', () => { | ||
function getProps(overrideProps: Partial<FieldProps> = {}): FieldProps { | ||
const { idSchema = {} as IdSchema, schema = {}, name = '', uiSchema = {}, required = false, title } = overrideProps; | ||
return { | ||
// required FieldProps stubbed | ||
autofocus: false, | ||
disabled: false, | ||
errorSchema: {}, | ||
formContext: undefined, | ||
formData: undefined, | ||
onBlur: noop, | ||
onChange: noop, | ||
onFocus: noop, | ||
readonly: false, | ||
title, | ||
required, | ||
// end required FieldProps | ||
idSchema, | ||
schema, | ||
uiSchema, | ||
name, | ||
registry: { | ||
templates: { | ||
...templates(), | ||
TitleFieldTemplate: TestTitleField, | ||
}, | ||
} as Registry, | ||
}; | ||
} | ||
|
||
test('default render with no title is empty render', () => { | ||
const props = getProps(); | ||
const { container } = render(<LayoutHeaderField {...props} />); | ||
|
||
expect(container).toBeEmptyDOMElement(); | ||
}); | ||
|
||
test('name is provided, and its required', () => { | ||
const props = getProps({ name: TITLE_BOLD, required: true }); | ||
render(<LayoutHeaderField {...props} />); | ||
|
||
// renders header field and has expected text and no id | ||
const headerField = screen.getByTestId(TEST_ID); | ||
expect(headerField).toHaveTextContent(TITLE_BOLD); | ||
expect(headerField).toHaveAttribute('id', titleId('undefined')); | ||
|
||
// Is required | ||
const requiredSpan = within(headerField).getByTestId(REQUIRED_ID); | ||
expect(requiredSpan).toBeInTheDocument(); | ||
}); | ||
|
||
test('name is provided, schema has title, idSchema has ID_KEY, not required', () => { | ||
const props = getProps({ | ||
name: TITLE_BOLD, | ||
schema: { title: TITLE_NORMAL }, | ||
idSchema: { [ID_KEY]: 'foo' } as IdSchema, | ||
}); | ||
render(<LayoutHeaderField {...props} />); | ||
|
||
// renders header field and has expected text and id | ||
const headerField = screen.getByTestId(TEST_ID); | ||
expect(headerField).toHaveTextContent(TITLE_NORMAL); | ||
expect(headerField).toHaveAttribute('id', titleId(props.idSchema[ID_KEY])); | ||
|
||
// Is not required | ||
const requiredSpan = within(headerField).queryByTestId(REQUIRED_ID); | ||
expect(requiredSpan).not.toBeInTheDocument(); | ||
}); | ||
|
||
test('title prop is passed, schema has title, idSchema has ID_KEY, required', () => { | ||
const props = getProps({ | ||
title: TITLE_BOLD, | ||
schema: { title: TITLE_NORMAL }, | ||
idSchema: { [ID_KEY]: 'foo' } as IdSchema, | ||
required: true, | ||
}); | ||
render(<LayoutHeaderField {...props} />); | ||
|
||
// renders header field and has expected text and id | ||
const headerField = screen.getByTestId(TEST_ID); | ||
expect(headerField).toHaveTextContent(TITLE_BOLD); | ||
expect(headerField).toHaveAttribute('id', titleId(props.idSchema[ID_KEY])); | ||
|
||
// Is not required | ||
const requiredSpan = within(headerField).getByTestId(REQUIRED_ID); | ||
expect(requiredSpan).toBeInTheDocument(); | ||
}); | ||
|
||
test('uiSchema has ui:title, title prop is passed, no id, not required', () => { | ||
const props = getProps({ | ||
title: TITLE_BOLD, | ||
uiSchema: { | ||
'ui:title': TITLE_BOLD_2, | ||
}, | ||
idSchema: { [ID_KEY]: 'foo' } as IdSchema, | ||
}); | ||
render(<LayoutHeaderField {...props} />); | ||
|
||
// renders header field and has expected text and no id | ||
const headerField = screen.getByTestId(TEST_ID); | ||
expect(headerField).toHaveTextContent(TITLE_BOLD_2); | ||
expect(headerField).toHaveAttribute('id', titleId(props.idSchema[ID_KEY])); | ||
|
||
// Is not required | ||
const requiredSpan = within(headerField).queryByTestId(REQUIRED_ID); | ||
expect(requiredSpan).not.toBeInTheDocument(); | ||
}); | ||
}); |
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.
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.