-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ContentPane.tsx
1140 lines (1071 loc) · 35.8 KB
/
ContentPane.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { css, SerializedStyles } from '@emotion/react';
import * as React from 'react';
import { useState, ReactElement, ReactNode } from 'react';
import ArrowRightIcon from '@mui/icons-material/ArrowRight';
import ArrowBackIcon from '@mui/icons-material/ArrowBack';
import toPath from 'lodash/toPath';
import { Field, Formik, useField, useFormikContext } from 'formik';
import {
Typography,
Paper,
List,
Divider,
ListItem,
ListItemText,
ListItemSecondaryAction,
IconButton,
FormControlLabel,
Radio,
RadioGroup,
Button,
ListItemButton,
MenuItem,
Tooltip,
useRadioGroup,
ToggleButton,
InputAdornment,
} from '@mui/material';
import {
TextField,
Switch,
Checkbox,
Select as FormikMuiSelect,
ToggleButtonGroup,
} from 'formik-mui';
import { HighlightSearchTerms } from './HighlightSearchTerms';
import { FilterForSearchText } from './FilterForSearch';
import { SearchContext } from './SearchContextProvider';
type valueGetter = () => Object;
const disabledGrey = 'rgba(5, 1, 1, 0.26)';
const secondaryGrey = 'rgba(0, 0, 0, 0.54)';
const FocusPageContext = React.createContext({
focussedSubPagePath: '',
setFocussedSubPagePath: (p: string) => {},
});
export const ContentPane: React.FunctionComponent<
React.PropsWithChildren<{
// this is the whole settings object that we are editing
initialValues: object;
currentGroupIndex?: number;
children:
| React.ReactElement<typeof ConfigrGroup>
| React.ReactElement<typeof ConfigrGroup>[];
setValueGetter?: (vg: valueGetter) => void;
onChange?: (currentValues: any) => void;
}>
> = (props) => {
// We allow a single level of nesting (see ConfigrSubPage), that is all that is found in Chrome Settings.
// A stack would be easy but it would put some strain on the UI to help the user not be lost.
const [focussedSubPagePath, setFocussedSubPagePath] = useState('');
const valuesToReportRef = React.useRef(props.initialValues);
const valuesToReportJsonRef = React.useRef(JSON.stringify(props.initialValues));
const onChangeWrapper = (newValues: any) => {
if (!props.onChange) return;
let valueToReport = newValues;
if (valueToReport[kOverrideValuePrefix]) {
// Note: this is a shallow clone which means after the top level it's pointing to
// the original objects. This is fine, because we're only going to use it to remove
// a single property from the top level.
valueToReport = { ...newValues };
delete valueToReport[kOverrideValuePrefix];
}
// Note: If we start using text inputs, we wonder if the speed of this could
// become a problem as it will be stringifying the whole object on every keystroke.
// But this is a common strategy for solving this "did something really change"
// problem in react. If it were a problem we could perhaps switch to onBlur
// instead of onChange for ConfigrInput with text.
const newStringValues = JSON.stringify(valueToReport);
// It's very important that we don't call onChange with a new cloned
// object each time, or we're likely to get into an infinite loop of re-renders,
// as some parent re-renders this whole component when its settings state changes.
// So we use the two ref objects to make sure we pass the same instance each time
// unless the JSON has changed.
// This also allows us to avoid calling onChange at all if nothing has changed.
if (newStringValues === valuesToReportJsonRef.current) return;
valuesToReportJsonRef.current = newStringValues;
valuesToReportRef.current = valueToReport;
props.onChange(valuesToReportRef.current);
};
return (
<FocusPageContext.Provider
value={{
focussedSubPagePath: focussedSubPagePath,
setFocussedSubPagePath: setFocussedSubPagePath,
}}
>
<Formik initialValues={props.initialValues} onSubmit={(values) => {}}>
{({
values,
errors,
touched,
handleChange,
handleBlur,
handleSubmit,
isSubmitting,
}) => {
if (props.setValueGetter)
props.setValueGetter(() => {
return values;
});
onChangeWrapper(values);
return (
<form
onSubmit={handleSubmit}
css={css`
flex-grow: 1;
`}
>
<VisibleGroups
currentGroup={props.currentGroupIndex}
focussedSubPagePath={focussedSubPagePath}
>
{props.children}
</VisibleGroups>
</form>
);
}}
</Formik>
</FocusPageContext.Provider>
);
};
const VisibleGroups: React.FunctionComponent<
React.PropsWithChildren<{
currentGroup?: number;
focussedSubPagePath?: string;
children:
| React.ReactElement<typeof ConfigrGroup>
| React.ReactElement<typeof ConfigrGroup>[];
}>
> = (props) => {
return (
<SearchContext.Consumer>
{({ searchString }) => {
return (
<div
id="groups"
css={css`
//overflow-y: scroll; //allows us to scroll the groups without
//scrolling the heading tabs
overflow-y: auto;
`}
>
{searchString ? (
<HighlightSearchTerms
searchString={searchString}
focussedSubPagePath={props.focussedSubPagePath}
>
{props.children}
</HighlightSearchTerms>
) : (
React.Children.toArray(props.children).filter(
(c: React.ReactNode, index: number) => index === props.currentGroup,
)
)}
</div>
);
}}
</SearchContext.Consumer>
);
};
export const ConfigrGroup: React.FunctionComponent<
React.PropsWithChildren<{
label: string;
description?: string | React.ReactNode;
// use hasSubgroups when this contains ConfigrSubGroups that provide their own background
level?: undefined | 1 | 2;
}>
> = (props) => {
return (
<FilterForSearchText {...props} kids={props.children}>
<div
className="indentIfInSubPage"
css={css`
//margin-top: 21px !important;
margin-bottom: 12px !important;
`}
>
<Typography variant={props.level === 2 ? 'h3' : 'h2'}>{props.label}</Typography>
<Typography variant={'caption'}>
{descriptionToReact(props.description)}
</Typography>
</div>
{props.level === 1 ? (
<div className="indentIfInSubPage">{props.children}</div>
) : (
<PaperGroup>{props.children}</PaperGroup>
)}
</FilterForSearchText>
);
};
const PaperGroup: React.FunctionComponent<
React.PropsWithChildren<{
label?: string;
}>
> = (props) => {
const childrenWithStore = getChildrenWithStore(props);
return (
<Paper
className="indentIfInSubPage"
elevation={2}
css={css`
//width: 100%; doesn't work with shadow
margin-left: 2px; //needed to show shadow
margin-right: 2px; //needed to show shadow
margin-bottom: 12px !important;
`}
>
<List
component="nav"
css={css`
width: calc(100% - 20px);
`}
>
<FilterAndJoinWithDividers>{childrenWithStore}</FilterAndJoinWithDividers>
</List>
</Paper>
);
};
function getChildrenWithStore(props: React.PropsWithChildren<{}>) {
return React.Children.map(props.children, (c, index) => {
if (React.isValidElement(c)) {
return React.cloneElement(c, {
...c.props,
});
} else return null;
});
}
// For each child element, determine if we want it to be visible right now,
// and if we want to stick a horizontal divider beneath it.
const FilterAndJoinWithDividers: React.FunctionComponent<React.PropsWithChildren<{}>> = (
props,
) => {
const count = React.Children.toArray(props.children).length;
return props.children
? React.Children.toArray(props.children).reduce(
(result: any, child: React.ReactNode, index: number) => {
if (!React.isValidElement(child)) {
throw Error('We only expect to be given full elements not, e.g., strings');
}
const childElement = child as ReactElement;
const wrappedForFiltering = (
<FilterForSubPage {...childElement.props} key={'filter' + index}>
{childElement}
{index < count - 1 && <Divider component="li" key={index} />}
</FilterForSubPage>
);
return result.concat(wrappedForFiltering);
},
[],
)
: null;
};
const ConfigrRowOneColumn: React.FunctionComponent<
React.PropsWithChildren<{
label: string;
description?: string | React.ReactNode;
control: React.ReactNode;
}>
> = (props) => {
return (
<ListItem
//className={'MuiListItem-alignItemsFlexStart'}
css={css`
flex-direction: column;
// I don't understand why this is needed. Else, it's centered
align-items: flex-start;
`}
>
<ListItemText
primaryTypographyProps={{ variant: 'h4' }}
primary={props.label}
secondary={descriptionToReact(props.description)}
/>
{props.control}
</ListItem>
);
};
// If a subPage is in effect, only render if we are part of it
const FilterForSubPage: React.FunctionComponent<
React.PropsWithChildren<{
path: string;
}>
> = (props) => {
return (
<FocusPageContext.Consumer>
{({ focussedSubPagePath }) => {
if (focussedSubPagePath)
if (
!(
(
props.path === focussedSubPagePath ||
isParent(props.path, focussedSubPagePath) || // we are a parent of the focused thing
isParent(focussedSubPagePath, props.path)
) // we are a child of the focused thing
)
)
return null;
return <React.Fragment>{props.children}</React.Fragment>;
}}
</FocusPageContext.Consumer>
);
};
type StringEditorComponent = React.FunctionComponent<{
value: string;
onChange: (value: string) => void;
}>;
type BooleanEditorComponent = React.FunctionComponent<{
value: boolean;
disabled?: boolean;
onChange: (value: boolean) => void;
}>;
const ConfigrRowTwoColumns: React.FunctionComponent<
React.PropsWithChildren<{
label: string;
labelCss?: SerializedStyles;
path: string;
description?: string;
control: React.ReactElement;
disabled?: boolean;
height?: string;
indented?: boolean;
onClick?: () => void;
}>
> = (props) => {
const inner = (
<SearchContext.Consumer>
{({ searchRegEx }) => {
const row = (
<div
css={css`
display: flex;
flex-direction: column;
width: 100%;
`}
>
{/* Left side */}
<ListItemText
primaryTypographyProps={{ variant: 'h4' }}
title={props.path}
css={css`
color: ${props.disabled ? disabledGrey : 'unset'};
${props.height ? 'height:' + props.height : ''}
${props.indented && 'margin-left: 30px;'}
user-select: none;
* {
${props.labelCss}
}
`}
primary={props.label}
/>
{/* Right side */}
<ListItemSecondaryAction
css={css`
// OK, this feels like a hack. But the MUI default puts it at
// top:50% which is fine until you have a secondary label, in
// which case the whole thing gets very tall but really the
// button should be top-aligned.
/// Months later.. but it messed up toggleGroups and I'm not seeing the problem it was solving, at the moment.
//top: 22px;
`}
>
{props.control}
</ListItemSecondaryAction>
<Typography
variant="caption"
// enhance: the default component, span, ignores the line-height of our caption
// but if we use p, we get a console error because the parent is already a p.body2
//component={'p'}
css={css`
&,
* {
// this is a hack... we need to figured out how to have this MUI List stuff allow a text along the bottom
max-width: calc(100% - 200px);
color: ${props.disabled ? disabledGrey : 'unset'};
}
`}
>
{descriptionToReact(props.description)}
</Typography>
</div>
);
if (searchRegEx) {
const count = React.Children.toArray(props.children).filter((c) =>
searchRegEx.exec((c as any).props.label as string),
).length;
if (count) {
return (
// I haven't managed to get this work yet
// <Tooltip open={true} title="hello">
// {row}
// </Tooltip>
<div>
{row}
<span
css={css`
background-color: yellow;
`}
>
{`${count} matches`}
</span>
</div>
);
}
}
return row;
}}
</SearchContext.Consumer>
);
return props.onClick ? (
<ListItemButton onClick={props.onClick} disabled={props.disabled}>{inner}</ListItemButton>
) : (
<ListItem
css={css`
height: ${props.height};
`}
>
{inner}
</ListItem>
);
};
// Properties that are common to (nearly?) all Configr leaf controls.
// In particular useoverrideValue manipulates path and disabled to implement
// the overrideValue feature. The others are just included here so we don't
// have to repeat them on each control.
interface IConfigrProps<T> {
path: string;
label: string;
description?: string;
disabled?: boolean;
// If overrideValue is set, then the control is disabled, and the value shown
// is based on overrideValue rather then the value indicated by the path.
// The value in the main settings object is not affected and may be different from
// the value determined by overrideValue and shown in the control.
overrideValue?: T;
// explain why it is over-ridden
overrideDescription?: string;
}
// This hook implements the overrideValue prop. It does this by putting
// the override value into temporary property and setting the path to
// point to it. The control is then disabled, so we end up with a
// disabled control that shows the override value.
// It takes your props and gives you a replacement.
function useModifyForOverride<T>(props: IConfigrProps<T>): IConfigrProps<T> {
// We don't need this if we're not using overrideValue, but the rules of hooks
// won't let us call it conditionally.
const { values } = useFormikContext();
if (props.overrideValue === undefined) return props;
// Something elsewhere in configr treats dot and square brackets specially,
// as paths into child objects and arrays. If we leave them in, then the
// code won't find a overrideValue that just uses the whole path as a prop name.
// So we replace them with underscores.
const disabledPath = props.path.replace(/[.[\]]/g, '_');
let overrideValues = (values as any)[kOverrideValuePrefix];
if (!(values as any)[kOverrideValuePrefix]) {
// this is the first overrideValue we've seen, so create a place to put them
(values as any)[kOverrideValuePrefix] = overrideValues = {};
}
// Note: We're modifying an object that is part of the formik state, which might
// be a sketchy thing to do. But the thing we're adding is unlikely
// to be noticed by any other code, and it works.
// If we decide this is unacceptable, the only other idea I've had is to pass
// a list of overrideValue paths to the ContentPane, and have it modify the
// initialValues object before passing it to Formik.
overrideValues[disabledPath] = props.overrideValue;
const description = `${props.description ?? ''} (${props.overrideDescription ?? ''})`
.replace(' ()', '')
.trim();
return {
...props,
description,
disabled: true,
path: kOverrideValuePrefix + '.' + disabledPath,
};
}
const kOverrideValuePrefix = 'overrideValue$';
// Note: string|number covers 'text' | 'number' | 'email' but may need to be extended if we support other types.
// function getCheckedStateProps(props: any) {
// return {
// checked: props.store!.useState(props.get),
// onChange: (e: any) =>
// props.store!.update((s: any) => props.set(s, e.target.checked)),
// };
// }
// function getStringStateProps(props: any) {
// return {
// value: props.store!.useState(props.get),
// error: props.store!.useState(
// props.getErrorMessage ?? ((s: any) => undefined)
// ),
// helperText: props.store!.useState(
// props.getErrorMessage ?? ((s: any) => undefined)
// ),
// onChange: (e: any) =>
// props.store!.update((s: any) => props.set(s, e.target.value)),
// };
// }
// TODO: the spinner control isn't visually disabling correctly (but nothing happens when you click it)
export const ConfigrInput: React.FunctionComponent<
React.PropsWithChildren<
IConfigrProps<string | number> & {
className?: string;
type?: 'text' | 'number' | 'email'; // I don't really know what all the options are in formik
units?: string;
getErrorMessage?: (data: any) => string | undefined;
}
>
> = (props) => {
props = useModifyForOverride(props);
return (
<ConfigrRowTwoColumns
{...props}
control={
<Field
component={TextField}
variant="standard"
name={props.path}
type={props.type ?? 'text'} // type "number" gives you a spinner control
InputProps={
props.units
? {
endAdornment: (
<InputAdornment position="end">{props.units}</InputAdornment>
),
}
: undefined
}
css={css`
input {
text-align: end;
}
`}
//className={props.className}
/>
}
></ConfigrRowTwoColumns>
);
};
interface ICustomStringInputProps extends IConfigrProps<string> {
control: React.FunctionComponent<{
value: string;
disabled?: boolean;
onChange: (value: string) => void;
}>;
getErrorMessage?: (data: any) => string | undefined;
}
// Clients can use this to create their own custom inputs based on string data.
// For example, <DefaultColorPicker> or some other color picker.
export const ConfigrCustomStringInput: React.FunctionComponent<
React.PropsWithChildren<ICustomStringInputProps>
> = (props) => {
props = useModifyForOverride(props) as ICustomStringInputProps;
const [field, meta, helpers] = useField(props.path);
const { value } = meta;
const { setValue } = helpers;
return (
<ConfigrRowTwoColumns
{...props}
control={
<props.control disabled={props.disabled} value={value} onChange={setValue} />
}
></ConfigrRowTwoColumns>
);
};
interface ICustomBooleanInputProps extends IConfigrProps<boolean> {
control: BooleanEditorComponent;
getErrorMessage?: (data: any) => string | undefined;
}
// Clients can use this to create their own custom inputs based on boolean data.
// Note, this is untested, but based on ConfigrCustomStringInput which is tested.
export const ConfigrCustomBooleanInput: React.FunctionComponent<
React.PropsWithChildren<ICustomBooleanInputProps>
> = (props) => {
props = useModifyForOverride(props) as ICustomBooleanInputProps;
const [field, meta, helpers] = useField(props.path);
const { value } = meta;
const { setValue } = helpers;
return (
<ConfigrRowTwoColumns
{...props}
control={
<props.control disabled={props.disabled} value={value} onChange={setValue} />
}
></ConfigrRowTwoColumns>
);
};
interface ICustomNumberInputProps extends IConfigrProps<number> {
control: React.FunctionComponent<
React.PropsWithChildren<{
value: number;
disabled?: boolean;
onChange: (value: number) => void;
}>
>;
getErrorMessage?: (data: any) => string | undefined;
}
// Clients can use this to create their own custom inputs based on number data.
// Note, this is untested, but based on ConfigrCustomStringInput which is tested.
export const ConfigrCustomNumberInput: React.FunctionComponent<
React.PropsWithChildren<ICustomNumberInputProps>
> = (props) => {
props = useModifyForOverride(props) as ICustomNumberInputProps;
const [field, meta, helpers] = useField(props.path);
const { value } = meta;
const { setValue } = helpers;
return (
<ConfigrRowTwoColumns
{...props}
control={
<props.control disabled={props.disabled} value={value} onChange={setValue} />
}
></ConfigrRowTwoColumns>
);
};
interface ICustomObjectInputProps<T> extends IConfigrProps<unknown> {
control: React.FunctionComponent<
React.PropsWithChildren<{
value: T;
disabled?: boolean;
onChange: (value: T) => void;
}>
>;
getErrorMessage?: (data: any) => string | undefined;
}
// Clients can use this to create their own custom inputs based on object data.
// Note, this is untested, but based on ConfigrCustomStringInput which is tested.
export function ConfigrCustomObjectInput<T>(
props: React.PropsWithChildren<ICustomObjectInputProps<T>>,
) {
props = useModifyForOverride(props) as ICustomObjectInputProps<T>;
const [field, meta, helpers] = useField(props.path);
const { value } = meta;
const { setValue } = helpers;
return (
<ConfigrRowTwoColumns
{...props}
control={
<props.control value={value} disabled={props.disabled} onChange={setValue} />
}
></ConfigrRowTwoColumns>
);
}
interface ISelectProps extends IConfigrProps<string> {
indented?: boolean;
options: Array<{ value: string; label?: string; description?: string } | number>;
enableWhen?: string | ((currentValues: object) => boolean);
getErrorMessage?: (data: any) => string | undefined;
}
export const ConfigrSelect: React.FunctionComponent<
React.PropsWithChildren<ISelectProps>
> = (props) => {
props = useModifyForOverride(props) as ISelectProps;
const disabled = props.disabled || !useBooleanBasedOnValues(true, props.enableWhen);
return (
<ConfigrRowTwoColumns
{...props}
disabled={disabled}
control={
<Field
name={props.path}
disabled={disabled}
component={FormikMuiSelect}
sx={{ minWidth: 180 }}
css={css`
.MuiSelect-select {
padding: 3px !important;
padding-left: 9px !important;
background-color: #f1f1f1;
}
* {
border-style: none;
}
`}
>
{/* Allow a list of numbers (typically font sizes) instead of label/value objects */}
{props.options.map((o) => {
if (typeof o === 'number') {
return (
<MenuItem value={o} key={o}>
<span>{o}</span>
</MenuItem>
);
}
const labelToUse = o.label ?? o.value;
const valueToUse = o.value ?? o.label;
if (labelToUse?.startsWith('--')) {
return (<Divider key={labelToUse} />);
}
return (
<MenuItem value={valueToUse} key={labelToUse}>
{o.description ? (
<Tooltip title={o.description}>
<span>{labelToUse}</span>
</Tooltip>
) : (
<span>{labelToUse}</span>
)}
</MenuItem>
);
})}
</Field>
}
></ConfigrRowTwoColumns>
);
};
export const ConfigrSubgroup: React.FunctionComponent<
React.PropsWithChildren<{
label: string;
path: string;
description?: string | React.ReactNode;
getErrorMessage?: (data: any) => string | undefined;
}>
> = (props) => {
return (
<FilterForSubPage {...props}>
<ConfigrGroup {...props} level={2}>
{props.children}
</ConfigrGroup>
</FilterForSubPage>
);
};
// In Chrome Settings, most controls live under pages that you get
// to by clicking on a right-facing triangle control. When clicked,
// the whole settings area switches to that of the page, and a back
// button, labeled with the name of the page, is shown at the top.
// We only allow a single level of nesting.
export const ConfigrSubPage: React.FunctionComponent<
React.PropsWithChildren<{
label: string;
labelCss?: SerializedStyles;
path: string;
getErrorMessage?: (data: any) => string | undefined;
}>
> = (props) => {
return (
<FocusPageContext.Consumer>
{({ focussedSubPagePath, setFocussedSubPagePath }) => {
if (focussedSubPagePath === props.path) {
return (
<React.Fragment>
<div css={props.labelCss}>
<IconButton onClick={() => setFocussedSubPagePath('')}>
<ArrowBackIcon />
</IconButton>
{props.label}
</div>
<div
css={css`
.indentIfInSubPage {
margin-left: 20px;
//margin-right: 20px;
}
`}
>
<FilterAndJoinWithDividers>{props.children}</FilterAndJoinWithDividers>
</div>
</React.Fragment>
);
}
// We are not the focussed page, so show a row with a button that would make
// us the focussed page
else
return (
<ConfigrRowTwoColumns
onClick={() => setFocussedSubPagePath(props.path)}
control={<ArrowRightIcon />}
{...props}
/>
);
}}
</FocusPageContext.Consumer>
);
};
// Used to display the child component for each member of an array
// Note, this `render` function leaves it to you to take the index and build
// out the full path. I originally set out to instead just take some children elements
// and then render them using relative paths. I figured out how to do it this way sooner,
// is probably possible with a bunch of cloning so that the path prop could be changed
// to the full path that formik requires. E.g. path="./iso" could be changed to path="project.languages[0].iso"
export const ConfigrForEach: React.FunctionComponent<
React.PropsWithChildren<{
path: string; // really, `path`
searchTerms: string;
render: (pathPrefix: string, index: number) => React.ReactNode;
getErrorMessage?: (data: any) => string | undefined;
}>
> = (props) => {
const { values } = useFormikContext();
const items = getFormValueFromPath(values, props.path);
return (
<React.Fragment>
{items.map((_item: any, index: number) =>
props.render(`${props.path}[${index}]`, index),
)}
</React.Fragment>
);
};
export const ConfigrBoolean: React.FunctionComponent<
React.PropsWithChildren<
IConfigrProps<boolean> & {
// When immediateEffect is true, overrideValue will
// misbehave: the control will seem to work but not actually save the setting.
immediateEffect?: boolean;
// When true, the control will be disabled, but the label will not be greyed out, only the checkbox.
// This is useful when the checkbox is one of a set and is disabled because it is the only one that
// is turned on; in that case, it should not look less prominent than the others.
locked?: boolean;
}
>
> = (props) => {
props = useModifyForOverride(props);
const [field, meta, helpers] = useField(props.path);
// we're not supporting indeterminate state here (yet), so treat an undefined value as false
if (field.value === undefined || field.value === null) {
// get a console error if we make this change while rendering
window.setTimeout(() => helpers.setValue(false), 0);
}
const control = props.immediateEffect ? (
<Field component={Switch} type="checkbox" name={props.path} label={props.label} disabled={props.disabled || props.locked}/>
) : (
<Field
component={Checkbox}
type="checkbox"
disabled={props.disabled || props.locked}
name={props.path}
label={props.label}
/>
);
return (
<ConfigrRowTwoColumns
// clicking the row is the same as clicking the toggle control
onClick={() => {
// if locked, we can't change the value, but we didn't tell the component it is disabled
// so we still get the click.
if (props.locked) return;
helpers.setValue(!field.value);
}}
control={control}
{...props}
disabled = {props.disabled && !props.locked}
/>
);
};
interface IRadioGroupProps extends IConfigrProps<string> {
row?: boolean;
}
// TODO: overrideValue sets the value and prevents changing the value, but we
// don't yet have CSS to make the whole group LOOK disabled.
export const ConfigrRadioGroup: React.FunctionComponent<
React.PropsWithChildren<IRadioGroupProps>
> = (props) => {
props = useModifyForOverride(props) as IRadioGroupProps;
return (
// I could imagine wanting the radio buttons in the right column. There aren't any examples of this in chrome:settings.
// Note that normally in chrome:settings, radios are the sole child of an entire group (e.g. "on startup", "cookie settings",
// "safe browsing"). When the choices are short and don't need explanation, then a combobox is used instead (e.g. "Search engine")
// But to do that, we'll have to fix some css problems (e.g. the radio doesn't know its width and so doesn't line up properly
// on its left edge.)
<ConfigrRowOneColumn
{...props}
control={<ConfigrRadioGroupRaw {...props} />}
></ConfigrRowOneColumn>
);
};
const ConfigrRadioGroupRaw: React.FunctionComponent<
React.PropsWithChildren<{
path: string;
label: string;
row?: boolean;
description?: string;
disabled?: boolean;
}>
> = (props) => {
// Enhance: it's not clear what are we using out of `field` in the RadioGroup below. Probably onchange, value
const [field] = useField(props.path);
return (
<RadioGroup row={props.row} {...field} {...props}>
{props.children}
</RadioGroup>
);
};
export const ConfigrRadio: React.FunctionComponent<
React.PropsWithChildren<{
value: any;
label?: string; // either include a label or a single child
}>
> = (props) => {
const radioContext = useRadioGroup();
console.log('useRadioGroup ' + JSON.stringify(radioContext));
if (props.label) {
return (
<FormControlLabel value={props.value} control={<Radio />} label={props.label} />
);
} else {
return <React.Fragment>{props.children}</React.Fragment>;
}
};
interface IToggleGroupProps extends IConfigrProps<string> {
row?: boolean;
height?: string;
}
export const ConfigrToggleGroup: React.FunctionComponent<
React.PropsWithChildren<IToggleGroupProps>
> = (props) => {
props = useModifyForOverride(props) as IToggleGroupProps;
return (
<ConfigrRowTwoColumns
{...props}
control={<ConfigrToggleGroupRaw {...props} />}
></ConfigrRowTwoColumns>
);
};
const ConfigrToggleGroupRaw: React.FunctionComponent<
React.PropsWithChildren<{
path: string;
label: string;
row?: boolean;
height?: string;
description?: string;
disabled?: boolean;
}>
> = (props) => {
return (
<Field component={ToggleButtonGroup} name={props.path} type="checkbox" exclusive>
{props.children}
</Field>
);
};
// This cannot be a React.FunctionComponent because then the ToggleGroup stops working.
// So we have to transparently just return the ToggleButton
export function ConfigrMakeToggle(value: any, content: ReactNode) {
return <ToggleButton value={value}>{content}</ToggleButton>;
}