Skip to content

Commit

Permalink
Make material renderers aware of input variant in theme
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianfrey committed Sep 29, 2023
1 parent 8fc90d3 commit 77fde14
Show file tree
Hide file tree
Showing 21 changed files with 262 additions and 85 deletions.
22 changes: 15 additions & 7 deletions packages/examples-react/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type AppProps = {
examples: ExampleDescription[];
cells: JsonFormsCellRendererRegistryEntry[];
renderers: JsonFormsRendererRegistryEntry[];
Wrapper?: React.JSXElementConstructor<any>;
};

type Action = {
Expand Down Expand Up @@ -69,7 +70,12 @@ const getProps = (
};
};

const App = ({ examples, cells, renderers }: AppProps) => {
const App = ({
examples,
cells,
renderers,
Wrapper = React.Fragment,
}: AppProps) => {
const [currentExample, setExample] = useState<ExampleDescription>(
examples[0]
);
Expand All @@ -88,7 +94,7 @@ const App = ({ examples, cells, renderers }: AppProps) => {
[exampleProps.uischema]
);

const actions: Action[] = currentExample.actions;
const actions: Action[] = currentExample.actions ?? [];

useEffect(() => {
const hash = window.location.hash.replace('#', '');
Expand Down Expand Up @@ -192,11 +198,13 @@ const App = ({ examples, cells, renderers }: AppProps) => {
))}
</div>
<div className='demo'>
<JsonForms
key={currentIndex}
{...exampleProps}
onChange={({ data }) => changeData(data)}
/>
<Wrapper>
<JsonForms
key={currentIndex}
{...exampleProps}
onChange={({ data }) => changeData(data)}
/>
</Wrapper>
</div>
</div>
</div>
Expand Down
11 changes: 9 additions & 2 deletions packages/examples-react/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,18 @@ import { getExamples } from '@jsonforms/examples';

export const renderExample = (
renderers: { tester: RankedTester; renderer: any }[],
cells: { tester: RankedTester; cell: any }[]
cells: { tester: RankedTester; cell: any }[],
Wrapper?: React.JSXElementConstructor<any>
) => {
console.info({ Wrapper });
const examples = getExamples();
ReactDOM.render(
<App examples={examples} renderers={renderers} cells={cells} />,
<App
examples={examples}
renderers={renderers}
cells={cells}
Wrapper={Wrapper}
/>,
document.getElementById('root')
);
};
28 changes: 0 additions & 28 deletions packages/material-renderers/example/index.ts

This file was deleted.

94 changes: 94 additions & 0 deletions packages/material-renderers/example/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
The MIT License
Copyright (c) 2017-2019 EclipseSource Munich
https://github.com/eclipsesource/jsonforms
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

import React from 'react';
import {
Divider,
FormControl,
InputLabel,
MenuItem,
Select,
SelectChangeEvent,
Stack,
TextFieldProps,
ThemeProvider,
createTheme,
} from '@mui/material';
import { renderExample } from '../../examples-react/src/index';
import { materialRenderers, materialCells } from '../src';

const MuiWrapper = ({ children }: React.PropsWithChildren<unknown>) => {
const [variant, setVariant] =
React.useState<TextFieldProps['variant']>('standard');

const handleVariantChange = (event: SelectChangeEvent<unknown>) => {
setVariant(event.target.value as TextFieldProps['variant']);
};

const theme = React.useMemo(() => {
return createTheme({
components: {
MuiTextField: {
defaultProps: {
variant,
},
},
// avoid jammed look of input fields when variant is not 'standard'
...(variant !== 'standard'
? {
MuiFormControl: {
styleOverrides: {
root: {
marginTop: '8px',
},
},
},
}
: {}),
},
});
}, [variant]);

const label = 'TextField variant';

return (
<ThemeProvider theme={theme}>
<Stack spacing={2}>
<FormControl sx={{ width: 200 }} variant='outlined'>
<InputLabel>{label}</InputLabel>
<Select value={variant} label={label} onChange={handleVariantChange}>
<MenuItem value='standard'>Standard</MenuItem>
<MenuItem value='outlined'>Outlined</MenuItem>
<MenuItem value='filled'>Filled</MenuItem>
</Select>
</FormControl>
<Divider />
{children}
</Stack>
</ThemeProvider>
);
};

renderExample(materialRenderers, materialCells, MuiWrapper);
9 changes: 6 additions & 3 deletions packages/material-renderers/src/cells/MaterialIntegerCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,13 @@ import {
} from '@jsonforms/core';
import { withJsonFormsCellProps } from '@jsonforms/react';
import { MuiInputInteger } from '../mui-controls/MuiInputInteger';
import { useInputComponent } from '../util';

export const MaterialIntegerCell = (props: CellProps & WithClassname) => {
const { InputComponent } = useInputComponent(props);
return <MuiInputInteger {...props} InputComponent={InputComponent} />;
};

export const MaterialIntegerCell = (props: CellProps & WithClassname) => (
<MuiInputInteger {...props} />
);
export const materialIntegerCellTester: RankedTester = rankWith(
2,
isIntegerControl
Expand Down
17 changes: 10 additions & 7 deletions packages/material-renderers/src/cells/MaterialNumberCell.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
/*
The MIT License
Copyright (c) 2017-2019 EclipseSource Munich
https://github.com/eclipsesource/jsonforms
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Expand All @@ -32,10 +32,13 @@ import {
} from '@jsonforms/core';
import { withJsonFormsCellProps } from '@jsonforms/react';
import { MuiInputNumber } from '../mui-controls/MuiInputNumber';
import { useInputComponent } from '../util';

export const MaterialNumberCell = (props: CellProps & WithClassname) => {
const { InputComponent } = useInputComponent(props);
return <MuiInputNumber {...props} InputComponent={InputComponent} />;
};

export const MaterialNumberCell = (props: CellProps & WithClassname) => (
<MuiInputNumber {...props} />
);
/**
* Default tester for number controls.
* @type {RankedTester}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,15 @@ import {
} from '@jsonforms/core';
import { withJsonFormsCellProps } from '@jsonforms/react';
import { MuiInputNumberFormat } from '../mui-controls/MuiInputNumberFormat';
import { useInputComponent } from '../util';

export const MaterialNumberFormatCell = (
props: CellProps & WithClassname & Formatted<number>
) => <MuiInputNumberFormat {...props} />;
) => {
const { InputComponent } = useInputComponent(props);
return <MuiInputNumberFormat {...props} InputComponent={InputComponent} />;
};

/**
* Default tester for text-based/string controls.
* @type {RankedTester}
Expand Down
8 changes: 5 additions & 3 deletions packages/material-renderers/src/cells/MaterialTextCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ import {
} from '@jsonforms/core';
import { withJsonFormsCellProps } from '@jsonforms/react';
import { MuiInputText } from '../mui-controls/MuiInputText';
import { useInputComponent } from '../util';

export const MaterialTextCell = (props: CellProps & WithClassname) => (
<MuiInputText {...props} />
);
export const MaterialTextCell = (props: CellProps & WithClassname) => {
const { InputComponent } = useInputComponent(props);
return <MuiInputText {...props} InputComponent={InputComponent} />;
};

/**
* Default tester for text-based/string controls.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ export const MaterialDateControl = (props: ControlProps) => {
InputLabelProps: data ? { shrink: true } : undefined,
onFocus: onFocus,
onBlur: onBlur,
variant: 'standard',
},
}}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ export const MaterialDateTimeControl = (props: ControlProps) => {
InputLabelProps: data ? { shrink: true } : undefined,
onFocus: onFocus,
onBlur: onBlur,
variant: 'standard',
},
}}
/>
Expand Down
13 changes: 11 additions & 2 deletions packages/material-renderers/src/controls/MaterialInputControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {

import { Hidden, InputLabel, FormControl, FormHelperText } from '@mui/material';
import merge from 'lodash/merge';
import { useFocus } from '../util';
import { useFocus, useInputComponent } from '../util';

export interface WithInput {
input: any;
Expand All @@ -50,6 +50,7 @@ export const MaterialInputControl = (props: ControlProps & WithInput) => {
config,
input,
} = props;
const { InputComponent, variant } = useInputComponent(props);
const isValid = errors.length === 0;
const appliedUiSchemaOptions = merge({}, config, uischema.options);

Expand All @@ -68,14 +69,20 @@ export const MaterialInputControl = (props: ControlProps & WithInput) => {
const secondFormHelperText = showDescription && !isValid ? errors : null;
const InnerComponent = input;

const InputMore: { label?: string } = {};

if (variant === 'outlined') {
InputMore.label = label;
}

return (
<Hidden xsUp={!visible}>
<FormControl
fullWidth={!appliedUiSchemaOptions.trim}
onFocus={onFocus}
onBlur={onBlur}
variant={variant}
id={id}
variant={'standard'}
>
<InputLabel
htmlFor={id + '-input'}
Expand All @@ -89,9 +96,11 @@ export const MaterialInputControl = (props: ControlProps & WithInput) => {
</InputLabel>
<InnerComponent
{...props}
{...InputMore}
id={id + '-input'}
isValid={isValid}
visible={visible}
InputComponent={InputComponent}
/>
<FormHelperText error={!isValid && !showDescription}>
{firstFormHelperText}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ export const MaterialTimeControl = (props: ControlProps) => {
InputLabelProps: data ? { shrink: true } : undefined,
onFocus: onFocus,
onBlur: onBlur,
variant: 'standard',
},
}}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ export const MuiAutocomplete = (
return (
<TextField
label={label}
variant={'standard'}
type='text'
inputProps={params.inputProps}
inputRef={params.InputProps.ref}
Expand Down
Loading

0 comments on commit 77fde14

Please sign in to comment.