- Add DatiTimeFormat ('12h'|'24h') and DatiDateOrder aliases to dati_types.ts;
replace numeric HourCycle throughout dati_engine so actx and tests compile
- Add missing logger field to TerraConfigState.opts
- Fix percent NumberField: use edit-mode formatter on focus/commit/watch.pre
- Fix test pages: Toolbar.Group value type (string), Progress tuple cast
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
main
parent
ffdbb79234
commit
ad1a1f8946
@ -0,0 +1,93 @@
|
||||
import { createTerraContext } from '$terra/utils/context';
|
||||
|
||||
import { type Active, type ActiveProps, readableActive } from '$terra/utils';
|
||||
|
||||
import type { TerraConfigPropsWithoutChildren } from './types';
|
||||
|
||||
|
||||
type TerraConfigStateProps = ActiveProps<TerraConfigPropsWithoutChildren>;
|
||||
|
||||
|
||||
|
||||
export const TerraConfigContext = createTerraContext<TerraConfigState>('TerraConfig');
|
||||
|
||||
|
||||
|
||||
export function getTerraConfig() {
|
||||
|
||||
const fallback = new TerraConfigState(null, {});
|
||||
|
||||
return TerraConfigContext.getOr(fallback).opts;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
export function useTerraConfig(opts: TerraConfigStateProps) {
|
||||
|
||||
return TerraConfigContext.set(new TerraConfigState(TerraConfigContext.getOr(null), opts));
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
export class TerraConfigState {
|
||||
|
||||
readonly opts: Required<TerraConfigStateProps>;
|
||||
|
||||
|
||||
|
||||
constructor(parent: TerraConfigState | null, opts: TerraConfigStateProps) {
|
||||
|
||||
const resolveConfigOption = createConfigResolver(parent, opts);
|
||||
|
||||
this.opts = {
|
||||
|
||||
defaultPortalTo: resolveConfigOption((config) => config.defaultPortalTo),
|
||||
|
||||
defaultLocale: resolveConfigOption((config) => config.defaultLocale),
|
||||
|
||||
defaultDir: resolveConfigOption((config) => config.defaultDir),
|
||||
|
||||
defaultDateOrder: resolveConfigOption((config) => config.defaultDateOrder),
|
||||
|
||||
defaultTimeFormat: resolveConfigOption((config) => config.defaultTimeFormat),
|
||||
|
||||
logger: resolveConfigOption((config) => config.logger),
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
type ConfigOptionGetter<T> = (config: TerraConfigStateProps) => Active<T> | undefined;
|
||||
|
||||
type ConfigOptionResolver = <T>(getter: ConfigOptionGetter<T>) => Active<T | undefined>;
|
||||
|
||||
|
||||
|
||||
function createConfigResolver(
|
||||
|
||||
parent: TerraConfigState | null,
|
||||
|
||||
currentOpts: TerraConfigStateProps
|
||||
|
||||
): ConfigOptionResolver {
|
||||
|
||||
return <T>(getter: ConfigOptionGetter<T>) => {
|
||||
return readableActive(() => {
|
||||
const value = getter(currentOpts)?.current;
|
||||
|
||||
if (value !== undefined) return value;
|
||||
|
||||
if (parent === null) return undefined;
|
||||
|
||||
return getter(parent.opts)?.current;
|
||||
});
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
Loading…
Reference in new issue