ProgressNS / nativescript-ui-feedback

This repository is used for customer feedback regarding Telerik UI for NativeScript. The issues system here is used by customers who want to submit their feature requests or vote for existing ones.
Other
115 stars 21 forks source link

Improve API of Dialogs (action) #1366

Open jscti opened 4 years ago

jscti commented 4 years ago

Using an actions dialog, the actions list only support a string of arrays .. 2 problems :

Why don't have a basic approach like a good old HTML select option, with both label: string and value: any ?

Actual code, not very happy with it, especially the action().then:

const LESSON_TYPES = [{id:0, label: 'action_0_translation_key'}, {id:1, label: 'action_1_translation_key'}];

let options = {
    title: this.translate.instant('LESSON.START.TITTLE_TYPE'),
    cancelButtonText: this.translate.instant('GENERAL.CANCEL'),
    actions: LESSON_TYPES.map(i => this.translate.instant('ACTION_TRANSLATION_FOR_KEY__' + i.label))
};

action(options).then((result) => {
    if (result !== this.translate.instant('GENERAL.CANCEL')) { // wtf ...
        selectedLesson = LESSON_TYPE.find(i => this.translate.instant('ACTION_TRANSLATION_FOR_KEY__' + i.label) === result);
    }
});

Code that would be so much better :

const LESSON_TYPES = [{id:0, label: 'action_0_translation_key'}, {id:1, label: 'action_1_translation_key'}];

let options = {
    title: this.translate.instant('LESSON.START.TITTLE_TYPE'),
    cancelButtonText: this.translate.instant('GENERAL.CANCEL'),
    actions: LESSON_TYPES,
    // if labelCallback is provided : call it on the action item. Else, directly action item (that should be a string)
    labelCallback : i => this.translate.instant('ACTION_TRANSLATION_FOR_KEY__' + i.label)
};

action(options).then((result) => {
    if (result) { // if dialog canceled, result should be null or something but not its own button label
        selectedLesson = result;
    }
});
AchrafBardan commented 2 years ago

Found a good solution:

const options: Record<string, any> = {
  'distance': 'Distance ascending',
  '-price': 'Price ascending',
}

Dialogs.action({
  message: 'Sort by',
  cancelButtonText: 'Cancel',
  actions: Object.values(options),
}).then((result) => {
  if (result) {
    const key = Object.keys(options).find(key => options[key] === result)
    console.log(key);
  }
});