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

docs(fw-select): added docs for setting/getting selected values #298

Merged
merged 6 commits into from
Nov 24, 2021
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
75 changes: 39 additions & 36 deletions packages/crayons-core/src/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,10 @@ export namespace Components {
"value": string;
}
interface FwListOptions {
/**
* Place a checkbox.
*/
"checkbox": boolean;
"clearFilter": () => Promise<void>;
/**
* Debounce timer for the search promise function.
Expand All @@ -278,10 +282,6 @@ export namespace Components {
*/
"filterText": any;
"getSelectedOptions": () => Promise<any>;
/**
* Place a checkbox.
*/
"isCheckbox": boolean;
/**
* Works with `multiple` enabled. Configures the maximum number of options that can be selected with a multi-select component.
*/
Expand Down Expand Up @@ -319,11 +319,14 @@ export namespace Components {
*/
"selectedOptions": any[];
"setSelectedOptions": (options: any[]) => Promise<any>;
"setSelectedValues": (values: string[]) => Promise<any>;
/**
* Pass an array of string in case of multi-select or string for single-select.
*/
"setSelectedValues": (values: string | string[]) => Promise<any>;
/**
* Value of the option that is displayed as the default selection, in the list box. Must be a valid value corresponding to the fw-select-option components used in Select.
*/
"value": any[];
"value": string | string[];
/**
* Standard is the default option without any graphics other options are icon and avatar which places either the icon or avatar at the beginning of the row. The props for the icon or avatar are passed as an object via the graphicsProps.
*/
Expand Down Expand Up @@ -508,6 +511,10 @@ export namespace Components {
"value"?: any | null;
}
interface FwSelect {
/**
* Place a checkbox.
*/
"checkbox": boolean;
/**
* Debounce timer for the search promise function.
*/
Expand All @@ -521,10 +528,6 @@ export namespace Components {
*/
"forceSelect": boolean;
"getSelectedItem": () => Promise<any>;
/**
* Place a checkbox.
*/
"isCheckbox": boolean;
/**
* Label displayed on the interface, for the component.
*/
Expand Down Expand Up @@ -553,6 +556,10 @@ export namespace Components {
* Allow to search for value. Default is true.
*/
"options": any;
/**
* Standard is the default option without any graphics other options are icon and avatar which places either the icon or avatar at the beginning of the row. The props for the icon or avatar are passed as an object via the graphicsProps.
*/
"optionsVariant": DropdownVariant;
/**
* Text displayed in the list box before an option is selected.
*/
Expand All @@ -578,7 +585,7 @@ export namespace Components {
*/
"selectedOptions": any[];
"setSelectedOptions": (options: any[]) => Promise<any>;
"setSelectedValues": (values: string[]) => Promise<any>;
"setSelectedValues": (values: string | string[]) => Promise<any>;
/**
* Theme based on which the list box is styled.
*/
Expand All @@ -599,12 +606,12 @@ export namespace Components {
* Value of the option that is displayed as the default selection, in the list box. Must be a valid value corresponding to the fw-select-option components used in Select.
*/
"value": any;
/**
* Standard is the default option without any graphics other options are icon and avatar which places either the icon or avatar at the beginning of the row. The props for the icon or avatar are passed as an object via the graphicsProps.
*/
"variant": DropdownVariant;
}
interface FwSelectOption {
/**
* Place a checkbox.
*/
"checkbox": boolean;
/**
* Sets the state of the option to disabled. The selected option is disabled and greyed out. If the attribute’s value is undefined, the value is set to false.
*/
Expand All @@ -625,10 +632,6 @@ export namespace Components {
* HTML content that is displayed as the option.
*/
"htmlContent"?: string;
/**
* Place a checkbox.
*/
"isCheckbox": boolean;
/**
* Alternate text displayed on the interface, in place of the actual HTML content.
*/
Expand Down Expand Up @@ -1446,6 +1449,10 @@ declare namespace LocalJSX {
"value"?: string;
}
interface FwListOptions {
/**
* Place a checkbox.
*/
"checkbox"?: boolean;
/**
* Debounce timer for the search promise function.
*/
Expand All @@ -1454,10 +1461,6 @@ declare namespace LocalJSX {
* The text to filter the options.
*/
"filterText"?: any;
/**
* Place a checkbox.
*/
"isCheckbox"?: boolean;
/**
* Works with `multiple` enabled. Configures the maximum number of options that can be selected with a multi-select component.
*/
Expand Down Expand Up @@ -1505,7 +1508,7 @@ declare namespace LocalJSX {
/**
* Value of the option that is displayed as the default selection, in the list box. Must be a valid value corresponding to the fw-select-option components used in Select.
*/
"value"?: any[];
"value"?: string | string[];
/**
* Standard is the default option without any graphics other options are icon and avatar which places either the icon or avatar at the beginning of the row. The props for the icon or avatar are passed as an object via the graphicsProps.
*/
Expand Down Expand Up @@ -1718,6 +1721,10 @@ declare namespace LocalJSX {
"value"?: any | null;
}
interface FwSelect {
/**
* Place a checkbox.
*/
"checkbox"?: boolean;
/**
* Debounce timer for the search promise function.
*/
Expand All @@ -1730,10 +1737,6 @@ declare namespace LocalJSX {
* If true, the user must select a value. The default value is not displayed.
*/
"forceSelect"?: boolean;
/**
* Place a checkbox.
*/
"isCheckbox"?: boolean;
/**
* Label displayed on the interface, for the component.
*/
Expand Down Expand Up @@ -1774,6 +1777,10 @@ declare namespace LocalJSX {
* Allow to search for value. Default is true.
*/
"options"?: any;
/**
* Standard is the default option without any graphics other options are icon and avatar which places either the icon or avatar at the beginning of the row. The props for the icon or avatar are passed as an object via the graphicsProps.
*/
"optionsVariant"?: DropdownVariant;
/**
* Text displayed in the list box before an option is selected.
*/
Expand Down Expand Up @@ -1818,12 +1825,12 @@ declare namespace LocalJSX {
* Value of the option that is displayed as the default selection, in the list box. Must be a valid value corresponding to the fw-select-option components used in Select.
*/
"value"?: any;
/**
* Standard is the default option without any graphics other options are icon and avatar which places either the icon or avatar at the beginning of the row. The props for the icon or avatar are passed as an object via the graphicsProps.
*/
"variant"?: DropdownVariant;
}
interface FwSelectOption {
/**
* Place a checkbox.
*/
"checkbox"?: boolean;
/**
* Sets the state of the option to disabled. The selected option is disabled and greyed out. If the attribute’s value is undefined, the value is set to false.
*/
Expand All @@ -1844,10 +1851,6 @@ declare namespace LocalJSX {
* HTML content that is displayed as the option.
*/
"htmlContent"?: string;
/**
* Place a checkbox.
*/
"isCheckbox"?: boolean;
/**
* Triggered when an option is selected.
*/
Expand Down
58 changes: 42 additions & 16 deletions packages/crayons-core/src/components/options-list/list-options.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class ListOptions {
/**
* Value of the option that is displayed as the default selection, in the list box. Must be a valid value corresponding to the fw-select-option components used in Select.
*/
@Prop({ reflect: true, mutable: true }) value = [];
@Prop({ reflect: true, mutable: true }) value: string | string[] = '';
/**
* Works with `multiple` enabled. Configures the maximum number of options that can be selected with a multi-select component.
*/
Expand All @@ -75,7 +75,7 @@ export class ListOptions {
/**
* Place a checkbox.
*/
@Prop() isCheckbox = false;
@Prop() checkbox = false;
/**
* Default option to be shown if the option doesn't match the filterText.
*/
Expand Down Expand Up @@ -127,7 +127,7 @@ export class ListOptions {
: [];
}
this.isInternalValueChange = true;
this.value = this.selectedOptionsState.map((options) => options.value);
this.setValue(this.selectedOptionsState);
}

@Method()
Expand All @@ -142,23 +142,27 @@ export class ListOptions {
async getSelectedOptions(): Promise<any> {
return this.selectedOptionsState;
}

/**
* Pass an array of string in case of multi-select or string for single-select.
*/
@Method()
async setSelectedValues(values: string[]): Promise<any> {
async setSelectedValues(values: string | string[]): Promise<any> {
if (this.options) {
this.selectedOptionsState = this.options.filter((option) =>
values.includes(option.value)
);
this.selectedOptionsState = this.options.filter((option) => {
return this.multiple
? values.includes(option.value)
: values === option.value;
});
this.isInternalValueChange = true;
this.value = values;
this.setValue(this.selectedOptionsState);
}
}

@Method()
async setSelectedOptions(options: any[]): Promise<any> {
this.selectedOptionsState = options;
this.isInternalValueChange = true;
this.value = options.map((option) => option.value);
this.setValue(options);
}

@Watch('options')
Expand Down Expand Up @@ -220,9 +224,11 @@ export class ListOptions {

setSelectedOptionsByValue(values) {
if (this.options) {
this.selectedOptionsState = this.options.filter((option) =>
values.includes(option.value)
);
this.selectedOptionsState = this.options.filter((option) => {
return this.multiple
? values.includes(option.value)
: values === option.value;
});
} else {
throw new Error('Options must be passed if value is set');
}
Expand All @@ -233,15 +239,30 @@ export class ListOptions {
return {
...option,
...{
isCheckbox: option.isCheckbox || this.isCheckbox,
checkbox: option.checkbox || this.checkbox,
variant: option.variant || this.variant,
selected: option.selected || this.value.includes(option.value),
disabled: option.disabled || this.value?.length >= this.max,
selected:
(this.multiple
? this.value.includes(option.value)
: this.value === option.value) || option.selected,
disabled:
option.disabled ||
(this.multiple && this.value?.length >= this.max),
},
};
});
}

setValue(options) {
if (options?.length > 0) {
this.value = this.multiple
? options.map((option) => option.value)
: options[0].value;
} else {
this.value = this.multiple ? [] : '';
}
}

setDataSource(dataSource) {
if (dataSource.length > 0) {
this.selectOptions = this.serializeData(dataSource);
Expand Down Expand Up @@ -273,6 +294,11 @@ export class ListOptions {
this.value = this.selectedOptionsState.map((option) => option.value);
} else if (this.value.length > 0) {
this.setSelectedOptionsByValue(this.value);
} else {
this.setValue([]);
}
if (this.multiple && typeof this.value === 'string') {
throw Error('value must be a array of string when multiple is true');
}
this.setDataSource(this.options);
}
Expand Down
8 changes: 4 additions & 4 deletions packages/crayons-core/src/components/options-list/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ The data-source and the visual variant for the list options can be altered via t

| Property | Attribute | Description | Type | Default |
| ----------------- | ---------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------- | ---------------------------- |
| `checkbox` | `checkbox` | Place a checkbox. | `boolean` | `false` |
| `debounceTimer` | `debounce-timer` | Debounce timer for the search promise function. | `number` | `300` |
| `filterText` | `filter-text` | The text to filter the options. | `any` | `undefined` |
| `isCheckbox` | `is-checkbox` | Place a checkbox. | `boolean` | `false` |
| `max` | `max` | Works with `multiple` enabled. Configures the maximum number of options that can be selected with a multi-select component. | `number` | `Number.MAX_VALUE` |
| `multiple` | `multiple` | Enables selection of multiple options. If the attribute’s value is undefined, the value is set to false. | `boolean` | `false` |
| `noDataText` | `no-data-text` | Text to be displayed when there is no data available in the select. | `string` | `'No Data available'` |
Expand All @@ -81,7 +81,7 @@ The data-source and the visual variant for the list options can be altered via t
| `searchText` | `search-text` | Placeholder to placed on the search text box. | `string` | `'Search...'` |
| `searchable` | `searchable` | Enables the input with in the popup for filtering the options. | `boolean` | `false` |
| `selectedOptions` | -- | The option that is displayed as the default selection, in the list box. Must be a valid value corresponding to the fw-select-option components used in Select. | `any[]` | `[]` |
| `value` | -- | Value of the option that is displayed as the default selection, in the list box. Must be a valid value corresponding to the fw-select-option components used in Select. | `any[]` | `[]` |
| `value` | `value` | Value of the option that is displayed as the default selection, in the list box. Must be a valid value corresponding to the fw-select-option components used in Select. | `string \| string[]` | `''` |
| `variant` | `variant` | Standard is the default option without any graphics other options are icon and avatar which places either the icon or avatar at the beginning of the row. The props for the icon or avatar are passed as an object via the graphicsProps. | `"avatar" \| "icon" \| "standard"` | `'standard'` |


Expand Down Expand Up @@ -125,9 +125,9 @@ Type: `Promise<any>`



### `setSelectedValues(values: string[]) => Promise<any>`

### `setSelectedValues(values: string | string[]) => Promise<any>`

Pass an array of string in case of multi-select or string for single-select.

#### Returns

Expand Down
Loading