-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into PullRequest48
# Conflicts: # muirwik-components/build.gradle.kts # muirwik-components/src/main/kotlin/com/ccfraser/muirwik/components/ThemeProvider.kt # muirwik-testapp/build.gradle.kts
- Loading branch information
Showing
16 changed files
with
497 additions
and
98 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
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
55 changes: 55 additions & 0 deletions
55
muirwik-components/src/main/kotlin/com/ccfraser/muirwik/components/ErrorBoundary.kt
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,55 @@ | ||
package com.ccfraser.muirwik.components | ||
|
||
import kotlinext.js.jsObject | ||
import react.* | ||
|
||
typealias ErrorBoundaryErrorEvent = (error: Throwable, info: RErrorInfo) -> Unit | ||
|
||
interface ErrorBoundaryProps : RProps { | ||
/** | ||
* Content you want to display on an error (note, we don't want to add this as a child, i.e. don't use the normal RBuilder | ||
* in a render (e.g. use a different (or new) RBuilder or for material components, set addAsChild = false if available | ||
*/ | ||
var fallbackContent: ReactElement | ||
|
||
/** | ||
* If you want to log the error, you can also get notified by using this event | ||
*/ | ||
var onError: ErrorBoundaryErrorEvent? | ||
} | ||
|
||
interface ErrorBoundaryState : RState { | ||
var hasError: Boolean | ||
} | ||
|
||
class ErrorBoundary(props: ErrorBoundaryProps) : RComponent<ErrorBoundaryProps, ErrorBoundaryState>(props) { | ||
|
||
override fun RBuilder.render() { | ||
if (state.hasError) { | ||
child(props.fallbackContent) | ||
} else { | ||
children() | ||
} | ||
} | ||
|
||
override fun componentDidCatch(error: Throwable, info: RErrorInfo) { | ||
props.onError?.let { it(error, info) } | ||
} | ||
|
||
companion object : RStatics<ErrorBoundaryProps, ErrorBoundaryState, ErrorBoundary, Nothing>(ErrorBoundary::class) { | ||
init { | ||
getDerivedStateFromError = { | ||
val result: ErrorBoundaryState = jsObject() | ||
result.hasError = true | ||
result | ||
} | ||
} | ||
} | ||
} | ||
|
||
fun RBuilder.errorBoundary(fallbackContent: ReactElement, onError: ErrorBoundaryErrorEvent? = null, handler: RHandler<RProps>) = child(ErrorBoundary::class) { | ||
attrs.fallbackContent = fallbackContent | ||
attrs.onError = onError | ||
handler() | ||
} | ||
|
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
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
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
77 changes: 77 additions & 0 deletions
77
muirwik-components/src/main/kotlin/com/ccfraser/muirwik/components/lab/Rating.kt
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,77 @@ | ||
package com.ccfraser.muirwik.components.lab | ||
|
||
import com.ccfraser.muirwik.components.EnumPropToString | ||
import com.ccfraser.muirwik.components.StyledPropsWithCommonAttributes | ||
import com.ccfraser.muirwik.components.createStyled | ||
import com.ccfraser.muirwik.components.setStyledPropsAndRunHandler | ||
import kotlinext.js.Object | ||
import react.* | ||
import styled.StyledHandler | ||
|
||
@JsModule("@material-ui/lab/Rating") | ||
private external val module: dynamic | ||
|
||
@Suppress("UnsafeCastFromDynamic") | ||
private val component: RComponent<MRatingProps, RState> = module.default | ||
|
||
@Suppress("EnumEntryName") | ||
enum class MRatingSize { | ||
large, medium, small | ||
} | ||
|
||
interface MRatingProps : StyledPropsWithCommonAttributes { | ||
var defaultValue: Number | ||
var disabled: Boolean | ||
var emptyIcon: ReactElement | ||
var emptyLabelText: String | ||
var getLabelText: (value: Number) -> String | ||
var icon: ReactElement | ||
@JsName("IconContainerComponent") | ||
var iconContainerComponent: FunctionalComponent<MIconContainerProps> | ||
var max: Number | ||
var name: String | ||
var onChange: (event: Object, newValue: Number) -> Unit | ||
var onChangeActive: (event: Object, hoverValue: Number) -> Unit | ||
var precision: Number | ||
var readOnly: Boolean | ||
var value: Number? | ||
} | ||
var MRatingProps.size by EnumPropToString(MRatingSize.values()) | ||
|
||
interface MIconContainerProps : StyledPropsWithCommonAttributes { | ||
var value: Int | ||
} | ||
|
||
fun RBuilder.mRating( | ||
name: String, | ||
value: Number? = null, | ||
max: Number = 5, | ||
precision: Number = 1, | ||
onChange: ((event: Object, newValue: Number) -> Unit)? = null, | ||
defaultValue: Number? = null, | ||
readOnly: Boolean = false, | ||
disabled: Boolean = false, | ||
icon: ReactElement? = null, | ||
emptyIcon: ReactElement? = null, | ||
emptyLabelText: String = "Empty", | ||
size: MRatingSize = MRatingSize.medium, | ||
|
||
addAsChild: Boolean = true, | ||
className: String? = null, | ||
handler: StyledHandler<MRatingProps>? = null | ||
) = createStyled(component, addAsChild) { | ||
defaultValue?.let { attrs.defaultValue = it } | ||
attrs.disabled = disabled | ||
emptyIcon?.let { attrs.emptyIcon = it } | ||
attrs.emptyLabelText = emptyLabelText | ||
icon?.let { attrs.icon = icon } | ||
attrs.max = max | ||
attrs.name = name | ||
onChange?.let { attrs.onChange = it } | ||
attrs.precision = precision | ||
attrs.readOnly = readOnly | ||
attrs.size = size | ||
value?.let { attrs.value = it } | ||
|
||
setStyledPropsAndRunHandler(className, handler) | ||
} |
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
Oops, something went wrong.