-
Notifications
You must be signed in to change notification settings - Fork 110
/
requiredField.js
36 lines (30 loc) · 1.11 KB
/
requiredField.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import getConstant from '../constants';
import { isObject } from './isType';
const asterisk = `<span class="red" title="${getConstant('REQUIRED_FIELD_TEXT')}">* </span>`;
const addAsterisk = (el) => {
const target = $(el);
if (isObject(target)) {
// If the element is part of a Fieldset then place the asterisk before the <fieldset><legend>
if (target.closest('fieldset').length > 0 && target.closest('fieldset').find('legend').length > 0) {
const legend = target.closest('fieldset').find('legend');
legend.html(`${asterisk} ${legend.html()}`);
// If the element is a radio button or checkbox place the asterisk after the label
} else if (target.is('[input="checkbox"]') || target.is('[input="radio"]')) {
target.after(asterisk);
// Else place the asterisk before the corresponding label
} else {
const label = target.closest('.form-group').find('label');
if (isObject(label)) {
$(label[0]).before(asterisk);
}
}
}
};
export default(el) => {
addAsterisk(el);
};
$(() => {
$('[aria-required=true]').each((idx, el) => {
addAsterisk(el);
});
});