setting locale in blueprintjs datetime react component - reactjs

I'm trying to set the locale of blueprintjs DateRangeInput. The docs state that the Component uses Moment.js for localisation. So I tried to set locale="de", but the language is English still. Any ideas what is missing to get a translated date input?
I'm fairly new to React programming so I can't be sure that it has nothing to do with my React skills, even tho passing the props seems quite right to me.
<DateRangeInput
locale={"de"}
value={dates}
onChange={...}
/>

try adding this import "moment/locale/de" to the top of the file
i think blueprint uses new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/) to remove the locales from moment which is pretty common since moment is really large.
if that doesn't work try adding this to your webpack plugins
new webpack.ContextReplacementPlugin(/moment[\/\\]locale$/, /de/)

The DateTime components (Date picker, Date range picker, Time picker, Date time picker, Date input, and Date range input) all depend on react-day-picker.
To set the desired locale, according to the react-day-picker documentation you need to import the desired locale from moment as mentioned above and set the localeUtils (a set of functions used to render the component based on the current locale).
The code below is how it works.
import React from 'react';
import { DateRangeInput } from "#blueprintjs/datetime";
import 'react-day-picker/lib/style.css';
// Include the locale utils designed for moment
import MomentLocaleUtils from 'react-day-picker/moment';
// Make sure moment.js has the required locale data
import 'moment/locale/ja';
export default class LocalizedExample extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<div>
<DateRangeInput localeUtils={MomentLocaleUtils} locale='ja' />
</div>
);
}
}

Related

react-redux subscribe to store outside of component

I am woking on localization for a react app, and for maintainability I want to define all localized text in separate files outside of the component. I have a localizationReducer which controls the selected locale - so far I've been able to work with useSelector and useDispatch hooks to interact with the reducer/store, but I'm wondering if I can subscribe to the store from a separate file which is not a react component. Here is an example of what I'm trying to do.
//Component which needs the localized text
import * as localizedText from "#localization/modules/fundsMovementTemplates"
<
<div>
<DropDown
extraClass={"localizationSelector"}
defaultValue={""}
value={localeFromStore}
listItems={localesDidNotLoad.current ? [localeOptions[0]] : localeOptions}
handleChange={(e) => updateLocale(e.target.value)}
/>
<span>{localizedText.pageTitle}</span>
</div>
//separate file with localized text
import translate from "#localization/translate"
import intl from "react-intl-universal"
export const titleText= translate(
intl,
"HOMEPAGE.labels.TITLETEXT.title",
initComplete,
"Page Title"
)
//Translate function that interacts with reducer
/*
This method wraps intl.get(), allowing for the loading of locale data asynchronously
(in LocalizationDropDown) and avoiding empty/null locale data errors/warnings.
Params (ALL REQUIRED):
1) intl: the instance of the react-intl-universal singleton
2) selector: the key of the locale value (e.g. 'RULE.title')
3) initComplete: boolean indicating the init status of the intl instance
4) defaultText: fallback in case the locale data doesn't load, etc.
*/
export const translate = (intl, selector, initComplete, defaultText) => {
if (initComplete) {
return intl.get(selector).defaultMessage(defaultText)
}
return defaultText
}
How can I subscribe a simple file like this to the store so I can import the localized text into another component?

Timestamp format is not supporting the React `react-datepicker` as default value

My date-time format from the api result is 2019-12-30T06:16:48.453Z. It is not supported by the react-datepicker.
My code is following and is a functional component. I didn't write the complete code below. Added just the required parts.
import DatePicker from "react-datepicker";
import "react-datepicker/dist/react-datepicker.css";
<DatePicker
selected={'2019-12-30T06:16:48.453Z'}
{...props}
/>
I also tried adding moment by importing it and used as selected={moment('2019-12-30T06:16:48.453Z')}
I'm getting the error in both cases like Maximum update depth exceeded.
Still version 1.8 of react datepicker they were using moment, to reduce the package size they are using the native Date Objects. reference
So you can update your code as shown below
function App() {
return (
<div className="App">
<DatePicker selected={new Date("2019-12-30")} />
<DatePicker selected={new Date("2019-12-30T06:16:48.453Z")} />
</div>
);
}
working codesandbox
Update
to get the required format react datepicker has a prop called dateFormat, so you can add like this dateFormat="dd/MM/yyyy", See here
Maximum update depth exceeded
This error happens when your function call inside return of render method exceeds limits. Can you checkout any other function calls inside your component.
And for
react-datepicker
try basic JavaScript Date api.
selected={new Date('2019-12-30T06:16:48.453Z')}
This should work
const datee='2019-12-30T06:16:48.453Z';
let filt = Date(datee);
console.log(filt);
document.write(filt);
This error about infinite loop .
You can check this issue : ReactJS: Maximum update depth exceeded error
And also you can set dateFormat in DatePicker component like : dateFormat="MMMM d, yyyy h:mm aa"
Check this for formats: https://reactdatepicker.com/

How to get Material UI Icon SVG path? React Icon Picker

I have a react application using Material UI's Icons. The main objective is to have an Icon Picker, where a user can select a MUI Icon and that selected icon's SVG path is saved within the state.
This svg path will then be saved out to an API where it can be displayed in various places. etc.
I've searched through documentation on MUI's site regarding icons, but it's all about implementation, which I can do just fine. I've looked for an npm package, without much luck.
I did come across this package (https://github.com/DMDc0de/material-ui-icon-picker), which is essentially what I'd like the picker to be - but it outputs the icon's name for an icon component <i />. Not what I want. I need the source of the SVG path.
Any direction towards this would be super helpful.
Go to the icon site: https://material.io/tools/icons/
Click on an icon
Click on "Selected icon" button (bottom left)
Click on the "SVG" button to download the SVG version
Alternatively, go to the GitHub repo and download the SVGs there.
One way of doing that programmatically is to load the component and to render it in a string. Then to extract the path from the string.
To do so, we can use the renderToString or renderToMarkupString method of ReactDomServer.
Than we can extract the path from the generated string. We can either parse the svg XML with the DOMParser or with a regexp.
Here's an example in TypeScript:
import EditIcon from '#material-ui/icons/Edit';
import ReactDOMServer from 'react-dom/server';
export function getEditIconPath(): string {
const iconString = ReactDOMServer.renderToString(<EditIcon />);
const parser = new DOMParser();
const svgDoc = parser.parseFromString(iconString, 'image/svg+xml');
const iconPath = svgDoc.querySelector('path')?.getAttribute('d') as string;
return iconPath;
}
Another way to achieve that would be to use the React Test Renderer, thus we can get directly a json including the different properties (including the path). However, it looks like this method is around 10 times slower than the previous method.
Here's an example with the second method:
import EditIcon from '#material-ui/icons/Edit';
import TestRenderer from 'react-test-renderer'; // ES6
export function getEditIconPath(): string {
const iconComponent = TestRenderer.create(<EditIcon />);
const iconJson = iconComponent.toJSON();
const path = iconJson.children[0].props.d;
return path;
}

Display time from database in React Semantic UI with React Moment

A timestamp string is stored in database like so:
startsAt: 1535705100000
endsAt: 1535708100000
currently there is as a const endTime that should convert the timestamp string into a human-readable, as well as a TimeInput field like this:
import Moment from "react-moment";
..
const endTime = (
<Moment unix format="HH:mm">
{endsAt / 1000}
</Moment>
);
.
..
import { TimeInput } from "semantic-ui-calendar-react";
<TimeInput
..
name="startsAt"
placeholder="00:00"
value={endTime}
..
/>
With that const endTime declared, what is being rendered to the input field is [object Object].
The question is - whether I can use to create a constant at all to be displayed as a value in the TimeInput component.
OR/ALSO - what is the best way to render a readable time from a string in such scenario? Desired output: H:mm
Many thanks in advance!
Take a look at your console. My guess is you are probably also getting a prop-type error for the value prop on your TimeInput component. It is rendering [object Object] because you are passing a React component there instead of a string. So it looks like value does not accept a React component as input.
You are using react-moment as your package to convert time. But it is always going to render some sort of html tag around your string when converting the time displayed. You should instead try using moment instead. The main library will return you an actual string which you can directly pass to your value.
Q: whether I can use to create a constant at all to be displayed as a value in the TimeInput component.
A: Yes you can!
BTW you have messed up types - this is a cause why your example doesn't work correctly.
Let's look on types:
endTime is described as const of type JSX.Element
property value of TimeInput element has type of string
So you are using incompatible types as compatible!
The example of how Moment, moment and TimeInput should be used together:
import "./styles.css";
import moment from "moment";
import Moment from "react-moment";
import { TimeInput } from "semantic-ui-calendar-react";
export default function App() {
const endsAt = 1535708100000;
return (
<div className="App">
<h1>React-moment usage example</h1>
<Moment unix format="HH:mm">
{endsAt / 1000}
</Moment>
<TimeInput
name="startsAt"
placeholder="00:00"
value={moment(endsAt).format("HH:mm")}
onChange={() => {
return;
}}
/>
</div>
);
}
Take a look onto working example here: https://codesandbox.io/s/epic-napier-kc6q6k?file=/src/App.tsx

reactjs - digital time picker insted of analog time picer

I'm using meterial-ui-datetimepicker in order to represent calender and time picker.
My time picker looks like that:
My analog time picker
And I want to display it in digital mode, like this:
Digital time picker
My code:
import React from 'react';
import DateTimePicker from 'libs/DateTimePicker';
import DatePickerDialog from 'material-ui/DatePicker/DatePickerDialog';
import TimePickerDialog from 'material-ui/TimePicker/TimePickerDialog';
class DatetimeFilter extends React.Component {
render() {
return (
<DateTimePicker
DatePicker={DatePickerDialog}
TimePicker={TimePickerDialog}
onChange={this.handleTimeChange}
floatingLabelText={this.props.label}
timeFormat={'24hr'}
/>);
}
}
Does meterial-ui have a digital form of time picker ?
If not, how can I do that ?
1 No it doesn't.
2 I use rc-time-picker

Resources