Detalhes do pacote

@xgovformbuilder/govuk-react-jsx

xgovformbuilder13.1kMIT7.1.2

This package is no longer maintained and will not receive updates bringing it beyond govuk-frontend 4.0.1. If you are using this in your project the simplest way forward is to copy and paste the components from here into your project allowing you to kee

govuk, react, jsx, components

readme (leia-me)

GOV.UK React components (govuk-react-jsx)

This package is no longer maintained and will not receive updates bringing it beyond govuk-frontend 4.0.1. If you are using this in your project the simplest way forward is to copy and paste the components from here into your project allowing you to keep them up to date with govuk-frontend yourselves.

<summary>View the readme</summary> govuk-frontend 4.0.1 version (Pipeline) MIT License Please note - the version number of govuk-react-jsx is independent of the govuk-frontend version number. View demo at https://govuk-react-jsx.netlify.app/ View example app at https://surevine.github.io/govuk-react-jsx-examples/ (for which the source code is at https://github.com/surevine/govuk-react-jsx-examples) ## WARNING > If you do not need the characteristics of a Single Page App framework like React, please consider using something else. Government services should be progressively enhanced, and should function without JavaScript enabled. > > If you have an explicit requirement that cannot be delivered effectively in a progressively enhanced manner then you might have a case for using React. If you have plans to build your React app in a progressively enhanced way, you might be fine. Whatever you do, be prepared to defend it at a service assessment. ## Quick install $ npm install govuk-react-jsx (See installation and usage for more details) ## Motivation This repository contains govuk-frontend compatible React components. The aim of this package is to steer closely to govuk-frontend by consuming the CSS directly from the govuk-frontend npm package. And to strike a balance between mirroring the GOV.UK Nunjucks params vs ideomatic React props. This has the following benefits - Generated markup is identical to the output from the govuk-frontend macros. This allows us to benefit from the hard work that GDS has put into forming good markup patterns, including use of aria attributes. - Keeping up to date with upstream changes in govuk-frontend is as simple as updating the package.json version and mirroring any markup changes made. The test suite helps with this by comparing our output against the reference Nunjucks output - any differences constitute a test failure. - Anyone that knows the GOV.UK Nunjucks macros will quickly feel familiar with the structure of these components ## Comparison with govuk-react govuk-react is the other main option in this space. Naturally the first thing people ask is why one might use this repository instead of govuk-react. Here's my take on the matter: | govuk-react | govuk-react-jsx (This repository) | | ----------- | --------------------------------- | | An implementation of the govuk design system.
CSS, JS and Markup patterns have all been rewritten from scratch.
Aria attributes missing. | Directly consumes the govuk-frontend CSS/JS and accurately mirrors their markup patterns, including Aria attributes | | Upstream CSS/JS changes in govuk-frontend need to be manually transferred across and/or rebuilt | Upstream CSS/JS changes are pulled in automatically. Only markup changes need to manually transferred (But are validated as correct by the test suite) | | Relatively complex code | Simpler code - just plain JSX ports of the Nunjucks | | Uses StyledComponents
Great if you like them and use them. But if you prefer a different library then you would end up needing both in your toolchain.
Increased maintenance burden on govuk-react team | Uses plain Sass compilation of the govuk-frontend code.
You are free to use a CSSinJS library of your choice for your own styles if you wish | | Cleaner component props since it has been designed from the ground up for React | Props mostly mirror the govuk-frontend Nunjucks params with some exceptions as below. This has been done in order to steer as closely to govuk-frontend as possible, and to facilitate the test suite checking the output against the original.

This is possibly the main argument against this repository and for govuk-react. It's a tradeoff. One that is worth making in my opinion but make your own call on that. | | More comprehensive set of components.
Includes components for headings, paragraphs, spacing etc | Only includes components that are direct equivalents of the Nunjucks templates in govuk-frontend.
(Although a future release is planned that will include grid, headings and paragraphs etc.)
Spacing classes will likely never be a component in this repository - some things like that I feel are already sufficiently well served by just using the CSS classes directly. This repository does not attempt to abstract you away from the fact that you are using govuk-frontend CSS. | | Allows code splitting of the styles, since they are defined in each component | No code splitting - all GOV.UK CSS is loaded. Although - it is relatively easy to omit the Scss files that you don't need so this is only a small downside | | No complications from integrating with other libraries | As per the React docs on Integrating with other libraries there is a very small chance that the integration of the GOV.UK JS might result in bugs if React tries to update a component that the GOV.UK JS has also updated. This is fairly unlikely but is something to be mindful of. | ## Assumptions These components assume you: - Have compiled the govuk-frontend scss and have included it in your page (create-react-app is able to compile the gov.uk Sass files) - Are using react-router - Have read the exceptions below ## Known issues - The "None of these" JavaScript initialised as part of govuk-frontend does not currently function with these components. Because external JS cannot influence the checked state of controlled components in React, the govuk-frontend approach does not work here. If you need this functionality in your service it would be best to implement it within the form framework you are using (E.g. Formik and the like). ## Exceptions Exceptions to the conformance with govuk-frontend nunjucks params are as follows: - Links - Anywhere that accepts an href / text combo of params to create a hyperlink, will also accept a to prop instead of href, which will be used in a react-router <Link> element. - Header links - homepageUrl and serviceUrl become homepageUrlHref / homepageUrlTo and serviceUrlHref / serviceUrlTo, with the To variants being passed to a react-router <Link> the Href variants being a plain html <a> tag - Anywhere that accepts an html or text param in Nunjucks will instead accept a children prop which should be passed either a string, or JSX. Params such as summaryText or summaryHtml become summaryChildren - classes becomes className - spellcheck becomes spellCheck - inputmode becomes inputMode - describedBy becomes aria-describedby - colspan and rowspan become colSpan and rowSpan - autocomplete becomes autoComplete - ariaLabel becomes aria-label - List keys - Anywhere that you specify an array of items such as a list of links, you may optionally specify a reactListKey for each item. This will be used instead of the index when doing .map over the items. React uses these keys internally to work out whether to re-render items. This is crucial for dynamic components where you might re-sort the list items for example. For static data it is less important and the key can be omitted. (See https://reactjs.org/docs/lists-and-keys.html#keys for more) (The only exception to this rule is the tab component, where the tabs are already sufficiently keyed by id) - The <Select> component and <Radios> components take a top level value prop, instead of setting checked or selected on the individual items. This is more in line with React and React-based form libraries - The <ErrorSummary> component does not automatically focus itself when errors occur - it is up to the calling app to focus it when appropriate, such as when a submit button is pressed. - The <Table> component data structure is changed slightly to accomodate react list keys on table rows. Note the addition of a cells key inside each row, rather than a simple nested array of rows / cells. See utils/processExampleData.js for the complete list of transformations. ## Installation & Usage ### Installation npm install govuk-react-jsx NB: govuk-react-jsx has a number of peer dependencies that you will need to install yourself: - govuk-frontend - react-router - react - react-router-dom - react-helmet A full list can be found in the dependencies section of /scripts/package.json An example of setting up create-react-app to use govuk-react-jsx can be viewed in this commit over on govuk-react-jsx-examples. ### Using Styles govuk-react-jsx uses the same styles as govuk-frontend, the only difference being styles need to be applied using className rather than class as it's React. e.g. to use govuk-frontend typography styles: html <p className="govuk-body"> Use this design system to make your service consistent with GOV.UK. </p> An example of using govuk-frontend styles with govuk-react-jsx can be viewed at https://surevine.github.io/govuk-react-jsx-examples/. ### Using Components All components are documented in Storybook with example usage code. e.g. to render a panel component html import { Panel } from 'govuk-react-jsx'; <Panel titleChildren="Application complete"> Your reference number: HDJ2123F </Panel> ### Using with form libraries An example of using govuk-react-jsx to create forms using Formik and react-hook-form, complete with error validation can be viewed at https://surevine.github.io/govuk-react-jsx-examples/. ### Refs for form components In the case of form components React.forwardRef is used to forward any ref passed to a component onto the underlying DOM element. For example: const ref = useRef(); <Input ref={ref} id="input-example" label={{ children: [ 'National Insurance number' ] }} name="test-name" type="text" /> In this code, ref will contain a reference to the actual DOM input element. You could use this to then call .focus() on the element. For components such as DateInput, Radios, Checkboxes and anywhere that uses an items prop to represent many form elements to be returned by the component, each object in the items array can accept a ref key which will then return a reference to the rendered form element for that item. Rather than adding them everywhere I have currently restricted it to form components where I can imagine a use case. Please open an issue if you need refs forwarded onto other types of elements - I may have missed a use case. ## Versioning This repository is versioned separately and follows standard semver procedures. ## Tests The test suite passes the example data from the govuk-frontend repository through the JSX components and compares the output with the reference output provided in govuk-frontend. Any differences here constitute a failure. Tests run in Github actions. ## Currently used by - Public Health England (See https://github.com/PublicHealthEngland/coronavirus-dashboard) - UKRI - Cabinet Office ## Contributors Andy Mantell (Primary maintainer) Dave Hudson Mick Jones (Helped to build the original JSX ports found at https://github.com/LandRegistry/govuk-react-components) ## Development sponsored by Surevine HM Land Registry

changelog (log de mudanças)

Changelog

Unreleased

Fixes

Features

Breaking changes


Releases

v7.1.0

Features

  • React 18 support (Simply by loosening the peer dependency requirement).

v7.0.1

Fixes

v7.0.0

Fixes

Breaking changes

v6.2.1

Fixes

v6.2.0

Features

Fixes

v6.1.0

Features

v6.0.0

Fixes

Breaking changes

  • Table data structure changed in order to accomodate react list keys. Each row in the rows array now has a cells key instead of simply being a nested array of rows / cells.
    See the updated demos on the storybook.
    This breaking change was necessary in order to accomodate react list keys on table rows.

Features

v5.1.0

Fixes

Features

v5.0.0

v4.1.1

Fixes

v4.1.0

Features

  • Larger release than usual since the 3.9.x update caused a few issues with testing. Therefore have jumped straight to 3.10.0

v4.0.3

Fixes

v4.0.2

Fixes

  • Add webpack magic comments to checkboxes and radios (accidentally missed from previous release)
  • Tweak babel to allow dynamic imports through to the final package. Previously they were being resolved before being published.

v4.0.1

Fixes

  • Add webpack magic comments to optimize the dynamically imported govuk-frontend js

v4.0.0

Features

Breaking changes

  • Upshot of the switch to govuk-frontend-diff was that it became necessary to dynamically import the GOVUK components which interact with the DOM, in order to enable Server Side Rendering for the tests.
    This is therefore marked up as a breaking change since it may require changes to your Webpack config to support code splitting (Although create-react-app supports this out the box, in which case you don't need to do anything).
    See https://reactjs.org/docs/code-splitting.html#import for more details.
    This should pave the way for serverside rendering of the components although this isn't tested or formally supported yet (But may well work just fine...)

v3.1.0

Features

v3.0.0

Breaking changes

  • Moved the published package's dependencies to peerDependencies, to avoid issues with multiple copies of React etc being installed (Such as https://reactjs.org/warnings/invalid-hook-call-warning.html#duplicate-react).
    If you aren't already, you will need to install compatible versions of govuk-frontend, react-router, react, react-router-dom, react-helmet since these are no longer installed as dependencies of this package

Fixes

  • Updated the package's govuk-frontend dependency to 3.6.0 - it was incorrectly pinned to an older version

Features

  • Updated dependency on react-helmet to 6

v2.0.1

Fixes

  • Remove erroneous space before comma in footer OGL statement

v2.0.0

Breaking changes

  • Header links - homepageUrl and serviceUrl have become homepageUrlHref / homepageUrlTo and serviceUrlHref / serviceUrlTo, with the To variants being passed to a react-router <Link> the Href variants being a plain html <a> tag

v1.5.0

Fixes

Features

v1.4.2

Fixes

  • Fix error summary not focusing after first form submission. Now correctly focuses every time submit is pressed

v1.4.1

Fixes

  • Fix bug when using controlled inputs whereby Radios would switch from uncontrolled to controlled and throw a console warning. Caused by the new features added in v1.4.0

v1.4.0

Features

  • defaultValue top level prop added to radios component to complement the value prop and facilitate compatibility with form libraries dealing with uncontrolled form inputs (Such as react-hook-form)

v1.3.0

Features

  • React.forwardRef now added to all singular form components allowing you to pass useRef refs into the component props. Components that return multiple form elements now accept a ref key for each item in the items array prop.

v1.2.0

Fixes

  • Update the date input component to use input type=text inputmode=numeric.
  • Update checkboxes and radio buttons to include item hint classes on item hint.

Features

Breaking changes

v1.1.0

Fixes

More extensive test suite added, resulting in several minor fixes as follows

  • Fix date input, file upload, input, select, textarea, checkboxes & radios not receiving correct aria-describedby attribute
  • Fix date input, file upload, input, select, textarea, checkboxes & radios hints and error messages receiving incorrect ids
  • Fix incorrect casing of date input labels
  • Fix lack of colon after error message visually hidden text
  • Fix missing attributes from error summary links
  • Prevent assetPath prop making it through to header component in case someone passes it
  • Prevent tag attributes making it through from phase banner (behaviour now consistent with govuk-frontend)
  • Fix missing attributes from select options
  • Omit role attribute from checkboxes/radios fieldset to be consistent with govuk-frontend

Features

  • onChange prop assigned directly to <DateInput> will now get passed through to the individual inputs (No need to put change handlers individually in the items now). This mirrors the behaviour already seen on other compound fields such as Radios and Checkboxes

v1.0.1

Fixes

  • Fix for uncontrolled -> controlled radio inputs caused by bug in value assignment
  • Slight tweak for Select component so that it accepts a top level value prop instead of individual selected booleans on each item to make it more React friendly

v1.0.0

Initial release