Get the value of radio button using material-ui for summary page - material-ui

I'm creating a form and now attempting to get the value for a summary page.
I'm not having trouble in selecting the radio buttons. The problem lies in getting the value of radio button in material-ui for a summary page/print the value.
The following are my code:
Registering Office: <br></br>
<Grid container direction={'row'} spacing={1}>
<Grid item xs={3}>
<Radio id="RegOffice" value="Head Office" checked={RegOffice==="Head Office"} onChange={handleChangeRegOffice}
color="primary"
/>
Head Office
</Grid>
<Grid item xs={3}>
<Radio id="RegOffice" value="Branch Office" checked={RegOffice==="Branch Office"} onChange={handleChangeRegOffice}
color="primary" />
Branch Office
</Grid>
<Grid itemxs={3}>
<Radio id="RegOffice" value="Facility" checked={RegOffice==="Facility"}
onChange={handleChangeRegOffice}
color="primary" //label="Facility"
/>
Facility
</Grid>
</Grid>
for handlechange:
const [RegOffice, setRegOffice] = React.useState('Head Office')
const handleChangeRegOffice = (event) => { setRegOffice(event.target.value)
console.log(RegOffice) }
=end=
With no luck, every time I call "RegOffice", it always returns blank value.
Appreciate any help. thank you

You code is fine, but you're doing console.log(RegOffice), before the state is updated. React set/update state Asynchronously, which means when you're viewing the console.log(RegOffice) the regOffice state still contains the old state value. The updating state is a really vital React concept and learning about it will be very beneficial for creating/understanding the React Code. Here is a docs link
I've created the codesandbox link out of your code and just moved the console.log() outside the handleChangeRegOffice function.
const handleChangeRegOffice = (event) => {
setRegOffice(event.target.value);
};
console.log(RegOffice);
the updated function and console statement

Related

.net maui Listview ItemSelected not selected when inner elements focused

I have a ListView with one button and one entry on it's ItemTemplate
<ListView
x:Name="LstMedios"
Grid.Row="2"
ItemsSource="{Binding MediosDePago}"
SelectedItem="{Binding MedioPagoSeleccionado,Mode=TwoWay}"
SelectionMode="Single">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid ColumnDefinitions="2*,*" >
<Button
Text="My button"
HeightRequest="40"
HorizontalOptions="FillAndExpand" Margin="5,5" />
<Entry
Text="{Binding NumValor,StringFormat='{0:c}'}"
Keyboard="Numeric"
Grid.Column="1"
HorizontalTextAlignment="End" Margin="0,5,5,5">
</Entry>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
I expect when my Entry is focused then my listview SelectedItem must be set, but it remains null.
What is the proper way to force my listview to automatically set SelectedItem when any of its controls on ItemTemplate is focused?
Full sample
To begin with, what you expect is that when directly hitting on the Button or the Entry(get focused), the SelectedItem below should be triggered.
public TestItem SelectedItem
{
get => _SelectedItem;
set => SetProperty(ref _SelectedItem, value);
}
However, the SelectedItem will only be set if clicking on the frame border or outside of it and the background color of the selected item will be Orange. And this potential issue happens both in ListView and CollectionView.
In conclusion, this potential issue is related or similar to CollectionView's SelectionChanged method cannot be triggered when tapping the item directly #9567. You can try this workaround provided by PhoenixWyllow or follow up on it. Besides, you can also raise a new issue on Github if you are willing to.
Update:
For now, Juan implemented a transparent button over the Entry element this way can get access to selected item posting it from CommandParameter.

How to set MUI breakpoint values in KotlinJS

I am using https://github.com/JetBrains/kotlin-wrappers/tree/master/kotlin-mui which is a KotlinJS wrapper around Material UI for React.
I am trying to port the following code to Kotlin:
<Grid container spacing={3}>
<Grid xs="auto">
<Item>variable width content</Item>
</Grid>
<Grid xs={6}>
<Item>xs=6</Item>
</Grid>
<Grid xs>
<Item>xs</Item>
</Grid>
</Grid>
The issue I am having is that I don't know how to set the value for the xs breakpoint.
Okay apparently this can be done by creating extension properties:
inline var GridProps.xs: Any?
get() = asDynamic().xs
set(value) = asDynamic().xs = value
(https://github.com/JetBrains/kotlin-wrappers/issues/1856)

MUI V5, how to minify class names in production build

How is possible to there is no option to minify the prod class names? It is a a big step back. Please say me that I am not right.
In Material v4 can be achieved with 3 simple rules:
Only one theme provider is used (No theme nesting)
The style sheet has a name that starts with Mui (all Material-UI components).
The disableGlobal option of the class name generator is false (the default).
In v5.5 in my experiment I don`t use any custom styles, just default provided from MUI components.
"react": "^18.0.0",
"react-dom": "^18.0.0",
"#emotion/react": "^11.8.1",
"#mui/material": "^5.5.2"
import { ThemeProvider, createTheme } from '#mui/material/styles'
<ThemeProvider theme={theme}>
<CssBaseline />
<Component {...this.props} />
</ThemeProvider>
import { Paper, Grid, Typography, Box } from '#mui/material'
<Grid container component={'main'}>
<Grid item xs={false} sm={4} md={8} />
<Grid item xs={12} sm={8} md={4} component={Paper} elevation={6} square>
<Typography component={'h1'} variant={'h4'} align={'center'}>
<Box fontWeight={'fontWeightLight'} m={1}>
{'MY ACCOUNT'}
</Box>
</Typography>
</Grid>
</Grid>
In production build I expect to be something like this:
jss1 jss2 jss3 jss4 jss5 jss6 and etc...
but the result is:
MuiGrid-root MuiGrid-container css-1d3bbye
MuiGrid-root MuiGrid-item MuiGrid-grid-sm-4 MuiGrid-grid-md-8 css-18mwvdj
I think i found it
https://v5-0-6.mui.com/guides/classname-generator/
Converts from
<button
class="MuiButton-root MuiButton-text MuiButton-textPrimary MuiButton-sizeMedium MuiButton-textSizeMedium MuiButtonBase-root css-1ujsas3"
>
Button
</button>
to
<button
class="foo-bar-MuiButton-root foo-bar-MuiButton-text foo-bar-MuiButton-textPrimary foo-bar-MuiButton-sizeMedium foo-bar-MuiButton-textSizeMedium foo-bar-MuiButtonBase-root css-1ujsas3"
>
Button
</button>
I have just tried it with latest MUIv5 and works great.

React, Material-UI, Grid empty item

I have this code:
<Grid item xs={12}>
<Grid container spacing={8} >
<Grid item xs={3} ></Grid>
{ someState && <SomeComponent /> }
</Grid>
</Grid>
At the first render, someState will be always false and only when I change that state I will show SomeComponente but I need to have that Grid visible and empty, in other words, using the row space in the dom. I don't want it hidden. How can I do it? I tried by putting the Gridcomponent empty as I show in the code, but the Grid container isn't using that space.
const SomeComponent = () => (
<Fragment>
<Grid item xs={3}>
<Typography variant="body1">Title</Typography>
</Grid>
<Grid item xs={3}>
asdasdasd
</Grid>
</Fragment>
);
Try this:
<Grid item xs={3} >{someState && <SomeComponent />}</Grid>
This will create the Grid regardless of someState and will fill the space, after someState is thruthy.

My autogenerated columns do not show in Datagrid

Most posts regarding DataGrid AutoGenerateColumns seem to deal with how to circumvent default behavior. Unfortunately the default is what I am struggling with.
I have several database tables created with Entity Framework 6.0, and would like to display them in my View using one and the same Datagrid for all tables, and AutoGenerating the Columns. Desired outcome: for the class-specific Viewmodel bound at run-time, display the columns with headers and at least one row.
XAML of the View's user control:
<UserControl.Resources>
<DataTemplate x:Key="TableDataTemplate">
<DataGrid
AutoGenerateColumns="True"
GridLinesVisibility="All"
HorizontalScrollBarVisibility="Visible"
VerticalScrollBarVisibility="Visible">
</DataGrid>
</DataTemplate>
</UserControl.Resources>
<StackPanel Height="720" Width="980">
<!-- shows user which menu item was chosen -->
<TextBlock Text="{Binding DisplayName}"/>
<!-- defines the Input data grid for adding to DB table -->
<Grid Height="80" MaxHeight="200">
<ItemsControl
ItemsSource="{Binding CurrentDataTable}"
ItemTemplate="{StaticResource TableDataTemplate}">
</ItemsControl>
</Grid>
The user control is bound to a class-specific 'Class'TableViewModel, implemented for all Database Tables. One example:
class ClientsAdminTableViewModel : TableViewModel
{
// the Property the view binds to
private IList<Client> currentDataTable;
public ObservableCollection<Client> CurrentDataTable
{
get { return CollectionExtensions.ToObservableCollection<Client>(currentDataTable); }
set { currentDataTable = value; OnPropertyChanged("CurrentDataTable")}
}
public ClientsAdminTableViewModel()
{
DisplayName = Strings.ClientAdminDisplayName;
currentDataTable = context.Clients.ToList<Client>();
}
When bound to {Binding CurrentDataTable} this code produces a UI, where I can see a grey body (probably the rows) surrounded by horizontal and vertical scrollbars, on top of which one long line (which must be the header line), but no columns.
Any help is appreciated.
your datagrid needs to have its itemsource set
try adding
ItemsSource="{Binding CurrentDataTable}"
to the datagrid declaration. Also, do you get any binding errors in the output window?
This code now works (Thank you J. King!) The trick was to get rid of the data template, create the data grid right in the grid with the exact same binding statements.
<StackPanel Height="720" Width="980">
<!-- shows user which menu item was chosen -->
<TextBlock Text="{Binding DisplayName}"/>
<!-- defines the Input data grid for adding to DB table -->
<Grid Height="200" MaxHeight="400">
<DataGrid
ItemsSource="{Binding CurrentDataTableNew}"
AutoGenerateColumns="True"
GridLinesVisibility="All"
HorizontalScrollBarVisibility="Visible"
VerticalScrollBarVisibility="Visible" >
</DataGrid>
</Grid>
</userControl>