Skip to content

Commit

Permalink
feat: select support readonly
Browse files Browse the repository at this point in the history
  • Loading branch information
igauch authored and fengtianze committed Jan 16, 2024
1 parent 5158fd4 commit 4504ba8
Show file tree
Hide file tree
Showing 13 changed files with 55 additions and 24 deletions.
5 changes: 5 additions & 0 deletions .changeset/beige-bananas-grin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@alauda/ui': minor
---

- feat: select support readonly
6 changes: 4 additions & 2 deletions src/form/common-form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,16 @@ export class CommonFormControl<V, M = V> implements ControlValueAccessor {
}

set disabled(val: boolean | '') {
this._disabled = coerceAttrBoolean(val);
this._disabled = this._readonly || coerceAttrBoolean(val);
}

@Input()
get readonly() {
return this.disabled;
return this._readonly;
}

set readonly(val) {
this._readonly = coerceAttrBoolean(val);
this.disabled = val;
}

Expand All @@ -56,6 +57,7 @@ export class CommonFormControl<V, M = V> implements ControlValueAccessor {
protected onTouched: () => void;
private _propValue: V;
private _disabled = false;
private _readonly: boolean;

model: M = null;
model$ = new ReplaySubject<M>(1);
Expand Down
12 changes: 8 additions & 4 deletions src/input/input.component.scss
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,24 @@
@include outline-shadow(primary);
}

&[readonly] {
&[disable],
&[disabled] {
background-color: use-rgb(n-8);
border-color: use-rgb(n-7) !important;
cursor: default;
}

&[disabled] {
background-color: use-rgb(n-8);
border-color: use-rgb(n-7) !important;
color: use-text-color(disabled);
-webkit-text-fill-color: use-text-color(disabled);
cursor: not-allowed;
}

&[readonly] {
color: use-text-color(main);
-webkit-text-fill-color: use-text-color(main);
cursor: default;
}

&--large {
height: use-var(inline-height-l);
font-size: use-var(font-size-l);
Expand Down
9 changes: 3 additions & 6 deletions src/paginator/__snapshots__/paginator.component.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,8 @@ exports[`PaginatorComponent should change page size correctly 1`] = `
<input
aui-input=""
autocomplete="off"
class="aui-select__input aui-input aui-input--medium"
class="aui-select__input aui-input aui-input--medium aui-select__input-inaction"
placeholder=""
readonly=""
/>
<div
class="aui-input aui-input--medium aui-select__label-container"
Expand Down Expand Up @@ -415,9 +414,8 @@ exports[`PaginatorComponent should render current template 1`] = `
<input
aui-input=""
autocomplete="off"
class="aui-select__input aui-input aui-input--medium"
class="aui-select__input aui-input aui-input--medium aui-select__input-inaction"
placeholder=""
readonly=""
/>
<div
class="aui-input aui-input--medium aui-select__label-container"
Expand Down Expand Up @@ -702,10 +700,9 @@ exports[`PaginatorComponent should render current template 2`] = `
<input
aui-input=""
autocomplete="off"
class="aui-select__input aui-input aui-input--medium"
class="aui-select__input aui-input aui-input--medium aui-select__input-inaction"
disabled=""
placeholder=""
readonly=""
/>
<div
class="aui-input aui-input--medium aui-select__label-container"
Expand Down
2 changes: 1 addition & 1 deletion src/select/base-select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export abstract class BaseSelect<T, V = T>
return this.tooltipRef.isCreated;
}

get inputReadonly() {
get inaction() {
return !(this.filterable && this.opened);
}

Expand Down
9 changes: 8 additions & 1 deletion src/select/multi-select/multi-select.component.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
<div
[class]="rootClass"
[ngClass]="{
isDisabled: disabled,
isFocused: focused,
isClearable: displayClearBtn,
withHeightLimit: maxRowCount > 0,
isReadonly: readonly
}"
#selectRef
#tooltipRef="auiTooltip"
[auiTooltip]="templateRef"
Expand Down Expand Up @@ -53,7 +60,7 @@
<input
#inputRef
autocomplete="off"
[readonly]="inputReadonly"
[readonly]="readonly"
[class]="inputClass"
(focus)="onInputFocus()"
(blur)="onInputBlur()"
Expand Down
9 changes: 9 additions & 0 deletions src/select/multi-select/multi-select.component.scss
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,13 @@ $block: aui-multi-select;
.aui-tag--info {
background-color: use-rgb(n-10);
}

&.isReadonly {
cursor: default;

.aui-tag {
color: use-text-color(main);
background-color: use-rgb(n-10);
}
}
}
8 changes: 2 additions & 6 deletions src/select/multi-select/multi-select.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,7 @@ export class MultiSelectComponent<T = unknown>

get rootClass() {
const size = this.size || ComponentSize.Medium;
return `aui-input ${this.bem.block(size)} ${
this.disabled ? 'isDisabled' : ''
} ${this.focused ? 'isFocused' : ''} ${
this.displayClearBtn ? 'isClearable' : ''
} ${this.maxRowCount > 0 ? 'withHeightLimit' : ''}`;
return `aui-input ${this.bem.block(size)}`;
}

get tagSize() {
Expand All @@ -152,7 +148,7 @@ export class MultiSelectComponent<T = unknown>

get inputClass() {
return `${this.bem.element('input', {
hidden: this.inputReadonly,
hidden: this.inaction,
})} aui-tag aui-tag--${this.tagSize}`;
}

Expand Down
4 changes: 3 additions & 1 deletion src/select/select.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@
<input
#inputRef
class="aui-select__input"
[class.aui-select__input-inaction]="inaction"
autocomplete="off"
aui-input
[disabled]="disabled"
[size]="size"
[readonly]="inputReadonly"
[readonly]="readonly"
[placeholder]="!(hasSelected$ | async) ? placeholder : ''"
(input)="onInput($event)"
(keydown)="onKeyDown($event)"
Expand All @@ -46,6 +47,7 @@
*ngIf="(selectedOption$ | async) && !filterString"
class="aui-select__label-container aui-input aui-input--{{ size }}"
[attr.disabled]="disabled ? true : null"
[attr.readonly]="readonly ? true : null"
>
<div class="aui-select__label">
<ng-container *ngIf="(selectedOption$ | async).label as optionLabel">
Expand Down
4 changes: 2 additions & 2 deletions src/select/select.component.scss
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
@include text-overflow;
}

&__input.aui-input[readonly] {
&__input-inaction {
background-color: use-rgb(main-bg);
border-color: use-rgb(n-7) !important;
cursor: text;
Expand All @@ -41,7 +41,7 @@
}
}

&__input.aui-input[disabled] {
&__input[disabled] {
background-color: use-rgb(n-8);
border-color: use-rgb(n-7);
}
Expand Down
9 changes: 8 additions & 1 deletion stories/select/basic.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { SelectModule } from 'src/select';
[disabled]="disabled"
[clearable]="clearable"
[filterable]="filterable"
[readonly]="readonly"
placeholder="select a value"
>
<ng-container *auiOptionContent>
Expand Down Expand Up @@ -59,9 +60,15 @@ export default class SelectBasicComponent {
@Input()
filterable = true;

/**
* 是否只读
*/
@Input()
readonly = false;

arr = Array.from({ length: 11 })
.fill(null)
.map((_, i) => `option${i + 1}`);

value = '';
value = 'option1';
}
1 change: 1 addition & 0 deletions stories/select/basic.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ export const Basic: Story = {
loading: false,
clearable: false,
filterable: true,
readonly: false,
},
};
1 change: 1 addition & 0 deletions stories/select/select.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import * as multiple from './multiple.stories';
| loading | boolean | false | 是否加载中 |
| clearable | boolean | false | 选项是否可清除 |
| filterable | boolean | true | 是否支持选项过滤 |
| readonly | boolean | false | 是否只读 |
| defaultFirstOption | boolean | true | 是否第一项作为默认选项 |
| lazy | boolean | true | 关闭候选项时是否缓存 Option 组件 |
| filterFn | OptionFilterFn | Array.property.includes | 自定义过滤方法,filterable 为 true 时设置 |
Expand Down

0 comments on commit 4504ba8

Please sign in to comment.