Skip to content

Commit fdd54b5

Browse files
Mnickiigavinbarron
andauthored
docs: add people-picker form associated custom elements spec (#3143)
* add form-based validation elements * add initial draft of inclusion of form-associated custom elements in people-picker * apply review suggestions * apply review comments * adds in the types of the form validation properties * Update specs/mgt-people-picker.md --------- Co-authored-by: Gavin Barron <[email protected]>
1 parent 35b86ff commit fdd54b5

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed

specs/mgt-people-picker.md

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,113 @@ The people picker components provides a way for developers to select users, grou
4646
<mgt-people-picker group-ids="02bd9fd6-8f93-4758-87c3-1fb73740a315,06f62f70-9827-4e6e-93ef-8e0f2d9b7b23"></mgt-people-picker>
4747
```
4848

49+
## Form validation
50+
51+
The mgt-people-picker component can be used as a form control in a form, and the form can be submitted with the attributes as part of the form data. It makes use of the form-associated custom elements API, which provides a new set of capabilities that make custom controls work like built-in form controls. The component will implement the form-associated behaviors to participate in form submission, validation, and other form-related behaviors. [Read more](https://docs.google.com/document/d/1JO8puctCSpW-ZYGU8lF-h4FWRIDQNDVexzHoOQ2iQmY/edit?pli=1#heading=h.2hgix04sc53t) about the form-associated custom elements API and [how to create custom form controls](https://css-tricks.com/creating-custom-form-controls-with-elementinternals/).
52+
53+
The following properties are used as part of the required field validation logic for the component.
54+
55+
56+
| Property on the object | Description | Type of property |
57+
| ------------------ | ----------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------ |
58+
| `required` | Determines if the required field validation is to be executed. When `true` the field is invalid if the the selectedPeople property contains zero items. | `boolean` |
59+
| `selectedPeople` | Sets the selected people programmatically. | `array` | |
60+
| `disabled` | Sets whether the people picker is disabled. When disabled, the user is not able to search or select people. | `boolean` |
61+
62+
## Implementing the form-associated behaviors
63+
64+
The implementation of the form-associated custom elements will include the following:
65+
66+
### Identify the element as a form-associated custom element
67+
68+
The `mgt-people-picker` component is identified as a form-associated custom element by adding a static `formAssociated` property to the custom element class to tell the browser to treat the element like a form control.
69+
70+
```javascript
71+
export class MgtPeoplePicker extends MgtTemplatedTaskComponent {
72+
// ...
73+
static formAssociated = true;
74+
// ...
75+
}
76+
```
77+
78+
### Providing access to the form control's internals
79+
80+
The component will implement the attachInternals() method on the element to get access to extra methods and properties for form controls, like setFormValue() and setValidity(). This method returns the `ElementInternals` object that provides access to the form control APIs.
81+
82+
It will also set the internal value of the control.
83+
84+
```javascript
85+
export class MgtPeoplePicker extends MgtTemplatedTaskComponent {
86+
constructor() {
87+
super();
88+
this._internals = this.attachInternals();
89+
// ...
90+
}
91+
}
92+
```
93+
94+
### Managing the form control's value
95+
96+
The mgt-people-picker will manage its form value through the value property. This value will be the selected people in the picker.
97+
98+
```javascript
99+
export class MgtPeoplePicker extends MgtTemplatedTaskComponent {
100+
// ...
101+
get value() {
102+
return this.selectedPeople.join(',');
103+
}
104+
// ...
105+
}
106+
```
107+
108+
### Handling form control validation state
109+
110+
The mgt-people-picker will handle validation by using the setValidity() provided by the ElementInternals object, which you get by calling attachInternals() on your custom element, to indicate whether the element is currently valid or not.
111+
112+
```javascript
113+
export class MgtPeoplePicker extends MgtTemplatedTaskComponent {
114+
// ...
115+
checkValidity() {
116+
const isValid = this.selectedPeople && this.selectedPeople.length > 0;
117+
this._internals.setValidity({
118+
customError: !isValid
119+
}, isValid ? '' : 'At least one person must be selected.');
120+
121+
return isValid;
122+
}
123+
// ...
124+
}
125+
```
126+
127+
### Manage Form Submission
128+
129+
The mgt-people-picker will manage form submission by implementing the formDisabledCallback() and formResetCallback() methods.
130+
131+
```javascript
132+
export class MgtPeoplePicker extends MgtTemplatedTaskComponent {
133+
// ...
134+
formDisabledCallback(isDisabled) {
135+
this.disabled = isDisabled;
136+
}
137+
138+
formResetCallback() {
139+
this.selectedPeople = [];
140+
}
141+
// ...
142+
}
143+
```
144+
## Future Validation Enhancements
145+
146+
In future versions of the `mgt-people-picker`, we plan to add more advanced validation features to give developers more control over the selection process and ensure that users select the correct number of people.
147+
148+
### Range Validation
149+
150+
We plan to add support for range validation. This will allow developers to specify a minimum and maximum number of people that can be selected. This will be useful in scenarios where there is a hard limit on the number of people that can be selected. For example, a developer could specify that at least 2 people and at most 5 people must be selected.
151+
152+
```html
153+
<mgt-people-picker min-selected="2" max-selected="5"></mgt-people-picker>
154+
```
155+
49156
## Events
50157

51158
The following events are fired from the component.

0 commit comments

Comments
 (0)