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

feat: Combo Option to add item at the top of the list #5270

Merged
merged 1 commit into from
Sep 29, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions docs/zh-CN/components/form/combo.md
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,7 @@ combo 还有一个作用是增加层级,比如返回的数据是一个深层
| joinValues | `boolean` | `true` | 默认为 `true` 当扁平化开启的时候,是否用分隔符的形式发送给后端,否则采用 array 的方式。 |
| delimiter | `string` | `false` | 当扁平化开启并且 joinValues 为 true 时,用什么分隔符。 |
| addable | `boolean` | `false` | 是否可新增 |
| addattop | `boolean` | `false` | 在顶部添加 |
| removable | `boolean` | `false` | 是否可删除 |
| deleteApi | [API](../../../docs/types/api) | | 如果配置了,则删除前会发送一个 api,请求成功才完成删除 |
| deleteConfirmText | `string` | `"确认要删除?"` | 当配置 `deleteApi` 才生效!删除时用来做用户确认 |
Expand Down
16 changes: 15 additions & 1 deletion packages/amis/src/renderers/Form/Combo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ export interface ComboControlSchema extends FormBaseControlSchema {
*/
addable?: boolean;

/**
* Add at top
*/
addattop?: boolean;

/**
* 数组输入框的子项
*/
Expand Down Expand Up @@ -459,7 +464,7 @@ export default class ComboControl extends React.Component<ComboProps> {
}

addItemWith(condition: ComboCondition) {
const {flat, joinValues, delimiter, scaffold, disabled, submitOnChange} =
const {flat, joinValues, addattop, delimiter, scaffold, disabled, submitOnChange} =
this.props;

if (disabled) {
Expand All @@ -481,13 +486,18 @@ export default class ComboControl extends React.Component<ComboProps> {
value = value.join(delimiter || ',');
}

if (addattop === true){
value.unshift(value.pop());
}

this.props.onChange(value, submitOnChange, true);
}

async addItem() {
const {
flat,
joinValues,
addattop,
delimiter,
scaffold,
disabled,
Expand Down Expand Up @@ -527,6 +537,10 @@ export default class ComboControl extends React.Component<ComboProps> {
value = value.join(delimiter || ',');
}

if (addattop === true){
value.unshift(value.pop());
}

this.props.onChange(value, submitOnChange, true);
}

Expand Down