-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated core components to the latest react features
- Loading branch information
1 parent
fc8acae
commit 3b1ce71
Showing
15 changed files
with
1,900 additions
and
1,946 deletions.
There are no files selected for viewing
2,881 changes: 1,441 additions & 1,440 deletions
2,881
docs/showcase/assets/scommons-client-showcase-opt.js
Large diffs are not rendered by default.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,51 @@ | ||
package scommons.client.ui | ||
|
||
import io.github.shogowada.scalajs.reactjs.React | ||
import io.github.shogowada.scalajs.reactjs.VirtualDOM._ | ||
import io.github.shogowada.scalajs.reactjs.classes.ReactClass | ||
import io.github.shogowada.scalajs.reactjs.events.MouseSyntheticEvent | ||
import org.scalajs.dom.raw.HTMLButtonElement | ||
import scommons.react.UiComponent | ||
import scommons.react._ | ||
import scommons.react.hooks._ | ||
|
||
case class ImageButtonProps(data: ImageButtonData, | ||
onClick: () => Unit, | ||
disabled: Boolean = false, | ||
showTextAsTitle: Boolean = false, | ||
requestFocus: Boolean = false) | ||
|
||
object ImageButton extends UiComponent[ImageButtonProps] { | ||
|
||
private case class ImageButtonState(setButtonRef: HTMLButtonElement => Unit, | ||
getButtonRef: () => HTMLButtonElement) | ||
|
||
protected def create(): ReactClass = React.createClass[PropsType, ImageButtonState]( | ||
getInitialState = { _ => | ||
var buttonRef: HTMLButtonElement = null | ||
|
||
ImageButtonState({ ref => | ||
buttonRef = ref | ||
}, { () => | ||
buttonRef | ||
}) | ||
}, | ||
componentDidUpdate = { (self, prevProps, _) => | ||
val buttonRef = self.state.getButtonRef() | ||
if (self.props.wrapped.requestFocus | ||
&& self.props.wrapped.requestFocus != prevProps.wrapped.requestFocus) { | ||
|
||
buttonRef.focus() | ||
} | ||
}, | ||
render = { self => | ||
val props = self.props.wrapped | ||
val data = props.data | ||
|
||
val image = if (props.disabled) data.disabledImage else data.image | ||
val primaryClass = if (data.primary) "btn-primary" else "" | ||
|
||
val attributes = ( | ||
if (props.showTextAsTitle) Some(^.title := data.text) | ||
else None | ||
) :: List( | ||
^.`type` := "button", | ||
^.className := s"btn $primaryClass", | ||
^.disabled := props.disabled, | ||
^.ref := { ref: HTMLButtonElement => | ||
self.state.setButtonRef(ref) | ||
}, | ||
^.onClick := { _: MouseSyntheticEvent => | ||
self.props.wrapped.onClick() | ||
object ImageButton extends FunctionComponent[ImageButtonProps] { | ||
|
||
protected def render(compProps: Props): ReactElement = { | ||
val buttonRef = useRef[HTMLButtonElement](null) | ||
val props = compProps.wrapped | ||
|
||
useLayoutEffect({ () => | ||
val button = buttonRef.current | ||
if (button != null) { | ||
if (props.requestFocus) { | ||
button.focus() | ||
} | ||
) | ||
} | ||
}, List(props.requestFocus)) | ||
|
||
val data = props.data | ||
val image = if (props.disabled) data.disabledImage else data.image | ||
val primaryClass = if (data.primary) "btn-primary" else "" | ||
|
||
val attributes = ( | ||
if (props.showTextAsTitle) Some(^.title := data.text) | ||
else None | ||
) :: List( | ||
^.`type` := "button", | ||
^.className := s"btn $primaryClass", | ||
^.disabled := props.disabled, | ||
^.reactRef := buttonRef, | ||
^.onClick := { _: MouseSyntheticEvent => | ||
props.onClick() | ||
} | ||
) | ||
|
||
<.button(attributes)( | ||
if (props.showTextAsTitle) ImageLabelWrapper(image, None) | ||
else ImageLabelWrapper(image, Some(data.text)) | ||
) | ||
} | ||
) | ||
<.button(attributes)( | ||
if (props.showTextAsTitle) ImageLabelWrapper(image, None) | ||
else ImageLabelWrapper(image, Some(data.text)) | ||
) | ||
} | ||
} |
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
Oops, something went wrong.