Skip to content

Commit

Permalink
Merge branch 'main' into selectively-validate-empty-values
Browse files Browse the repository at this point in the history
  • Loading branch information
garrettmflynn committed Nov 2, 2023
2 parents 6c031eb + 13db3c4 commit 03a8d8c
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 13 deletions.
8 changes: 8 additions & 0 deletions schemas/base-metadata.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ export const preprocessMetadataSchema = (schema: any = baseMetadataSchema) => {
// Add unit to weight
schema.properties.Subject.properties.weight.unit = 'kg'

schema.properties.Subject.properties.sex.enumLabels = {
M: 'Male',
F: 'Female',
U: 'Unknown',
O: 'Other'
}


// Override description of keywords
schema.properties.NWBFile.properties.keywords.description = 'Terms to describe your dataset (e.g. Neural circuits, V1, etc.)' // Add description to keywords
return schema
Expand Down
5 changes: 4 additions & 1 deletion src/renderer/src/stories/JSONSchemaInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,10 @@ export class JSONSchemaInput extends LitElement {
>
<option disabled selected value>${info.placeholder ?? "Select an option"}</option>
${info.enum.map(
(item, i) => html`<option value=${i} ?selected=${this.value === item}>${item}</option>`
(item, i) =>
html`<option value=${i} ?selected=${this.value === item}>
${info.enumLabels?.[item] ?? item}
</option>`
)}
</select>
`;
Expand Down
53 changes: 41 additions & 12 deletions src/renderer/src/stories/Table.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ export class Table extends LitElement {

// Enumerate Possible Values
if (colInfo.enum) {
info.source = colInfo.enum;
info.source = colInfo.enumLabels ? Object.values(colInfo.enumLabels) : colInfo.enum;
if (colInfo.strict === false) info.type = "autocomplete";
else info.type = "dropdown";
}
Expand Down Expand Up @@ -270,11 +270,14 @@ export class Table extends LitElement {
const isRequired = this.isRequired(k);

const validator = async function (value, callback) {

const validateEmptyCells = ogThis.validateEmptyCells;
const willValidate =
validateEmptyCells === true ||
(Array.isArray(validateEmptyCells) && validateEmptyCells.includes(k));

value = ogThis.#getValue(value, colInfo);

// Clear empty values if not validated
if (!value && !willValidate) {
ogThis.#handleValidationResult(
Expand Down Expand Up @@ -404,8 +407,10 @@ export class Table extends LitElement {

if (this.table) this.table.destroy();

console.log("Rendered data", this.#getRenderedData(data));

const table = new Handsontable(div, {
data,
data: this.#getRenderedData(data),
// rowHeaders: rowHeaders.map(v => `sub-${v}`),
colHeaders: displayHeaders,
columns,
Expand Down Expand Up @@ -449,17 +454,18 @@ export class Table extends LitElement {

const isUserUpdate = initialCellsToUpdate <= validated;

value = this.#getValue(value, entries[header]);

// Transfer data to object (if valid)
if (isValid) {
if (header === this.keyColumn) {
if (value && value !== rowName) {
const old = target[rowName] ?? {};
this.data[value] = old;
delete target[rowName];
delete unresolved[row];
rowHeaders[row] = value;
}
if (header === this.keyColumn) {
if (isValid && value && value !== rowName) {
const old = target[rowName] ?? {};
this.data[value] = old;
delete target[rowName];
delete unresolved[row];
rowHeaders[row] = value;
}

}

// Update data on passed object
Expand Down Expand Up @@ -522,8 +528,31 @@ export class Table extends LitElement {
data.forEach((row, i) => this.#setRow(i, row));
}

#getRenderedValue = (value, colInfo) => {
// Handle enums
if (colInfo.enumLabels) return colInfo.enumLabels[value] ?? value;
return value;
};

#getRenderedData = (data) => {
return Object.values(data).map((row) =>
row.map((value, j) => this.#getRenderedValue(value, this.schema.properties[this.colHeaders[j]]))
);
};

#getValue = (value, colInfo) => {
// Handle enums
if (colInfo.enumLabels)
return Object.keys(colInfo.enumLabels).find((k) => colInfo.enumLabels[k] === value) ?? value;

return value;
};

#setRow(row, data) {
data.forEach((value, j) => this.table.setDataAtCell(row, j, value));
data.forEach((value, j) => {
value = this.#getRenderedValue(value, this.schema.properties[this.colHeaders[j]]);
this.table.setDataAtCell(row, j, value);
});
}

#handleValidationResult = (result, row, prop) => {
Expand Down

0 comments on commit 03a8d8c

Please sign in to comment.