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

Ensure Correct COT Selection in QB When Sharing Formatters #6252

Open
wants to merge 9 commits into
base: production
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ function eventHandlerForToMany(related, field) {
switch (event) {
case 'saverequired': {
this.handleChanged();
if (related.models?.[0]?.specifyTable?.name !== 'CollectionRelationship') {this.trigger.apply(this, args)}
if (
related.models?.[0]?.specifyTable?.name !== 'CollectionRelationship'
) {
this.trigger.apply(this, args);
}
break;
}
case 'change':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@ type SimpleFormatter = {
readonly isDefault: boolean;
};

export const formatterSeparator = '|||';

export function createCompositeFormatter(name: string, index: number): string {
return `${name}${formatterSeparator}${index}`;
}

export function parseFormatterValue(value: string): { readonly name: string; readonly index: number } | null {
const [name, indexString] = value.split(formatterSeparator);
const index = Number.parseInt(indexString, 10);

if (!name || isNaN(index)) return null;

return { name, index };
}

export function QueryFieldRecordFormatter({
type,
tableName,
Expand Down Expand Up @@ -140,9 +155,24 @@ function FormatSelect({
readonly onChange: ((formatter: string | undefined) => void) | undefined;
}): JSX.Element | null {
const [formatterSelectIsOpen, setFormatterSelect] = React.useState(false);

const [selectedIndex, setSelectedIndex] = React.useState<number | null>(null);
const id = useId('formatters-selection');

const compositeValue = React.useMemo(() => {
if (availableFormatters && availableFormatters.length > 0) {
if (selectedIndex !== null && availableFormatters[selectedIndex]) {
return createCompositeFormatter(availableFormatters[selectedIndex].name, selectedIndex);
}
const foundIndex = availableFormatters.findIndex(
(f) => f.name === currentFormat
);
if (foundIndex !== -1) {
return createCompositeFormatter(availableFormatters[foundIndex].name, foundIndex);
}
}
return '';
}, [selectedIndex, availableFormatters, currentFormat]);
Comment on lines +174 to +190
Copy link
Contributor

Choose a reason for hiding this comment

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

Same thing as the prior comment! With the grouping of the CollectionObjectTypes by format, do we need to keep track of which option the user has selected anymore?


return availableFormatters === undefined ? (
typeof currentFormat === 'string' ? (
<>{commonText.loading()}</>
Expand Down Expand Up @@ -173,13 +203,26 @@ function FormatSelect({
className={customSelectElementBackground}
disabled={handleChange === undefined}
id={id('list')}
value={currentFormat}
onValueChange={handleChange}
value={compositeValue}
onValueChange={(value) => {
const parsed = parseFormatterValue(value);
if (parsed) {
setSelectedIndex(parsed.index);
if (handleChange) {
handleChange(parsed.name);
}
} else {
setSelectedIndex(null);
if (handleChange) {
handleChange(value);
}
}
}}
>
<option />
{availableFormatters.map(({ name, title, isDefault }, index) => (
<option key={index} value={name}>
{`${title} ${isDefault ? resourcesText.defaultInline() : ''}`}
<option value="" />
{availableFormatters.map((formatter, index) => (
<option key={index} value={createCompositeFormatter(formatter.name, index)}>
{`${formatter.title} ${formatter.isDefault ? resourcesText.defaultInline() : ''}`}
</option>
))}
{currentFormat !== undefined &&
Expand Down
Loading