Skip to content

Commit b0736cb

Browse files
committed
readonly style + other fixes for forms
1 parent 3b7c6cf commit b0736cb

File tree

10 files changed

+166
-52
lines changed

10 files changed

+166
-52
lines changed

packages/uui-base/lib/mixins/FormControlMixin.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,35 @@ export declare abstract class FormControlMixinInterface extends LitElement {
1616
protected _value: FormDataEntryValue;
1717
protected _internals: any;
1818
protected abstract getFormElement(): HTMLElement | undefined;
19+
protected addValidator: (
20+
flagKey: FlagTypes,
21+
getMessageMethod: () => String,
22+
checkMethod: () => boolean
23+
) => void;
1924
pristine: boolean;
2025
required: boolean;
2126
requiredMessage: string;
2227
error: boolean;
2328
errorMessage: string;
2429
}
2530

31+
/* FlagTypes type options originate from:
32+
* https://developer.mozilla.org/en-US/docs/Web/API/ValidityState
33+
* */
2634
type FlagTypes =
27-
| 'valueMissing'
28-
| 'typeMismatch'
35+
| 'badInput'
36+
| 'customError'
2937
| 'patternMismatch'
30-
| 'tooLong'
31-
| 'tooShort'
38+
| 'rangeOverflow'
3239
| 'rangeUnderflow'
3340
| 'stepMismatch'
41+
| 'tooLong'
42+
| 'tooShort'
43+
| 'typeMismatch'
44+
| 'valueMissing'
3445
| 'badInput'
35-
| 'customError';
46+
| 'valid';
47+
3648
interface Validator {
3749
flagKey: FlagTypes;
3850
getMessage: () => String;

packages/uui-css/lib/custom-properties/interface.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
--uui-interface-border-hover: var(--uui-color-grey-dark);
2222
--uui-interface-border-disabled: var(--uui-color-grey-dimmed);
2323
--uui-interface-border-focus: var(--uui-color-grey-dark);
24+
--uui-interface-border-readonly: var(--uui-color-grey-light);
2425

2526
--uui-interface-border-style: solid;
2627
--uui-interface-font-weight: 400;

packages/uui-form-validation-message/lib/uui-form-validation-message.story.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ export const ForAnotherElement: Story = () =>
5353
required
5454
required-message="You must enter a phone number">
5555
</uui-input>
56+
<uui-textarea
57+
label="Textarea"
58+
name="textarea"
59+
value="Some long text that needs more space"
60+
minlength="10"
61+
maxlength="30"
62+
required>
63+
</uui-textarea>
5664
</div>
5765
<uui-form-validation-message for="myCustomScope">
5866
</uui-form-validation-message>`;

packages/uui-form/lib/uui-form.story.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,17 @@ export const Overview: Story = () => html` <form
7575
</uui-slider>
7676
</div>
7777
78+
<div style="margin-bottom: 15px;">
79+
<uui-textarea
80+
label="Textarea"
81+
name="textarea"
82+
value="Some long text that needs more space"
83+
minlength="10"
84+
maxlength="30"
85+
required>
86+
</uui-textarea>
87+
</div>
88+
7889
<div style="margin-bottom: 15px;">
7990
<input
8091
name="nativeCheckbox"

packages/uui-input/lib/uui-input.element.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,19 @@ export class UUIInputElement extends FormControlMixin(LitElement) {
7979
--uui-input-background-color-disabled,
8080
var(--uui-interface-surface-disabled)
8181
);
82-
border: 1px solid
83-
var(
84-
--uui-input-border-color-disabled,
85-
var(--uui-interface-border-disable)
86-
);
82+
border-color: var(
83+
--uui-input-border-color-disabled,
84+
var(--uui-interface-surface-disabled)
85+
);
8786
8887
color: var(--uui-interface-contrast-disabled);
8988
}
89+
:host([readonly]) {
90+
border-color: var(
91+
--uui-input-border-color-readonly,
92+
var(--uui-interface-border-readonly)
93+
);
94+
}
9095
9196
:host(:not([pristine]):invalid),
9297
/* polyfill support */
@@ -97,7 +102,7 @@ export class UUIInputElement extends FormControlMixin(LitElement) {
97102
input {
98103
font-family: inherit;
99104
padding: var(--uui-size-1) var(--uui-size-space-3);
100-
font-size: 15px;
105+
font-size: inherit;
101106
color: inherit;
102107
border-radius: 0;
103108
box-sizing: border-box;
@@ -111,7 +116,7 @@ export class UUIInputElement extends FormControlMixin(LitElement) {
111116
input::placeholder {
112117
transition: opacity 120ms;
113118
}
114-
input:focus::placeholder {
119+
:host(:not([readonly])) input:focus::placeholder {
115120
opacity: 0;
116121
}
117122
@@ -138,6 +143,15 @@ export class UUIInputElement extends FormControlMixin(LitElement) {
138143
@property({ type: Boolean, reflect: true })
139144
disabled = false;
140145

146+
/**
147+
* Sets the input to readonly mode, meaning value cannot be changed but still able to read and select its content.
148+
* @type {boolean}
149+
* @attr
150+
* @default false
151+
*/
152+
@property({ type: Boolean, reflect: true })
153+
readonly = false;
154+
141155
/**
142156
* Label for input element.
143157
* @type {string}
@@ -219,6 +233,7 @@ export class UUIInputElement extends FormControlMixin(LitElement) {
219233
placeholder=${this.placeholder}
220234
aria-label=${this.label}
221235
.disabled=${this.disabled}
236+
?readonly=${this.readonly}
222237
@input=${this._onInput}
223238
@change=${this._onChange} />
224239
${this.renderAppend()}

packages/uui-input/lib/uui-input.story.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ export default {
1111
component: 'uui-input',
1212
id: 'uui-input',
1313
args: {
14+
disabled: false,
15+
readonly: false,
1416
value: '',
1517
label: 'Label',
1618
placeholder: 'Placeholder',
@@ -39,6 +41,7 @@ const Template: Story = props =>
3941
html`
4042
<uui-input
4143
.disabled=${props.disabled}
44+
.readonly=${props.readonly}
4245
.error=${props.error}
4346
.label=${props.label}
4447
.type=${props.type}
@@ -100,6 +103,7 @@ export const PrependAndAppend: Story = props =>
100103
html`
101104
<uui-input
102105
.disabled=${props.disabled}
106+
.readonly=${props.readonly}
103107
.error=${props.error}
104108
.hideLabel=${props.hideLabel}
105109
.label=${props.label}
@@ -144,6 +148,7 @@ export const MultipleInputs: Story = props =>
144148
html`
145149
<uui-input
146150
.disabled=${props.disabled}
151+
.readonly=${props.readonly}
147152
.error=${props.error}
148153
.hideLabel=${props.hideLabel}
149154
.label=${props.label}

packages/uui-input/lib/uui-input.test.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ describe('UuiInputElement', () => {
2626
it('has a name property', () => {
2727
expect(element).to.have.property('name');
2828
});
29-
it('has a error property', () => {
30-
expect(element).to.have.property('error');
31-
});
3229
it('has a type property', () => {
3330
expect(element).to.have.property('type');
3431
});
@@ -41,6 +38,21 @@ describe('UuiInputElement', () => {
4138
it('has a disabled property', () => {
4239
expect(element).to.have.property('disabled');
4340
});
41+
it('has a readOnly property', () => {
42+
expect(element).to.have.property('readOnly');
43+
});
44+
it('has a error property', () => {
45+
expect(element).to.have.property('error');
46+
});
47+
it('has a errorMessage property', () => {
48+
expect(element).to.have.property('errorMessage');
49+
});
50+
it('has a required property', () => {
51+
expect(element).to.have.property('required');
52+
});
53+
it('has a requiredMessage property', () => {
54+
expect(element).to.have.property('requiredMessage');
55+
});
4456

4557
it('disable property set input to disabled', async () => {
4658
element.disabled = true;

packages/uui-radio/lib/uui-radio.element.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ export class UUIRadioElement extends LitElement {
167167
return this._checked;
168168
}
169169
public set checked(value) {
170+
const oldValue = this._checked;
170171
this._checked = value;
171172
if (value === true) {
172173
this.setAttribute('aria-checked', '');
@@ -177,6 +178,7 @@ export class UUIRadioElement extends LitElement {
177178
this.setAttribute('tabindex', '-1');
178179
this.removeAttribute('aria-checked');
179180
}
181+
this.requestUpdate('checked', oldValue);
180182
}
181183
private _checked = false;
182184

0 commit comments

Comments
 (0)