Created
March 10, 2021 00:10
-
-
Save freelancing-solutions/e813a199e0dc2b26394b4c9d2fb9eecd to your computer and use it in GitHub Desktop.
React Admin Customer Component with validations
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
import * as React from "react"; | |
import { | |
List, | |
Datagrid, | |
Edit, | |
Create, | |
DateField, | |
TextField, | |
EditButton, | |
TextInput, | |
SelectInput, | |
TabbedForm, | |
FormTab | |
} from 'react-admin'; | |
import Customer from '@material-ui/icons/AssignmentIndOutlined'; | |
import {required} from "ra-core"; | |
export const CustomerIcon = Customer; | |
const provinces = [ | |
{ | |
id:'limpopo', | |
name:'limpopo' | |
}, | |
{ | |
id:'gauteng', | |
name:'Gauteng' | |
}, | |
{ | |
id:'northwest', | |
name:'North West' | |
}, | |
{ | |
id:'mpumalanga', | |
name:'Mpumalanga' | |
}, | |
{ | |
id:'kzn', | |
name:'Kwazulu Natal' | |
}, | |
{ | |
id:'westerncape', | |
name:'Western Cape' | |
}, | |
{ | |
id:'easterncape', | |
name:'Eastern Cape' | |
}, | |
{ | |
id:'northerncape', | |
name:'Northern Cape' | |
}, | |
{ | |
id:'orangefreestate', | |
name:'Orange Free State' | |
} | |
] | |
export const CustomerList = (props) => ( | |
<List {...props}> | |
<Datagrid> | |
<TextField source="id" /> | |
<TextField source="name" /> | |
<TextField source="last_name" /> | |
<DateField source="email" /> | |
<TextField source="cell" /> | |
<EditButton basePath="/api/v1/customer" /> | |
</Datagrid> | |
</List> | |
); | |
const CustomerTitle = ({ record }) => { | |
return <span>Customers {record ? `"${record.title}"` : ''}</span>; | |
}; | |
export const CustomerEdit = (props) => ( | |
<Edit title={<CustomerTitle />} {...props}> | |
<TabbedForm> | |
<FormTab label={'Personal Details'}> | |
<TextInput disabled source="id" /> | |
<TextInput source="name" validate={required('Customer name is a required field')}/> | |
<TextInput source="last_name" validate={required('Customer last name is required')}/> | |
<TextInput source="email" validate={required('Customer Email is required')}/> | |
<TextInput source="cell" validate={required('Customer Cell is a required')}/> | |
<TextInput source="tel" /> | |
</FormTab> | |
<FormTab label={'Postal Address'}> | |
<TextInput source='postal-address' label={'Postal Address'} validate={required('Please indicate Customer Postal Address')}/> | |
<TextInput source="postal-code" validate={required('Postal Code is a required field')}/> | |
<SelectInput source="province" choices={provinces} validate={required('Select Customer Province')}/> | |
</FormTab> | |
<FormTab label={'Physical Address'}> | |
<TextInput source='physical-address' label={'Physical Address'} validate={required('Please indicate Customer Physical Address')}/> | |
<TextInput source="postal-code" validate={required('Postal Code is a required field')}/> | |
<SelectInput source="province" choices={provinces} validate={required('Please select customer Province')}/> | |
</FormTab> | |
</TabbedForm> | |
</Edit> | |
); | |
export const CustomerCreate = (props) => ( | |
<Create title="Create Customers" {...props}> | |
<TabbedForm> | |
<FormTab label={'Personal Details'}> | |
<TextInput source="name" validate={required('Customer Name is required')}/> | |
<TextInput source="last_name" validate={required('Customer Last Name is required')}/> | |
<TextInput source="email" validate={required('Customer Email is required')}/> | |
<TextInput source="cell" validate={required('Customer Cell is required')}/> | |
<TextInput source="tel" /> | |
</FormTab> | |
<FormTab label={'Postal Address'}> | |
<TextInput source='postal-address' label={'Postal Address'} validate={required('Please indicate Customer Postal Address')}/> | |
<TextInput source="postal-code" validate={required('Postal Code is a required field')}/> | |
<SelectInput source="province" choices={provinces} validate={required('Select Customer Province')}/> | |
</FormTab> | |
<FormTab label={'Physical Address'}> | |
<TextInput source='physical-address' label={'Physical Address'} validate={required('Please indicate Customer Physical Address')}/> | |
<TextInput source="postal-code" validate={required('Postal Code is a required field')}/> | |
<SelectInput source="province" choices={provinces} validate={required('Please select customer Province')}/> | |
</FormTab> | |
</TabbedForm> | |
</Create> | |
); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment