Skip to content
Open
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
63 changes: 32 additions & 31 deletions src/components/hv-picker-field/index.ios.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ import { View } from 'react-native';
* opens a system dialog.
*/
export default class HvPickerField extends PureComponent<HvComponentProps> {
static namespaceURI = Namespaces.HYPERVIEW;
pickerItemsCache: Array<{ label: string | null | undefined; value: string | null | undefined }> | null = null;

static namespaceURI = Namespaces.HYPERVIEW;
static localName = LOCAL_NAME.PICKER_FIELD;

static localNameAliases = [];

static getFormInputValues = (element: Element): Array<[string, string]> => {
Expand All @@ -37,50 +37,51 @@ export default class HvPickerField extends PureComponent<HvComponentProps> {
getPickerInitialValue = (): string => {
const value = this.getValue();
const pickerItems: Element[] = this.getPickerItems();
if (
pickerItems
.map((item: Element) => item.getAttribute('value'))
.includes(value)
) {
const valueExists = pickerItems.some(
item => item.getAttribute('value') === value,
);
if (valueExists) {
return value;
}
if (pickerItems.length > 0) {
return pickerItems[0].getAttribute('value') || '';
}
return '';
return pickerItems.length > 0
? pickerItems[0].getAttribute('value') || ''
: '';
Comment on lines +40 to +48
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Shaykoo your branch needs a rebase with the latest master, this is your previous change that got merged in #954

};

/**
* Returns a string representing the value in the field.
*/
getValue = (): string => this.props.element.getAttribute('value') || '';

/**
*
* Cache the value-label pairs when the picker items are fetched.
*/
getPickerItemsCache = () => {
if (!this.pickerItemsCache) {
const pickerItemElements = this.props.element.getElementsByTagNameNS(
Namespaces.HYPERVIEW,
LOCAL_NAME.PICKER_ITEM,
);
this.pickerItemsCache = Array.from(pickerItemElements).map(item => ({
value: item.getAttribute('value'),
label: item.getAttribute('label'),
}));
}
return this.pickerItemsCache;
};

/**
* Gets the label from the picker items for the given value.
* If the value doesn't have a picker item, returns null.
*/
getLabelForValue = (value: DOMString): string | null | undefined => {
const pickerItemElements: HTMLCollectionOf<Element> = this.props.element.getElementsByTagNameNS(
Namespaces.HYPERVIEW,
LOCAL_NAME.PICKER_ITEM,
getLabelForValue = (value: DOMString): string | null => {
const item = this.getPickerItemsCache().find(
item => item.value === value,
);

let item: Element | null | undefined = null;
for (let i = 0; i < pickerItemElements.length; i += 1) {
const pickerItemElement:
| Element
| null
| undefined = pickerItemElements.item(i);
if (
pickerItemElement &&
pickerItemElement.getAttribute('value') === value
) {
item = pickerItemElement;
break;
}
}
return item ? item.getAttribute('label') : null;
return item ? item.label ?? null : null;
};


/**
* Returns a string representing the value in the picker.
Expand Down