-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added description tooltip to Boolean Widget (#290)
* Added description tooltip to Boolean Widget * Cleanup code smell * smell is required. Added default value for const
- Loading branch information
Showing
2 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* eslint-disable import/prefer-default-export */ | ||
import React, { useEffect, useState } from 'react'; | ||
import { FormControlLabel, Switch, Tooltip } from '@mui/material'; | ||
import { | ||
TransTitle, WidgetProps, WithValue, useUIStore, | ||
} from '@ui-schema/ui-schema'; | ||
import { MuiWidgetBinding } from '@ui-schema/ds-material/widgetsBinding'; | ||
import { useUID } from 'react-uid'; | ||
|
||
export function BooleanWidget<P extends WidgetProps<MuiWidgetBinding> = WidgetProps<MuiWidgetBinding>>({ | ||
storeKeys, schema, onChange, | ||
}: P & WithValue): React.ReactElement { | ||
const uid = useUID(); | ||
const { store } = useUIStore(); | ||
|
||
function getNestedValue(obj: any, path: any) { | ||
console.log('getNestedValue: ', obj, path); | ||
console.log('getNestedValueRES: ', path.reduce((xs: any, x: any) => (xs && xs[x] ? xs[x] : false), obj)); | ||
return path.reduce((xs: any, x: any) => (xs && xs[x] ? xs[x] : false), obj); | ||
} | ||
|
||
const [value, setValue] = useState<boolean>(getNestedValue(store?.toJS().values, storeKeys.toJS())); | ||
const isConst = schema.get('const') as boolean ?? false; | ||
|
||
useEffect(() => { | ||
if (isConst) { | ||
setValue(getNestedValue(store?.toJS().values, storeKeys.toJS())); | ||
} | ||
}, [store]); | ||
|
||
const handleToggle = (event: React.ChangeEvent<HTMLInputElement>) => { | ||
setValue(event.target.checked); | ||
onChange({ | ||
storeKeys, | ||
scopes: ['value'], | ||
type: 'set', | ||
schema, | ||
required: schema.get('required') as boolean, | ||
data: { value: event.target.checked }, | ||
}); | ||
}; | ||
|
||
const description = schema.get('description') as string; | ||
|
||
return ( | ||
// eslint-disable-next-line react/jsx-no-useless-fragment | ||
isConst ? <></> | ||
: ( | ||
<Tooltip title={description || ''}> | ||
<FormControlLabel | ||
control={( | ||
<Switch | ||
checked={value} | ||
onChange={handleToggle} | ||
color="primary" | ||
id={`uis-${uid}`} | ||
/> | ||
)} | ||
label={schema.get('title') as string ?? <TransTitle schema={schema} storeKeys={storeKeys} />} | ||
/> | ||
</Tooltip> | ||
) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters