Skip to content
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

Update docs for definitions #236

Merged
merged 8 commits into from
Aug 28, 2018
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
194 changes: 143 additions & 51 deletions docs/building-a-form/common-definitions.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,29 @@

# Common definitions

Definitions are pieces of the form config that can be dropped in to represent specific types of questions. Most often used in `uiSchema`, definitions include features such as label text, validation functions, error messages, and rules for which widget to render.
Definitions, located in [/src/js/definitions](../../src/js/definitions), are pieces of the form config that can be dropped in to represent specific types of questions. They include features such as label text, validation functions, error messages, and rules for which widget to render.

There are common types of definitions: `schema`/`uiSchema` objects and functions that return `schema`/`uiSchema` objects. For the function versions, there is documentation within the fields for the parameters. Definitions are located in [/src/js/definitions](../../src/js/definitions).

- Simple definitions are provided as `schema` and `uiSchema` objects that you can import and overwrite to customize.
- More complex definitions are functions that require certain parameters.
Definitions export `schema` and `uiSchema` objects, which are functions that require certain parameters.

### Using definitions

To use a definition, import it near the top of the file you want to reference it in:

```js
import currencyUI from 'us-forms-system/lib/js/definitions/currency';
import currencyConfig from 'us-forms-system/lib/js/definitions/currency';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be imported using curly brackets now: import { currencyConfig } from .... The reason is because before we were using default exports in the definitions file, and now we're not (basically changing how we export/import things).

```

Then, call it to add all the `uiSchema` definitions. In this example, the definition is a function that takes the title for that field, which is used to populate the 'ui:title' property in uiSchema:
Then, call it to add the `schema` and `uiSchema` functions. The definition is a function that takes the title for that field, which is used to populate the 'ui:title' property in uiSchema:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to change this language a little bit. It's still pretty specific to uiSchema. Here's my recommended edit:

Then, use the definition by calling the schema and uiSchema functions on it where needed. Sometimes the functions take arguments to configure how the definitions are used. In this particular case, the uiSchema for currency takes an optional argument for the title of the field. In this example we want to set the title to "Currency", so we pass that as a string to the uiSchema function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 I can't remember making this change... it might have been a half implementation of another thought. Let me know if it needs changing.


```js
uiSchema: {
...
monthlyWages: currencyUI('Monthly wages')
currency: currencyConfig.uiSchema('Currency'),
...
}
schema: {
...
currency: currencyConfig.schema(),
...
}
```
Expand All @@ -45,22 +47,25 @@ Available definitions are:
### Autosuggest

A common type-ahead widget that lets a user type in values and narrow down a longer list of options. It is most commonly used with an `enum` of the available options as shown here. Define the uiSchema by calling the function that you import. You can pass an object with additional uiSchema properties.

```js
import { uiSchema as autosuggestUI } from 'us-forms-system/lib/js/definitions/autosuggest';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This import statement needs to change to import { autosuggestConfig } from 'us-forms-system/lib/js/definitions/autosuggest';.


schema: {
type: 'object',
properties: {
officeLocation: {
type: 'string',
enum: [
'LA', 'NY', 'CH'
],
enumNames: [
'Los Angeles',
'New York',
'Chicago'
]
function schema() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think what these code samples are trying to show is what this would look like in the form config, not what the definition code is. So I think this would actually remain the way it was before, where the schema is an object in the form config. You can just revert this change for the schema.

This particular definition is a little confusing because here we're not actually using the schema from the definition, but instead writing our own schema into the form config, so I think we should clean that up as a separate issue. I'll create a ticket for that.

return {
type: 'object'
properties: {
officeLocation: {
type: 'string',
enum: [
'LA', 'NY', 'CH'
],
enumNames: [
'Los Angeles',
'New York',
'Chicago'
]
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this one last } should be deleted!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It didn't look right but I couldn't figure out how to check it. Thanks!

}
}
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The usage for the uiSchema below also needs to change, where instead it would look like this:

uiSchema: {
  officeLocation: autosuggestConfig.uiSchema(
    ...
  )
}

(what is contained in the ... is exactly as it is now (lines 74-81), it's just where the uiSchema function is called that needs to change)

Expand All @@ -81,63 +86,105 @@ Source: [/src/js/definitions/autosuggest.js](../../src/js/definitions/autosugges

### Currency

Formats and validates a US currency field. The display includes a leading `$` character. Call this exported function and pass it the label to be used on the field.
Formats and validates a US currency field that includes a leading `$` character. You can pass this function a label to be used on the field.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would change the language in the second sentence to be: "You can pass the uiSchema function a label to be used on the field".


![Two currency fields, one with the field selected](../images/definitions/currency.png)

```js
import currencyUI from 'us-forms-system/lib/js/definitions/currency';
import { currencyConfig } from 'us-forms-system/lib/js/definitions/currency';

uiSchema: {
payments: currencyUI('Total Payments')
currency: currencyConfig.uiSchema('Currency')
},
schema: {
type: 'object',
properties: {
currency: currencyConfig.schema()
}
}
```
Source: [/src/js/definitions/currency.js](../../src/js/definitions/currency.js)

### Current or past dates

The common date field with current or past validation set (i.e., dates in the future are not valid). Call this exported function and pass it the label to be used on the field.
The common date field with validation warning that dates in the past or future are not valid. You can pass this function a label to be used on the field. Dates must be on or before January 1, 1900.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anywhere where it says "You can pass this function a label", I would specify to be "You can pass the uiSchema function a label" because there are now 2 functions that are exported by a definition: the schema() and the uiSchema(). It's important to specify which of the two takes the additional argument.


![Four date fields, with and without focus, with an invalid year, and with valid data](../images/definitions/currentOrPastDate.png)

```js
import currentOrPastDateUI from 'us-forms-system/lib/js/definitions/currentOrPastDate';
import { currentOrPastDateConfig } from 'us-forms-system/lib/js/definitions/currentOrPastDate';

uiSchema: {
birthdate: currentOrPastDate('Date of Birth')
currentOrPastDate: currentOrPastDateConfig.uiSchema()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to show how this takes an additional argument for the label like we used to? So:

currentOrPastDate: currentOrPastDateConfig.uiSchema('Date of Birth')

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd keep the birthdate as well since it's a good example of when you might want a date not in the future (assuming the person filling out the form is giving their own birthdate 🤔).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes, good point @dmethvin-gov!

},
schema: {
type: 'object'
properties: {
currentOrPastDate: currentOrPastDateConfig.schema()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This key also needs to be birthdate, so:

        birthdate: currentOrPastDateConfig.schema()

}
}
```
Source: [/src/js/definitions/currentOrPastDate.js](../../src/js/definitions/currentOrPastDate.js)

### Current or past month/year

The common date field without the day field and with current or past validation set (i.e., dates in the future are not valid). Call this exported function and pass it the label to be used on the field.
The common date field with only month and year fields and with validation warning that dates in the past or future are not valid. You can pass this function a label to be used on the field. Dates must be on or before January 1, 1900.

![Three date fields, one empty, one with invalid data, and one with valid data](../images/definitions/currentOrPastMonthYear.png)

```js
import currentOrPastMonthYear from 'us-forms-system/lib/js/definitions/currentOrPastMonthYear';
import { currentOrPastMonthYearConfig } from 'us-forms-system/lib/js/definitions/currentOrPastMonthYear';

uiSchema: {
lastContact: currentOrPastMonthYear('Last Contact')
currentOrPastMonthYear: currentOrPastMonthYearConfig.uiSchema()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing about showing the label that can be passed to it

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The lastContact was similarly trying to give an example of when you might want this type of validated date.

},
schema: {
type: 'object',
properties: {
currentOrPastMonthYear: currentOrPastMonthYearConfig.schema()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This key also needs to be lastContact, the same as above.

}
}
```
Source: [/src/js/definitions/currentOrPastMonthYear.js](../../src/js/definitions/currentOrPastMonthYear.js)

### Date

The common date field with basic date validation. Call this exported function and pass it the label to be used on the field.
The common date field with basic date validation. You can pass this function the label to be used on the field. Dates must be on or before January 1, 1900.

![Two date fields, one with an invalid year](../images/definitions/date.png)

```js
import dateUI from 'us-forms-system/lib/js/definitions/date';
import { dateConfig } from 'us-forms-system/lib/js/definitions/date';

uiSchema: {
startDate: dateUI('startDate')
date: dateConfig.uiSchema()
},
schema: {
type: 'object',
properties: {
date: dateConfig.schema()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

startDate here instead of date

}
}
```
Source: [/src/js/definitions/date.js](../../src/js/definitions/date.js)

### Date range

Two common date fields with validation to ensure they form a valid range. Call this exported function.
Two common date fields with validation to ensure they form a valid range. Dates must be on or before January 1, 1900.

![Two date fields, signifying a date range](../images/definitions/dateRange.png)

```js
import dateRangeUI from 'us-forms-system/lib/js/definitions/dateRange';
import { dateRangeConfig } from 'us-forms-system/lib/js/definitions/dateRange';

uiSchema: {
servicePeriod: dateRangeUI('servicePeriod')
dateRange: dateRangeConfig.uiSchema()
},
schema: {
type: 'object',
properties: {
dateRange: dateRangeConfig.schema()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

servicePeriod instead of dateRange

}
}
```
Source: [/src/js/definitions/dateRange.js](../../src/js/definitions/dateRange.js)
Expand All @@ -150,60 +197,105 @@ Source: [/src/js/definitions/file.js](../../src/js/definitions/file.js)

### Month/year

The common date field, excluding day field, with basic validation. Call this exported function with the label to be displayed on the field.
The common date field, excluding day field, with basic validation. Dates must be on or before January 1, 1900. You can call this function with the label to be displayed on the field.

![Two date fields, one unfilled and one filled with a date](../images/definitions/date.png)

```js
import monthYearUI from 'us-forms-system/lib/js/definitions/monthYear';
import { monthYearConfig } from 'us-forms-system/lib/js/definitions/monthYear';

uiSchema: {
serviceStart: monthYearUI('Month/Year Service Started')
monthYear: monthYearConfig.uiSchema()
},
schema: {
type: 'object',
properties: {
monthYear: monthYearConfig.schema()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

serviceStart instead of monthYear

}
}
```
Source: [/src/js/definitions/monthYear.js](../../src/js/definitions/monthYear.js)

### Month/year range

Two common date fields, excluding day field, with validation to ensure the dates form a valid range. Similar to the `Date range` above but without the days. Call this exported function.
Two common date fields, excluding day field, with validation to ensure the dates form a valid range. Dates must be on or before January 1, 1900.

![Four range fields, one empty, one with valid data, and two with invalid data](../images/definitions/monthYearRange.png)

```js
import monthYearRangeUI from 'us-forms-system/lib/js/definitions/monthYearRange';
import { monthYearRangeConfig } from 'us-forms-system/lib/js/definitions/monthYearRange';

uiSchema: {
serviceStart: monthYearRangeUI()
monthYearRange: monthYearRangeConfig.uiSchema()
},
schema: {
type: 'object',
properties: {
monthYearRange: monthYearRangeConfig.schema()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

serviceRange instead of monthYearRange

}
}
```
Source: [/src/js/definitions/monthYearRange.js](../../src/js/definitions/monthYearRange.js)

### Phone

A phone number with basic validation. Call this exported function, optionally passing it the label for the field (the default is "Phone").
A phone number with basic validation. You can pass this function the label to be used on the field.

![A phone field without focus, with focus, invalid data, and valid data](../images/definitions/phone.png)

```js
import phoneUI from 'us-forms-system/lib/js/definitions/phone';
import { phoneConfig } from 'us-forms-system/lib/js/definitions/phone';

uiSchema: {
homePhone: phoneUI('Home Phone')
phone: phoneConfig.uiSchema()
},
schema: {
type: 'object',
properties: {
phone: phoneConfig.schema()
}
}
```
Source: [/src/js/definitions/phone.js](../../src/js/definitions/phone.js)

### Social Security Number

A social security number with default label text and validation. This is an object.
A US social security number field with validation.

![Three social security fields, one unselected, one selected, and one with invalid data](../images/definitions/ssn.png)

```js
import ssnUI from 'us-forms-system/lib/js/definitions/ssn';
import { ssnConfig } from 'us-forms-system/lib/js/definitions/ssn';

uiSchema: {
ssn: ssnUI
ssn: ssnConfig.uiSchema()
},
schema: {
type: 'object',
properties: {
ssn: ssnConfig.schema()
}
}
```
Source: [/src/js/definitions/ssn.js](../../src/js/definitions/ssn.js)

### Year

A text field that validates the current or a past year. This is an object.
A text field that validates the current or a past year. Dates must be on or before January 1, 1900.

![Four year fields, selected, unselected, and with valid and invalid data](../images/definitions/year.png)

```js
import yearUI from 'us-forms-system/lib/js/definitions/year';
import { yearConfig } from 'us-forms-system/lib/js/definitions/year';

uiSchema: {
yearBorn: yearUI
year: yearConfig.uiSchema()
},
schema: {
type: 'object',
properties: {
year: yearConfig.schema()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

taxYear instead of year

}
}
```
Source: [/src/js/definitions/year.js](../../src/js/definitions/year.js)
Expand Down
Binary file added docs/images/definitions/currency.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/definitions/currentOrPastDate.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/definitions/date.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/definitions/dateRange.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/definitions/monthYearRange.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/definitions/phone.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/definitions/ssn.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/definitions/year.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.